pytest用例除了常用的命令行运行方式外,还有另外一种运行方式,即为代码方式,通过pytest.main()
来执行,即可抓取此main文件所在的目录和同级下所有目录里的用例;
如果要使用命令行方式运行某些用例,则可以通过添加参数的方式来进行
def main(args=None, plugins=None):
""" return exit code, after performing an in-process test run.
:arg args: list of command line arguments.
:arg plugins: list of plugin objects to be auto-registered during
initialization.
"""
如上为main函数的说明,其有两个参数,第一个参数args
为pytest自带的各种参数,可以添加多个,为数组,plugins
参数亦为一个数组,可以添加与pytest匹配的各种插件参数;
args
参数此处可以添加的参数可以在命令行中用pytest --help
来看,每组参数为一个字符串,多个参数组合为一个数组即可,如下用几个基础使用方式来进行说明
譬如添加一个-v
参数,pytest.main(["-v"])
E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 7 items
Test_example.py::Test_example::test_aaa PASSED [ 14%]
Test_example.py::Test_example2::test_bbb PASSED [ 28%]
Test_simple.py::Test_simple::test_case1 PASSED [ 42%]
Test_simple.py::Test_simple::test_case2 FAILED [ 57%]
Test_simple.py::Test_simple::test_case3 PASSED [ 71%]
cases_test.py::Test_case::test_fff PASSED [ 85%]
test_case.py::Test_case::test_ddd PASSED [100%]
指定测试方法,main函数就应当这样写:pytest.main(["-v","test_case.py::Test_case::test_ddd"])
:
E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 1 item
test_case.py::Test_case::test_ddd PASSED [100%]
========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
使用pytest-html插件:
#-*- coding: utf-8 -*-
import pytest
if __name__ == '__main__':
pytest.main(args=["-v","test_case.py::Test_case::test_ddd","--html=report/report.html","--self-contained-html"])
运行结果:
E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 1 item
test_case.py::Test_case::test_ddd PASSED [100%]
--- generated html file: E:\pyspace\testSimple\testcase\report\report.html ----
========================== 1 passed in 0.04 seconds ===========================
Process finished with exit code 0
plugins
参数目前还不清楚用法,待后续研究