First, the application background

Since the content of this chapter is completed by using THE JQ tool, some space will be devoted to the use of THE JQ machine at the beginning of the chapter. If you are already familiar with JQ, you can skip this part.

First, let’s look at the application scenario. App often has some list pages for displaying information, such as dishes of merchants, companies of stocks and articles, etc., such as the following:

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p6-tt.byteimg.com/origin/pgc-image/47008d8a88dc4408a51fb0f6058cfcd7?from=pc)

If dishes are not enough to eat ~ no, the number of dishes is too small to test. If we want to test hundreds of dishes, will there be performance impact when brushing, display is normal, list display boundary value test, etc.;

And to measure must have data display, this time if through the back-end to find or create so much data is a bit of trouble, in fact, the test point of our test is to test the front-end page display and performance, as long as there is this part of the data can, does not matter whether the data is real from the database.

At this time, we can use mock means to generate equivalence classes for this kind of big data, so that the front-end can get this kind of data, and Charles is one of our powerful tools to implement manual mock test!

Ii. Introduction to JQ

2.1 Overview and usage of JQ

As usual, the jQ website explains:

Stedolan.github. IO /jq/

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p1-tt.byteimg.com/origin/pgc-image/ec3a96d8b92043c798ce2b1aeb26002c?from=pc)

Lightweight and flexible Json processing command line.

use

At this point, some of you may have a sense of what it can be used for. Why do you use JQ for manual mocks? That’s right! You use it to mock the JSON data returned by the interface.

2.2 Download and install JQ

  • Mac: As always, the Mac is as simple as brew install Jq with Homebrew installed
  • Other systems: THE official website of JQ is very considerate and provides the download method of each system and the link of relevant download tools. For details, please check the official Internet cafe, the link is as follows

Stedolan. Making. IO/jq/download…

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p6-tt.byteimg.com/origin/pgc-image/951208c1f1a3436dac1099ea820318ed?from=pc)

2.3 Basic use of JQ

  • Basic filtering.

The simplest filter,. This is a filter that takes input and generates it unchanged as output.

Simply put, it is output as is:

    $ echo '{"jq": "jqTest"}' | jq '.'    {      "jq": "jqTest"    }
Copy the code
  • Object filtering.key,.key1.key2,.[“key”]

Key1. Key2 can be used to get the corresponding value. Isn’t that a bit like jsonPath?

    $ echo '{"jq": {"jqTest": 1} }' | jq '.jq'    {      "jqTest": 1    }    $ echo '{"jq": {"jqTest": 1} }' | jq '.jq.jqTest'    1    $ echo '{"jq": {"jqTest": 1} }' | jq '.["jq"]'    {      "jqTest": 1    }
Copy the code
  • Key [index], key[startIndex: endIndex]

In fact, on the basis of object filtering add an index, learned programming all understand ~ left closed right open, of course, is for the array manipulation, here is an object filtering method based on the example,

.key[index]

    $ echo '{"jq": ["jqTest",1] }' | jq '.jq[0]'    "jqTest"
Copy the code

.key[ startIndex: endIndex]

    $ echo '{"jq": ["jqTest",1] }' | jq '.jq[0:1]'    [      "jqTest"    ]
Copy the code
  • Object construction {}, []

You can build new objects (arrays) with {}, []

    $ echo '{"jq": ["jqTest",1] }' | jq '{ newJq: .jq[1], }'    {      "newJq": 1    }
Copy the code

If one of the expressions has more than one result, the output will also produce more than one result

    $ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' |jq '{user, title: .titles[]}'    {      "user": "stedolan",      "title": "JQ Primer"    }    {      "user": "stedolan",      "title": "More JQ"    }
Copy the code

If key does not exist, value will be assigned null

    $ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' |jq '{user111, title: .titles[]}'    {      "user111": null,      "title": "JQ Primer"    }    {      "user111": null,      "title": "More JQ"    }
Copy the code

Parentheses around the key mean that it will be evaluated as an expression.

    $ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' |jq '{(.user): .titles[]}'    {      "stedolan": "JQ Primer"    }    {      "stedolan": "More JQ"    }
Copy the code
  • Other common uses:

To calculate

echo "10" | jq '(.+2)*5' echo null | jq '{a: 1} + {b: 2} + {c: 3} + {a: 42} 'echo' [XML, yaml, "json"] '| jq'. - [" XML "] 'echo' {" a ": 5} '| jq." a + 10 ='Copy the code

To obtain the length of the

Echo '[[1, 2], "string", 2} {" a ":, null]' | jq. '[] | length'Copy the code
  • Array operation

Here’s a special look at the array operations, which are the way to do this

Jq can have union (‘ + ‘) and difference (‘ – ‘) operations on arrays:

Union (‘+’) : has arrays A,b; A +b gives a new array containing all the elements of the array a and b:

$echo '{" a ": [1, 2, 3]," b ": [three, four, five]}' | jq '. A + b '[1, 2, 3, 3, 4, 5]Copy the code

