Pytest-ordering用户自定义用例执行顺序,skip/skipif装饰器用法详解(三)

  Pytest-ordering用户自定义用例执行顺序以及skip/skipif装饰器用法

  

  Pytest提供了很强大的第三方库来实现执行顺序控制Pytest-ordering,用例设计之间应该是可以相互独立执行的,

没有一定的前后依赖关系的,如果我们真的有前后依赖,想指定用例的先后顺序,可以用到pytest-ordering插件解

决这个问题!

 

Pytest-ordering 插件下载:

  pip install  Pytest-ordering -i https://pypi.douban.com/simple

 

前言:

  Pytest用例执行顺序介绍:

  1.了解过Unittest单元测试框架的小伙伴都知道Unittest单元测试框架的执行顺序师按照ASCII的顺序进行执行

  2.Pytest单元测试框架是按照默认是按照代码的先后顺序进行执行

  3.pytest改变默认的执行顺序可以使用:mark装饰器进行标记

 

示例:

 

#执行全部case
class
Test_Case: def test01(self): print(" case 01") def test02(self): print(" case 02") def test03(self): print(" case 03") def test05(self): print(" case 04") def test06(self): print(" case 05")

if __name__ == "mian":
   pytest.main(['./case_api/test_case01.py',"-vs"])

响应示例:

case_api/test_case01.py::Test_Case::test01 case 01
PASSED
case_api/test_case01.py::Test_Case::test02 case 02
PASSED
case_api/test_case01.py::Test_Case::test03 case 03
PASSED
case_api/test_case01.py::Test_Case::test05 case 04
PASSED
case_api/test_case01.py::Test_Case::test06 case 05
PASSED

============================== 5 passed in 0.09s =======================



 

结果:以上可以看见,pytest 是按照默认代码顺序进行执行case

 

使用mark装饰器进行修饰case执行的顺序:

示例:

这里我使用 mark装饰器修饰了case的执行顺序 进行倒序,我们看下实际的执行结果

 

import pytest
class Test_Case:

    @pytest.mark.run(order=5)
    def test01(self):
        print(" case 01")

    @pytest.mark.run(order=4)
    def test02(self):
        print(" case 02")

    @pytest.mark.run(order=3)
    def test03(self):
        print(" case 03")

    @pytest.mark.run(order=2)
    def test05(self):
        print(" case 04")

    @pytest.mark.run(order=1)
    def test06(self):
        print(" case 05")

 

 

 

实际执行结果:

大家可以看到实际的输出结果顺序,是按照我的order顺序进行执行的

 

 

case_api/test_case01.py::Test_Case::test06  case 05
PASSED
case_api/test_case01.py::Test_Case::test05  case 04
PASSED
case_api/test_case01.py::Test_Case::test03  case 03
PASSED
case_api/test_case01.py::Test_Case::test02  case 02
PASSED
case_api/test_case01.py::Test_Case::test01  case 01
PASSED

============================== 5 passed in 0.07s ========================

 

 

pytest当中如果遇到不想执行的case怎么办呢? pyest也提供了跳过执行case的方法具体我们来看看

1)skip装饰器:直接跳过当前case

2)skipif装饰器: 条件成立跳过当前case

 

 

示例:skip装饰器进行标记case跳过case执行

import pytest
class Test_Case:
age = 18
@pytest.mark.run(order=5)
@pytest.mark.skip(reason="功能暂时不通过")
def test01(self):
print(" case 01")

@pytest.mark.run(order=4)
def test02(self):
print(" case 02")

@pytest.mark.run(order=3)
def test03(self):
print(" case 03")

@pytest.mark.run(order=2)
def test05(self):
print(" case 04")

@pytest.mark.run(order=1)
def test06(self):
print(" case 05")


#输出结果

case_api/test_case01.py::Test_Case::test06 case 05
PASSED
case_api/test_case01.py::Test_Case::test05 case 04
PASSED
case_api/test_case01.py::Test_Case::test03 case 03
PASSED
case_api/test_case01.py::Test_Case::test02 case 02
PASSED
case_api/test_case01.py::Test_Case::test01 SKIPPED (功能暂时不通过)

======================== 4 passed, 1 skipped in 0.11s =========================

输出结果当中大家可以清晰的看到,执行通过了4条cass 1条casa 没有执行

case_api/test_case01.py::Test_Case::test01 SKIPPED (功能暂时不通过)

 

 

示例二:skipif 条件判断跳过case,条件成立时跳过当前的case

 

import pytest
class Test_Case:
    
    @pytest.mark.run(order=5)
    @pytest.mark.skipif(1 == 1,reason="满足条件跳过用例") #
    def test01(self):
        print(" case 01")

    @pytest.mark.run(order=4)
    @pytest.mark.skipif(1 == 2, reason="不满足条件不跳过用例")
    def test02(self):
        print(" case 02")

    @pytest.mark.run(order=3)
    def test03(self):
        print(" case 03")

    @pytest.mark.run(order=2)
    def test05(self):
        print(" case 04")

    @pytest.mark.run(order=1)
    def test06(self):
        print(" case 05")

case_api/test_case01.py::Test_Case::test06  case 05
PASSED
case_api/test_case01.py::Test_Case::test05  case 04
PASSED
case_api/test_case01.py::Test_Case::test03  case 03
PASSED
case_api/test_case01.py::Test_Case::test02  case 02
PASSED
case_api/test_case01.py::Test_Case::test01 SKIPPED (满足条件跳过用例)

======================== 4 passed, 1 skipped in 0.08s =========================

 

 输出结果:大家可以清楚的看到skipif 条件成立的用例进行标记跳过了,不满足的条件的case

   执行成功

 

posted @ 2022-04-09 21:01  多测师-星sir  阅读(168)  评论(0)    收藏  举报