Performing the same test on two different lights

I have a test that currently works with a single device like this:

@pytest.fixture()
def foo():
    return 'foo'


def test_something(foo):
    # assert something about foo

Now I am creating a slightly different mount, say

@pytest.fixture
def bar():
    return 'bar'

I need to repeat the same test with this second mount. How can I do this without copying or pasting the test and changing the parameter name?

+4
source share
1 answer

In addition to generating the test, you can make this “fixture” for any number of sub-fixtures used dynamically. To do this, determine the actual device that will be used as the parameter:

@pytest.fixture
def arg(request):
    return request.getfuncargvalue(request.param)

am ( arg arg ):

@pytest.mark.parametrize('arg', ['foo', 'bar'], indirect=True)
def test_me(arg):
    print(arg)

, :

@pytest.fixture
def foo():
    return 'foo'

@pytest.fixture
def bar():
    return 'bar'

, :

$ pytest test_me.py -s -v -ra
collected 2 items                                                                                

test_me.py::test_me[foo] foo
PASSED
test_me.py::test_me[bar] bar
PASSED
+1

Source: https://habr.com/ru/post/1685693/


All Articles