Difference set (‘-‘) : has arrays A,b; A-b will get a new array containing only elements from A and no elements from B:

$echo '{" a ": [1, 2, 3]," b ": [three, four, five]}' | jq '. A -, b '[1, 2]Copy the code
  • More usage

For more usage, please refer to the official website:

Stedolan. Making. IO/jq/manual / #…

Mock implementation

3.1 Achieving goals

There are 2 popular science articles in the popular science recommendation column in the picture below. Now I want to mock dozens or even hundreds of articles or advertisements.

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p6-tt.byteimg.com/origin/pgc-image/8be1075f09094887beca293a8d8b7438?from=pc)

3.2 Data Preparation

  • The response JSON packets of the captured interface are as follows:

    { "code": 1, "msg": null, "data": { "pageNumber": 0, "pageSize": 10, "totalElements": 12, "totalPages": 2, "pageList": 18, [{" id ":" title ":" alzheimer's disease what harm is there? ", "type" : "patient", "img" : "https://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png", "content": "< / p > < p > https://fx.wxbjq.net.cn/preview/6cAH"}, {" id ": 17," title ":" do you know how alzheimer's disease? ", "type" : "patient", "img" : "https://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/953fc541af7b4576b233d2d6b364bddf_700_360.png", "content": "<p>https://fx.wxbjq.net.cn/preview/6cAK</p>" }] } }Copy the code
  • We store the data in a JSON file and assign it to a variable

    $ mockData=$(cat /tmp/guanggao.json) $ echo "$mockData" { "code": 1, "msg": null, "data": { "pageNumber": 0, "pageSize": 10, "totalElements": 12, "totalPages": 2, "pageList": [{"id": 18, "title": "alzheimer's disease ", "type": "patient", "img": "https://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png", "content": "< / p > < p > https://fx.wxbjq.net.cn/preview/6cAH"}, {" id ": 17," title ":" do you know how alzheimer's disease? ", "type" : "patient", "img" : "https://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/953fc541af7b4576b233d2d6b364bddf_700_360.png", "content": "<p>https://fx.wxbjq.net.cn/preview/6cAK</p>" }] } }Copy the code
  • We then perform a double increment on the pageList list using JQ, passing the result to the mockData variable.

Here we use jQ’s union of arrays (‘ + ‘), adding the two arrays together to create a new array containing all the elements of both arrays.

mockData=$(echo "$mockData" | jq '.data.pageList+=.data.pageList') $ echo "$mockData" { "code": 1, "msg": null, "data": { "pageNumber": 0, "pageSize": 10, "totalElements": 12, "totalPages": 2, "pageList": [ { "id": 18, "title": "What are the risks of Alzheimer's disease? ", "type": "patient", "img": "https://gv.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png", "content": "< / p > < p > https://fx.wxbjq.net.cn/preview/6cAH"}, {" id ": 17," title ":" do you know how alzheimer's disease? ", "type" : "patient", "img" : "https://gv.oss-cn-shanghai.aliyuncs.com/hospital/953fc541af7b4576b233d2d6b364bddf_700_360.png", "content": "< / p > < p > https://fx.wxbjq.net.cn/preview/6cAK"}, {" id ": 18," title ":" alzheimer's disease what harm is there? ", "type" : "patient", "img" : "https://gv.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png", "content": "< / p > < p > https://fx.wxbjq.net.cn/preview/6cAH"}, {" id ": 17," title ":" do you know how alzheimer's disease? ", "type" : "patient", "img" : "https://gv.oss-cn-shanghai.aliyuncs.com/hospital/953fc541af7b4576b233d2d6b364bddf_700_360.png", "content": "<p>https://fx.wxbjq.net.cn/preview/6cAK</p>" } ] } }Copy the code

In this way, the number of elements in the list is doubled each time, that is, n operations become 2 to the n times, the exponential growth rate is not much to say ~

The most important data preparation process is completed:

    $ echo "$mockData" > /tmp/MockTest.json
Copy the code

3.3 Mock implementation of Charles

Charles has a feature called Map Local, which you can find by right clicking on the request to mock:

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p6-tt.byteimg.com/origin/pgc-image/623d6c12485f4e5c9be8a2c2cc413a10?from=pc)

Map Local can replace the specified interface return with a Local file, such as the mocktest.json data file generated in the previous section:

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p1-tt.byteimg.com/origin/pgc-image/a3a64f6744b342cba2636078d7d4e24d?from=pc)

Then refresh the page again and see what happens. Instead of adding data to the database, you can save a JSON file and use it immediately:

! [interface testing Mock combat (2) combined with jq mass of complete manual Mock] (https://p3-tt.byteimg.com/origin/pgc-image/214392a8c235483d8328e33a197d0d9f?from=pc)

Above, look forward to everyone to exchange, a lot of correction.

Click to collect: Automation + side open + performance + RESUME + interview core tutorial materials