【问题标题】:Running unit tests in parallel with pytest? [duplicate]与 pytest 并行运行单元测试? [复制]
【发布时间】:2019-05-31 07:05:03
【问题描述】:

如何并行执行用 pytest 编写的单元测试?我可以选择哪种并行策略?

【问题讨论】:

    标签: python python-3.x unit-testing multiprocessing pytest


    【解决方案1】:

    为了并行运行 pytest,您需要安装 pytest-xdist。请参阅下面列出的不同并行策略,您可以使用其中任何一种(但我敢打赌,其中一种最适合您的特定情况):

    pip install pytest-xdist
    
    # The most primitive case, sending tests to multiple CPUs:
    pytest -n NUM
    
    # Execute tests within 3 subprocesses.
    pytest --dist=each --tx 3*popen//python=python3.6
    
    # Execute tests in 3 forked subprocess. Won't work on windows.
    pytest --dist=each --tx 3*popen//python=python3.6 --boxed
    
    # Sending tests to the ssh slaves
    pytest --dist=each --tx ssh=first_slave --tx ssh=seconds_slave --rsyncdir package package
    
    # Sending tests to the socket server, link is available below.
    python socketserver.py :8889 &
    python socketserver.py :8890 &
    pytest --dist=each --tx socket=localhost:8889 --tx socket=localhost:8890
    
    

    您可以为 --dist(-d) 参数提供不同的值,该参数处理测试在工作人员之间分布的方式,有关使用 --dist 的更多信息,请参阅文档。

    注意:一旦执行测试,socket_server.py 就会关闭。我建议您从单独的终端窗口运行套接字服务器以进行调试

    您可以引入更复杂的流程,例如在 docker 容器中运行测试,其中已启动套接字服务器类型的“pytest worker”和另一个与它们通信并充当“pytest runner”的 docker 容器。

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多