Skip to content

Commit 0c9bb52

Browse files
author
Sylvain MARIE
committed
Added tests for issue #126
1 parent 3489432 commit 0c9bb52

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import pytest
2+
from pytest_cases import parametrize_with_cases
3+
4+
5+
@pytest.fixture()
6+
def dependent_fixture():
7+
return 0
8+
9+
10+
class Foo:
11+
def case_requirement_1(self, dependent_fixture):
12+
return Foo, dependent_fixture + 1
13+
14+
def case_requirement_2(self, dependent_fixture):
15+
return Foo, dependent_fixture - 1
16+
17+
18+
def case_requirement_1(dependent_fixture):
19+
return case_requirement_1.__module__, dependent_fixture + 2
20+
21+
22+
def case_requirement_2(dependent_fixture):
23+
return case_requirement_1.__module__, dependent_fixture - 2
24+
25+
26+
@parametrize_with_cases("a,b", cases=(Foo, "."), prefix="case", debug=True)
27+
def test_functionality(a, b):
28+
do_assert(test_functionality, a, b)
29+
30+
31+
@parametrize_with_cases("a,b", cases=(".", Foo), prefix="case", debug=True)
32+
def test_functionality_again(a, b):
33+
do_assert(test_functionality_again, a, b)
34+
35+
36+
class TestNested:
37+
@parametrize_with_cases("a,b", cases=(Foo, "."), prefix="case", debug=True)
38+
def test_functionality_again2(self, a, b):
39+
do_assert(TestNested.test_functionality_again2, a, b)
40+
41+
42+
# init our markers
43+
markers_dict = {}
44+
for host in (test_functionality, test_functionality_again, TestNested.test_functionality_again2):
45+
markers_dict[host] = ({-1, 1}, {-2, 2}) # [0] is for cases in Foo, [1] is for cases in module
46+
47+
48+
def do_assert(host, a, b):
49+
"""used in tests below to make sure that all cases are used"""
50+
if a is Foo:
51+
markers_dict[host][0].remove(b)
52+
elif a == case_requirement_1.__module__:
53+
markers_dict[host][1].remove(b)
54+
else:
55+
raise ValueError()
56+
57+
58+
def test_synthesis(module_results_dct):
59+
# assert that all fixtures have been used once in all tests
60+
for host in (test_functionality, test_functionality_again, TestNested.test_functionality_again2):
61+
assert markers_dict[host] == (set(), set())
62+
63+
assert list(module_results_dct) == [
64+
'test_functionality[a_b_is__requirement_1]',
65+
'test_functionality[a_b_is__requirement_2]',
66+
'test_functionality[a_b_is__requirement_1_]',
67+
'test_functionality[a_b_is__requirement_2_]',
68+
'test_functionality_again[a_b_is__requirement_1_]', # <- note: same fixtures than previously
69+
'test_functionality_again[a_b_is__requirement_2_]', # idem
70+
'test_functionality_again[a_b_is__requirement_1]', # idem
71+
'test_functionality_again[a_b_is__requirement_2]', # idem
72+
'test_functionality_again2[a_b_is__requirement_1]', # idem
73+
'test_functionality_again2[a_b_is__requirement_2]', # idem
74+
'test_functionality_again2[a_b_is__requirement_1_]', # idem
75+
'test_functionality_again2[a_b_is__requirement_2_]' # idem
76+
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
from pytest_cases.common_pytest_marks import has_pytest_param
4+
from pytest_cases import parametrize_with_cases
5+
6+
7+
# only do this for pytest version 3+
8+
if has_pytest_param:
9+
@pytest.fixture
10+
def b():
11+
return -1
12+
13+
14+
@pytest.fixture(name='a')
15+
def a_in_module():
16+
return 1
17+
18+
19+
class TestA:
20+
@pytest.fixture(name='a')
21+
def a_nested(self):
22+
return 2
23+
24+
def test_a(self, a):
25+
assert a == 2
26+
27+
@parametrize_with_cases('o', debug=True)
28+
def test_foo_nested(self, o):
29+
assert o == 'case!'
30+
31+
32+
def test_bar(a):
33+
assert a == 1
34+
35+
36+
@parametrize_with_cases('o', debug=True)
37+
def test_foo(o):
38+
assert o == 'case!'
39+
40+
41+
def test_synthesis(module_results_dct):
42+
assert list(module_results_dct) == [
43+
'test_a',
44+
'test_foo_nested[o_is_a_]',
45+
'test_bar',
46+
'test_foo[o_is_a_]'
47+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def case_a(b, a):
2+
# a and b are fixtures defined in caller module/class
3+
# note that case id is also 'a'. The goal is to check that no conflict happens here
4+
assert a in (1, 2)
5+
assert b == -1
6+
return 'case!'

0 commit comments

Comments
 (0)