Previous introduction: Because the content of this chapter is suitable for JQ tools, so in the beginning of the first will spend a certain length to introduce the JQ machine, if you are familiar with JQ can directly skip this part

1. Application scenarios

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

    If the dishes are not enough to eat ~ no, it is because the number of dishes is not enough to test. If we want to test hundreds of dishes, will there be any dishes when brushingPerformance impact.showIs it normal? The list showsBoundary valueTesting, 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!

2. Introduction to JQ

2.1 Overview and usage of JQ

Portal: JQ official website

Json

  • useSpeaking of which, some of you probably have a sense of what it can be used for, why manualmockIn order to usejqYes! It is used to process the return from the interfacejsonData to achievemockThe effect of

2.2 Download and install JQ

  • Mac: Mac is as simple as everHomebrewWords a commandbrew install jqCan be
  • Other systems:

    Jq’s website is very intimate, gives the way to download the system and its related links to download the tools needed, specific Internet cafes to see officer, link below stedolan. Making. IO/jq/download…

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 to filter .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
Copy the code
$ echo '{"jq": {"jqTest": 1} }' | jq '.["jq"]'
{
  "jqTest": 1}Copy the code
  • The index filtration .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 to build {}[]

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: calculations

    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 more usage may refer to 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

3.2 Data Preparation

  • Grab the interface’s responsejsonThe message is as follows:
{
	"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://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png"."content": "<p>https://fx.wxbjq.net.cn/preview/6cAH</p>"
		}, {
			"id": 17."title": "Do you know how Alzheimer's develops?"."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": "What are the risks of Alzheimer's disease?"."type": "patient"."img": "https://greenvalley.oss-cn-shanghai.aliyuncs.com/hospital/a295c2081459450ba6679db48d6a2471_700_360.png"."content": "<p>https://fx.wxbjq.net.cn/preview/6cAH</p>"
		}, {
			"id": 17."title": "Do you know how Alzheimer's develops?"."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
  • And then we usejqrightpageListThe list in+ =Operation to perform a double growth and pass the result to the variablemockData

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>https://fx.wxbjq.net.cn/preview/6cAH</p>"
      },
      {
        "id": 17."title": "Do you know how Alzheimer's develops?"."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>"
      },
      {
        "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>https://fx.wxbjq.net.cn/preview/6cAH</p>"
      },
      {
        "id": 17."title": "Do you know how Alzheimer's develops?"."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 will be doubled each time, that is, the number of elements in the list will be 2 ^ n times, and the exponential growth rate will not need to be mentioned.

$ 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:

Map Local
MockTest.json

json