2020-08-11

HttpRunner3.X開源接口測試框架學(xué)習(xí)(二)

debugtalk.py

每個項(xiàng)目應(yīng)該并且只能有一個debugtalk.py文件。該文件具有多種功能古沥。

作為項(xiàng)目的根路徑錨素标,測試用例中的相對路徑(例如引用測試用例或CSV文件)都基于此根路徑

存儲自定義python函數(shù)称诗,在測試用例中調(diào)用的函數(shù)均在此文件中定義

HttpRunner框架中的變量

全局變量

全局變量在.env文件中定義,編寫測試用例腳本時(shí)使用中${ENV(KEY)} 獲取變量

pytest格式


用例級變量

在使用pytest格式編寫測試用例時(shí)头遭,variables用來定義變量寓免,請注意如果全局環(huán)境變量與測試用例腳本中存在相同變量名,則優(yōu)先使用用例級變量计维。即:測試用例變量>導(dǎo)出變量>測試套件配置變量>引用的測試用例配置變量


創(chuàng)建HttpRunner項(xiàng)目

httprunner框架提供了自動生成手腳架項(xiàng)目的功能袜香,可以快速創(chuàng)建新項(xiàng)目。

#使用httprunner命令創(chuàng)建項(xiàng)目

httprunnerstartprojectdemo創(chuàng)建名為demo的httprunner項(xiàng)目

#自動生成的文件目錄如下: ? ? ?

createdfolder:demo/har#har接口錄制導(dǎo)出文件鲫惶,用于生成測試腳本

createdfolder:demo/reports? ? ? ? ? ? ? ? #測試報(bào)告存放目錄? ? ? ? ? ? ? ? ?

createdfolder:demo/testcases#測試用例存放目錄

createdfile:demo/testcases/demo_testcase_request.yml#測試用例模板示例

createdfile:demo/testcases/demo_testcase_ref.yml#測試用例模板示例

createdfile:demo/debugtalk.py#debugtalk.py文件? #用于編寫自定義函數(shù)

createdfile:demo/.env? ? ? ? ? ? ? ? #全局環(huán)境變量定義文件

createdfile:demo/.gitignore? ? ? ? ?


示例創(chuàng)建baidutest項(xiàng)目:

C:\github>httprunnerstartprojectbaidutest

2020-08-1114:32:19.860|INFO|httprunner.scaffold:create_scaffold:43-Createnewproject:baidutest

ProjectRootDir:C:\github\baidutest

?

createdfolder:baidutest

createdfolder:baidutest\har

createdfolder:baidutest\testcases

c:\programs\python\python38\lib\site-packages\urllib3\request.py:171:InvalidProxyConfigurationWarning:YourproxyconfigurationspecifiedanHTTPSschemefortheproxy.AreyousureyouwanttouseHTTPStocontacttheproxy?Thismostlikelyindicatesanerrorinyourconfiguration.Readthisissueformoreinfo:https://github.com/urllib3/urllib3/issues/1850

returnself.urlopen(method,url,**extra_kw)

createdfolder:baidutest\reports

createdfile:baidutest\testcases\demo_testcase_request.yml

createdfile:baidutest\testcases\demo_testcase_ref.yml

createdfile:baidutest\debugtalk.py

createdfile:baidutest\.env

createdfile:baidutest\.gitignore

?

$treebaidutest-a

2020-08-1114:32:19.921|WARNING|httprunner.scaffold:show_tree:29-treecommandnotexists,ignore.

Sentryisattemptingtosend0pendingerrormessages

Waitingupto2seconds

PressCtrl-Breaktoquit

編寫httprunner框架接口測試用例

httprunner提供了自動生成測試腳本的功能蜈首,利用har2case? xxx.har命令可以將抓包工具導(dǎo)出的har文件轉(zhuǎn)換為pytest格式測試用例(默認(rèn)格式),也可以轉(zhuǎn)換成yaml欠母、json格式欢策。

使用抓包工具錄制業(yè)務(wù):

如圖使用charles工具錄制導(dǎo)出har文件


har2case命令詳解:

$har2case-h

usage:har2casehar2case[-h] [-2y] [-2j] [--filterFILTER]

[--excludeEXCLUDE]

[har_source_file]

?

positionalarguments:

har_source_fileSpecifyHARsourcefile

?

optionalarguments:

-h,--helpshowthishelpmessageandexit

-2y,--to-yml,--to-yaml

ConverttoYAMLformat,ifnotspecified,convertto

pytestformatbydefault.

-2j,--to-jsonConverttoJSONformat,ifnotspecified,convertto

pytestformatbydefault.

--filterFILTERSpecifyfilterkeyword,onlyurlincludefilterstring

willbeconverted.

--excludeEXCLUDESpecifyexcludekeyword,urlthatincludesexclude

stringwillbeignored,multiplekeywordscanbe

