pytest 并行运行测试 [英] pytest run tests parallel

查看:84
本文介绍了pytest 并行运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我当前的设置如下:

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 运行我的测试.

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

有没有办法并行运行pytest?

Is there any way of running pytest in parallel?

推荐答案

你想要 pytest-xdist.我认为 Qxf2 解释得很好:Pytest-Xdist 上的 Qxf2

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

不过,他们的 Linux 命令行对于我的口味来说有点过于冗长;我使用:

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.

这篇关于pytest 并行运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