当写的用例比较多的的时候,我们需要对测试用例进行模块划分,比如,抽出一部分作为冒烟用例。部分用例只能在安卓系统上运行,部分用例只能在IOS上运行等等
这时候就要用到@pytest.mark的功能,给每条用例打上标签,方便运行
使用案例
import pytestimport sysclass Test_Mark():def test_mark_01(self):assert(1 == 1)@pytest.mark.IOS@pytest.mark.smokeTestdef test_mark_02(self):print("run in IOS")@pytest.mark.Androiddef test_mark_03(self):print("run in Android")@pytest.mark.IOSdef test_mark_04(self):print("run in IOS")if __name__ == '__main__':#pytest.main(['MyPytest.py', '-m', 'smokeTest'])#pytest.main(['MyPytest.py', '-m', 'IOS'])pytest.main(['MyPytest.py', '-m', 'smokeTest and IOS'])
运行结果:
collected 4 items / 3 deselected / 1 selected
collected 4 items / 3 deselected / 1 selectedMyPytest.py . [100%]============================== warnings summary ===============================MyPytest.py:9MyPytest.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@pytest.mark.IOSMyPytest.py:10MyPytest.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@pytest.mark.smokeTestMyPytest.py:14MyPytest.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@pytest.mark.AndroidMyPytest.py:18MyPytest.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@pytest.mark.Android-- Docs: https://docs.pytest.org/en/stable/warnings.html================= 1 passed, 3 deselected, 4 warnings in 0.07s =================***Repl Closed**
解决PytestUnknownMarkWarning的问题
- 创建一个 pytest.ini 文件(后续详解)
- 加上自定义 mark,如下

注意: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标识的用例
