1

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.

Kjeld Flarup
  • 1,471
  • 10
  • 15
  • 1
    Possible duplicate of [Run code before and after each test in py.test?](https://stackoverflow.com/questions/22627659/run-code-before-and-after-each-test-in-py-test) – hoefling Mar 23 '18 at 16:46
  • No this is just how to make a teardown! – Kjeld Flarup Mar 28 '18 at 07:27
  • Did you actually look at the question I linked? There is an example of setup/teardown fixtures in the answers. – hoefling Mar 28 '18 at 13:07
  • I tried them, but all the teardowns run after the test verdict has been done. And decorators I haven't had success with, then I try to decorate a test using a fixture. – Kjeld Flarup Mar 29 '18 at 21:09

0 Answers0