joinedwith'|'

執(zhí)行har2case? doctest.har 命令后會在當(dāng)前文件夾生成doctest_test.py測試用例文件

C:\github\baidutest\har>har2casedoctest.har

2020-08-1114:51:57.340c:\programs\python\python38\lib\site-packages\urllib3\request.py:171:InvalidProxyConfigurationWarning:YourproxyconfigurationspecifiedanHTTPSschemefortheproxy.AreyousureyouwanttouseHTTPStocontacttheproxy?Thismostlikelyindicatesanerrorinyourconfiguration.Readthisissueformoreinfo:https://github.com/urllib3/urllib3/issues/1850

returnself.urlopen(method,url,**extra_kw)

|INFO|httprunner.ext.har2case.core:gen_testcase:356-StarttogeneratetestcasefromC:\github\baidutest\har\doctest.har

2020-08-1114:51:57.354|INFO|httprunner.ext.har2case.core:_make_testcase:347-ExtractinfofromHARfileandpreparefortestcase.

2020-08-1114:51:57.358|INFO|httprunner.compat:ensure_testcase_v3:219-ensurecompatibilitywithtestcaseformatv2

2020-08-1114:51:57.438|INFO|httprunner.loader:load_dot_env_file:127-LoadingenvironmentvariablesfromC:\github\baidutest\.env

2020-08-1114:51:57.456|DEBUG|httprunner.utils:set_os_environ:33-SetOSenvironmentvariable:USERNAME

2020-08-1114:51:57.462|DEBUG|httprunner.utils:set_os_environ:33-SetOSenvironmentvariable:PASSWORD

2020-08-1114:51:57.471|INFO|httprunner.make:make_testcase:349-starttomaketestcase:C:\github\baidutest\har\doctest.har

2020-08-1114:51:57.480|INFO|httprunner.make:make_testcase:442-generatedtestcase:C:\github\baidutest\har\doctest_test.py

2020-08-1114:51:57.484|INFO|httprunner.make:format_pytest_with_black:170-formatpytestcaseswithblack...

reformattedC:\github\baidutest\har\doctest_test.py

Alldone!????

1filereformatted.

2020-08-1114:51:58.705|INFO|httprunner.ext.har2case.core:gen_testcase:377-generatedtestcase:C:\github\baidutest\har\doctest_test.py


doctest_test用例文件:

# NOTE: Generated By HttpRunner v3.1.4

# FROM: har\doctest.har

?

?

fromhttprunnerimportHttpRunner,Config,Step,RunRequest,RunTestCase

?

?

classTestCaseDoctest(HttpRunner):

?

config=Config("testcase description").verify(False)

?

teststeps= [

Step(

RunRequest("/controller/login/login.html")

.get("http://www.doclever.cn/controller/login/login.html")

.with_headers(

**{

"Host":"www.doclever.cn",

"Upgrade-Insecure-Requests":"1",

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",

"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",

"Referer":"http://www.doclever.cn/controller/index/index.html",

"Accept-Encoding":"gzip, deflate",

"Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8",

"Cookie":"pgv_pvid=7266193040",

"Connection":"keep-alive",

? ? ? ? ? ? ?? }

? ? ? ? ?? )

.with_cookies(**{"pgv_pvid":"7266193040"})

.validate()

.assert_equal("status_code",200)

.assert_equal('headers."Content-Type"',"text/html")

? ? ?? ),

Step(

RunRequest("/user/login")

.post("http://www.doclever.cn:8090/user/login")

.with_headers(

**{

"Host":"www.doclever.cn:8090",

"Content-Length":"28",

"Accept":"application/json, text/plain, */*",

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",

"content-type":"application/x-www-form-urlencoded",

"Origin":"http://www.doclever.cn",

"Referer":"http://www.doclever.cn/controller/login/login.html",

"Accept-Encoding":"gzip, deflate",

"Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8",

"Cookie":"pgv_pvid=7266193040; __qc_wId=29",

"Connection":"keep-alive",

? ? ? ? ? ? ?? }

? ? ? ? ?? )

.with_cookies(**{"pgv_pvid":"7266193040","__qc_wId":"29"})

.with_data({"name":"lanTest","password":"123456"})

.validate()

.assert_equal("status_code",200)

.assert_equal('headers."Content-Type"',"application/json; charset=utf-8")

.assert_equal("body.code",200)

.assert_equal("body.msg","ok")

? ? ?? ),

?? ]

?

?

if__name__=="__main__":

TestCaseDoctest().test_start()

?

執(zhí)行httprunner測試用例

使用hrun doctest_test.py 或者 pytest? doctest_test.py 均可執(zhí)行測試用例。


C:\github\baidutest\har>hrundoctest_test.py

