一.环境搭建
1.首先使用pip安装pytest
pip install pytest
pip install pytest-html 原生态报告模板
2.查看pytest是否安装成功
pip show pytest
二.Pytest执行测试用例
1.使用pytest执行用例需要准守的规则:
1、.py测试文件必须以_test开头(或者以_test结尾)
2、测试类必须以Test开头,并且不能有init方法
3、测试方法必须以test_开头
4、断言必须使用assert
2.pytest中有四只种setup和teardown:
1、setup_module和teardown_module在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次;
2、setup_class和teardown_class则在整个文件中的一次class中所有用例的前后运行;
3、setup_method和teardown_method在class内的每个方法运行前后运行;
4、setup_function和teardown_function则是在非class下属的每个测试方法的前后运行;
三、数据驱动
pytest是使用pytest.mark.parametrize装饰器来实现数据驱动效果
例如:
import pytest
# 参数化
@pytest