For more technical articles to share and free materials to collect click on: ‘qrcode.testing-studio.com/f?from=juej…

For more technical articles to share and free materials to pick up click on the link Interface testing is not just about mastering requests or other powerful libraries. You also need to be able to customize an interface automation testing framework based on your business and needs. So in this section, I will focus on interface test case analysis and how the general process encapsulation is accomplished.

More dry goods can pay attention to the public number one key access oh ~

First before making a use case analysis, through tracing the company a year all the cause of the problem of positioning problem arises, or with the CTO, product manager, research and development, operations, test investigation, get quality pain points, also can analyze the business architecture, process calls, as well as the monitoring and control system realized using the data of the business, quality requirements are achieved.

After obtaining the quality requirements, the testing plan of the company will be determined through the communication with the product manager, project manager and R&D director to know the business scope to be tested, business scenario use cases and business interface analysis. Analysis of test plans in conjunction with quality requirements begins the design of business use cases, including interface test case analysis.

Interface encapsulation can be divided into three dimensions: configuration, interface encapsulation and business process. Configuration is used to obtain the initial configuration and dependency based on the configuration file. Interface encapsulation follows the DESIGN pattern of APIObject, which abstracts the invocation of the interface. Business processes are responsible for data initialization, business use case design, and include process definitions formed by multiple apis, without any interface implementation details, and assertions. The following will be combined with actual combat cases, for detailed introduction.

For information security reasons, many interfaces encrypt the request and response during transmission. It is obviously impossible to assert the data directly. You can assert the decrypted interface only after decrypting the interface.

You need to prepare an interface to encrypt the response before the actual combat. When you make a GET request to it, you get an encrypted response.

Prepare a JSON demo

{"topics":
{
"orange":"movie",
"shool":"testing-studio",
"president":"seveniruby"
}
}

Copy the code

Use base64 to do encryption, get an encrypted file demo64.txt

base64 demo.json >demo64.txt

Copy the code

Use the python command to start a service in the demo64. TXT directory

python -m http.server 10000

Copy the code

After startup, the appearance is shown as follows:

Use the curl command to make a get request for this service

The curl http://127.0.0.1:10000/demo64.txtCopy the code

If the request succeeds, the environment is ready to succeed

Call Base64, decrypt the returned request directly, and get the decrypted response. Convert the decrypted response to JSON format. At this point, you can assert the return value without error

import base64 import json import requests class TestEncode: Url = "http://127.0.0.1:10000/demo64.txt" def test_encode (self) : r = requests.get(self.url) encode = json.loads(base64.b64decode(r.content)) assert encode["topics"]["president"] == "seveniruby"Copy the code

If the protocol of the interface under test changes and the Requests library cannot support the changed protocol, you need to call another third library to send the request message, you still need to modify the underlying source code. In this case, you can add a layer of encapsulation to construct a more generic sending method.

First of all, we need to store all request information through a dictionary structure, including the protocol sent, decoding method, request method, etc. This dictionary structure also lays an important foundation for the subsequent data-driven transformation.

Req_data = {" schema ":" HTTP ", "method" : "get", "url" : "http://127.0.0.1:10000/demo64.txt", headers: None}"Copy the code

Add criteria to select different request protocols through the schema in the structure of the request information. For example, if the schema is “HTTP,” choose to call the wrapped Requests library.

Def send(self, data: dict): if "HTTP" == data["schema"] : res = requests.request( data["method"],data["url"],header=data["headers"]) return json.loads(base64.decode(res.content))  elif "dubbo" == data["schema"]: pass elif "websocket" == data["schema"]: pass else: passCopy the code

Call the send method in the ApiRequest class to send a request and make an assertion. For more technical articles to share and free materials to pick up click on the link