The author | Mimi

wiremock-py

Wiremock-py is implemented based on Wiremock. It uses Python to generate mock data in batches for different HTTP apis in different test scenarios, and then acts as a mock Server to quickly and comprehensively test the API.

background

In the front end test of Tellan real estate application, the front end generally relies on the data of the back end, and the front end obtains the data through the HTTP API published by the back end on the gateway. To fully test the front end, it is ideal to wait for the back-end deployment to complete and input different types of data sources directly at the data layer, then the front end directly calls the API that the back end publishes to the gateway for testing.

However, the reality is that the development schedule of the front end and the back end is not exactly the same. If the front end is developed first, the test can only be started after the corresponding API development of the back end is completed, and the data layer is not easy to construct.

To solve this problem, the gateway platform does a simple mock function. Each API can fill in one mock data, which is then used directly when the front-end calls the API:

In this way, the gateway acts as a mock server:

But since everyone uses the same gateway, an API can only hold one copy of mock data, so there are some disadvantages:

  • Different test scenarios require different mock data to test. In this case, you need to delete the mock data of the last test scenario and create the mock data of the new scenario to test

  • Mock data for apis cannot be generated dynamically according to certain rules based on test scenarios

  • When you test the same API, you can only use the same mock data

Wiremock-py solves these problems: Wiremock-py generates different mock data by passing in different test scenario parameters. Mock data used in different test scenarios can be saved. Wiremock-py supports using Python and JS code to generate mock data on the fly. (It also supports using JSON data directly. Using code generation is easier); Different testers using their own mock Server does not affect other testers’ tests.

What the tester needs to do is to determine which apis to mock and what the corresponding mock rules are for different test scenarios.

Depend on the environment

Java 1.8.0 comes with _144

The Node v8.6.0

Python rule 3.4.3

demo

Quick start

The test environment of v1.1.0 is taken as an example to demonstrate the test method of wiremoCk-PY for the distribution of floor passenger flow and store passenger flow in three scenarios

Ensure that the local browser will be able to access mall-data.com:9012

To prepare

Cloning code

Git clone git.dtwave-inc.com: 30000 / baomi WBM/w…

Install dependencies

cd wiremock-py

pip install -r requirements.txt

npm install mockjs

Generated directory

python mock.py -g “demo”

➜ wiremock-py git:(master) qualify python mock.py -g “demo”

DEBUG:root:mockdir=, scene=, target=, proxy_port=5506, generate=demo, wiremock=False, rewrite=False

The DEBUG: root: being generated directory/Users/wangbaomi/this/wiremock – py/demo

DEBUG:root: Directory creation succeeds: demo

DEBUG:root: Directory creation succeeds: demo/js

DEBUG:root: Directory creation succeeds: demo/json

DEBUG:root: Directory creation succeeds: demo/ Python

DEBUG:root: Directory creation succeeds: demo/wiremock

DEBUG:root: The file is successfully created: demo/ mapping.json

The DEBUG: root: build directory: / Users/wangbaomi/this/wiremock – py/demo

Json, json, python, and JS data

Fill in the following content in mapping.json:

[

{

    "response": {

        "default": {

            "proxyBaseUrl": "target"
Copy the code

}

    },

    "mapping_name": "request url not start with /api"."request": {

        "method": "ANY"."urlPattern": "/ (? ! api).*"}}, {"mapping_name": "Floor Traffic Distribution"."request": {

        "urlPattern": "/api/v1/mall_data/customer_flow/every_floor\\? (. *)"."method": "POST"

    },

    "response": {

        "default": {

            "proxyBaseUrl": "target"

        },

        "Test Scenario 1": {

            "bodyFileName": {

                "json": "Floor traffic distribution. Json"}},"Test Scenario 2": {

            "bodyFileName": {

                "python": "Floor flow distribution. Py"."python_args": "Test Scenario 2"}},"Test Scenario 3": {

            "bodyFileName": {

                "js": "Floor passenger flow distribution.js"}}}}, {"mapping_name": "Shop Traffic Distribution"."request": {

        "urlPattern": "/api/v1/mall_data/customer_flow/every_shop\\? (. *)"."method": "POST"

    },

    "response": {

        "default": {

            "proxyBaseUrl": "target"

        },

        "Test Scenario 1": {

            "bodyFileName": {

                "js": "Store traffic distribution.js"}},"Test Scenario 2": {

            "bodyFileName": {

                "json": "Store traffic distribution. Json"}},"Test Scenario 3": {

            "bodyFileName": {

                "python": "Store traffic distribution. Py"."python_args": "Test Scenario 3"}}}}Copy the code

]

The.js file in the js folder contains the following contents:

var r = {
"success": true."code": null,
"message": null,
"content": {
"meta": {},

"multi": {

  "group": [{"id": "rank"."name": "排名"."value": [1, 2, 3, 4]}],"result": [{"id": "the_shop"."name": "Shop"."value": [

        "The store 1"."Shop 2"."Three stores"."The fourth shop"] {},"id": "customer_count"."name": "The number"."value": [10, 100, 1000, 3242]}]}"single": []}}; console.log(JSON.stringify(r));Copy the code

The.js file in the js folder contains the following contents:

var r = {

"success": true."code": null,

"message": null,

"content": {"meta": {},

"multi": {

  "group": [{"id": "the_floor"."name": "Floor"."value": [

        "- 1 / f,".1 / f, ""."The second floor"."The third floor",]}],"result": [{"id": "customer_count"."name": "The number"."value": [100, 1000, 5000, 567]}]}"single": []}}; console.log(JSON.stringify(r)); Json folder to create store customer flow distribution. Json, the content is: {"success": true."code": null,

"message": null,

"content": {
"meta": {},
"multi": {
Copy the code