httprunner學習20-跳過用例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.yml
get request
s

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK (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 requests

host = "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_token

if __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.yml
INFO     Loading environment variables from D:\soft\untitled\projectdemo\.env
get user info case1
INFO     GET http://127.0.0.1:8000/api/v1/user/info/
INFO     status_code: 200, response_time(ms): 261.48 ms, response_length: 190 bytes
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.276s

OK
INFO     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.yml
INFO     Loading environment variables from D:\soft\untitled\projectdemo\.env
大兄弟,返回的不是標准json格式,獲取沒取到token, 別問我為什么報錯, 返回內容:
 codemsg
get user info case1
s

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK (skipped=1)
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\untitled\projectdemo\reports\1571154164.html

skipIf 的用法和 skipUnless 恰好相反。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM