发布于2019-08-20 17:35 阅读(1676) 评论(0) 点赞(1) 收藏(2)
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
参数目前还不清楚用法,待后续研究
作者:fggfg
链接:https://www.pythonheidong.com/blog/article/49478/f19134b37859862a3bf2/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!