1. 程式人生 > 實用技巧 >錄製和生成測試用例

錄製和生成測試用例

抓取http請求和響應

在我們編寫測試用例之前,我們應該知道API的詳細資訊,我們可以使用類似與Fiddle,Charles這樣的抓包工具來抓取http請求

下面,我將使用一個測試環境

我們可以選擇需要的請求,匯出成.har的格式

使用har2case生成測試用例

獲取 HAR 檔案後,可以使用內建命令將其轉換為 httpRunner 測試用例

har2case幫助

har2case-h usage: har2case har2case [-h] [-2y] [-2j] [--filterFILTER] [--exclude EXCLUDE] [har_source_file]
positional arguments: har_source_file Specify HAR sourcefile optional arguments: -h,--helpshow thishelpmessageandexit -2y,--to-yml,--to-yaml Convert to YAMLformat,ifnotspecified, convert to pytestformatby default. -2j,--to-json Convert to JSONformat,ifnotspecified, convert to pytestformatby default.
--filterFILTERSpecifyfilterkeyword, only url includefilterstring will be converted. --exclude EXCLUDE Specify exclude keyword, url that includes exclude string will be ignored, multiple keywords can be joined with'|'

生成測試用例

在3.0中,由於httpRunner預設會將HAR檔案轉成pytest,建議使用pytest,而不是之前的yaml、json格式

har2case test.har
2020-06-1515:08:01.187| INFO | httprunner.ext.har2case.core:gen_testcase:332-Start to generate testcasefromhar/postman-echo-post-form.har 2020-06-1515:08:01.187| INFO | httprunner.ext.har2case.core:_make_testcase:323-Extract infofromHARfileandpreparefortestcase. 2020-06-1515:08:01.191| INFO | httprunner.loader:load_dot_env_file:130-Loading environment variablesfrom/Users/debugtalk/Desktop/demo/.env 2020-06-1515:08:01.191| DEBUG | httprunner.utils:set_os_environ:32-SetOS environment variable: USERNAME 2020-06-1515:08:01.191| DEBUG | httprunner.utils:set_os_environ:32-SetOS environment variable: PASSWORD 2020-06-1515:08:01.193| INFO | httprunner.make:make_testcase:310-start to make testcase:/Users/debugtalk/Desktop/demo/har/postman-echo-post-form.har 2020-06-1515:08:01.193| INFO | httprunner.make:make_testcase:383-generated testcase:/Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py 2020-06-1515:08:01.194| INFO | httprunner.make:format_pytest_with_black:147-formatpytest cases with black ... reformatted/Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py Alldone! 1filereformatted. 2020-06-1515:08:01.469| INFO | httprunner.ext.har2case.core:gen_testcase:353-generated testcase:/Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py

生成的pytest檔案是如下標準的pytest檔案,它可以直接執行:

#NOTE:GeneratedByHttpRunnerv3.1.3 #FROM:test.har fromhttprunnerimportHttpRunner,Config,Step,RunRequest,RunTestCase classTestCaseTest(HttpRunner): config=Config("testcasedescription").verify(False) teststeps=[ Step( RunRequest("/YYDZPY/login") .post("http://192.168.3.70:8070/YYDZPY/login") .with_headers( **{ "Host":"192.168.3.70:8070", "Connection":"keep-alive", "Content-Length":"53", "Pragma":"no-cache", "Accept":"application/json,text/plain,*/*", "Cache-Control":"no-cache", "X-Requested-With":"XMLHttpRequest", "User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.89Safari/537.36Edg/84.0.522.40", "Content-Type":"application/json", "Origin":"http://192.168.3.70:8070", "Referer":"http://192.168.3.70:8070/YYDZPY/index.jsp", "Accept-Encoding":"gzip,deflate", "Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", "Cookie":"JSESSIONID=0E40D65F4C701A93621D2BE023A309C1", } ) .with_cookies(**{"JSESSIONID":"0E40D65F4C701A93621D2BE023A309C1"}) .with_json({"XTZH":"U1lTVEVN","YHMM":"B/RGSNco4fpCWqdYQ3qSNg=="}) .validate() .assert_equal("status_code",200) .assert_equal('headers."Content-Type"',"application/json;charset=UTF-8") .assert_equal("body.msg","登入成功!") .assert_equal("body.errormsg","使用者未設定所屬部門,部分功能會受影響,請聯絡管理員設定") ), ] if__name__=="__main__": TestCaseTest().test_start()

它可以使用命令或本機命令執行

hrun pytest

測試用例的組織形式:

在httpRunner中,測試用例的組織形式一共有三種:分別是。json,yaml和pytest

如何生成這些json、yaml、pytest檔案?

第一步:

抓包

第二步:

把請求包儲存為har格式

第三步:

使用命令生成

可以使用 -2y 來將 har檔案生成yaml格式的用例

可以使用 -2j 來將 har檔案生成json格式的用例

例如:現有有一個test.har的檔案

可以使用har2case test.har -2y 命令 生成yaml格式的用例

har2case test.har-2y $ har2case har/postman-echo-post-form.har-2j 2020-06-1515:32:02.955| INFO | httprunner.ext.har2case.core:gen_testcase:332-Start to generate testcasefromhar/postman-echo-post-form.har 2020-06-1515:32:02.955| INFO | httprunner.ext.har2case.core:_make_testcase:323-Extract infofromHARfileandpreparefortestcase. 2020-06-1515:32:02.958| INFO | httprunner.ext.har2case.utils:dump_json:122-dump testcase to JSONformat. 2020-06-1515:32:02.959| INFO | httprunner.ext.har2case.utils:dump_json:131-Generate JSON testcase successfully: har/postman-echo-post-form.json 2020-06-1515:32:02.959| INFO | httprunner.ext.har2case.core:gen_testcase:353-generated testcase: har/postman-echo-post-form.json

可以使用 har2case test.har -2j 命令生成json格式的用例

har2case test.har-2j $ hrun har/postman-echo-post-form.json 2020-06-1515:37:15.621| INFO | httprunner.loader:load_dot_env_file:130-Loading environment variablesfrom/Users/debugtalk/Desktop/demo/.env 2020-06-1515:37:15.622| DEBUG | httprunner.utils:set_os_environ:32-SetOS environment variable: USERNAME 2020-06-1515:37:15.622| DEBUG | httprunner.utils:set_os_environ:32-SetOS environment variable: PASSWORD 2020-06-1515:37:15.623| INFO | httprunner.make:make_testcase:310-start to make testcase:/Users/debugtalk/Desktop/demo/har/postman-echo-post-form.json 2020-06-1515:37:15.625| INFO | httprunner.make:make_testcase:383-generated testcase:/Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py 2020-06-1515:37:15.625| INFO | httprunner.make:format_pytest_with_black:147-formatpytest cases with black ... reformatted/Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py Alldone! 1filereformatted,1fileleft unchanged. 2020-06-1515:37:15.962| INFO | httprunner.cli:main_run:56-start to run tests with pytest. HttpRunner version:3.0.12 ======================================================================test session starts====================================================================== platform darwin--Python3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir:/Users/debugtalk/Desktop/demo plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1 collected1item har/postman_echo_post_form_test.py . [100%] =======================================================================1passedin2.03s=======================================================================

如果,你使用的3.0的版本,建議以新使用pytest格式