如下是目录结构:
1、主函数模式
(1)运行所有 pytest.main()
(2)指定模块 pytest.main(’[-vs],’,’./testcase/test_day1.py’)
(3)指定目录pytest.main(’[-vs]’),’./testcase’)
(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成
#pytest.main(["-vs"],’./interface_testcase/test_day3.py::test_demo11’)
#pytest.main(["-vs"],’./interface_testcase/test_day3.py::TestLogin::test_01_qianghong1’)
2、命令行模式
(1)运行所有:pytest
(2)指定模块 pytest -vs ./testcase/test_day1.py
(3)指定目录 pytest -vs ./testcase
(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成
pytest -vs ./interface_testcase/test_day3.py::test_demo11
pytest -vs ./interface_testcase/test_day3.py::TestLogin::test_01_qianghong1
参数详解:
-s:表示输出调试信息,包括print打印的信息
-v显示更详细的信息
-vs一起使用
-n支持多线程或者分布式运行测试用例
#如 #pytest.main([’-vs’,’./testcase/test_day1.py’,’-n=2’])
# pytest -vs ./testcase/test_day1.py -n 2
#reruns==number 表示失败用例重跑
#pytest -vs ./testcase/test_day2.py --reruns 2
#pytest.main([’–vs’,’./testcase/test_day2.py’,‘reruns=2’]) #失败得的用例重跑两次
#-x表示只要一个用例报错,那么测试停止运行
#–maxfail=2 出现两个失败用例停止
#-k 根据测试用例的部分字符串指定测试用例 pytest -vs test_day2 -k “yang”
3、通过读取pytest ini配置文件运行 (最主要运用的方式)
#pytest.ini是pytest单元测试框架中的核心配置文件
(1)位置:一般是放在项目的根目录
(2)编码:必须是ANSI,可以使用notepad++来修改编码格式
(3)作用:改变pytest的默认行为
(4)运行的规则:不管是主函数的模式运行该,命令行模式,都会区读取这个配置文件
[pytest]
addopts=-vs #命令行参数 用空格分离
testpaths=./testcase #测试用例的路径 ./testcase/test_day2.py #
python_files=test_*.py #模块名的规则
python_classes=Test* #类名的规则
python_functions=test #方法名的规则
#实际项目配置中,尽量删除注释,确保不必要的问题出现