#规则: #①文件命名以test开头 #②类的名称 #③不能有初始化方法 #④测试函数必须以test开头 #⑤断言assert即可 import pytest class TestLoginCase(object): #测试类以Test开头 # def __init__(self): 不能定义init方法 def test01(self): #测试函数以test开头 print('test01') assert 1 ==1 def test02(self): print('test02') assert 1 == 2 if __name__ == '__main__': pytest.main(['test_pytest.py']) #main方法里面有个列表,列表里面的参数指的是测试模块的名称 # 输出结果collected(代表一个集合)这个集合里面有2 个item,也说执行两个测试 # if __name__ == '__main__': # pytest.main(['-s','test_pytest.py']) #加个参数-s,才能在控制台打印print内容,否则不会输出 # if __name__ == '__main__': # pytest.main(['-v','test_pytest.py']) #-v,用于显示每个测试函数的执行结果,一般都加这个 # if __name__ == '__main__': # pytest.main(['-q','test_pytest.py']) #-q,用于显示整体测试结果 # if __name__ == '__main__': # pytest.main(['-x','test_pytest.py']) #-x,在第一个错误或测试失败时立即退出,-x或者--exitfirst # if __name__ == '__main__': # pytest.main(['-h','test_pytest.py']) #-h,帮助 # if __name__ == '__main__': # pytest.main(['-vs','test_pytest.py']) #一般用-v合-s组合这两个参数
执行测试:
①配置pycharm
Tools - 》Python integrated tools - 》Default test runner
②main方法
pytest.main(['sv','模块名.py’])
③命令行
Python控制台下面在Terminal 执行pytest -s -v test.py
命令行的执行好处就是将来我们想把这个测试集成到其他环境里面去,比如集成到Jenkis里面。