There are what might look like duplicates of this question, but these just adresses implementing a teardown, but I wan't to run this before the test is finished and a verdict made. Teardown is ran after this!
I want to run a check on all my test functions. If something bad is logged in, then pytest.fail should be called.
So far I have done this in a finalizer teardown, to an auto use fixture. BUT this code is executed after pytest has marked the testcase as passed. Same problem with using @pytest.yield_fixture
Putting a decorator on all test functions might do
@commoncheck
def test_xxx()
But it is a LOT of test functions to decorate. This trick can be done
for fn in dir() :
if fn.startswith('test'):
locals()[fn] = commoncheck(locals()[fn])
But so far I need to put that in the end of each test module. And adding fixtures to the test, I haven't figured out yet.
I'm running out of ideas. I haven't been able to find the pytest fixture that gets executed after the test function, but before making a report.
Any ideas?
EDIT! This code does most of what I want.
import mycheck,pytest
@pytest.fixture
def x():
return 0
def test_t1(x):
assert 1 == 1
def test_t2():
assert 1 == 1
# These three lines needs to be in every test module file
for fn in dir() :
if fn.startswith('test'):
locals()[fn] = mycheck.deco(locals()[fn])
And in mycheck.py I have
def deco(func):
def wrapper(func, *args, **kwargs):
assert 2 == 1
return func(*args, **kwargs)
return decorator.decorator(wrapper, func)
Still I would like to be able to move the three last files out of the test module file. The problem however is that locals() only is valid exactly in this file.