pytest简易教程(32):pytest常用插件 - 依赖执行(pytest-dependency)
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
应用场景
用例之间有依赖关系
注意:不分组并发执行会打乱依赖执行顺序,与pytest-xdist冲突
插件安装
pip install pytest-dependency

使用方式
函数或者方法上加装饰器:@pytest.mark.dependency(depends=["test_xxx"])
通过函数名指定依赖
示例:同时包含有依赖装饰器函数和无依赖装饰器函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
def test_d():
print("---test_d")
pass
@pytest.mark.dependency(depends=["test_a"])
# @pytest.mark.dependency(depends=["test_a","test_c"]) # 可以依赖多个
def test_b():
print("---test_b")
pass
@pytest.mark.xfail(reason="预期失败")
def test_a():
print("---test_a")
assert 1==2
def test_c():
print("---test_c")
pass
结果:
同时包含有dependency依赖和没有dependency依赖的函数,都是从上到下执行
test_b依赖test_a,且test_b在前面,test_b结果是skipped,说明执行test_b的时候是先执行了依赖的test_a且test_a执行结果是失败,只是下面没展示依赖执行过程
test_b下面展示的是test_a,因为test_a也是测试函数,会被执行,相当于test_a被执行了两次(可以通过真实请求验证,看日志)


示例:同时包含有依赖装饰器函数和有order装饰器函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
@pytest.mark.run(order=2)
def test_d():
print("---test_d")
pass
@pytest.mark.dependency(depends=["test_a"])
# @pytest.mark.dependency(depends=["test_a","test_c"]) # 可以依赖多个
def test_b():
print("---test_b")
pass
@pytest.mark.xfail(reason="预期失败")
def test_a():
print("---test_a")
assert 1==2
@pytest.mark.run(order=1)
def test_c():
print("---test_c")
pass
结果:有order的先执行

通过函数名别名指定依赖
通过name给函数取别名,此时用函数名和别名都可以
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
def test_d():
print("---test_d")
pass
@pytest.mark.dependency(depends=["d"]) # 依赖的不存在,结果也是skipped
def test_e():
print("---test_e")
pass
@pytest.mark.dependency(depends=["b"])
def test_f():
print("---test_f")
pass
# @pytest.mark.dependency(depends=["a"])
@pytest.mark.dependency(name="b", depends=["test_a","c"]) # 可以依赖多个
def test_b():
print("---test_b")
pass
@pytest.mark.dependency(name="a")
@pytest.mark.xfail(reason="预期失败")
def test_a():
print("---test_a")
assert 1==2
@pytest.mark.dependency(name="c")
def test_c():
print("---test_c")
pass
结果:
依赖可以传递,test_b依赖test_a,test_f依赖test_b
依赖的函数别名不存在,结果是skipped

类中测试方法通过类指定依赖
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
class Test01:
def test_d(self):
print("---test_d")
pass
@pytest.mark.dependency(depends=["test_b"]) # 类方法也可以直接指定依赖的方法
def test_e(self):
print("---test_e")
@pytest.mark.dependency(depends=["Test01::test_a","Test01::test_c"]) # 可以依赖多个
def test_b(self):
print("---test_b")
pass
@pytest.mark.xfail(reason="预期失败")
def test_a(self):
print("---test_a")
assert 1==2
def test_c(self):
print("---test_c")
pass
结果:

__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!

浙公网安备 33010602011771号