Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test case execution order in pytest

Tags:

python

pytest

I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order?

like image 620
Kocka Avatar asked Jul 10 '13 13:07

Kocka


2 Answers

In general you can configure the behavior of basically any part of pytest using its well-specified hooks.

In your case, you want the "pytest_collection_modifyitems" hook, which lets you re-order collected tests in place.

That said, it does seem like ordering your tests should be easier -- this is Python after all! So I wrote a plugin for ordering tests: "pytest-ordering". Check out the docs or install it from pypi. Right now I recommend using @pytest.mark.first and @pytest.mark.second, or one of the @pytest.mark.order# markers, but I have some ideas about more useful APIs. Suggestions welcome :)

Edit: pytest-ordering seems abandoned at the moment, you can also check out pytest-order (a fork of the original project by the author).

Edit2: In pytest-order, only one marker (order) is supported, and the mentioned examples would read @pytest.mark.order("first"), @pytest.mark.order("second"), or @pytest.mark.order(#) (with # being any number).

like image 127
Frank T Avatar answered Sep 20 '22 17:09

Frank T


Maybe you can consider using dependency pytest plugin, where you can set the test dependencies easily.

Be careful - the comments suggest this does not work for everyone.

@pytest.mark.dependency() def test_long():     pass  @pytest.mark.dependency(depends=['test_long']) def test_short():     pass 

This way test_short will only execute if test_long is success and force the execution sequence as well.

like image 26
asterio gonzalez Avatar answered Sep 21 '22 17:09

asterio gonzalez