简体   繁体   中英

pytest: Parameterized test cases via fixtures

How do you write a fixture (a method) that yields/returns parameterized test parameters?

For instance, I have a test as the following:

@pytest.mark.parametrize(
    "input,expected", 
    [("hello", "hello"),
    ("world", "world")])
def test_get_message(self, input, expected):
    assert expected == MyClass.get_message(input)

Instead of having input and expected to be passed via @pytest.mark.parametrize , I am interested in an approach as the following:

@pytest.fixture(scope="session")
def test_messages(self):
    # what should I write here to return multiple 
    # test case with expected value for each?
    pass

def test_get_message(self, test_messages):
    expected = test_messages["expected"] # somehow extracted from test_messages?
    input = test_messages["input"]       # somehow extracted from test message?
    assert expected == MyClass.get_message(input)

To move the parameters into a fixture, you can use fixture params:

@pytest.fixture(params=[("hello", "hello"),
    ("world", "world")], scope="session")
def test_messages(self, request):
    return request.param

def test_get_message(self, test_messages):
    input = test_messages[0]   
    expected = test_messages[1]
    assert expected == MyClass.get_message(input)

You can also put the params into a separate function (same as wih parametrize ), eg

def get_test_messages():
    return [("hello", "hello"), ("world", "world")]

@pytest.fixture(params=get_test_messages(), scope="session")
def test_messages(self, request):
    return request.param

To me, it seems you want to return an array of dicts:

@pytest.fixture(scope="session")
def test_messages():
    return [
        {
            "input": "hello",
            "expected": "world"
        },
        {
            "input": "hello",
            "expected": "hello"
        }
    ]

To use it in a test case, you would need to iterate over the array:

def test_get_message(self, test_messages):
    for test_data in test_messages:
        input = test_data["input"]
        expected = test_data["expected"]
        assert input == expected

But I not sure if this is the best approach, because it's still considered as only one test case, and so it will show up as only one test case in the output/report.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM