The best in the whole network, a detailed summary of the interface test mock, completely open the interface test...


foreword

What is a mock?

Mock is the interface that development needs to rely on in the development process, but the other party does not provide or the environment, etc., in short, there is no such thing, then the development uses the mock server's own mock data, which is convenient for normal development and self-testing of the written function.

In the current software development process, especially the app part, a lot of data and content needed come from the server-side API, but there is no guarantee that when the client is developed, the API has been developed on the server side and is waiting for the front-end to call.

The ideal situation is that when the front-end is being developed, someone has already written the interface, and it is ok to call it directly, but the whole is an ideal state. If the students who provided the API did not provide it during the front-end development, then we need our mock data at this time.

The specific steps of mock data

1. You can download moco-runner-0.11.0-standalone.jar from https://github.com/dreamhead/moco address;

2. Write login.json;

3. Run this package;
java -jar moco-runner-0.10.0-standalone.jar http -p 12306 -c login.json
Note:
The mock server and the written login.json file are in the same directory, -p: specifies the port, -c: specifies the file)

4. We use postman to verify whether the login interface of our mock is ok

5. Let's use python to write an interface test case to verify the code for changing the login interface and obtaining its token.

Next, write a json file for a login and a business (that is, enter a license plate number, query the display duration of the license plate number, and its parking fee)

[
  {
    
    
    "request":
    {
    
    
      "method":"post",
      "uri":"/login",
      "json":
      {
    
    
        "username":"admin",
        "password":"admin",
        "roleID":22
      }
    },
    "response":
    {
    
    
      "file":"login_response.json"
    }
  },
  {
    
    
    "request":
    {
    
    
      "method":"post",
      "uri":"/parkinside",
      "json":
      {
    
    
        "token":"asdgfhh32456asfgrsfss",
        "vpl":"AJ3585"
      }
    },
    "response":
    {
    
    
      "file":"parkinside.json"
    }
  }
]

]

B1

The response data of the login page: login_response.json

 {
    
    
        "username":"wuya",
        "userID":22,
        "token":"asdgfhh32456asfgrsfss"
}

Response data on the business side: parkinside.json

{
    
    
  "vplInfo":
  {
    
    
    "userID":22,
    "username":"wuya",
    "vpl":"京AJ3585"
  },
  "Parking time long":"20小时18分钟",
  "Parking fee":"20$"
}

Run the package:

java -jar  moco-runner-1.3.0-standalone.jar http -p 12306 -c login.json

Note:
The mock server and the login.json file are in the same directory, -p: is the specified port -c is the specified file

B2

Next, we use postman to verify whether the login interface and business interface of our mock are OK

B3

B4

We use python to write an interface test case to verify the login interface and obtain its token.
Implementation code:

import unittest
import requests


class MockTest(unittest.TestCase):
    def setUp(self):
        self.url = 'http://localhost:12306'

    def tearDown(self):
        pass

    def test_login(self, url='/login'):
        '''验证登录的接口'''
        data = {
    
    
            "username": "admin",
            "password": "admin",
            "roleID": 22
        }
        r = requests.post(self.url + url, json=data)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.json()['username'], 'wuya')

    def getToken(self, url='/login'):
        '''登录成功后获取token'''
        data = {
    
    
            "username": "admin",
            "password": "admin",
            "roleID": 22
        }
        r = requests.post(self.url + url, json=data)
        return r.json()['token']


    def test_parkingside(self, url='/parkinside'):
        '''验证查询停车时长接口'''
        data = {
    
    
            "token": self.getToken(),
            "vpl": "AJ3585"
        }
        r = requests.post(self.url + url, json=data)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.json()['Parking time long'], u'20小时18分钟')
        self.assertEqual(r.json()['Parking fee'], u'20$')

if __name__ == '__main__':
    unittest.main(verbosity=2)
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

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

As long as you keep moving forward, every step is a ladder to success. Believe in your own abilities and persist in striving, you will reap your ideals and achievements. No matter how difficult the road is, as long as you embrace your dreams, you will be able to soar in the vast sky of life.

As long as you have a dream in your heart and work hard, you will not be afraid of any difficulties. Every sweat flow is a step towards success. Believe in yourself, go forward, omnipotent!

As long as there is determination, there is no mountain that cannot be climbed; as long as there is perseverance, there is no gully that cannot be crossed; as long as there is courage, there is no difficulty that cannot be overcome. Believe in yourself, keep striving, success will belong to you!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/131810126