[py-dev] Change behavior in pytest_runtest_setup hooks based on available funcargs

Andreas Pelme andreas at pelme.se
Wed May 2 11:46:10 CEST 2012


Hi,

I am trying to implement a live_server funcarg, which will manage a Django LiveServerThread, passing in the live server URL to the test function.

pytest_runtest_setup is used to start a database transaction, and then pytest_runtest_teardown will rollback the transaction after the test run - this is the standard way Django's TestCase works.

I also have a pytest mark: transaction_test_case, when used, instead of running the test in a transaction and rolling back at the end, the database is flushed (this is equivalent to Django's TransactionTestCase).

When running tests with the live_server funcarg, the live server is running in another thread, and the data needs to be committed to the database, in order for the live server to see the data. So, transaction_test_case behavior should always be used when the live_server funcarg is used.

The relevant code is available here: https://gist.github.com/9a090afd645026b34bfb

I would like to change behavior in my pytest_runtest_setup hook, when a certain funcarg is used. The problem is that funcargs are not available in item.keywords in pytest_runtest_setup (since the pytest_funcarg__*() functions have not yet been called). When pytest_runtest_teardown is called however, item.keywords are available, and the database can be teared down in the right way.

I am using request.applymarker(transaction_test_case) in pytest_funcarg__live_server(), but as stated above, it won't be visible in pytest_runtest_setup. It would be nice to know *which* funcargs are present in pytest_runtest_setup, even though they are not yet initialized and have gotten their values.

One solution would be to manually mark all live_server test-cases with transaction_test_case, but that is a bit too much typing for my taste: :)

@pytest.mark.transaction_test_case
def test_foo(live_server):
pass

Is there any way to see what funcargs are specified from within pytest_runtest_setup? If not, is there another way I should approach this problem? Or should I just give up and decorate all live server tests with @pytest.mark.transaction_test_case ?

Best regards/Andreas

(I am trying to make a complete pytest-django package, I have forked an old package, put it on PyPI and added some docs:https://github.com/pelme/pytest_django , I will post an announcement here later when it has seen some more progress!)



More information about the Pytest-dev mailing list