In terms of overall testing efficiency, API testing technology is one of the most effective means to improve testing efficiency, because its execution efficiency is very high,
Another point is the separation of front-end and back-end development mode, which also requires us to devote more energy and time to API testing technology and API testing technology
Landing and application of technology in enterprises. Of course, this is only at the functional level, and non functional points need to be considered, such as queues, scheduling mechanisms and services
Performance testing, stability factors, these are very many. In this article, we only consider the solution of association in API testing technology
Ideas and case application. In fact, the core of API testing does not lie in the testing of a single API. A single API can not guarantee the coverage of business, so we pay more attention to it
Many of these points need to be tested in combination with business scenarios, but once combined with specific business scenarios, it also involves the idea of association. The so-called association is actually
We can understand that the output of the previous API is the input of the next API. The following is a demonstration of this part in combination with mainstream testing tools and code
Solutions and case studies.
The following source code mainly shows the micro service of an order. We need to log in to view the detailed data of the order
That is to say, after the login is successful, the interface of the order details can be returned to the order detail data. If it is not logged in, it will return to 401.
Error message for. The specific process can be described in detail as follows: log in successfully, return the token information after successful login, and when accessing the next interface
You need to bring the information of the token value returned after successful login to access the order details normally, and one thing to pay special attention to is that each login is successful
The returned tokens are random strings, and it is necessary to keep the token of the access interface consistent with the token value returned after successful login
The idea of the problem is very simple, that is, use the idea of association to solve it. The case source code is as follows:
from flask import Flask,make_response,jsonify,abort,request from flask_restful import Api,Resource from flask import Flask from flask_jwt import JWT, jwt_required, current_identity from werkzeug.security import safe_str_cmp app=Flask(__name__) app.debug = True app.config['SECRET_KEY'] = 'super-secret' api=Api(app=app) orders=[ {'id':1,'author':'wuya','bookName':'Python Interface automation test practice','done':True,'payType':'Alipay','price':69.9}, ] class User(object): def __init__(self, id, username, password): self.id = id self.username = username self.password = password def __str__(self): return "User(id='%s')" % self.id users = [ User(1, 'wuya', 'asd888') ] username_table = {u.username: u for u in users} userid_table = {u.id: u for u in users} def authenticate(username, password): user = username_table.get(username, None) if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')): return user def identity(payload): user_id = payload['identity'] return userid_table.get(user_id, None) jwt = JWT(app, authenticate, identity) class Order(Resource): decorators=[jwt_required()] def get(self): return jsonify({'status':0,'msg':'ok','datas':orders}) api.add_resource(Order,'/v1/order') if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
PostMan's Association solution
After clarifying the business scenarios and ideas, use the PostMan test tool to demonstrate its application. PostMan is very mainstream
API testing tool. In the daily R & D process, test development and development students will use this tool to verify the accuracy of the interface. Detailed below
Demonstrate the solution to association.
Get token
First, we need to obtain the token value information in the PostMan test tool. The accessed interface address information is / auth, as follows:
As you can see above, access will be returned after the request is successful_ The information about the token value needs to be considered in the PostMan test tool
Get access from the response data in tests_ The value of token, and a variable is defined in tests to store the obtained access_ The value of token in tests
You can use JS code to get the data of the response, that is, you can transpose the JSON string to the JSON object through JSON.parse(), and then define a
Variable to store the obtained JSON object, and then use the environment in PostMan to define a variable in this variable to store the obtained JSON object
access_ The value of token. The specific process is as follows:
//An object that converts a JSON string to JSON var jsonData=JSON.parse(responseBody) console.log(jsonData)
The specific information in PostMan is shown as follows:
In the above screenshot, we can see that the response data has been obtained. The next step is to separate access from the response data_ The value of token, and define
A variable to store the obtained access_token, the updated JS code is:
//An object that converts a JSON string to JSON var jsonData=JSON.parse(responseBody) console.log(jsonData) //Define the token variable to store the separated access_token pm.environment.set("token", jsonData.access_token); //Gets the value of the defined variable and checks whether it is obtained console.log(pm.environment.get("token"))
The overall screenshot information is as follows:
Call variable
Ah, ah