遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

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

  

结果:

 

posted @ 2024-02-24 10:41  全栈测试笔记  阅读(97)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end