httprunner2.x--跳过用例指定步骤:skip、skipIf、skipUnless

HttpRunner 还可以用 skip 关键字跳过某个用例步骤的执行。而且这个功能也是借助于 unittest 的skip来实现的。

 

HttpRunner 跳过步骤执行包括以下几个关键字:

  • skip: reason 确定跳过本步骤
  • skipIf: condition 如果条件成立,则跳过本步骤
  • skipUnless: condition, reason 如果条件不成立,则跳过本步骤

这三种方法中,skip 可以立即跳过执行,但是 skipIf 和 skipUnless 需要进行一次条件判断,在 HtpRunner 的测试用例中,不能直接编写表达式,但是可以调用外部文件(debugtalk.py)提前编写好的函数,在该函数中实现条件判断。

源码httprunner-2.5.5\httprunner\runner.py:

from unittest.case import SkipTest
......
 def _handle_skip_feature(self, test_dict):
        """ handle skip feature for test
            - skip: skip current test unconditionally
            - skipIf: skip current test if condition is true
            - skipUnless: skip current test unless condition is true
            ...............
        """
        # TODO: move skip to initialize
        skip_reason = None
 
        if "skip" in test_dict:
            skip_reason = test_dict["skip"]
 
        elif "skipIf" in test_dict:
            skip_if_condition = test_dict["skipIf"]
            if self.session_context.eval_content(skip_if_condition):
                skip_reason = "{} evaluate to True".format(skip_if_condition)
 
        elif "skipUnless" in test_dict:
            skip_unless_condition = test_dict["skipUnless"]
            if not self.session_context.eval_content(skip_unless_condition):
                skip_reason = "{} evaluate to False".format(skip_unless_condition)
 
        if skip_reason:
            raise SkipTest(skip_reason)

skip 方法使用格式:  

- test:
    skip: <跳过本步骤的原因> 

 

测试用例

config:
    name: 跳过测试步骤

teststeps:
-
    name: 步骤1-正常
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]
-
    name: 步骤2-跳过
    # 此步骤执行将被跳过
    skip: ok
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]
-
    name: 步骤3-正常
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]

 

执行

 

 

 

 

 

 skipIf 和 skipUnless 方法使用格式:

- test:
    # 调用 debugtalk.py 文件中的 func 函数
    # 如果函数返回为“真”,则跳过本步骤; 否则不跳过。
    skipIf: ${func(a,b)}

  

- test:
    # 调用 debugtalk.py 文件中的 func 函数
    # 如果函数返回为“假”,则跳过本步骤; 否则不跳过。
    skipUnless: ${func(a,b)}

测试用例  

config:
    name: 跳过测试步骤
    variables:
        x: True
teststeps:
-
    name: 步骤1
    # 本步骤没有使用 skip 相关关键字,不会被跳过
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]
-
    name: 步骤2
    # 本步骤使用了 skipUnless关键字,判断 x 变量的取值
    # 由于前面定义了 x 为“假”,所以本步骤将不允许被执行
    skipUnless: $x
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]
-
    name: 步骤3
    # 本步骤使用了 skipIf 关键字,判断 x 变量的取值
    # 由于前面定义了 x 为“真”,所以本步骤将被执行
    skipIf: $x
    api: api/baidu_validate.yml
    validate:
        - eq: ["status_code", 200]

  

执行该用例

  • 第一个步骤,没有使用 skip 相关的关键字,被正常执行。
  • 第二个步骤,使用了 skipUnless 关键字,而且判断条件成立,所以该步骤没有被跳过,而是被执行。
  • 第三个步骤,使用了 skipIf 关键字,而且判断条件成立,所以该步骤被跳过(显示 s),未被执行。

 

 

 

posted @ 2021-06-04 10:45  莫使娇躯空对月  阅读(362)  评论(0编辑  收藏  举报