Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest run tests parallel

Tags:

python

pytest

I want to run all my pytest tests in parallel instead of sequentially.

my current setup looks like:

class Test1(OtherClass):     @pytest.mark.parametrize("activity_name", ["activity1", "activity2"])     @pytest.mark.flaky(reruns=1)     def test_1(self, activity_name, generate_test_id):     """     """          test_id = generate_random_test_id()         test_name = sys._getframe().f_code.co_name          result_triggers = self.proxy(test_name, generate_test_id, test_id, activity_name)          expected_items = ["response"]         validate_response("triggers", result_triggers, expected_items)       @pytest.mark.parametrize("activity_name", ["activity1", "activity2"])     @pytest.mark.flaky(reruns=1)     def test_2(self, activity_name, generate_test_id):     """     """          #same idea... 

I run my tests using pytest -v -s.

The result is that my tests are running sequentially, which takes a lot of time since some of them wait for responses from remote servers (integration tests).

Is there any way of running pytest in parallel?

like image 396
Noy Mizrahi Avatar asked Aug 17 '17 11:08

Noy Mizrahi


People also ask

Can pytest run tests in parallel?

Learn Pytest From Scratch By default, pytest runs tests in sequential order. In a real scenario, a test suite will have a number of test files and each file will have a bunch of tests. This will lead to a large execution time. To overcome this, pytest provides us with an option to run tests in parallel.

Is pytest multithreaded?

This plugin makes it possible to run tests quickly using multiprocessing (parallelism) and multithreading (concurrency).

How do I run a specific test in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .


1 Answers

You want pytest-xdist. I think Qxf2 explains it quite well: Qxf2 on Pytest-Xdist

Their Linux command-line is slightly too verbose for my tastes though; I use:

pytest -n <NUM> 

where <NUM> is the number of parallel workers.

like image 198
Claire Nielsen Avatar answered Sep 28 '22 17:09

Claire Nielsen