pytest简易教程(29):pytest常用插件 - 控制函数执行顺序(pytest-ordering)
pytest简易教程汇总,详见:https://www.cnblogs.com/uncleyong/p/17982846
应用场景
用例执行顺序,默认是按照从上到下的顺序进行执行的,详见:https://www.cnblogs.com/uncleyong/p/17956862
如果想自定义执行顺序,也就是改变执行优先级,那么可以使用pytest-ordering
插件安装
pip install pytest-ordering

使用方式
标记于被测试函数、方法、类:@pytest.mark.run(order=x),根据order值来决定运行顺序
x的值可以是正数、负数、0
order值全为正数或者负数,值越小,优先级越高
示例:order都为正数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
class Test01:
@pytest.mark.run(order=1)
def test_c(self):
print("---test_c")
@pytest.mark.run(order=3)
def test_b(self):
print("---test_b")
@pytest.mark.run(order=2)
def test_a(self):
print("---test_a")
结果:

示例:order都为负数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
class Test01:
@pytest.mark.run(order=-3)
def test_c(self):
print("---test_c")
@pytest.mark.run(order=-1)
def test_b(self):
print("---test_b")
@pytest.mark.run(order=-2)
def test_a(self):
print("---test_a")
结果:

优先级:order=0 > order=正数 > 无order > order=负数
示例:同时存在正数、负数、0、无装饰器的
#!/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")
@pytest.mark.run(order=-3)
def test_c(self):
print("---test_c")
@pytest.mark.run(order=0)
def test_b(self):
print("---test_b")
@pytest.mark.run(order=1)
def test_a(self):
print("---test_a")
结果:

补充:修饰测试类
示例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import pytest
@pytest.mark.run(order=-1)
class Test04:
def test_d2(self):
print("---test_d2")
def test_d(self):
print("---test_d")
class Test03:
def test_c2(self):
print("---test_c2")
def test_c(self):
print("---test_c")
@pytest.mark.run(order=1)
class Test02:
def test_a2(self):
print("---test_a2")
def test_a(self):
print("---test_a")
@pytest.mark.run(order=0)
class Test01:
def test_b2(self):
print("---test_b2")
def test_b(self):
print("---test_b")
结果:优先级也是order=0 > order=正数 > 无order > order=负数

__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号