1、学习基于unittest扩展的pytest框架(由于nose对python支持性太差,就不想花精力去看了);
2、学习logging模块简单的应用
发现一个好的比较参考来源,对pytest讲的比较详细
https://www.jianshu.com/nb/33805779
生成测试报告:https://www.jianshu.com/p/8fa34a3c82bd
https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-report.html
(在unittest中,方法名以test开头的方法就是测试用例)
一、潜规则
pytest会找当前以及递查找子文件夹下面所有的test_*.py或*_test.py的文件,把其当作测试文件;
在这些文件里,pytest会收集下面的一些函数或方法,当作测试用例
1、不在类定义中的以test_开头的函数或方法
2、在以Test开头的类中(不能包含__init__方法),以test_开头的方法
二、断言
1、assert关键字后面可以接一个表达式,只要表达式的最终结果为True,那么断言通过,用例执行成功,否则用例执行失败;
2、表达式后面可添加备注信息,当断言失败时,备注信息会以assertionerror抛出,并在控制台输出;
3、异常断言,https://www.jianshu.com/p/14204b1723b1
unittest断言方法:https://www.cnblogs.com/NancyRM/p/8377721.html
三、数据准备
pytest的fixture可以满足 初始化测试数据或对象
http://www.testclass.net/pytest/fixture
四、运行方式
1、命令行的方式
直接运行:pytest
详细执行:pytest -v
显示输出流信息,比如print信息:pytest -s
运行标记的用例:pytest -m "标签名"
不运行标记的用例:pytest -m "not 标签名"
关键字匹配运行:pytest -k "关键字"
关键字匹配运行:pytest -k "not 关键字"
运行某个特定的测试用例:pytest 文件名.py::类名::方法名
2、脚本中运行
if __name__ == '__main__': pytest.main(['-v','-s'])
将命令行中的命令以数组的方式填到main参数中的数组即可
五、测试报告
html测试报告插件:pytest-html
在当前路径生成测试报告命令:pytest --html=./report.html
更多测试报告参考文章开头链接
六、logging
pip安装失败,提示如下语法错误:
raise NotImplementedError, 'emit must be implemented '
嫌麻烦就暂时放弃学习了,我有一篇博客用logbook模块实现日志的管理,如下链接: