zoukankan      html  css  js  c++  java
  • pytest之多进程运行测试用例(pytest-xdist)

    背景:

      我们日常的工作当中进行自动化测试编写的测试用例会非常多,测试用例一个一个的执行所需要花费的时间会很长,你想象一下如果开发改动一块代码,我们需要回归一下,这时候执行一下自动化用例需要花费一小时或者好几个小时的时间,这是我们无法容忍的,为了解决这个问题,我们采用pytest的插件pytest-xdist来进行多进程的并发执行测试用例,大大的缩短测试用例的执行时间,提高效率。

    并发运行测试用例:

    1.安装pytest-xdist

    pip install pytest-xdist

    2.多进程并发执行测试用例

    pytest test_add.py -n NUM    # NUM表示并发的进程数

    举例:

    项目结构如下:

     代码参考:

    # file_name: test_a.py
    
    import pytest
    import time
    
    
    def test_a_01():
        print("----------------->>> test_a_01")
        time.sleep(1)
        assert 1
    
    
    def test_a_02():
        print("----------------->>> test_a_02")
        time.sleep(1)
        assert 1
    
    
    def test_a_03():
        print("----------------->>> test_a_03")
        time.sleep(1)
        assert 1
    
    
    def test_a_04():
        print("----------------->>> test_a_04")
        time.sleep(1)
        assert 1
    
    
    if __name__ == '__main__':
        pytest.main(["-s", "test_a.py"])
    # file_name: test_b.py
    
    
    import pytest
    import time
    
    
    def test_b_01():
        print("----------------->>> test_b_01")
        time.sleep(1)
        assert 1
    
    
    def test_b_02():
        print("----------------->>> test_b_02")
        time.sleep(1)
        assert 1
    
    
    def test_b_03():
        print("----------------->>> test_b_03")
        time.sleep(1)
        assert 1
    
    
    def test_b_04():
        print("----------------->>> test_b_04")
        time.sleep(1)
        assert 1
    
    
    if __name__ == '__main__':
        pytest.main(["-s", "test_b.py"])

    正常运行以上代码,耗时:8.09s

     设置并行运行数量为4,耗时:3.48s,大大的缩短了测试用例的执行时间。

  • 相关阅读:
    jsp 认知(2)
    jsp 认知
    Java 匿名类
    Usage of readonly and const
    Javascript 原型继承(续)—从函数到构造器的角色转换
    JavaScript 原型继承开端
    JS 函数调用
    Javascript Basic Operation Extraction
    JS单词形式的运算符
    git问题收集
  • 原文地址:https://www.cnblogs.com/lwjnicole/p/14363452.html
Copyright © 2011-2022 走看看