pytest测试框架系列 - Pytest pytest-assume 多重校验神器如何使用你知道吗
## 前言
**注意点:pytest-assume 导包和使用都已经改变,源码注释未进行更新**
场景
- 在一个支付场景里面,我们在商品界面进行了支付成功,想要看下支付金额是否正确、订单记录是否正确、剩余金额是否正确、支付页面金额显示等
- 就是一条用例执行完成,存在多个检查点
- 如果使用普通的断言方法就是写多个 `assert` 进行断言
这种情况存在一个弊端,如果其中一条断言失败了,其他断言就不会执行,这与我们测试的初衷就不一样。
为了解决这个问题,我们引入多重断言校验 ` pytest-assume` 插件
## pytest-assume详解 (建议掌握程度:☆☆☆☆☆)
### **安装**
- 在命令行窗口输入: `pip install pytest-assume`
- 查看安装版本:`pip show pytest-assume`
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210709213351126.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70)
### **使用**
**普通断言示例:**
```python
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time :2021/7/9 21:31
# @Author : king
# @File :test_assume.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
def add(a, b):
return a + b
def test_assume():
assert add(1, 2) == 4
assert 1 == 2
assert False
if __name__ == '__main__':
pytest.main()
```
执行结果:
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210709220531469.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70)
**pytest-assume 多重断言**
```python
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time :2021/7/9 21:31
# @Author : king
# @File :test_assume.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
from pytest_assume.plugin import assume
def add(a, b):
print("我开始执行")
return a + b
def test_assume():
with assume:
assert add(1, 2) == 4
assert 1 == 2
assert False
assert add(1, 3) == 4
if __name__ == '__main__':
pytest.main(["-s"])
```
执行结果:
```python
Testing started at 22:07 ...
F:\pytest_demo\Scripts\python.exe "D:\JetBrains\PyCharm 2019.2.5\helpers\pycharm\_jb_pytest_runner.py" --path E:/pytest_demo/class_06/test_assume.py
Launching pytest with arguments E:/pytest_demo/class_06/test_assume.py in E:\pytest_demo\class_06
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- F:\pytest_demo\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\pytest_demo\class_06
plugins: assume-2.4.3, rerunfailures-10.1
collecting ... collected 1 item
test_assume.py::test_assume FAILED [100%]我开始执行
test_assume.py:19 (test_assume)
def test_assume():
with assume:
> assert add(1, 2) == 4
E pytest_assume.plugin.FailedAssumption:
E 1 Failed Assumptions:
E
E test_assume.py:25: AssumptionFailure
E >> assert add(1, 3) == 4
E AssertionError: assert 3 == 4
test_assume.py:22: FailedAssumption
================================== FAILURES ===================================
_________________________________ test_assume _________________________________
def test_assume():
with assume:
> assert add(1, 2) == 4
E pytest_assume.plugin.FailedAssumption:
E 1 Failed Assumptions:
E
E test_assume.py:25: AssumptionFailure
E >> assert add(1, 3) == 4
E AssertionError: assert 3 == 4
test_assume.py:22: FailedAssumption
---------------------------- Captured stdout call -----------------------------
我开始执行
=========================== short test summary info ===========================
FAILED test_assume.py::test_assume - pytest_assume.plugin.FailedAssumption:
============================== 1 failed in 0.04s ==============================
Process finished with exit code 0
Assertion failed
Assertion failed
```
## 总结
- 普通 `assert` 遇见失败后面就不执行
- pytest_assume 会执行所有的 `assert` 断言
以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!
>**如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章**
个人微信号:搜索 【测试之路笔记】