|
| 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 | + ] |
0 commit comments