Moco of the Python interface test

Source: Internet
Author: User

In the current software development process, especially the part of the app, much of the data and content required is from the server side of the API, but there is no guarantee

In the client development, the API in the server side has been developed, specifically waiting for the front-end to invoke, ideally the front-end in the development of the time, has been written

OK interface, direct call on it, but this is only the ideal situation, many times, the reality is always more than the ideal of a layer of thinking and suffering, if the front-end development

When the API provided by the classmate did not provide, then how to do? Wait or develop first, wait for sure is foolish, then develop how to solve

The API provides this problem of data, then using a mock can be a good solution, what is a mock? The simple understanding of mock-up is that in the development process,

Need to rely on a part of the interface, but the other side does not provide or environment and so on, in short, is not, then the development of using mock server itself to mock

Data to facilitate their normal development and self-test of the functions written.

Can download to Moco-runner-0.12.0-standalone.jar in Https://github.com/dreamhead/moco address, download down

is a jar, where the author describes the section in detail and starts the mock server method, which is not repeated again. Down we

Simply write a login, see the written Login.json string, one is the login request, and the other is the request for parking fee according to the license plate:

[
{
"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"
}
}
]

In the UI or interface Automation test, we know that in TDD mode, the separation of data, which is also for the convenience of maintenance, so that the purpose of the late automation test case extensibility, and its ease of maintenance.

Contents of the Login_response.json file:

{  "username":"Wuya""userID": "token":"  ASDGFHH32456ASFGRSFSS"}              

Contents of the Parkinside.json file:

{"Vplinfo":  {"Userid": 22,"Username":"Wuya", "VPL":" jing AJ3585" }, "Parking Time Long":" 20 hr 18 min ", "Parking fee":"20$"}     

Before running the command, it is best to have the mock server and the Login.json file written in the same directory, see the command executed:

Java-jar moco-runner-0.10.0-standalone.jar http-p 12306-c Login.json

In the command above, Java-jar Moco-runner-0.10.0-standalone.jar is the boot jar, there's nothing to say, HTTP means HTTP protocol,

-P followed by the port number, where the port number refers to the JSON file that is written behind 12306,-c, here is Login.json, see the command after the execution of the

Information (remember that no error message indicates OK, if there is an error, slowly check the error), see:

OK, down we use postman to verify, our mock login interface is not OK, see postman Fill in the information, see:

The header is:

    • content-length→63
    • Content-type→application/json

Below we through the Python language, to the above two interfaces on the actual combat operation, remember Parkinside interface is in the login before the operation can operate the business, not logged in operation of the business, return 502 invalid

token, see Implementing the Code:

#!/usr/bin/env python#-*-Coding:utf-8-*-ImportUnitTestImportRequestsClassMocktest (unittest. TestCase):DefSetUp (self): self.url=‘http://localhost:12306‘DefTearDown (self):Passdef test_login (self,url=‘/login‘):‘‘‘Verify the Login interface‘‘‘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‘):‘‘‘Get token after successful login‘‘‘Data={"Username":"Admin","Password":"Admin","Roleid": 22} r=requests.post (self.url+url,json=Datareturn R.json () [‘Token‘]def test_parkingside (self,url=‘/parkinside‘):‘‘‘Verify query parking Time long interface‘‘‘Data={"Token": Self.gettoken (),"Vpl":"AJ3585"} r=requests.post (self.url+url,json=data) self.assertequal (R.status_code,200 "parking time Long '],u '  20 h 18 min  ") self.assertequal (R.json () [  "parking fee ' 20$ " ) if __name__== ' __main__ " : Unittest.main (verbosity=2)          

Moco of the Python interface test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.