環(huán)境搭建:

下載python3.8的安裝包 https://www.python.org/downloads/windows/

下載IDE,使用的pycharm :https://www.jetbrains.com/pycharm/download/#section=windows

安裝框架  pip install httprunner

完成以上步驟,環(huán)境搭建完畢

-----------------------------------------------------------------------------------

第一步:搭建項(xiàng)目

使用手腳架

httprunner startproject test

在會(huì)在當(dāng)前目錄下搭建項(xiàng)目,并生成目錄結(jié)構(gòu)如下:

 

 

testcases目錄存放測(cè)試用例

在debugtalk.py 文件中進(jìn)行自定義方法編寫

.env 文件用于存放環(huán)境變量例如 用戶名/密碼等

reports目錄存放測(cè)試報(bào)告

第二步:生產(chǎn)測(cè)試用例

只介紹py文件測(cè)試用例,json和yaml文件可以在網(wǎng)上找到很多教程

可以使用抓包工具charles/fiddler對(duì)接口請(qǐng)求抓包

如下圖是charles實(shí)例:

 

 

 導(dǎo)出har文件后,在保存文件的目錄下執(zhí)行

har2case test.har

 

 

 將在當(dāng)前目錄下生產(chǎn)test_test.py的測(cè)試用例文件

 

 

 將test_test.py 轉(zhuǎn)移到項(xiàng)目的testcases目錄下

第三步:在debugtalk.py文件中自定義方法,如下截圖

 

 

第四步:優(yōu)化測(cè)試用例(testcases目錄下的文件) 

class TestCaseTest(HttpRunner):
    config = Config("測(cè)試") \
        .variables(
        **{
            "base_url": "${baseurl()}",
            "tim": "${tim()}"
        }
    ).verify(False)

    teststeps = [
#POST請(qǐng)求
        Step(
            RunRequest("teststep1")
                .with_variables(
                **{
                    "url": "/test/test",
                    "now": "${nowtime()}",
                    "tim": "$tim",
                    "para": {"test": "test"}
                   }",
                }
            )
                .post(
                "https://$base_url$url"
            )
                .with_headers(
                **{ "now": "$now",
                   "Connection": "Keep-Alive",
                   "Accept-Encoding": "gzip",
                   "User-Agent": "okhttp/4.2.2"}
            )
                .with_json(
                "$para"
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("description", "SUCCESS")
                .assert_equal("resultcode", "0")
        ),
# GET請(qǐng)求
Step(
    RunRequest("teststep2")
        .with_variables(
        **{
            "url": "test",
            "para": {},
            "now": "${nowtime()}",
}"

        }
    )
        .get("https://$base_url$url")
        .with_params(**{})
        .with_headers(
        **{
            "now": "$now",     
            "accept-language": "zh-Hans-AZ;q=1",
            "accept": "*/*",
            "content-type": "application/json;",
            "enterpriseid": "CC01",
            "accept-encoding": "gzip, deflate, br",
        }
    )
        .validate()
        .assert_equal("status_code", 200)
        .assert_equal("resultcode", "0")
),
] 

第六步:測(cè)試用例詳解

1、調(diào)用debugtalk.py中的方法,使用格式

${nowtime()}

2、如下:test是當(dāng)前測(cè)試用例的名稱

Config("test")

3、如下:variables定義整個(gè)測(cè)試用例的共用參數(shù)

 Config("測(cè)試") \
        .variables(
        **{
            "base_url": "${baseurl()}",
            "tim": "${tim()}"
        }

4、如下:verify專門是用于https請(qǐng)求取消對(duì)服務(wù)端證書校驗(yàn),默認(rèn)值是True(校驗(yàn)服務(wù)端證書),指定False(不校驗(yàn)服務(wù)端證書)

    config = Config("測(cè)試") \
        .variables(
        **{
            "base_url": "${baseurl()}",
            "tim": "${tim()}"
        }
    ).verify(False)

5、如下,Step測(cè)試步驟,需要嚴(yán)格按照如下格式編寫

RunRequest ("teststep1")  RunRequest 測(cè)試步驟執(zhí)行關(guān)鍵字,teststep1是測(cè)試步驟名稱

with_variables與config中的variables類似,定義當(dāng)前測(cè)試步驟中使用的參數(shù)

用例中公用參數(shù),調(diào)用方式

 "tim": "$tim"

post請(qǐng)求;post(url)

        Step(
            RunRequest("teststep1")
                .with_variables(
                **{
                    "url": "/test/test",
                    "now": "${nowtime()}",
                    "tim": "$tim",
                    "para": {"test": "test"}
                   }",
                }
            )
                .post(
                "https://$base_url$url"
            )
                .with_headers(
                **{ "now": "$now",
                   "Connection": "Keep-Alive",
                   "Accept-Encoding": "gzip",
                   "User-Agent": "okhttp/4.2.2"}
            )
                .with_json(
                "$para"
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("description", "SUCCESS")
                .assert_equal("resultcode", "0")
        ),

with_headers 請(qǐng)求的頭文件;with_json 請(qǐng)求參數(shù);validate斷言標(biāo)識(shí),assert_equal具體斷言內(nèi)容,還有其他斷言類型可以參看源碼;

get請(qǐng)求;get(url)

Step(
    RunRequest("teststep2")
        .with_variables(
        **{
            "url": "test",
            "para": {"test":"test"},
            "now": "${nowtime()}",
}"

        }
    )
        .get("https://$base_url$url")
        .with_params(**{"test":"test"})
        .with_headers(
        **{
            "now": "$now",     
            "accept-language": "zh-Hans-AZ;q=1",
            "accept": "*/*",
            "content-type": "application/json;",
            "enterpriseid": "CC01",
            "accept-encoding": "gzip, deflate, br",
        }
    )
        .validate()
        .assert_equal("status_code", 200)
        .assert_equal("resultcode", "0")
),

with_params 請(qǐng)求參數(shù)

獲取接口返回?cái)?shù)據(jù),使用extract().with_jmespath("body.data","data");body.data是獲取接口返回的數(shù)據(jù)路徑,data是自定義的參數(shù)名,用于其他step調(diào)用;如下格式:

 Step(
            RunRequest("teststep1")
                .with_variables(
                **{
                    "url": "test",
                    "para":{"test":"test"}
                }
            )
                .post(
                "https://$base_url$url"
            )
                .with_headers(
                **{
                    "Content-Type": "application/json;charset=UTF-8",
                    "Content-Length": "82",
                    "Connection": "Keep-Alive",
                    "Accept-Encoding": "gzip",
                    "User-Agent": "okhttp/4.2.2"
                }
            )
                .with_json(
                "$para"
            )
                .extract().with_jmespath("body.data","data")
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("data", "$data")
        ),

第七步:測(cè)試用例執(zhí)行,在項(xiàng)目根目錄下執(zhí)行,將測(cè)試報(bào)告保存在reports/test.html中,--self-contained-html 參數(shù)限定生成獨(dú)立的報(bào)告(可以直接將html文件分享)

hrun testcases --html=reports/test.html  --self-contained-html

 

 

根據(jù)實(shí)際使用情況,總結(jié)以上知識(shí)點(diǎn)備忘!