Reputation: 684
I am using parametrizing tests, to test 2 different data sets. For example like this. some_data()
functions return test data.
test_data = [
(some_data1_v1(), some_data2_v2()),
(some_data1_v2(), some_data2_v2())
]
@pytest.mark.parametrize('a,b', test_data)
def test_fn(a,b):
pass
But for some tests, that checks only general behavior, I don't need to test 2 data sets, because both sets have appropriate general data.
I want to convert some_data1_v1()
function to fixture and use it in test, like this
def test_fn2(some_data1_v1):
pass
If I will do it, then call some_data1_v1()
during test_data
list construction raise exception Fixtures are not meant to be called directly...
So instead this
def test_fn2(some_data1_v1):
pass
I do like this
@pytest.mark.parametrize('some_data1_v1', [some_data1_v1()])
def test_fn2(some_data1_v1):
pass
This allow me to have one source of test data - "fake fixtures" (usual functions that return data), and pass test data in test_data
list (first example or last example). But I think my approach to store data in usual functions is break "fixture abstraction".
Is there a better approach, to do what I do?
Upvotes: 0
Views: 3012
Reputation: 70223
You can use a parametrized fixture. request.param
refers to the specific entry in the params
sequence
For example:
@pytest.fixture(params=(1, 2, 3))
def fix1(request):
return request.param
@pytest.fixture(params=(4, 5, 6))
def fix2(request):
return request.param
def test1(fix1, fix2):
print(fix1, fix2)
def test2(fix1):
print(fix1)
And execution:
$ pytest t.py
...
collected 12 items
t.py::test1[1-4] 1 4
PASSED
t.py::test1[1-5] 1 5
PASSED
t.py::test1[1-6] 1 6
PASSED
t.py::test1[2-4] 2 4
PASSED
t.py::test1[2-5] 2 5
PASSED
t.py::test1[2-6] 2 6
PASSED
t.py::test1[3-4] 3 4
PASSED
t.py::test1[3-5] 3 5
PASSED
t.py::test1[3-6] 3 6
PASSED
t.py::test2[1] 1
PASSED
t.py::test2[2] 2
PASSED
t.py::test2[3] 3
PASSED
Upvotes: 3