前言:
pytest可以规定那些要跑,那些不跑,跑特定的哪些?比如以下的这个例子:
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest @pytest.mark.runtest def test_run(): print("run") def test_not_run(): pass def test_not_run1(): pass class TestClass: def test_method(self): pass if __name__ == "__main__": pytest.main(["-s", "test_case.py", "-m=runtest"]) #代表只跑标识为runtest的case
运行结果:显示一个执行,3个没有被选择
Testing started at 13:56 ... /usr/local/bin/python3.7 "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_pytest_runner.py" --path /Users/newcomer/gitByMyself/python_work_apple/pytest_package/mark/test_case.py -- -m=runtest Launching py.test with arguments -m=runtest /Users/newcomer/gitByMyself/python_work_apple/pytest_package/mark/test_case.py in /Users/newcomer/gitByMyself ============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 4 items / 3 deselected python_work_apple/pytest_package/mark/test_case.py .run [100%] ==================== 1 passed, 3 deselected in 0.01 seconds ====================
只运行用runcase标记的测试,cmd运行的时候,加个-m 参数,指定参数值runcase
如果不想执行runcase标记的case的时候,只需要在配置里面添加一个not runcase,比如以下:
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest def test_run(): print("run") @pytest.mark.runtest def test_not_run(): print("run") def test_not_run1(): print("run") class TestClass: def test_method(self): pass
运行结果:三个执行完,一个没有被选择
Testing started at 14:11 ... /usr/local/bin/python3.7 "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_pytest_runner.py" --path /Users/newcomer/gitByMyself/python_work_apple/pytest_package/mark/test_case_01.py -- "-m=not runtest" Launching py.test with arguments -m=not runtest /Users/newcomer/gitByMyself/python_work_apple/pytest_package/mark/test_case_01.py in /Users/newcomer/gitByMyself ============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 4 items / 1 deselected python_work_apple/pytest_package/mark/test_case_01.py .run .run . [100%] ==================== 3 passed, 1 deselected in 0.02 seconds ==================== Process finished with exit code 0
cmd命令:pytest -v -m “not runtest”
-v 注定的函数节点id:
如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面testmethod用例
每个test开头(或_test结尾)的用例,函数(或方法)的名称就是用例的节点id,指定节点id运行用-v 参数
$ pytest -v test_server.py::TestClass::test_method
当然也能选择运行整个class
$ pytest -v test_server.py::TestClass
也能选择多个节点运行,多个节点中间空格隔开
$ pytest -v test_server.py::TestClass test_server.py::test_send_http
-k配皮用例名称
可以使用-k命令行选项指定在匹配用例名称的表达式
pytest -v -k not_run
也可以运行所有的测试,根据用例名称排除掉某些用例:
pytest -k “not not_run” -v
也可以同时选择匹配 “run” 和“not not_run”
pytest -k “run and not not_run” -v
原文地址:https://www.cnblogs.com/peiminer/p/9922282.html
时间: 2024-10-09 09:51:09