Reputation: 4865
I have a number of test files, such as
test_func1.py
test_func2.py
test_func3.py
I know in advance that test_func3.py
won't pass if I run Pytest in parallel, e.g. pytest -n8
. The reason is that test_func3.py
contains a number of parametrized tests that handle file i/o processes. Parallel writing to the same file leads to failures. In serial testing mode, all tests in this module passes.
I'm wondering how I can skip the whole module in case Pytest will be started with the option -n
? My thinking is to apply the skipif
marker. I need to check in my code whether the -n
argument has been passed to pytest
.
...>pytest # run all modules
...>pytest -n8 # skip module test_func3.py automatically
Upvotes: 2
Views: 2359
Reputation: 857
Old question, but xdist has a newer feature that may address the OP's answer.
Per the docs:
--dist loadgroup: Tests are grouped by the xdist_group mark. Groups are distributed to available workers as whole units. This guarantees that all tests with same xdist_group name run in the same worker.
@pytest.mark.xdist_group(name="group1")
def test1():
pass
class TestA:
@pytest.mark.xdist_group("group1")
def test2():
pass
This will make sure test1 and TestA::test2 will run in the same worker. Tests without the xdist_group mark are distributed normally as in the --dist=load mode.
Specifically, you could put the test functions in test_func3.py
into the same xdist_group.
Upvotes: 2
Reputation: 2682
There may be a case of test that affects some service settings. This test can not be run in parallel with any other test.
There is a way to skip individual test like this:
@pytest.mark.unparalleled
def test_to_skip(a_fixture):
if worker_id == "master":
pytest.skip('Can't run in parallel with anything')
The first drawback here is that it will be skipped, so you will need to run those test separately. For that matter you can put them in separate folder, or mark with some tag.
The second drawback is that any fixtures used in such will be initialized.
Upvotes: 0
Reputation: 4865
The pytest-xdist
package supports four scheduling algorithms:
Calling pytest -n
is a shortcut for load
scheduling, i.e. the scheduler will load balance the tests across all workers.
Using loadfile
scheduling, all test cases in a test file will be executed sequentially by the same worker.
pytest -n8 --dist=loadfile
will do the trick. The drawback may be that the whole test suite execution may be slower than using load
. The advantage is that all tests will be performed and no test will be skipped.
Upvotes: 3