Python pytest 并行运行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45733763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pytest run tests parallel
提问by Noy Mizrahi
I want to run all my pytest
tests in parallel instead of sequentially.
我想pytest
并行而不是顺序运行我的所有测试。
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
.
我使用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?
有没有办法并行运行pytest?
回答by Claire Nielsen
You want pytest-xdist
. I think Qxf2 explains it quite well: Qxf2 on Pytest-Xdist
你要pytest-xdist
。我认为 Qxf2 解释得很好:Pytest-Xdist 上的 Qxf2
Their Linux command-line is slightly too verbose for my tastes though; I use:
不过,他们的 Linux 命令行对于我的口味来说有点过于冗长;我用:
pytest -n <NUM>
where <NUM> is the number of parallel workers.
其中 <NUM> 是并行工作线程的数量。
回答by kevlened
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.
pytest-xdist
在大多数情况下是一个很好的解决方案,但集成测试是特殊的。向远程服务器发送请求后,可以在新线程上启动另一个测试,而不是等待响应。这是并发测试而不是并行测试。并发允许以更少的内存和处理开销同时进行更多的测试。
I wrote the pytest-parallel
plugin [py3.6+] to enable parallel and concurrent testing. Here's how to run your integration tests concurrently:
我编写了pytest-parallel
插件 [py3.6+] 来启用并行和并发测试。以下是同时运行集成测试的方法:
pytest --tests-per-worker auto
pytest --tests-per-worker auto