问题
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?
回答1:
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.
回答2:
pytest-xdist is a great solution for most cases, but integration tests are special. After sending a request to a remote server, another test can start on a new thread instead of waiting for a response. This is concurrent testing instead of parallel. Concurrency allows many more tests at once with much less memory and processing overhead.
I wrote the pytest-parallel plugin [py3.6+] to enable parallel and concurrent testing. Here's how to run your integration tests concurrently:
pytest --tests-per-worker auto
回答3:
pytest-xdist didn't work for me, because my tests were waiting 99.9999999% of the time for glue jobs from AWS to finish, and I was using a CodeBuild environment with 2 cores, so I could only run 2 tests at a time.
@kevelend's approach worked for me.
来源:https://stackoverflow.com/questions/45733763/pytest-run-tests-parallel