当前位置:   article > 正文

【HttpRunner v3.x】笔记 ——3. 录制生成测试用例_har2case如何生成py文件

har2case如何生成py文件

在正式手动编写case之前,我们可以先来熟悉下httprunner的录制生成用例功能。
用postman的童鞋都知道,里面有个功能可以将接口转换成代码,可以直接copy过来使用,提升case编写效率。
那httprunner的录制生成用例功能又是怎么回事呢?

一、har2case

其实,这都要依托于另一个独立的项目-har2case。
原理就是当前主流的抓包工具和浏览器都支持将抓取得到的数据包导出为标准通用的 HAR 格式(HTTP Archive),然后 HttpRunner 将 HAR 格式的数据包转换为YAML/JSON格式的测试用例文件。
比如,我现在用window系统上的fiddler去抓取一个百度首页的请求。
选中这个请求,点击左上角的File——Export Sessions——(可以选择导出选中的也可以导出所有),这里我们选择导出选中的,导出HTTPArchive,文件名baidu_home.har,保存到了项目的har目录下。

二、转换为pytest文件

运行命令将har文件转换成测试用例:

har2case baidu_home.har


生成完毕,在har目录下可以看到生成出的python文件:

  1. # NOTE: Generated By HttpRunner v3.1.1
  2. # FROM: har\baidu_home.har
  3. from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
  4. class TestCaseBaiduHome(HttpRunner):
  5. config = Config("testcase description").verify(False)
  6. teststeps = [
  7. Step(
  8. RunRequest("/")
  9. .get("https://www.baidu.com/")
  10. .with_headers(
  11. **{
  12. "Host": "www.baidu.com",
  13. "Connection": "keep-alive",
  14. "Upgrade-Insecure-Requests": "1",
  15. "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36",
  16. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
  17. "Accept-Encoding": "gzip, deflate, br",
  18. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
  19. "Cookie": "PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1; BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085; BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9; yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js; H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby; H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0",
  20. }
  21. )
  22. .with_cookies(
  23. **{
  24. "PSTM": "1582766561",
  25. "BAIDUID": "5F919C7A22A02E55FBC58E932E7495CD:FG=1",
  26. "BD_UPN": "12314353",
  27. "BIDUPSID": "B2A8970CF5106170D98A137A26C533F7",
  28. "H_WISE_SIDS": "143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085",
  29. "BDUSS": "ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ",
  30. "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
  31. "BDRCVFR[feWj1Vr5u3D]": "I67x6TjHwwYf0",
  32. "delPer": "0",
  33. "BD_CK_SAM": "1",
  34. "PSINO": "5",
  35. "BD_HOME": "1",
  36. "__yjsv5_shitong": "1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9",
  37. "yjs_js_security_passport": "db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js",
  38. "H_PS_645EC": "8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby",
  39. "H_PS_PSSID": "32095_1445_31672_21106_32139_31660_32045_31321",
  40. "BDSVRTM": "0",
  41. }
  42. )
  43. .validate()
  44. .assert_equal("status_code", 200)
  45. .assert_equal('headers."Content-Type"', "text/html;charset=utf-8")
  46. ),
  47. ]
  48. if __name__ == "__main__":
  49. TestCaseBaiduHome().test_start()

因为httprunner封装了pytest,所有既可以用hrun去运行,也可以用pytest去运行。
hrun
pytest

三、转换为YAML/JSON

很简单,只要在命令后面多加对应的参数就行了。-2y/--to-ymlor-2j/--to-json
转为YAML:

har2case baidu_home.har -2y


可以查看到转换生成的yaml文件了。

  1. config:
  2. name: testcase description
  3. variables: {}
  4. verify: false
  5. teststeps:
  6. - name: /
  7. request:
  8. cookies:
  9. BAIDUID: 5F919C7A22A02E55FBC58E932E7495CD:FG=1
  10. BDORZ: B490B5EBF6F3CD402E515D22BCDA1598
  11. BDRCVFR[feWj1Vr5u3D]: I67x6TjHwwYf0
  12. BDSVRTM: '0'
  13. BDUSS: ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ
  14. BD_CK_SAM: '1'
  15. BD_HOME: '1'
  16. BD_UPN: '12314353'
  17. BIDUPSID: B2A8970CF5106170D98A137A26C533F7
  18. H_PS_645EC: 8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby
  19. H_PS_PSSID: '32095_1445_31672_21106_32139_31660_32045_31321'
  20. H_WISE_SIDS: '143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085'
  21. PSINO: '5'
  22. PSTM: '1582766561'
  23. __yjsv5_shitong: 1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9
  24. delPer: '0'
  25. yjs_js_security_passport: db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js
  26. headers:
  27. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
  28. Accept-Encoding: gzip, deflate, br
  29. Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
  30. Connection: keep-alive
  31. Cookie: PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1;
  32. BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085;
  33. BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ;
  34. BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0;
  35. delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9;
  36. yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js;
  37. H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby;
  38. H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0
  39. Host: www.baidu.com
  40. Upgrade-Insecure-Requests: '1'
  41. User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
  42. like Gecko) Chrome/75.0.3770.80 Safari/537.36
  43. method: GET
  44. url: https://www.baidu.com/
  45. validate:
  46. - eq:
  47. - status_code
  48. - 200
  49. - eq:
  50. - headers.Content-Type
  51. - text/html;charset=utf-8

转换为JSON:

har2case baidu_home.har -2j


可以看的对应的json文件:

  1. {
  2. "config": {
  3. "name": "testcase description",
  4. "variables": {},
  5. "verify": false
  6. },
  7. "teststeps": [
  8. {
  9. "name": "/",
  10. "request": {
  11. "url": "https://www.baidu.com/",
  12. "method": "GET",
  13. "cookies": {
  14. "PSTM": "1582766561",
  15. "BAIDUID": "5F919C7A22A02E55FBC58E932E7495CD:FG=1",
  16. "BD_UPN": "12314353",
  17. "BIDUPSID": "B2A8970CF5106170D98A137A26C533F7",
  18. "H_WISE_SIDS": "143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085",
  19. "BDUSS": "ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ",
  20. "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
  21. "BDRCVFR[feWj1Vr5u3D]": "I67x6TjHwwYf0",
  22. "delPer": "0",
  23. "BD_CK_SAM": "1",
  24. "PSINO": "5",
  25. "BD_HOME": "1",
  26. "__yjsv5_shitong": "1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9",
  27. "yjs_js_security_passport": "db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js",
  28. "H_PS_645EC": "8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby",
  29. "H_PS_PSSID": "32095_1445_31672_21106_32139_31660_32045_31321",
  30. "BDSVRTM": "0"
  31. },
  32. "headers": {
  33. "Host": "www.baidu.com",
  34. "Connection": "keep-alive",
  35. "Upgrade-Insecure-Requests": "1",
  36. "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36",
  37. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
  38. "Accept-Encoding": "gzip, deflate, br",
  39. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
  40. "Cookie": "PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1; BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085; BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9; yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js; H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby; H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0"
  41. }
  42. },
  43. "validate": [
  44. {
  45. "eq": [
  46. "status_code",
  47. 200
  48. ]
  49. },
  50. {
  51. "eq": [
  52. "headers.Content-Type",
  53. "text/html;charset=utf-8"
  54. ]
  55. }
  56. ]
  57. }
  58. ]
  59. }

以上转换出的pytest、yaml、json这3种格式的文件效果都是一样的,用hrun都可以运行,但是用pytest执行的话只可以运行.py的文件了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/609111
推荐阅读
相关标签
  

闽ICP备14008679号