1

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 = [
     ['test@google.com', 'Site Title', True],
     ['', '', False ],
     ['test@google.com', '', 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]]
4

2 回答 2

1

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": [
    ["test@google.com", "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 ================
于 2018-03-19T10:58:36.210 回答
0

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 = 'test@google.com'
    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!

于 2018-02-21T23:11:16.883 回答