httprunner 8-検証バリデータを学習

序文

完全なテストケースでは、アサーションは不可欠なアサーションがhttprunnerの検証とテストの結果を比較するために、比較するために実際の結果と予想される結果を取ることです。

バリデータを検証します

内部httprunnerソースでは、utils.pyは、どのような検証方法を見つけることができます

def get_uniform_comparator(comparator):
    """ convert comparator alias to uniform name
    """
    if comparator in ["eq", "equals", "==", "is"]:
        return "equals"
    elif comparator in ["lt", "less_than"]:
        return "less_than"
    elif comparator in ["le", "less_than_or_equals"]:
        return "less_than_or_equals"
    elif comparator in ["gt", "greater_than"]:
        return "greater_than"
    elif comparator in ["ge", "greater_than_or_equals"]:
        return "greater_than_or_equals"
    elif comparator in ["ne", "not_equals"]:
        return "not_equals"
    elif comparator in ["str_eq", "string_equals"]:
        return "string_equals"
    elif comparator in ["len_eq", "length_equals", "count_eq"]:
        return "length_equals"
    elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
        return "length_greater_than"
    elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals", \
        "count_greater_than_or_equals"]:
        return "length_greater_than_or_equals"
    elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
        return "length_less_than"
    elif comparator in ["len_le", "count_le", "length_less_than_or_equals", \
        "count_less_than_or_equals"]:
        return "length_less_than_or_equals"
    else:
        return comparator

より多くの当量通常、EQは、関連する検証方法の下でここに要約、頭字語に等しくされます

  • EQは、「==」、「等しい」、期待結果と実際の結果が等しいかどうかを、「EQ」を使用することができる決定等しく、「あります」
  • LESS_THAN LT、実際の判定結果が所望の結果よりも小さい、「LT」、「LESS_THAN」とすることができます
  • ルは、実際の結果が所望の結果を以下に決定される、less_than_or_equals、「ル」、「less_than_or_equals」とすることができます
  • GREATER_THAN GT、実際の判定結果が所望の結果よりも大きい場合、「GT」、「GREATER_THAN」とすることができます
  • GEのgreater_than_or_equals、実際の判定結果が「GE」、「greater_than_or_equals」と、所望の結果に等しいより大きい
  • NEのnot_equals、決意の結果と等しくない所望であってもよく、「NE」、「not_equals」の実績
  • str_eqが比較決意結果をstring_equalsと実際の結果は、トランスフェクション後に所望の文字列と等しい、「str_eq」、「string_equals」を使用することができます
  • len_eq length_equals長文字列またはリストの分析、「len_eq」、「length_equals」、「count_eq」を使用することができます
  • 実際の長さを決定length_greater_than len_gtは、「count_greater_than」「len_gt」、「count_gt」、「length_greater_than」をすることができ、その結果、所望の結果よりも大きいです
  • len_ge length_greater_than_or_equals長さよりも大きいかまたは所望の結果に等しい実際の結果は、「len_ge」、「count_ge」、「length_greater_than_or_equals」、「count_greater_than_or_equals」とすることができます
  • length_less_than長len_lt実際の結果が所望の結果よりも小さい、「len_lt」、「count_lt」、「length_less_than」、「count_less_than」とすることができます
  • len_le length_less_than_or_equals長さより小さいか、所望の結果に等しい実際の結果は、「len_le」、「count_le」、「length_less_than_or_equals」、「count_less_than_or_equals」とすることができます

ケース

次のようにデモ例1に、内容を返します

C:\Users\dell>http http://127.0.0.1:8000/api/test/demo
HTTP/1.1 200 OK
Content-Length: 255
Content-Type: application/json
Date: Sun, 22 Sep 2019 10:11:07 GMT
Server: WSGIServer/0.2 CPython/3.6.0
X-Frame-Options: SAMEORIGIN

{
    "code": 0,
    "datas": [
        {
            "age": 20,
            "create_time": "2019-09-15",
            "id": 1,
            "mail": "[email protected]",
            "name": "yoyo",
            "sex": "M"
        },
        {
            "age": 21,
            "create_time": "2019-09-16",
            "id": 2,
            "mail": "[email protected]",
            "name": "yoyo111",
            "sex": "M"
        }
    ],
    "msg": "success!"
}

上記の書き込みいくつかの異なる検証のために結果を返し、チェックが結果を返し、エラーとtest_validate_demo.ymlを尋ねました

- config:
    name: test_demo
    variables: {}
- test:
    name: test_demo case1
    request:
        url: http://127.0.0.1:8000/api/test/demo
        method: GET
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: test
            password: 123456
    extract:
        - mail: content.datas.0.mail         # 提取mail
    validate:
        - eq: [status_code, 200]
        - less_than: [status_code, 400]
        - equals: [content.code, 0]
        - equals: [content.msg, success!]
        - length_equals: [content.datas, 2]
        - greater_than_or_equals: [content.datas.0.age, 18]

ユースケースを実行します

業績

D:\soft\untitled>hrun test_validate_demo.yml
test_demo case1
INFO     GET http://127.0.0.1:8000/api/test/demo
INFO     status_code: 200, response_time(ms): 5.99 ms, response_length: 255 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.011s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\untitled\reports\1569165498.html

レポートを見ます

おすすめ

転載: www.cnblogs.com/yoyoketang/p/11569942.html