pytest
注意!
该组件是基于Python语言的
pip install pytest插件
插件是需要单独使用 pip 下载的,使用插件之前需要确保pytest本体已经安装完毕
插件列表:https://docs.pytest.org/en/stable/reference/plugin_list.html
常用插件如下
pytest-html生成html报告pytest-xdist分布式执行(多线程执行)pytest-order控制用例的执行顺序pytest-rerunfailures重新运行失败用例pytest-base-url设置基础路径(开发、测试、生产、预发布)allure-pytest生成allure报告pytest-dependency用例依赖,上级用例出错后,避免执行有关联的下级用例pytest-assume多重断言
用例规范
- 模块名 必须以
test_开头 或 以_test结尾 - 类名 必须以
Test开头,且不能有init方法 - 用例方法必须以
test_开头
使用方式
满足规范之后,直接在项目根目录下,打开命令行,输入pytest,用例就会自动执行
更快捷的方式是在项目根目录下,创建一个 run.py 文件
文件中保存如下代码作为入口,每次测试的时候,运行如下代码即可
import pytest
if __name__ == '__main__':
pytest.main()执行参数
通过在命令行中输入以下命令,可查看全部执行参数
pytest -h常用参数
-v:详细测试输出信息-s:执行测试用例中的print()语句-q:简化测试输出信息-m:只执行指定标记的用例
用法示例
pytest -vs或者
import pytest
if __name__ == '__main__':
pytest.main(['-vs'])再或者通过配置文件来做
配置文件
在项目根目录下创建 pytest.ini 文件
在文件中写入以下内容
[pytest]
addopts = -vs这样在执行pytest命令时就不需要手动添加参数了
当使用插件的配置时,只需要在该行参数后面直接添加就可以,中间以空格分隔
生成html报告文件
修改配置文件为如下内容
[pytest]
addopts = -vs --html=./report/report.html执行pytest命令后,在文件夹的根目录下就会生成一个report文件夹
在文件夹内,有一个report.html文件,该文件就是html报告文件
重新执行失败用例
修改配置文件为如下内容
[pytest]
addopts = -vs --reruns 2该配置会让失败的用例重新执行2次
需要执行的用例位置
[pytest]
# 执行该文件夹下的所有模块
testpaths = ./testcases
# 执行指定模块
testpaths = ./testcases/test_api.py指定执行testcases目录下的测试用例
修改默认规则
[pytest]
python_files = mahe_*.py默认规则是:模块必须以 test_ 开头 或 以 _test 结尾
在经过如上修改后,规则变为:必须以 mahe_ 开头
同理,类和函数也可以修改
[pytest]
python_files = mahe_*.py
python_classes = Mahe*
python_functions = mahe_*配置基础路径
[pytest]
base_url = https://www.baidu.com用例标记
[pytest]
markers =
business: 业务测试
smoke: 冒烟测试配置好标记后,需要在测试用例(函数)上面添加注解 @pytest.mark.标记名
然后在执行时,需要指定标记执行
# 执行单个
pytest -m 标记名
# 执行多个
pytest -m "标记名1 or 标记名2"前后置,固件,夹具
# 每个用例(函数)前后执行
def setup(self):
print('用例执行前')
def teardown(self):
print('用例执行后')# 每个类前后执行
def setup_class(self):
print('类执行前')
def teardown_class(self):
print('类执行后')不过这种方式在工作中并不常用,更多的是使用 Fixture固件 和 conftext.py文件 实现前后置
Fixture固件
使用方式是注解 @pytest.fixture()
该注解需要写在一个独立的函数上(不在测试用例的类里)
对于前后置问题,具体实现是通过代码来实现的,如下
@pytest.fixture()
def example_method():
print('前置执行')
yield
print('后置执行')注意:在一个文件中设置的Fixture固件只能在该文件内部使用。如果希望能全局访问,则需要结合 conftext.py 文件
注意:所有fixture固件的参数都是关键字参数
注意: fixture命名不要以test开头,要和测试用例区分开
scope参数,作用域
该参数作用是:让被该注解方法,在执行什么的时候进行前后置执行
固定的值有四个:
function: 方法class: 类module: 模块session: 会话
autouse参数,自动执行
该参数是一个bool值:True为自动执行,False为手动执行
自动执行的作用是:scope内所有测试用例全部执行
手动执行的作用是:只有在测试用例中手动执行该固件,才会执行
第一种调用方式
在测试用例的参数内添加需要执行的函数名
写法如下:
@pytest.fixture()
def surround_func():
print('前置执行')
yield
print('后置执行')
class TestClass:
def test_func(self, surround_func):
print('测试用例')一般情况下,只有 scope="function" 时,才需要手动执行
fixture固件也有返回值,按照如上示例,想接收 surround_func 方法的返回值,只需要在 test_func 中操作fixture固件的同名参数即可
第二种调用方式
@pytest.fixture()
def surround_func():
print('前置执行')
yield
print('后置执行')
class TestClass:
@pytest.mark.usefixtures("surround_func")
def test_func(self):
print('测试用例')如果想手动调用多个Fixture固件,只需要在注解里面添加即可,使用 , 分割
params参数,参数化
@pytest.fixture(params=[['mahe666', 123456], ['mahe888', 654321]])
# 参数名不可改变
def example_method(request):
print('前置执行')
# 注意,这里的param单词是单数形式,不是复数形式
yield request.param
print('后置执行')
class TestClass:
# 这里只能通过这种方式,才能获取参数
def test_func(self, example_method):
print(example_method)
print('测试用例')可以发现,由于fixture固件的参数有两个元素,所以,该方法调用了两次
ids参数,参数别名
@pytest.fixture(params=[['mahe666', 123456], ['mahe888', 654321]], ids=['data1', 'data2'])
def example_method(request):
print('前置执行')
yield request.param
print('后置执行')
class TestClass:
def test_func(self, example_method):
print(example_method)用来改变参数名称
没啥用,知道就行
name参数,固件别名
@pytest.fixture(name="common_method")
def example_method():
print('前置执行')
yield
print('后置执行')
class TestClass:
@pytest.mark.usefixtures("common_method")
def test_func(self):
print('测试用例')使用手动执行固件的时候,需要通过固件名称进行调用
但是通过name参数指定固件别名之后,再进行手动执行,就需要通过别名来访问了