pytest运行测试p

2024-04-29 13:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想并行而不是顺序地运行我的所有pytest测试。

我当前的设置如下:

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...

我使用pytest -v -s运行测试。

结果是,我的测试是按顺序运行的,这需要很多时间,因为有些测试等待来自远程服务器的响应(集成测试)。

有没有并行运行pytest的方法?


Tags: nametestselfid顺序pytestactivitygenerate
3条回答

pytest xdist对我不起作用,因为我的测试在99.99999999%的时间内等待AWS的粘合作业完成,而且我使用的是具有两个内核的代码构建环境,因此我一次只能运行两个测试。

@kevelend的方法对我有效。

你想要pytest-xdist。我认为Qxf2解释得很好:Qxf2 on Pytest-Xdist

他们的Linux命令行对我来说有点太冗长了;我使用:

pytest -n <NUM>

其中,<;NUM>;是并行工作线程的数目。

^{}对于大多数情况来说是一个很好的解决方案,但是集成测试是特殊的。向远程服务器发送请求后,可以在新线程上启动另一个测试,而不是等待响应。这是并行测试,而不是并行测试。并发性允许同时进行更多的测试,而内存和处理开销要少得多。

我编写了^{}插件[py3.6+]来启用并行和并发测试。下面是如何同时运行集成测试:

pytest --tests-per-worker auto

相关问题 更多 >