安装命令:
pip install pytest -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
安装测试报告:pip install pytest-html
框架注意事项:
- .py测试文件必须以test_开头
- 测试类必须以Test开头,并且不能有init方法
- 测试方法必须以test开头
- 断言必须用assert
执行:
法一:cmd进入测试文件所在的目录:pytest test_login.py -s -s输入print信息
指定报告格式及路径:pytest xxx.py --html=report.html
pytest xxx.py --html=result/report.html #更改保存路径
报告独立显示:pytest --html=report.html --self-contained-html
法二:测试执行
if __name__=="__main__": pytest.main(['-s'])
conftest:test_xxx.py测试文件中无需import conftest,pytest会自动搜索同级目录中的conftest.py文件
在pytest中有四种setup和teardown
- setup_module和teardown_module在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次
- setup_class和teardown_class则在整个文件中的一个class中所有用例的前后运行
- setup_method和teardown_method在class内的每一个方法运行前后运行
- setup_function和teardown_function则是在非class下属的每一个测试方法的前后运行
数据驱动
1、数据驱动基本语法
@pytest.mark.parametrize('x,y',[(1,2),(3,4),(5,6)]) #装饰器