Reputation: 78
I am using Pytest and Pytest-variables with parametrized test functions to try and reduce duplication of code across test cases. I have this working with hard-coded strings within the test module: similar to this; however, I have no idea how to import the variables from my variable file outside of the scope of a function. Any help is greatly appreciated and I understand this may not be possible using pytest-variables, but I wanted to post here to make sure I am not missing something.
Working code
import pytest
account_email_field = [
['[email protected]', 'Site Title', True],
['', '', False ],
['[email protected]', '', False ],
['', 'Default Title', False]
]
@pytest.fixture(params=account_email_field)
def email_field(request):
return [request.param[0], request.param[1], request.param[2]]
def test_sample_spage(email_field):
return [email_field[0], email_field[1], email_field[2]]
None working code, but an example of what I would like.
import pytest
account_email_field = [
[variables['sample_email'], variables['site_title'], True],
[variables['empty'], variables['empty'], False ],
[variables['sample_email'], variables['empty'], False ],
[variables['empty'], variables['site_title'], False]
]
@pytest.fixture(params=account_email_field)
def email_field(request):
return [request.param[0], request.param[1], request.param[2]]
def test_sample_spage(email_field):
return [email_field[0], email_field[1], email_field[2]]
Upvotes: 1
Views: 2035
Reputation: 8223
The pytest-variables plugin stores the variables in the config object, so you could access this from a pytest_generate_tests
hook.
Given the following variables.json file:
{
"data": [
["[email protected]", "Site Title", true],
["", "Default Title", false]
]
}
With the following test_param_vars.py file:
def pytest_generate_tests(metafunc):
if 'data' in metafunc.fixturenames:
metafunc.parametrize('data', metafunc.config._variables['data'])
def test_variables(data):
assert data[0]
You should get output similar to:
$ pytest test_param_variables.py --variables=variables.json -ra -s
======================= test session starts ========================
platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/dhunt/workspace/pytest-scratchpad, inifile:
plugins: variables-1.7.1
collected 2 items
test_param_variables.py .F
===================== short test summary info ======================
FAIL test_param_variables.py::test_variables[data1]
============================= FAILURES =============================
______________________ test_variables[data1] _______________________
data = ['', 'Default Title', False]
def test_variables(data):
> assert data[0]
E AssertionError: assert ''
test_param_variables.py:6: AssertionError
================ 1 failed, 1 passed in 0.04 seconds ================
Upvotes: 1
Reputation: 1098
I would use a static class for lookup rather than a dictionary, because it's more IDE friendly. Within a file constants.py
(or some other name)
class variables:
sample_email = '[email protected]'
site_title = 'Site Title'
empty = ''
and then in a test file you can
from constants import variables as var
account_email_field = [
[var.sample_email, var.site_title, True ],
[var.empty, var.empty, False ],
[var.sample_email, var.empty, False ],
[var.empty, var.site_title, False ],
]
Hope this helps!
Upvotes: 0