简介 是一个非常成熟的全功能的Python测试框架。 *简单易上手 *文档丰富 *支持简单的单元测试和复杂的功能测试,也可以做selenium/appnium自动化测试、接口自动化测试 *有很多第三方插件,pytest-selenium,pytest-html等
安装:pip install pytest
编写规则: 测试文件以test开头/结尾 测试类以Test开头,并且不能带init方法 测试函数以test开头 断言使用基本的assert即可
控制台参数配置:
执行测试: main方法:pytest.main(['-s','-v','test01.py']) 配置PyCharm执行:Tools->Python Integrated tools->Default test runner 命令行:pytest -s -v test.py
pytest标记: 查找策略: *默认情况下,pytest会递归查找当前目录下的所有test开头或结尾的Python脚本 *并执行文件内所有以test开始或结束的函数或方法
标记测试函数(存在未完成的的功能)
*显示指定函数名:pytest test_no_mark.py::test_func1(test_func1是未完成的功能)
*模糊匹配,使用k选项标记:pytest -k func1 test_no_mark.py
使用pytest.mark在函数上进行标记
第三种方法需要创建一个配置文件.ini
[pytest]
markers =
do:do
undo:undo
然后在用例上打上标记
演示:
需要加上一个-m(表示使用marker标记进行过滤):pytest -m *.py
pytest参数化处理: *pytest中,可以使用参数化测试,让每组参数独立执行一次 *使用的工具就是pytest.mark.parametrize(argnames,argvalues)
什么叫参数化? 例如用户登录这个功能的测试,正常要有用户名正确密码错误, 用户名错误,密码正确用户名正确密码,正确用户名错误密码错误这些情况,参数化就是以数据驱动程序,减少测试用例的编写。 data = ['123','456'] 密码列表 @pytest.mark.parametrize('pwd',data) def test1(pwd): print(pwd)
pytest fixture: *类似于unittest中的setUp()和tearDown() *命名时不以test开头,跟用例区分开,有返回值,没有返回值默认为null
@pytest.fixture() def init(): return 1
def test1(init): print('test1')
def test2(init): print('test2')
if name == 'main': pytest.main(['-sv','test.py'])