文章目录
在文章开始前,先了解一下本文章使用到工程结构情况,本栏会围绕该工程进行举例说明。
Test_project工程的根目录有两个模块(主函数main()模块,test_token.py模块)和一个testcase包,testcase包中新建了名为test_login.py模块。
test_token.py模块下有一个test_01的方法,test_login.py模块新建TestLogin类,类包含了test02和test03两个方法。
1. 默认规则
pytest的命名默认规则如下:
1. 模块名必须以test_开头或以_test结尾(如,test_login.py)
2. 测试类必须以Test开头,并且不能有init方法(如,class TestLogin:)
3. 测试方法必须以test开头(如,def test_01()或test02())
2. 运行方式
pytest运行方式主要有三种,main()函数运行、终端运行和main()结合配置文件方式运行。
2.1 main()函数运行
(1)运行所有测试用例:pytest.main()
(2)运行指定路径的用例:pytest.main( ["./路径"] )
(3)运行指定模块的用例:pytest.main( ["模块名.py"] )
(4)运行指定用例:pytest.main( [ " ./路径/模块名::类名::方法"]