pytest框架学习(命名规则、断言、运行方式、测试报告、allure、logging)
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
4、自己封装的断言方法
import pytest def case_assert(*arg): result = True if arg: for i in arg: if eval(f"i[1] {i[0]} i[2]"): print(f"断言内容:{i[1]}{i[0]}{i[2]}") else: result = False print(f"断言失败:{i[1]}{i[0]}{i[2]}") assert result def test_001(): case_assert(["==", 1, 1], ["<", '1', 2]) if __name__ == '__main__': pytest.main(['-vs'])
三、数据准备
pytest的fixture可以满足 初始化测试数据或对象
http://www.testclass.net/pytest/fixture
常用装饰器

四、钩子函数
执行顺序: setup_module、
测试类( setup_class、setup、测试用例、teardown、teardown_class )
测试函数( setup_function、测试用例、teardown_function)
teardown_module
def setup_module(): print("setup_module:所有case执行的前置条件,只运行一次") def teardown_module(): print("teardown_module:所有case执行的后置条件,只运行一次") def setup_function(): print("setup_function:每个测试用例的前置条件") def teardown_function(): print("teardown_function:每个测试用例的后置条件") class TestDemo: def setup_class(self): print("setup_class:所有case执行的前置条件,只运行一次") def teardown_class(self): print("teardown_class:所有case执行的后置条件,只运行一次") def setup(self): print("setup:每个测试用例的前置条件") def teardown(self): print("teardown:每个测试用例的后置条件") def test_002(self): pass def test_001(): pass if __name__ == '__main__': import pytest pytest.main(['-vs'])
2021年11月15日,发现一个好用的钩子函数,作用是能改变console中用例名称的显示
而且官方文档也提到了这个conftest.py,https://learning-pytest.readthedocs.io/zh/latest/doc/fixture/intro.html#
使用方式:全局使用的话,就在项目根目录创建 conftest.py,再添加如下代码
def pytest_itemcollected(item): clas = item.parent.obj func = item.obj clas_doc = f'({clas.__doc__.strip()})' if clas.__doc__ else '' func_doc = f'({func.__doc__.strip()})' if func.__doc__ else '' item._nodeid = f'★ {clas.__class__.__name__}{clas_doc}★ {func.__name__}{func_doc}'
效果如下图

五、运行方式
1、命令行的方式
直接运行:pytest
详细执行:pytest -v
显示输出流信息,比如print信息:pytest -s
运行标记的用例:pytest -m "标签名"
不运行标记的用例:pytest -m "not 标签名"
关键字匹配运行:pytest -k "关键字"
关键字匹配运行:pytest -k "not 关键字"
运行某个特定的测试用例:pytest 文件名.py::类名::方法名
遇到问题:attrs() got an unexpected keyword argument 'xxx'解决办法
pip install attrs==19.2.0
2、脚本中运行
if __name__ == '__main__': pytest.main(['-v','-s'])
将命令行中的命令以数组的方式填到main参数中的数组即可
六、测试报告
html测试报告插件: pip install pytest-html
在当前路径生成测试报告命令:pytest --html=./report.html
更多测试报告参考文章开头链接
七、allure
1、下载zip文件,解压后把bin目录添加到环境变量:https://github.com/allure-framework/allure2/releases
2、安装模块:pip install allure-pytest
3、常用装饰器:

4、生成allure-results的cmd命令:pytest --clean-alluredir --alluredir=allure-results
5、生成allure-report的cmd命令:allure generate -c -o allure-report 然后在allure-report目录打开index.html即可查看报告
八、logging
pip安装失败,提示如下语法错误:
raise NotImplementedError, 'emit must be implemented '
嫌麻烦就暂时放弃学习了,我有一篇博客用logbook模块实现日志的管理,如下链接:
https://www.cnblogs.com/yinwenbin/p/10596227.html
九、群里拿的图


浙公网安备 33010602011771号