httprunner学习19-跳过用例skip/skipIf/skipUnless

前言

在实际工作中,我们有时候会需要对测试用例加判断,比如某个接口功能暂时去掉了,我们希望对这个用例skip不去执行。
当其它的接口依赖于登陆接口返回的token时候,如果登陆都失败了,后面的接口,我们希望也不用执行了。httprunner 中可以用 skipskipIf 去实现此功能。
unittest提供了一些跳过指定用例的方法

  • @unittest.skip(reason):强制跳转。reason是跳转原因
  • @unittest.skipIf(condition, reason):condition为True的时候跳转
  • @unittest.skipUnless(condition, reason):condition为False的时候跳转
  • @unittest.expectedFailure:如果test失败了,这个test不计入失败的case数目

httprunner 框架延用了 skip/skipIf/skipUnless 三个功能

skip跳过用例

skip是无条件跳过用例,不执行此用例,后面可以加上描述跳过此用例的原因

- config: name: httpbin api test request: base_url: http://www.httpbin.org- test: name: get request skip: 此功能已去除,skip掉 times: 1 request: url: /get method: GET validate: - eq: [status_code,200]

执行结果会显示此用例已经 skipped 跳过了

D:\soft>hrun test_httpbin.ymlget requests----------------------------------------------------------------------Ran 1 test in 0.002sOK (skipped=1)INFO Start to render Html report ...INFO Generated Html report: D:\soft\reports\1571152267.html

skipIf 和 skipUnless

比如我们现在的业务场景是,有个登陆的接口获取token,其它的接口用例依赖与登陆的token,可以在debugtalk.py写个获取登陆的函数获取token值
具体参考前面这篇https://www.cnblogs.com/yoyoketang/p/11588363.html

import requestshost = "http://127.0.0.1:8000/"def token(user="test", psw="123456"): ''' 登录获取token # # 上海悠悠,QQ交流群:750815713 :param user: 用户名 :param psw: 密码 :return: token ''' login_url = host+"api/v1/login/" headers = { "Content-Type": "application/json" } body = { "username": user, "password": psw } r = requests.post(login_url, headers=headers, json=body) try: return_token = r.json()["token"] except: print("大兄弟,返回的不是标准json格式,或者没取到token, 别问我为什么报错, 因为返回内容:\n %s" % r.text) return_token = '' return return_tokenif __name__ == "__main__": print("获取到token值:%s" % token())

函数 token() 实现的功能是获取到返回token值,登陆失败没token值,或者token值为空时默认返回None。那么获取到为True,没获取到为False。

  • skipIf 条件成立,返回值为True时候成立
  • skipUnless 条件不成立,返回值为False时候成立

接下来在用例里面写个判断,当函数 token() 为False的时候跳过用例,所以这里用skipUnless。
先调用 ${token(test1, 12345622) 函数,把返回值传给变量 token , 后面的用例全部引用 $token 这个变量就可以了。

- config: name: logincase variables: - token: ${token(test1, 123456)}# 上海悠悠,QQ交流群:750815713- test: name: get user info case1 skipUnless: $token request: url: http://127.0.0.1:8000/api/v1/user/info/ method: GET headers: Content-Type: application/json User-Agent: python-requests/2.18.4 Authorization: Token $token # 引用token validate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json] - eq: [content.0.age, 20] - eq: [content.0.name, yoyo] - eq: [content.0.mail, 283340479@qq.com]

接下来执行用例,是可以正常运行的

D:\soft\untitled\projectdemo>hrun test_skip_demo.ymlINFO Loading environment variables from D:\soft\untitled\projectdemo\.envget user info case1INFO GET http://127.0.0.1:8000/api/v1/user/info/INFO status_code: 200, response_time(ms): 261.48 ms, response_length: 190 bytesINFO start to validate..----------------------------------------------------------------------Ran 1 test in 0.276sOKINFO Start to render Html report ...INFO Generated Html report: D:\soft\untitled\projectdemo\reports\1571153932.html

把用例里面 token: ${token(test1, 123456111)} 密码改成错误的密码,这样获取不到token值,就会跳过此用例

D:\soft\untitled\projectdemo>hrun test_skip_demo.ymlINFO Loading environment variables from D:\soft\untitled\projectdemo\.env大兄弟,返回的不是标准json格式,获取没取到token, 别问我为什么报错, 返回内容: codemsgget user info case1s----------------------------------------------------------------------Ran 1 test in 0.001sOK (skipped=1)INFO Start to render Html report ...INFO Generated Html report: D:\soft\untitled\projectdemo\reports\1571154164.html

skipIf 的用法和 skipUnless 恰好相反。

相关文章