测试用例-默认规则

1.模块名必须以test_开头或者以_test结尾  

2.测试类必须以Test开头,且不能有init方法

3.测试方法必须以test开头 

运行方式

1.主函数模式 (建议单独拿个模块来写主函数进行执行用例--放在根目录)

(1)运行所有模块用例:

if __name__ == "__main__":

pytest.main()

(2)运行指定模块用例:

if __name__ == "__main__":

pytest.main(['-vs','test_case1.py'])

(3)指定文件夹:

if __name__ == "__main__":

pytest.main(['-vs','./test_case'])

(4)通过nodeid指定用例运行:nodeid由模块名、分隔符、类名、方法名、函数名组成(::为分隔符,后面的为函数名)

if __name__ == "__main__":

pytest.main(['-vs','./test_case/test_case2.py::test_02'])

或者:

if __name__ == "__main__":

pytest.main(['-vs','./test_case/test_case2.py::Test_case2::test_03'])

2.命令行模式--在Terminal中执行

(1)运行所有用例:直接执行pytest

(2)运行指定模块用例--注意要先将路径进入到指定文件夹里面,不然在根目录会找不到:pytest -vs test_case2.py

3.读取pytest.ini配置文件运行

pytest.ini:pytest单元测试框架配置文件

(1) 一般放在项目根目录。名字必须为pytest.ini。

(2) 编码格式为:必须是ANSI,可以使用notpad++修改编码格式----改变pytest默认规则,如可以不用test_开头等

(3)运行规则:无论如何都会读取该配置文件

(3)默认格式:

注意:

a.中文注释建议删除

b.运行后如果出现编码格式报错,则需要使用notpad++修改编码格式为ANSI

参数详解

-S:输出模块名+调试信息(打印信息)

pytest.main(['-s'])    or     pytest -s 

-V:输出模块名+类名+方法名+执行结果(passed)

pytest.main(['-v'])    or     pytest -v

-N number:支持多线程或者分布式运行测试,number为线程数

pytest.main(['./test_case2','-n=2'])    or   pytest  test_case2.py -n 2

 注意:如果运行时出现了下面报错,则需要安装cpu插件【pytest-xdist】

        ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]         pytest: error: unrecognized arguments: -n

--reruns number:重复执行失败用例        

pytest.main([''./test_case2','--reruns==2'])    or   pytest test_case2.py --reruns 2

-X:只要有一个用例报错,测试停止

pytest.main(['-vs','./test_case','-x'])   or   pytest -vs test_case2.py -x

--maxfail=2:出现两个用例失败就停止

pytest.main(['-vs','./code/test_case','--maxfail==2'])    or    pytest -vs test_case2.py --maxfail 2

 -K:根据用例的部分字符串指定测试用例

pytest.main(['-vs','./code/testcase','-k=ao'])   or    pytest -vs test_2.py -k "ao"

 -M:用户标记名称---该命令的用法是在测试案例前使用

pytest.main(['-vs','-m=smoke'])   or    pytest -vs test_2.py -m "smoke"

注意:如果是ini运行,需要在ini中配置【markers = test:测试】

 -HTML:生成html测试报告

前置条件:pytest.ini中配置 addopts = --html ./report/report.html 

pytest.main(['-vs','--html=./report/report.html'])   or    pytest -vs test_2.py --html ./report/report.html

参考文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: