nonbot
nonbot

Reputation: 983

How to use different fixtures with different data structures for one test in pytest?

I have a test that currently uses a smoke test fixture that covers a subset of the full test fixture. For one part of my testing, I'd like to use the smoke test fixture for my test, but then if I want to do a big test I'd like to use a full test fixture that employs a different data structure (in the case below, small uses tuples and big uses a list). Currently I have my test using one fixture as shown below and I'm having trouble figuring out how to interchange between fixtures with different data structures.

import pytest

@pytest.fixture(params=[
    (1, 1),
    (2, 2)
])
def small_fixture_of_numbers(request):
    # returns a list of pairs of numbers for smoke testing
    return request.param

@pytest.fixture(params=[
    1, 2, 3, 4
])
def big_fixture_of_numbers(request):
    # returns the full list of numbers for full-scale testing
    return request.param

def test_addition(small_fixture_of_numbers):
    (x, y) = small_fixture_of_numbers
    total = x + y
    assert total > 2

Upvotes: 2

Views: 467

Answers (1)

salezica
salezica

Reputation: 77109

It is not wise to have the same test run differently. Even less so if different runs are backed by different data structures.

The whole idea of a test case is that it provides a stable environment for code to run under the same conditions, each time. Test and fixture should be fixed partners, so that changes in code behavior are the only factor.

In other words, you seem to need two different tests. Your choice should be of what test to run, not how to run it.

Upvotes: 1

Related Questions