HTTPRUNNER_DEMO description:
Httprunner initialization json format interface test framework Demo: to achieve swagger automatically generate JSON test cases, test case file comparison and backup test case data, log collector and other functions.
Install the PYTHon3 runtime environment
- No one recommends installing PYTHon2 because it is no longer maintained;
- PIP installation httprunner. For details about the usage rules, see the Usage documents in Chinese.
This section describes the two httprunner framework commands
- Httprunner /hrun — startProject your_projectName Quickly generates the project structure
-
- API: Directory of generated API interface test cases
-
- Reports: Executes the default report directory for test cases
-
- Testcases: directory of testcases
-
- Testsuites: Test case suites
-
- Debugtalk. py: is a heat treatment file. The default name is debugtalk.py
-
- Env: environment management, this is not used yet
-
-
- Key information can be written here for management, how can it be referenced in a test case? Example: ${ENV(PARAM)} calls the PARAM argument in the ENV variable file
-
- Use fiddler/ Charles to capture packages, export them as HAR data files, run the har2case command to generate test cases in JSON format by default, and use -2y to generate test cases in YAML format. The default execution sequence is from top to bottom
-
- Config is the global configuration item and is scoped to the entire test case
-
- Test corresponds to a single test step and is scoped only to itself
- Hrun can execute individual testcases or testsuites in the testcases/testsuites directory
-
- Hrun testcases\ executes all of the following testcases
Dynamic association implementation in interface testing
An interface is not a single interface, but has certain dependencies, that is, there are business scenariosCopy the code
- Httprunner support parameter extraction, parameter reference function: extract, add “extract”: [{” token “: “content.token”}]
-
- Content is the default response parameter, followed by the. Dot at the json object directory level
-
- The token is used as a reference variable, and $token is used directly in the associated interface below
- Httprunner supports dynamic computing logic, which is the debugtalk.py file mentioned above
Import random def gen_random_num(min_num,max_num): return random. Randint (min_num,max_num)Copy the code
-
- Modify the data reference function under the API test case: ${gen_random_num(1,3)}
-
- It is important to note hrun command with debugtalk file directory, at the same level or it will tip can’t find the func: httprunner. Exceptions. FunctionNotFound: gen_random_num is not found.
Again, the Parameter data – driven httprunner interface testing framework
Roll to roll to the data driven module, artificial to add parameters, it is hard to avoid errors, or the test platform ahCopy the code
- To run test cases in data-driven mode in HttpRunner, you create a file that references the test case and uses the Parameters keyword to define the parameters and specify the data source value.
- This parameter has not been debutted. Currently, the httprunner framework is used to monitor the single-threaded interface of the live network
- See the user manual for details!
Ten thousand years unchanged stem: encapsulation method
Really talk about encapsulation, simple and crude to tell others to encapsulate a good method API encapsulation again, custom can be clear method.Copy the code
- Configparser: Configfile reading library
- Celery: Acts as a system directory comparison file
- For HTTP requests, webServices are available
- Logging: Log collector, which is required for log tracing of scripts
Important: The Swagger interface documentation automatically generates test cases in JSON format
The Python script is used to clean the JSON-formatted response parameters returned by the Swagger interface and add them to the defined JSON objectCopy the code
- Consider generating json test cases for the first time and then perfecting the interface test case. If the interface is changed, executing the Swagger script again will overwrite the original completed interface test case.
-
- Swagger needs to be executed each time to back up the use cases in the API directory and generate the HTML report
This article uses the article synchronization assistant to synchronize