ToB企服应用市场:ToB评测及商务社交产业平台

标题: Pytest框架 — 14、Pytest的标记(五)(控制测试用例执行顺序) [打印本页]

作者: 飞不高    时间: 2022-9-16 17:20
标题: Pytest框架 — 14、Pytest的标记(五)(控制测试用例执行顺序)
目录

1、前言

在执行自动化测试时,我们通常都希望能够控制执行测试用例的顺序。
安装方式:
pip install pytest-ordering
2、使用

直接在要控制顺序的测试用例上使用@pytest.mark.order(order=顺序值)装饰器来标记执行顺序。
示例:
  1. import pytest
  2. @pytest.mark.run(order=4)
  3. def test_pay():
  4.     print("第四步:支付订单")
  5. @pytest.mark.run(order=2)
  6. def test_add_cart():
  7.     print("第二步:加入购物车")
  8. @pytest.mark.run(order=1)
  9. def test_login():
  10.     print("第一步:登录")
  11. @pytest.mark.run(order=3)
  12. def test_place_order():
  13.     print("第三步:下订单")
  14. """
  15. 执行结果
  16. mark/ordering/pytest_ordering.py::test_login 第一步:登录
  17. PASSED
  18. mark/ordering/pytest_ordering.py::test_add_cart 第二步:加入购物车
  19. PASSED
  20. mark/ordering/pytest_ordering.py::test_place_order 第三步:下订单
  21. PASSED
  22. mark/ordering/pytest_ordering.py::test_pay 第四步:支付订单
  23. PASSED
  24. """
复制代码
注意:
3、标记最先执行和最后执行

可以通过@pytest.mark.firt和@pytest.mark.last来标记用例的最先执行和最后执行。
示例:
  1. import pytest
  2. @pytest.mark.first
  3. def test_login():
  4.     print("登录")
  5. @pytest.mark.last
  6. def test_logout():
  7.     print("注销")
  8. def test_place_order():
  9.     print("下单")
  10. def test_pay():
  11.     print("支付")
  12. """
  13. 执行结果
  14. mark/ordering/order_first_and_last.py::test_login 登录
  15. PASSED
  16. mark/ordering/order_first_and_last.py::test_place_order 下单
  17. PASSED
  18. mark/ordering/order_first_and_last.py::test_pay 支付
  19. PASSED
  20. mark/ordering/order_first_and_last.py::test_logout 注销
  21. PASSED
  22. """
复制代码
提示:
当我们在使用@pytest.mark.first和@pytest.mark.last装饰器时,python会把first和last当成自定义标记,从而出现如下提示
  1. PytestUnknownMarkWarning: Unknown pytest.mark is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
  2.     @pytest.mark.last
复制代码
此时我们可以在命令行中添加-p no:warnings来屏蔽错误提示。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4