httprunner learning 2-har2case generated by recording the script

Foreword

复制毁一生,录制穷三代, If only because you do not want to write the script, try to record a script, then I suggest you do not learn recorded.
Recording a script, just a transition, a transition from 0 to 1, so that if you directly write a script, you can not start, can be recorded script is rapidly converted to httprunner script file.
har2case .har file can be converted to a script file yaml httprunner json format or formats, can be generated by .har format or fiddler Charles capture tool.

Preparing the Environment

httprunner If you have already installed, it should be comes har2case package, if not, you can install with pip

pip install har2case==0.3.1

View the version number

-V har2case
0.3.1

View Help options -h

C:\Users\dell>har2case -h
usage: har2case [-h] [-V] [--log-level LOG_LEVEL] [-2y] [-fmt FMT_VERSION]
                [--filter FILTER] [--exclude EXCLUDE]
                [har_source_file]

Convert HAR(HTTP Archive) to YAML/JSON testcases for HttpRunner.

positional arguments:
  har_source_file       Specify HAR source file

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show version
  --log-level LOG_LEVEL
                        Specify logging level, default is INFO.
  -2y, --to-yml, --to-yaml
                        Convert to YAML format, if not specified, convert to
                        JSON format by default.
  -fmt FMT_VERSION, --format FMT_VERSION
                        Specify YAML/JSON testcase format version, v2
                        corresponds to HttpRunner 2.2.0+.
  --filter FILTER       Specify filter keyword, only url include filter string
                        will be converted.
  --exclude EXCLUDE     Specify exclude keyword, url that includes exclude
                        string will be ignored, multiple keywords can be
                        joined with '|'

Fiddler capture documents generated .har

Log in to the interface as an example, an interface to log information related documents as follows:

After sending the request on the interface Fiddler, following capture

Caught after this request, the upper right corner File-> Export Sessions-> Selected Sessions-> Select Export Format-> Check HTTPArchive v1.1

Check HTTPArchive v1.1the type, the next step is to export the file test_login_demo.har

har2case turn yaml format script

Next just generated test_login_demo.har file, use har2case turn into a script file format yam

har2case test_login_demo.har -2y

-2yParameter is set to turn the script .yml format, if you do not add this parameter, the default turn into json format

D:\>har2case test_login_demo.har -2y
INFO:root:Start to generate testcase.
INFO:root:dump testcase to YAML format.
INFO:root:Generate YAML testcase successfully: test_login_demo.yml

View test_login_demo.yml just born, as follows

# 上海悠悠,QQ交流群:750815713
-   config:
        name: testcase description
        variables: {}
-   test:
        name: /api/v1/login/
        request:
            headers:
                Content-Type: application/json
                User-Agent: Fiddler
            json:
                password: '123456'
                username: test
            method: POST
            url: http://127.0.0.1:8000/api/v1/login/
        validate:
        -   eq:
            - status_code
            - 200
        -   eq:
            - headers.Content-Type
            - application/json
        -   eq:
            - content.code
            - 0
        -   eq:
            - content.msg
            - login success!
        -   eq:
            - content.username
            - test
        -   eq:
            - content.token
            - a95b077eb4b884b9186f60a47f37b4746c7c6a60

Run a use case

.ymlAfter generating a script format, then the use cases using hrun Run

hrun test_login_demo.yml

D:\>hrun test_login_demo.yml
/api/v1/login/
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 437.79 ms, response_length: 109 bytes
INFO     start to validate.
ERROR    validate: content.token equals a95b077eb4b884b9186f60a47f37b4746c7c6a60(str)   ==> fail
c7dca34cc6ff93049985c44967f132c4146e995e(str) equals a95b077eb4b884b9186f60a47f37b4746c7c6a60(str)
ERROR    request:
headers: {'content-type': 'application/json', 'user-agent': 'Fiddler'}
json: {'password': '123456', 'username': 'test'}

ERROR    response:
status_code: 200
headers: {'Date': 'Sat, 21 Sep 2019 09:54:57 GMT', 'Server': 'WSGIServer/0.2 CPython/3.6.0', 'Content-Type': 'application/json', 'Vary': 'Accept, Cookie', 'Allow': 'POST, OPTIONS', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '109'}
body: '{"code": 0, "msg": "login success!", "username": "test", "token": "c7dca34cc6ff93049985c44967f132c4146e995e"}'

F

======================================================================
FAIL: runTest (httprunner.task.TestCase)
/api/v1/login/
----------------------------------------------------------------------
Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\task.py", line 27, in runTest
    self.test_runner.run_test(self.testcase_dict)
httprunner.exceptions.ValidationFailure

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\python36\lib\site-packages\httprunner\task.py", line 29, in runTest
    self.fail(repr(ex))
AssertionError: ValidationFailure()

----------------------------------------------------------------------
Ran 1 test in 0.468s

FAILED (failures=1)
INFO     Start to render Html report ...
INFO     Generated Html report: D:\reports\1569059697.html

D:\>

You will find that running a regular meeting fails to open the test reports, you will find a token check failed because token every time dynamically generated, so the token can not write the check dead

The solution is simple, remove this token to check

        -   eq:
            - content.token
            - a95b077eb4b884b9186f60a47f37b4746c7c6a60

Generate json format script

har2case default script generates json format, because personally prefer yaml format, so the json format written on the back.

har2case test_login_demo.har

D:\>har2case test_login_demo.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to JSON format.
INFO:root:Generate JSON testcase successfully: test_login_demo.json

D:\>

Generated as follows test_login_demo.json

# 上海悠悠,QQ交流群:750815713
[
    {
        "config": {
            "name": "testcase description",
            "variables": {}
        }
    },
    {
        "test": {
            "name": "/api/v1/login/",
            "request": {
                "url": "http://127.0.0.1:8000/api/v1/login/",
                "method": "POST",
                "headers": {
                    "User-Agent": "Fiddler",
                    "Content-Type": "application/json"
                },
                "json": {
                    "username": "test",
                    "password": "123456"
                }
            },
            "validate": [
                {
                    "eq": [
                        "status_code",
                        200
                    ]
                },
                {
                    "eq": [
                        "headers.Content-Type",
                        "application/json"
                    ]
                },
                {
                    "eq": [
                        "content.code",
                        0
                    ]
                },
                {
                    "eq": [
                        "content.msg",
                        "login success!"
                    ]
                },
                {
                    "eq": [
                        "content.username",
                        "test"
                    ]
                },
                {
                    "eq": [
                        "content.token",
                        "a95b077eb4b884b9186f60a47f37b4746c7c6a60"
                    ]
                }
            ]
        }
    }
]

filter and exclude filters

You can use the filterparameters of the filter url comprising xxx.com content, containing only are 127.0.0.1url requests

$ Har2case demo.har --filter 127.0.0.1

Filter may also be used to exclude, in addition to the content of xxx.com

$ har2case demo.har--exclude xxxx.com

Copy ruined his life, poor record three generations of Shanghai - a lengthy, QQ exchange group: 750 815 713

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11564028.html
Recommended