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=负数
标签:插件,ordering,---,print,pytest,test,order,def From: https://www.cnblogs.com/uncleyong/p/17956989