2020-08-1115:01:13.457c:\programs\python\python38\lib\site-packages\urllib3\request.py:171:InvalidProxyConfigurationWarning:YourproxyconfigurationspecifiedanHTTPSschemefortheproxy.AreyousureyouwanttouseHTTPStocontacttheproxy?Thismostlikelyindicatesanerrorinyourconfiguration.Readthisissueformoreinfo:https://github.com/urllib3/urllib3/issues/1850

returnself.urlopen(method,url,**extra_kw)

|INFO|httprunner.make:__make:512-makepath:C:\github\baidutest\har\doctest_test.py

2020-08-1115:01:13.495|INFO|httprunner.make:format_pytest_with_black:170-formatpytestcaseswithblack...

NoPathprovided.Nothingtodo??

2020-08-1115:01:13.814|INFO|httprunner.cli:main_run:56-starttoruntestswithpytest.HttpRunnerversion:3.1.4

=================================================testsessionstarts=================================================

platformwin32--Python3.8.5,pytest-5.4.3,py-1.9.0,pluggy-0.13.1

rootdir:C:\github\baidutest\har

plugins:forked-1.3.0,metadata-1.10.0,rerunfailures-9.0,allure-pytest-2.8.17,assume-2.2.1,html-2.1.1,xdist-1.34.0

collected1item

?

doctest_test.py. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? [100%]

?

==================================================1passedin0.93s==================================================

?

C:\github\baidutest\har>pytestdoctest_test.py

=================================================testsessionstarts=================================================

platformwin32--Python3.8.5,pytest-5.4.3,py-1.9.0,pluggy-0.13.1

rootdir:C:\github\baidutest\har

plugins:forked-1.3.0,metadata-1.10.0,rerunfailures-9.0,allure-pytest-2.8.17,assume-2.2.1,html-2.1.1,xdist-1.34.0

collected1item

?

doctest_test.py. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? [100%]

?

==================================================1passedin0.84s==================================================

?

生成接口測試報(bào)告

安裝httprunner模塊自動安裝了pytest_html模塊赏淌,可使用pytest doctest_test.py? --html=reports.html 命令執(zhí)行用例并生成報(bào)告



C:\github\baidutest\har>pytestdoctest_test.py--html=reports.html

=================================================testsessionstarts=================================================

platformwin32--Python3.8.5,pytest-5.4.3,py-1.9.0,pluggy-0.13.1

rootdir:C:\github\baidutest\har

plugins:forked-1.3.0,metadata-1.10.0,rerunfailures-9.0,allure-pytest-2.8.17,assume-2.2.1,html-2.1.1,xdist-1.34.0

collected1item

?

doctest_test.py. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? [100%]

?

--------------------------generatedhtmlfile:file://C:\github\baidutest\har\reports.html---------------------------

==================================================1passedin0.93s==================================================

reports測試報(bào)告:


未完待續(xù)踩寇,下一節(jié)用例參數(shù)化與關(guān)聯(lián)!A0乘铩辣卒!

歡迎大家關(guān)注我的訂閱號,會定期分享一些關(guān)于測試相關(guān)的文章睛榄,有問題也歡迎一起討論學(xué)習(xí)荣茫!訂閱號每一條留言都會回復(fù)!


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末场靴,一起剝皮案震驚了整個濱河市啡莉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌憎乙,老刑警劉巖票罐,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異泞边,居然都是意外死亡该押,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門阵谚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚕礼,“玉大人,你說我怎么就攤上這事梢什〉斓牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵嗡午,是天一觀的道長囤躁。 經(jīng)常有香客問我,道長荔睹,這世上最難降的妖魔是什么狸演? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮僻他,結(jié)果婚禮上宵距,老公的妹妹穿的比我還像新娘。我一直安慰自己吨拗,他們只是感情好满哪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著劝篷,像睡著了一般哨鸭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上携龟,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天兔跌,我揣著相機(jī)與錄音,去河邊找鬼峡蟋。 笑死坟桅,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蕊蝗。 我是一名探鬼主播仅乓,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蓬戚!你這毒婦竟也來了夸楣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤子漩,失蹤者是張志新(化名)和其女友劉穎豫喧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幢泼,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡紧显,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缕棵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片孵班。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖招驴,靈堂內(nèi)的尸體忽然破棺而出篙程,到底是詐尸還是另有隱情,我是刑警寧澤别厘,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布虱饿,位于F島的核電站,受9級特大地震影響触趴,放射性物質(zhì)發(fā)生泄漏氮发。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一雕蔽、第九天 我趴在偏房一處隱蔽的房頂上張望折柠。 院中可真熱鬧,春花似錦批狐、人聲如沸扇售。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽承冰。三九已至,卻和暖如春食零,著一層夾襖步出監(jiān)牢的瞬間困乒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工贰谣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娜搂,地道東北人迁霎。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像百宇,于是被迫代替她去往敵國和親考廉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355