pytest fixture of fixtures

后端 未结 2 2012
日久生厌
日久生厌 2020-12-17 02:07

I am currently writing tests for a medium sized library (~300 files). Many classes in this library share the same testing scheme which were coded using pytest:

File

2条回答
  •  庸人自扰
    2020-12-17 03:04

    One solution I found is to abuse the test cases as following:

    from test_for_class_a import *
    from test_for_class_b import *
    from test_for_class_c import *
    
    list_of_all_fixtures = []
    
    
    # This will force pytest to generate all sub-fixture for class a
    @pytest.mark.usefixtures(setup_class_a)
    def test_register_class_a_fixtures(setup_class_a)
        list_of_fixtures.append(setup_class_a)
    
    
    # This will force pytest to generate all sub-fixture for class b
    @pytest.mark.usefixtures(setup_class_b)
    def test_register_class_b_fixtures(setup_class_b)
        list_of_fixtures.append(setup_class_b)
    
    
    # This will force pytest to generate all sub-fixture for class c
    @pytest.mark.usefixtures(setup_class_c)
    def test_register_class_b_fixtures(setup_class_c)
        list_of_fixtures.append(setup_class_c)
    
    
    # This is the real test to apply on all fixtures
    def test_all_fixtures():
        for my_fixture in list_of_all_fixtures:
            # do something with my_fixture
    

    This implicitly rely on the fact that all test_all_fixture is executed after all the test_register_class*. It is obviously quite dirty but it works...

提交回复
热议问题