pytest测试框架系列 - Pytest 用例之间相互依赖你会处理吗?

## 前言 场景:存在一个增删改查相关的操作功能,当增加操作用例失败时,删除、修改、查询操作不执行,这种场景该怎么来处理呢? Pytest 框架提供了一个`pytest-dependency` 插件帮我们做了这件事情,我们只需要简单的使用即可。 ## pytest-dependency 详解 (建议掌握程度:☆☆☆☆) ### 安装 - 在命令行窗口输入: `pip install pytest-dependency` - 查看安装版本:`pip show pytest-dependency` ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=2021070923443944.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 使用 使用方法 : 用 `@pytest.mark.dependency()`对所依赖的用例进行标记,使用`@pytest.mark.dependency(depends=["测试用例名称"])` 引用依赖,测试用例名称可以是多个 说明:当依赖的用例执行失败,被依赖的用例直接跳过 **依赖用例失败示例:** ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/9 23:54 # @Author  : king # @File   :test_dependency.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest @pytest.mark.dependency() def test_add(): print("我是 test_add 用例") assert False @pytest.mark.dependency(depends=["test_add"]) def test_update(): print("我是 test_update 用例") assert False @pytest.mark.dependency(depends=["test_add"]) def test_delete(): print("我是 test_delete 用例") assert True @pytest.mark.dependency(depends=["test_add"]) def test_select(): print("我是 test_select 用例") assert True if __name__ == '__main__': pytest.main(["-s"]) ``` **执行结果:** ```python Testing started at 23:59 ... F:\pytest_demo\Scripts\python.exe "D:\JetBrains\PyCharm 2019.2.5\helpers\pycharm\_jb_pytest_runner.py" --path E:/pytest_demo/class_05/test_dependency.py Launching pytest with arguments E:/pytest_demo/class_05/test_dependency.py in E:\pytest_demo\class_05 ============================= 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_05 plugins: assume-2.4.3, dependency-0.5.1, ordering-0.6, rerunfailures-10.1 collecting ... collected 4 items test_dependency.py::test_add FAILED [ 25%]我是 test_add 用例 test_dependency.py:13 (test_add) @pytest.mark.dependency() def test_add(): print("我是 test_add 用例") > assert False E assert False test_dependency.py:17: AssertionError test_dependency.py::test_update SKIPPED (test_update depends on test...) [ 50%] Skipped: test_update depends on test_add test_dependency.py::test_delete SKIPPED (test_delete depends on test...) [ 75%] Skipped: test_delete depends on test_add test_dependency.py::test_select SKIPPED (test_select depends on test...) [100%] Skipped: test_select depends on test_add ================================== FAILURES =================================== __________________________________ test_add ___________________________________ @pytest.mark.dependency() def test_add(): print("我是 test_add 用例") > assert False E assert False test_dependency.py:17: AssertionError ---------------------------- Captured stdout call ----------------------------- 我是 test_add 用例 =========================== short test summary info =========================== FAILED test_dependency.py::test_add - assert False ======================== 1 failed, 3 skipped in 0.04s ========================= Process finished with exit code 0 Assertion failed Assertion failed ``` **依赖用例成功示例:** ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/9 23:54 # @Author  : king # @File   :test_dependency.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest @pytest.mark.dependency() def test_add(): print("我是 test_add 用例") assert True @pytest.mark.dependency(depends=["test_add"]) def test_update(): print("我是 test_update 用例") assert False @pytest.mark.dependency(depends=["test_add"]) def test_delete(): print("我是 test_delete 用例") assert True @pytest.mark.dependency(depends=["test_add"]) def test_select(): print("我是 test_select 用例") assert True if __name__ == '__main__': pytest.main(["-s"]) ``` **执行结果:** ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210710000719459.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) **多个依赖用例存在失败示例:** ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/9 23:54 # @Author  : king # @File   :test_dependency.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest @pytest.mark.dependency() def test_add(): print("我是 test_add 用例") assert True @pytest.mark.dependency() def test_update(): print("我是 test_update 用例") assert False @pytest.mark.dependency(depends=["test_add", "test_update"]) def test_delete(): print("我是 test_delete 用例") assert True @pytest.mark.dependency(depends=["test_add"]) def test_select(): print("我是 test_select 用例") assert True if __name__ == '__main__': pytest.main(["-s"]) ``` **执行结果:** ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210710001123145.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ## 总结 - 用 `@pytest.mark.dependency()`对所依赖的用例进行标记,使用`@pytest.mark.dependency(depends=["测试用例名称"])` 引用依赖,测试用例名称可以是多个 - 用例多存在多个依赖时,只要存在一个依赖失败,被依赖用例就跳过,所有依赖成功才执行 以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处! >**如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章** 个人微信号:搜索 【测试之路笔记】
上一篇:分享给ICer我个人Mark下的学习网站集合


下一篇:【图像隐写】基于matlab DCT数字水印嵌入+检测+攻击(测试鲁棒性)【含Matlab源码 1133期】