How to assert setup outside teardown in multiply tests

Viewed 25

I have a lot of tests, and every test assert something specific, but also assert the setup was done as expected. The setup cannot be verified before the tests starts.

My approach is to assert in the tear-down. Is there a better approach where I do not have to add the assert to every test file, since I have hundredths of them.

Minimal example:

my_test.py

    import pytest
    
    
    @pytest.mark.parametrize("number_a", [1, 2, 3, 4, 5, 6, 7])
    def test_low_numbers(number_a):
        run_test(number_a=number_a)
    
   
    def run_test(number_a):
        assert number_a != 4

conftest.py

    def very_important_check(number_a):
      assert number_a != 2, "Not two, stupid."
    
    
    @pytest.fixture(autouse=True, scope="function")
    def signal_handler(request, number_a):
        print("########Setup")
        yield
        print("########Teardown")
        very_important_check(number_a)

0 Answers
Related