Skip to content Skip to sidebar Skip to footer

Wrap Each Pytest Test Function Into Try-except

I want to wrap each of my test functions into a try-except block to execute code in the except block. This code should only be executed if the test is failing. I want to achieve th

Solution 1:

How about this:

deftest_dec(test_func):
    deftest_wrapper(fix):
        try:
            test_func(fix)
        except:
            run_only_if_exception_was_thrown(fix)
            # re-raise exception to make the test failraisereturn test_wrapper

Then in your test suite:

...
@test_decdeftest_one(fix):
    # test code

Post a Comment for "Wrap Each Pytest Test Function Into Try-except"