• Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析


    pytest常用Console参数:

    • -v 用于显示每个测试函数的执行结果
    • -q 只显示整体测试结果
    • -s 用于显示测试函数中print()函数输出
    • -x 在第一个错误或失败的测试中立即退出
    • -m 只运行带有装饰器配置的测试用例
    • -k 通过表达式运行指定的测试用例
    • -h 帮助

    首先来看什么参数都没加的运行情况

    1. class TestClass():
    2. def test_zne(self):
    3. print(1)
    4. assert 1==2
    5. def test_two(self):
    6. print(2)
    7. assert 1==2
    8. def test_a(self):
    9. print(3)
    10. assert 1==1
    11. if __name__ == '__main__':
    12. pytest.main()
    13. ============================= test session starts =============================
    14. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    15. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    16. plugins: allure-pytest-2.9.45
    17. collected 3 items
    18. test_page.py FF. [100%]
    19. ================================== FAILURES ===================================

    -v 用于显示每个测试函数的执行结果

    用于打印显示每条用例的执行情况

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. def test_two(self):
    7. print(2)
    8. assert 1==2
    9. def test_a(self):
    10. print(3)
    11. assert 1==1
    12. if __name__ == '__main__':
    13. pytest.main(['-v'])
    14. ============================= test session starts =============================
    15. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Users\72036454\AppData\Local\Programs\Python\Python38\python.exe
    16. cachedir: .pytest_cache
    17. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    18. plugins: allure-pytest-2.9.45
    19. collecting ... collected 3 items
    20. test_page.py::TestClass::test_zne FAILED [ 33%]
    21. test_page.py::TestClass::test_two FAILED [ 66%]
    22. test_page.py::TestClass::test_a PASSED [100%]
    23. ================================== FAILURES ===================================

    -q 只显示整体测试结果

    简化测试整体结果。F:代表测试失败、.:代表测试通过

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. def test_two(self):
    7. print(2)
    8. assert 1==2
    9. def test_a(self):
    10. print(3)
    11. assert 1==1
    12. if __name__ == '__main__':
    13. pytest.main(['-q'])
    14. FF. [100%]
    15. ================================== FAILURES ===================================

    -s 用于显示测试函数中print()函数输出

    显示测试用例中 print() 中的值

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. def test_two(self):
    7. print(2)
    8. assert 1==2
    9. def test_a(self):
    10. print(3)
    11. assert 1==1
    12. if __name__ == '__main__':
    13. pytest.main(['-s'])
    14. ============================= test session starts =============================
    15. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    16. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    17. plugins: allure-pytest-2.9.45
    18. collected 3 items
    19. test_page.py 1
    20. F2
    21. F3
    22. .
    23. ================================== FAILURES ===================================

    -x 在第一个错误或失败的测试中立即退出

    第一条用例执行失败,立即退出不在往下执行用例

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. def test_two(self):
    7. print(2)
    8. assert 1==2
    9. def test_a(self):
    10. print(3)
    11. assert 1==1
    12. if __name__ == '__main__':
    13. pytest.main(['-x','-s'])
    14. ============================= test session starts =============================
    15. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    16. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    17. plugins: allure-pytest-2.9.45
    18. collected 3 items
    19. test_page.py 1
    20. F
    21. ================================== FAILURES ===================================

    -m 只运行带有装饰器配置的测试用例

    用例中,第二和第三条用例加上了装饰器,装饰器最后一个单词分别为“slow” 和 “faster” ,-m 拿着两个单词去识别带这个装饰器的用例,识别到就执行,没有识别到的就不执行。

    -m后面接的是表达式:['-s','-m slow or faster'] 、['-s','-m slow and faster']、['-s','-m not slow'] 这些表达式都支持。

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. @pytest.mark.slow
    7. def test_two(self):
    8. print(2)
    9. assert 1==2
    10. @pytest.mark.faster
    11. def test_a(self):
    12. print(3)
    13. assert 1==1
    14. if __name__ == '__main__':
    15. pytest.main(['-s','-m slow or faster'])
    16. ============================= test session starts =============================
    17. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    18. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    19. plugins: allure-pytest-2.9.45
    20. collected 3 items / 1 deselected / 2 selected
    21. test_page.py 2
    22. F3
    23. .
    24. ================================== FAILURES ===================================

    -k 通过表达式运行指定的测试用例

    通过表达式匹配用例的函数名去执行用例,not test_zne 意思是不执行“test_zne”这条用例,所以就会执行第二第三条。同理 ['-s','-k test_zne'] 表示只执行第一条。

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. @pytest.mark.slow
    7. def test_two(self):
    8. print(2)
    9. assert 1==2
    10. @pytest.mark.faster
    11. def test_a(self):
    12. print(3)
    13. assert 1==1
    14. if __name__ == '__main__':
    15. pytest.main(['-s','-k not test_zne'])
    16. ============================= test session starts =============================
    17. platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    18. rootdir: D:\Users\72036454\Desktop\pythonProject\Base
    19. plugins: allure-pytest-2.9.45
    20. collected 3 items / 1 deselected / 2 selected
    21. test_page.py 2
    22. F3
    23. .
    24. ================================== FAILURES ===================================

    -h 帮助

    这才是重点,学会使用这个,剩余的都学会了

    1. import pytest
    2. class TestClass():
    3. def test_zne(self):
    4. print(1)
    5. assert 1==2
    6. @pytest.mark.slow
    7. def test_two(self):
    8. print(2)
    9. assert 1==2
    10. @pytest.mark.faster
    11. def test_a(self):
    12. print(3)
    13. assert 1==1
    14. if __name__ == '__main__':
    15. pytest.main(['-h'])

    pytest的执行顺序:

    安装插件:

    pip install pytest-ordering

    pytest-ordering使用:

    方式一

    • 第一个执行:@pytest.mark.first
    • 第二个执行:@pytest.mark.second
    • 倒数第二个执行:@pytest.mark.second_to_last
    • 最后一个执行:@pytest.mark.last

    方式二

    • 第一个执行:@pytest.mark.run('first')
    • 第二个执行:@pytest.mark.run('second')
    • 倒数第二个执行:@pytest.mark.run('second_to_last')
    • 最后一个执行:@pytest.mark.run('last')

    方式三

    • 第一个执行:@pytest.mark.run(order=1)
    • 第二个执行:@pytest.mark.run(order=2)
    • 倒数第二个执行:@pytest.mark.run(order=-2)
    • 最后一个执行:@pytest.mark.run(order=-1)

    对于以上三张方法,经常使用的不多,第一个执行和最后一个执行比较常用。

    如果对你有帮助的话,点个赞收个藏,给作者一个鼓励。也方便你下次能够快速查找。      

  • 相关阅读:
    【教程】多进程下载百度旋转验证码图片-制作数据集
    三对角矩阵原理及C++实现
    MSDC 4.3 接口规范(18)
    c++一级练习题
    9.3 链表从指定节点插入新节点
    【HarmonyOS】鸿蒙传感器采样评率、鸿蒙设置屏幕常亮问题小结
    UML中用例和用例图的概念
    shell编程之循环
    ArcGIS按点提取栅格
    【C++】静态库.a和动态库.so文件的生成和使用
  • 原文地址:https://blog.csdn.net/qq_43371695/article/details/132737376