Interface automated testing: Moco tool for mock server

what is mock server

Mock: English can be translated as imitation. Mock server is a technology we use to remove dependencies (coupling) and pretend to be implemented. For example, the front-end needs to use certain APIs for debugging, but the server has not developed these APIs. Then the front-end The work is blocked by the server, then you can use the mock server to pretend to implement these APIs, which can return specific data and help the front end to render the page. Of course, we need to make an agreement with the server for convenience, what is the content of the agreed interface .

restful interface specification

Transfer to Ruan Yifeng’s blog—RESTful API Design Guide: http://www.ruanyifeng.com/blog/2014/05/restful_api.html

Moco-convention uri (1)

The moco tool is an open source project on github. You can use the moco tool to set up a simple mock server to facilitate our debugging. The github address: https://github.com/dreamhead/moco. The downloaded one is a jar package. The current The version is 0.11.1. First, we need to write a config file, and write the request and response we need to "simulate" into this configuration file. The configuration file is in json format. Next, we write a relatively simple request to visit localhost: 12306/hello interface, return a plain text "moco", the moco tool has agreed on port 12306, no need to tangle, it is similar to tomcat's agreement on port 8080, the config.json file is as follows, and the json file should be placed in the same jar package as the moco folder. For example, the blogger's directory structure:

[
    {
        "request":
        {
            "uri":"/hello"
        },
        "response":
        {
            "text":"moco"
        }
    }
]

The configuration file is relatively simple. We request the interface, return a plain text, and start the command:

>java -jar moco-runner-0.11.1-standalone.jar http -p 12306 -c config.json

The http here is the http protocol, -p 12306 binds the port number 12306, -c config.json reads the config file
insert image description here

Seeing the above performance, it means that moco has been successfully started. We visit localhost:12306/hello and see the following results, which means that the mock server has successfully returned the agreed data "moco"
insert image description here 

Moco-convention uri (2)

Modify the config file as follows, note that the moco tool can monitor the changes in the json configuration file in real time and restart the server by itself
insert image description here

[
    {
        "request":
        {
            "uri":"/"
        },
        "response":
        {
            "text":"welcome to Moco"
        }
    },
    {
        "request":
        {
            "uri":"/hello"
        },
        "response":
        {
            "text":"moco"
        }
    }
]

 Next, visit localhost:12306 and 12306:12306/hello respectively, the results are as follows:
insert image description here
insert image description here

Moco-agreement get request

[
    {
        "request":
        {
            "method":"get",
            "uri":"/get"
        },
        "response":
        {
            "text":"moco get"
        }
    }
]

 Moco-agreement post request

[
    {
        "request":
        {
            "method":"post",
            "uri":"/post"
        },
        "response":
        {
            "text":"moco post"
        }
    }
]

Moco-Convention Request Parameters

[
    {
        "request":
        {
            "method":"get",
            "uri":"/get",
            "queries":
            {
                "id":"12306",
                "name":"moco"
            }
        },
        "response":
        {
            "text":"moco queries"
        }
    }
]

insert image description here

Moco-agreement request body must be in json format

[
    {
        "request":
        {
            "method":"post",
            "uri":"/post",
            "text":
            {
                "json":"{\"id\":\"12306\",\"name\":\"moco\"}"
            }
        },
        "response":
        {
            "status":"200"
        }
    }
]

 Moco-agreement request header

[
    {
        "request":
        {
            "method":"post",
            "uri":"/post",
            "headers":
            {
                "content-type":"application/json",
                "Connection":"keep-alive",
                "Content-Encoding":"gzip"
            }
        },
        "response":
        {
            "status":"200"
        }
    }
]

Moco-agreement return content

We have already seen the centralized return content of response such as text and status. Let’s show the returned file and set the file format, etc.

[
    {
        "request":
        {
            "method":"post",
            "uri":"/post",
        },
        "response":
        {
            "file":"data.js",
            "charset":"GBK",
            "version":"HTTP/1.0"
        }
    }
]

Moco-promise return status code
See the above json, which already contains the usage of return status code

Moco-use in unit testing (taking Python as an example)

[
    {
        "request":
        {
            "method":"get",
            "uri":"/api/hello"
        },
        "response":
        {
            "text":"hello Savitar!",
            "status":200
        }
    }
]

Here, a get request is simulated, and the plain text "hello Savitar!" and status code 200 are returned. First, visit localhost:12306/api/hello in the browser and the result is as follows:

insert image description here
It shows that the interface returns no problem, and then use Python requests+unittest to write a simple interface test case

#coding=utf-8
'''
@author=Savitar
'''
import unittest
import requests

class MocoTestApi(unittest.TestCase):
    def setUp(self):
        self.url = "http://localhost:12306"
    def test_moco_test_api(self):
        api = "/api/hello"
        url = self.url+api
        r = requests.get(url)
        self.assertEqual(r.status_code,200)
        self.assertEqual(r.text,"hello Savitar!")
    def tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main()

 The running results are as follows:
insert image description here
the above code implements the access interface, and the status code and return content returned by the assertion, the assertion is successful, indicating that the interface test passed


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project

5. Resume of first-tier manufacturers


6. Test and develop DevOps system

7. Commonly used automated testing tools


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

  

Guess you like

Origin blog.csdn.net/nhb687096/article/details/132147921