当写的用例比较多的的时候,我们需要对测试用例进行模块划分,比如,抽出一部分作为冒烟用例。部分用例只能在安卓系统上运行,部分用例只能在IOS上运行等等
这时候就要用到@pytest.mark的功能,给每条用例打上标签,方便运行

使用案例

  1. import pytest
  2. import sys
  3. class Test_Mark():
  4. def test_mark_01(self):
  5. assert(1 == 1)
  6. @pytest.mark.IOS
  7. @pytest.mark.smokeTest
  8. def test_mark_02(self):
  9. print("run in IOS")
  10. @pytest.mark.Android
  11. def test_mark_03(self):
  12. print("run in Android")
  13. @pytest.mark.IOS
  14. def test_mark_04(self):
  15. print("run in IOS")
  16. if __name__ == '__main__':
  17. #pytest.main(['MyPytest.py', '-m', 'smokeTest'])
  18. #pytest.main(['MyPytest.py', '-m', 'IOS'])
  19. pytest.main(['MyPytest.py', '-m', 'smokeTest and IOS'])

运行结果:
collected 4 items / 3 deselected / 1 selected

  1. collected 4 items / 3 deselected / 1 selected
  2. MyPytest.py . [100%]
  3. ============================== warnings summary ===============================
  4. MyPytest.py:9
  5. MyPytest.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.IOS - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
  6. @pytest.mark.IOS
  7. MyPytest.py:10
  8. MyPytest.py:10: PytestUnknownMarkWarning: Unknown pytest.mark.smokeTest - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
  9. @pytest.mark.smokeTest
  10. MyPytest.py:14
  11. MyPytest.py:14: PytestUnknownMarkWarning: Unknown pytest.mark.Android - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
  12. @pytest.mark.Android
  13. MyPytest.py:18
  14. MyPytest.py:18: PytestUnknownMarkWarning: Unknown pytest.mark.Android - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
  15. @pytest.mark.Android
  16. -- Docs: https://docs.pytest.org/en/stable/warnings.html
  17. ================= 1 passed, 3 deselected, 4 warnings in 0.07s =================
  18. ***Repl Closed**

解决PytestUnknownMarkWarning的问题

  • 创建一个 pytest.ini 文件(后续详解)
  • 加上自定义 mark,如下

image.png
注意:pytest.ini 需要和运行的测试用例同一个目录,或在根目录下作用于全局

运行参数说明

  • pytest -m “key1 or key2” #运行有key1标识或key2标识用例
  • pytest -m “key1 and key2” #运行有key1和key2标识的用例
  • pytest -m “not key1” #运行除了key1之外的标识的用例
  • pytest -m “key1 and not key2” #运行有key1和没有key2标识的用例