httprunner3.x 測試用例應用/變量傳遞(測試用例編寫)


用flask快速寫了2個接口,以供在本地調用:

from flask import Flask
from flask import request

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route(
'/getUserName', methods=['GET']) def get_user_name(): if request.method == 'GET': return { "username": "wesson", "age": "27", "from": "China", }
@app.route(
'/joinStr', methods=['GET']) def str_join(): if request.method == 'GET': str1 = request.args.get("str1") str2 = request.args.get("str2") after_join = str1 + " " + str2 return { "result": after_join }
if __name__ == '__main__': app.run()

一共有2個接口:

  1. /getUserName,查詢用戶名,返回是寫死的字典。
  2. /joinStr,兩個字符串拼接,返回的是拼接后的結果。

運行后可在瀏覽器查看:

 

 

 

 

一、編寫測試用例

因為這個接口沒有傳參,cookie之類的,就省掉了,只是demo用。

1. 接口:/getUserName

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseRequestWithGetUserName(HttpRunner):
    config = (
        Config("test /getUserName")
        .base_url("http://localhost:5000")
        .verify(False)
    )

    teststeps = [
        Step(
            RunRequest("getUserName")
            .get("/getUserName")
            .validate()
            .assert_equal("body.username", "wesson")
        ),

    ]


if __name__ == "__main__":
    TestCaseRequestWithGetUserName().test_start()

斷言是驗證返回的username字段值是不是“wesson”,運行一下,可以看到測試通過

     

若報錯:pytest運行報錯nknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin'

若報錯:httprunner 3運行報錯AttributeError: module 'allure' has no attribute 'severity_level'

 

2. 接口:/joinStr

這個接口就需要2個傳參了,那么在Step里通過.with_params()來傳參。

 

from httprunner import HttpRunner, Config, Step, RunRequest


class TestCaseRequestWithJoinStr(HttpRunner):
    config = (
        Config("test /joinStr")
        .base_url("http://localhost:5000")
        .verify(False)
    )

    teststeps = [
        Step(
            RunRequest("joinStr")
            .get("/joinStr")
            .with_params(**{"str1": "hello", "str2": "may"})
            .validate()
            .assert_equal("body.result", "hello may")
        ),

    ]


if __name__ == "__main__":
    TestCaseRequestWithJoinStr().test_start()

傳入的參數分別是“hello”、“may”,接口寫的這個字符串在拼接的時候是加了一個空格的,所以斷言的時候預期的值是"hello may"。
運行測試,可以看的測試通過。

 

 

 

二、testcase引用&變量傳遞

以上是2個分開的case,都可以分別正常運行。

假設,/joinStr接口的第二個參數,是依賴/getUserName接口的返回,那么現在這2個testcase之間就有了依賴關系。

那么在寫/getUserName接口用例的時候,就需要去引用/joinStr的測試用例了,並且需要把/getUserName用例的變量導出來,/joinStr的測試用例傳參時候使用。

 

1. 首先,先修改/getUserName接口的case:

from httprunner import HttpRunner, Config, Step, RunRequest


class TestCaseRequestWithGetUserName(HttpRunner):
    config = (
        Config("test /getUserName")
        .base_url("http://localhost:5000")
        .verify(False)
        .export(*["username"])  # 這里定義出要導出的變量

    )

    teststeps = [
        Step(
            RunRequest("getUserName")
            .get("/getUserName")
            .extract()
            .with_jmespath("body.username", "username")  # 提取出目標值,賦值給username變量
            .validate()
            .assert_equal("body.username", "wesson")
        ),

    ]


if __name__ == "__main__":
    TestCaseRequestWithGetUserName().test_start()

注釋部分的代碼,一個是config里定義了這個要導出的變量,另一個是在Step中,講目標值提取出來,賦值給變量username

2. 接下來,修改/joinStr接口的測試用例:調用上一個接口

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from .demo_testcase2_test import TestCaseRequestWithGetUserName  # 導入要引用的類


class TestCaseRequestWithJoinStr(HttpRunner):
    config = (
        Config("test /joinStr")
        .base_url("http://localhost:5000")
        .verify(False)
    )

    teststeps = [
        Step(
            RunTestCase("setUp getUserName")
            .call(TestCaseRequestWithGetUserName)  # 導入后就可以調用了
            .export(*["username"])  # 在RunTestCase步驟中定義這個變量的導出
        ),
        Step(
            RunRequest("joinStr")
            .get("/joinStr")
            .with_params(**{"str1": "hello", "str2": "$username"})  # 在第二個傳參中引用導出的變量
            .validate()
            .assert_equal("body.result", "hello $username")  # 斷言的預期值也引用變量
        ),

    ]


if __name__ == "__main__":
    TestCaseRequestWithJoinStr().test_start()

運行/joinStr接口的測試用例,可以看到運行通過

 

 

 

 

來源 https://www.cnblogs.com/pingguo-softwaretesting/p/13215627.html


免責聲明!

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



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