This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Test case format

HttpRunner test cases support two file formats: YAML and JSON.

JSON and YAML test cases are completely equivalent and contain exactly the same information content.

Which format to choose depends on your mood.

Test case structure

HttpRunnerIn, test case organization is based on three concepts:

  • The test set (testsuite) : corresponds to a folder containing one or more test case files (YAML/JSON)
  • Test cases (testcase) : Corresponding to oneYAML/JSONFile that contains one or more test steps
  • Test procedure (teststepCorresponding) :YAML/JSONOne of the filestest, describes the results of a single interface test, interface request, parse response, and verification

For a single YAML/JSON file, the data store structure is in the form of a list of dict, which may contain a global configuration item (config) and several test steps (tests).

  • config: as a global configuration item for the entire test case
  • test: Corresponds to a single test step (teststep), test cases have a sequential relationship, and the runtime runs each test step from front to back

The yamL format is as follows:

config:
    name: "demo testcase"
    .

teststeps:
-
    name: demo step 1
    .
-
    name: demo step 2
    .
Copy the code

Variable context

Within the test case, HttpRunner divides two variable context types.

Config: as the global configuration item of the entire test case, scoped to the entire test case; Test: applies to the test step, inherits or overrides what is defined in config; That is, variables defined in test have higher precedence than those defined in config.

The variable space of each test step is independent and does not affect each other. If you need to pass parameter values in more than one test step, you need to use the EXTRACT keyword and only pass them backwards.

config

Sample:

config:
    name: "demo testcase"
    variables:
        device_sn: "ABC"
        username: ${ENV(USERNAME)}
        password: ${ENV(PASSWORD)}
    base_url: "https://getman.cn/mock"
    setup_hooks:
        - ${hook_print(setup)}
    teardown_hooks:
        - ${hook_print(teardown)}
Copy the code

test

Sample:

-
    name: demo step 1
    api: api/demo_api.yml
    variables:
        user_agent: 'the Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
        device_sn: $device_sn
    extract:
        - token: content.name
    validate:
        - eq: ["status_code".200]
    setup_hooks:
        - ${hook_print(setup)}
    teardown_hooks:
        - ${hook_print(teardown)}
Copy the code

Common Fields description

extract

Used for data extraction and delivery.

Support a variety of extraction methods:

The response result is a JSON structure and can be used. Headers. Content-type, content.success; The response result is a text/ HTML structure, and can be a regular expression, such as blog-motto\”>(.*).

validate

Used for assertions.

Two formats are supported:

validate:
        - eq: ["status_code".200]
Copy the code
validate:
        - check: status_code
          comparator: eq
          expect: 200
Copy the code

hooks

A hook function that makes a particular call in a particular place.

Common assertions

"Eq," "equals", "= =", "is" means: "the actual result" and "expectation result" equal "lt", "less_than" means: "the actual result" less than "expectation result" "le", "less_than_or_equals" means: "Actual result" is less than or equal to "expected result" "gt", "greater_than" means: "actual result" is greater than" expected result ", "GE "," greater_THAN_or_equals "means: "Actual results" is greater than or equal to "expected results ", "ne", "not_equals" means:" Actual results "and" expected results "are not equal. "str_eq", "string_equals" means: "Len_eq ", "length_equals", "count_eq" means: Length of string or list, "actual result" equal to "expected result "len_gt", "count_gt"," length_GREATer_than ", "count_greater_than" means: "Actual result length" greater than "expected result" "len_ge", "count_ge", "length_GREATer_than_OR_EQUALS "," count_GREATer_than_or_equals" means: "The length of the actual result" is greater than or equal to the "desired result" "len_lt", "count_lt", "length_LESS_than "," count_LESS_THAN "means: "The length of the actual result" is less than the "expected result" "len_le", "count_le", "length_LESS_THAN_OR_EQUALS "," count_LESS_than_or_EQUALS" means: "The length of the actual result" is less than or equal to the "expected result".Copy the code