I’m a novice in python and also in py.test. I’m searching a way to run multiple tests on multiple items and cannot find it. I’m sure it’s quite simple when you know how to do it.
I have simplified what I’m trying to do to make it simple to understand.
If I have a Test class who defines a serie of tests like this one :
JavaScript
x
8
1
class SeriesOfTests:
2
def test_greater_than_30(self, itemNo):
3
assert (itemNo > 30), "not greather than 30"
4
def test_lesser_than_30(self, itemNo):
5
assert (itemNo < 30), "not lesser thant 30"
6
def test_modulo_2(self, itemNo):
7
assert (itemNo % 2) == 0, "not divisible by 2"
8
I want to execute this SeriesOfTest on each item obtained from a function like :
JavaScript
1
3
1
def getItemNo():
2
return [0,11,33]
3
The result i’m trying to obtain is something like :
JavaScript
1
16
16
1
RESULT :
2
Test "itemNo = 0"
3
- test_greater_than_30 = failed
4
- test_lesser_than_30 = success
5
- test_modulo_2 = success
6
7
Test "itemNo = 11"
8
- test_greater_than_30 = failed
9
- test_lesser_than_30 = success
10
- test_modulo_2 = failed
11
12
Test "itemNo = 33"
13
- test_greater_than_30 = success
14
- test_lesser_than_30 = failed
15
- test_modulo_2 = failed
16
How can I do this with py.test?
Than you guys (and girls also)
André
Advertisement
Answer
Forget about the previous answer. Given that you need the tests to be grouped by value, you can use scenarios. I’ve just adapted the example from the docs:
JavaScript
1
26
26
1
import pytest
2
3
def pytest_generate_tests(metafunc):
4
idlist = []
5
argvalues = []
6
for scenario in metafunc.cls.scenarios:
7
idlist.append(scenario[0])
8
items = scenario[1].items()
9
argnames = [x[0] for x in items]
10
argvalues.append(([x[1] for x in items]))
11
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
12
13
scenario1 = ('itemNo = 0', {'itemNo': 0})
14
scenario2 = ('itemNo = 11', {'itemNo': 11})
15
scenario3 = ('itemNo = 33', {'itemNo': 33})
16
17
class TestSeries:
18
scenarios = [scenario1, scenario2, scenario3]
19
20
def test_greater_than_30(self, itemNo):
21
assert (itemNo > 30), "not greather than 30"
22
def test_lesser_than_30(self, itemNo):
23
assert (itemNo < 30), "not lesser thant 30"
24
def test_modulo_2(self, itemNo):
25
assert (itemNo % 2) == 0, "not divisible by 2"
26
And the output is:
JavaScript
1
15
15
1
$ py.test -v
2
============ test session starts ==============================================
3
platform linux2 -- Python 2.7.4 -- pytest-2.4.2 -- /home/jose/.virtualenvs/pytest1/bin/python
4
collected 9 items
5
6
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 0] FAILED
7
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 0] PASSED
8
test_first.py:27: TestSeries.test_modulo_2[itemNo = 0] PASSED
9
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 11] FAILED
10
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 11] PASSED
11
test_first.py:27: TestSeries.test_modulo_2[itemNo = 11] FAILED
12
test_first.py:23: TestSeries.test_greater_than_30[itemNo = 33] PASSED
13
test_first.py:25: TestSeries.test_lesser_than_30[itemNo = 33] FAILED
14
test_first.py:27: TestSeries.test_modulo_2[itemNo = 33] FAILED
15
I think that’s the closest you can get.