Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - is it possible to run a script/command between all test scripts?

OK, this is definitely my fault but I need to clean it up. One of my test scripts fairly consistently (but not always) updates my database in a way that causes problems for the others (basically, it takes away access rights, for the test user, to the test database).

I could easily find out which script is causing this by running a simple query, either after each individual test, or after each test script completes.

i.e. pytest, or nose2, would do the following:

run test_aaa.py
run check_db_access.py  #ideal if I could induce a crash/abort
run test_bbb.py
run check_db_access.py
...

You get the idea. Is there a built-in option or plugin that I can use? The test suite currently works on both pytest and nose2 so either is an option.

Edit: this is not a test db, or a fixture-loaded db. This is a snapshot of any of a number of extremely complex live databases and the test suite, as per its design, is supposed to introspect the database(s) and figure out how to run its tests (almost all access is read-only). This works fine and has many beneficial aspects at least in my particular context, but it also means there is no tearDown or fixture-load for me to work with.

like image 880
JL Peyret Avatar asked Mar 10 '26 19:03

JL Peyret


1 Answers

import pytest

@pytest.fixture(autouse = True)
def wrapper(request):
    print('\nbefore: {}'.format(request.node.name))
    yield
    print('\nafter: {}'.format(request.node.name))

def test_a():
    assert True

def test_b():
    assert True

Example output:

$ pytest -v -s test_foo.py

test_foo.py::test_a
before: test_a
PASSED
after: test_a

test_foo.py::test_b
before: test_b
PASSED
after: test_b
like image 96
FMc Avatar answered Mar 12 '26 08:03

FMc