Automatic test method and system for network API (Application Program Interface)
Through automatic deployment and interception redirection technology, independent testing of the business server network API is realized, which solves the problems of inconvenient management and low efficiency in the existing technology, and improves the testing efficiency and system stability.
Patent Information
- Application Number
- CN202410883637.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-03
- Publication Date
- 2024-09-06
AI Technical Summary
In the existing technology, the business server of the game system has problems such as inconvenient management, low efficiency, and encryption of HTTP requests with third-party services during functional testing, making it difficult to achieve independent and efficient testing of the business server network API.
By automatically deploying target business services, simulating clients to send task requests, intercepting and redirecting HTTP requests from third-party services to local test services, and configuring mock interfaces and expected responses, test cases can be completed without third-party service and business code changes. execution.
It achieves efficient and independent testing of business services, improves the system launch speed and stability, simplifies the testing process, and avoids modification of the original code.
Smart Images

Figure CN118611976A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer programming technology, and in particular to an automated testing method and system for a network API, a computing device, and a computer-readable storage medium. Background Art
[0002] In the prior art, the implementation of the game system relies on business servers that provide various functions. Such servers not only receive requests from clients, but also usually interact with third-party servers over the network. Their functions are relatively complex, such as user login and registration through different channels, and interaction with various payment platforms. In functional testing of business servers, it is usually necessary to rely on clients or hard-code the logic into the server code. Even through configuration files, there are problems of inconvenient management and low efficiency. There are also problems such as encryption of HTTP requests with third-party services. Therefore, there is an urgent need for a unified automated testing method to independently and efficiently test the network API of business servers to improve the speed and stability of system launch. Summary of the invention
[0003] In view of this, the embodiments of the present application provide an automated testing method and system for a network API, a computing device, and a computer-readable storage medium to address the technical deficiencies in the prior art.
[0004] According to a first aspect of an embodiment of the present application, there is provided a method for automated testing of a network API, comprising:
[0005] Obtain a program package of the target business service according to the test task, and deploy it to the first server;
[0006] Trigger the test case to simulate the client sending a first task request to the target business service; the target business service sends an HTTP request to the third-party service according to the first task request;
[0007] The target business service intercepts the HTTP request and redirects it to the local test service;
[0008] The local test service receives the redirected HTTP request and returns the corresponding expected response according to the API path in the HTTP request to complete the execution of the test case.
[0009] According to a second aspect of an embodiment of the present application, there is provided an automated testing system for a network API, including:
[0010] An automatic deployment unit, used to obtain a program package of a target business service according to a test task, and deploy it to a first server;
[0011] The first test unit is used to trigger a test case and simulate the client sending a first task request to a target business service;
[0012] The target business service is used to send an HTTP request to a third-party service according to the first task request; and intercept the HTTP request and redirect it to a local test service;
[0013] The local test service is used to receive the redirected HTTP request and return the corresponding expected response according to the API path in the HTTP request to complete the execution of the test case.
[0014] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor executes the steps of the automated testing method of the network API.
[0015] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the instructions are executed by a processor, the steps of the automated testing method of the network API are implemented.
[0016] In an automated testing method for a network API provided in an embodiment of the present application, efficient and independent testing of business services that have an interactive relationship with third-party services is achieved. When testing, the target business service is first automatically deployed according to the test task, and the client is simulated to initiate a related task request and send it to the target business service. When the business service receives the request, the HTTP request of the third-party service involved is intercepted, and the third-party service domain name is replaced with the local test service domain name through hook technology; and in the local test service, different corresponding mock interfaces are configured according to different third-party service APIs, and the corresponding expected return values are set according to the requirements of the test case, thereby completing the execution of the entire test case without the need for third-party services or changes to the original business service code. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 is a structural block diagram of a computing device provided in an embodiment of the present application;
[0018] Figure 2 It is a flowchart of an automated testing method for a network API provided in an embodiment of the present application;
[0019] Figure 3 It is a schematic diagram of configuring a network API in a local test service provided in an embodiment of the present application;
[0020] Figure 4It is a structural diagram of an automated testing system for a network API provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0022] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0023] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "in response to determination".
[0024] In the present application, a method and system for automated testing of a network API, a computing device, and a computer-readable storage medium are provided, which are described in detail one by one in the following embodiments.
[0025] Figure 1 The structure block diagram of a computing device 100 according to an embodiment of the present application is shown. The components of the computing device 100 include but are not limited to a memory 110 and a processor 120. The processor 120 is connected to the memory 110 via a bus 130, and the database 150 is used to store data.
[0026] The computing device 100 also includes an access device 140 that enables the computing device 100 to communicate via one or more networks 160. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0027] In one embodiment of the present application, the above components of the computing device 100 and Figure 1 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 1 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0028] The computing device 100 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 100 may also be a mobile or stationary server.
[0029] The processor 120 may execute Figure 2 The steps in the automated testing method of a network API shown include steps 202 to 206.
[0030] Step 202: Obtain a program package of the target business service according to the test task, and deploy it to the first server.
[0031] In this step, in order to automate the test tasks, the following methods are adopted:
[0032] In continuous integration and continuous delivery tools, such as GitLab CI process, create a network API test task according to the target object of the test. Whenever a test package needs to be generated, this task will be triggered.
[0033] The test task automatically pulls code from the code repository, installs dependencies, compiles and packages, and generates a target package file for testing. The target package is a package that provides the target business service. In a feasible implementation, the package is an egg package or a wheel package. After packaging is completed, the target package is further deployed to the first server and executed to complete the startup of the target business service.
[0034] Step 204: triggering the test case, simulating the client sending a first task request to the target business service; the target business service sends an HTTP request to the third-party service according to the first task request.
[0035] In this step, after the target business service is started, the predefined test cases are obtained and triggered.
[0036] The test case includes multiple test tasks, such as multiple first tasks that need to interact with third-party services, and the first tasks have a front-and-back dependency relationship with the other multiple test tasks.
[0037] When executing the first task in the test case, the simulated client sends a first task request to the target business service on the first server, wherein the completion of the first task request requires the target business service to perform HTTP requests with a third-party service, including but not limited to payment requests, registration and login requests, redemption requests, etc.
[0038] Step 206: The target business service intercepts the HTTP request and redirects it to the local test service.
[0039] In this step, after receiving the first task request, the target business service generates an HTTP request to access the third-party service according to the content of the first task. For example, the target business service provides an interface to receive the payment task of the client, and then generates a new HTTP request according to the content of the payment task and sends it to the third-party payment platform to complete the payment. The following is a schematic code:
[0040] from flask import Flask,request
[0041] import requests
[0042] app = Flask(__name__)
[0043] @app.route(' / task',methods=['POST'])
[0044] def receive_task():
[0045] #Get the content of the first task
[0046] task_data=request.get_json()
[0047] #Generate a new HTTP request based on the content of the first task
[0048] target_url=task_data.get('target_url')
[0049] request_method=task_data.get('request_method','GET')
[0050] request_headers=task_data.get('headers',{})
[0051] request_data=task_data.get('data',{})
[0052] try:
[0053] #Send HTTP request to third-party payment platform
[0054] response = requests.request(
[0055] method = request_method,
[0056] url=target_url,
[0057] headers = request_headers,
[0058] data = request_data )
[0060] It should be noted that the above process of generating a new HTTP request according to the content of the payment request and sending it to the third-party payment platform is a normal functional implementation of the target business service.
[0061] Furthermore, the target business service intercepts the HTTP request and redirects it to the local test service. Specifically, the HTTP request can be hooked by a plug-in, a decorator, or a middleware, and the original HTTP request can be modified using the pre-configured configuration in the configuration file to point to the local test service.
[0062] For example, in the Flask Web framework, the before_request decorator is used to intercept and modify HTTP requests before entering the routing function (business code). In this way, the routing function executes normally without knowing the existence of the modification, thereby achieving the decoupling of the business logic and test function of the target business service. The schematic code is as follows:
[0063] Use decorators to intercept and modify HTTP requests before entering the routing function:
[0064] from flask import Flask,request,redirect
[0065] import json
[0066] app = Flask(__name__)
[0067] #Read the configuration file
[0068] with open('config.json')as config_file:
[0069] data = json.load(config_file)
[0070] new_domain=data['new_domain']
[0071] @app.before_request
[0072] def before_request():
[0073] #Replace the domain name of the business service with the test service domain name in the configuration file. For example, if an original HTTP request is http: / / third-party service domain name / V2 / game / xiaomi / logon, replace its domain name with http: / / new_domain / V2 / game / xiaomi / logon
[0074] new_url = request.url.replace (third-party service domain name, new_domain)
[0075] #Redirect the request to the new URL and use the 307 code to ensure that the request method remains unchanged
[0076] return redirect(new_url,code=307)
[0077] In the above example, the before_request decorator intercepts and modifies the HTTP request before entering the routing function and sends it to the local test service. The routing function is the business code that implements normal functions. It does not need to care whether the request has been intercepted or modified, which achieves decoupling from the hook logic.
[0078] Preferably, the above-mentioned interception and redirection functions of the target business service are controlled by a switch.
[0079] Step 208: The local test service receives the redirected HTTP request and returns a corresponding expected response according to the API path in the redirected HTTP request, thereby completing the execution of the test case.
[0080] In step 206, the target business service intercepts and redirects the HTTP request so that the request to the third party is sent to the local test service. In the local test service, different corresponding mock interfaces are configured according to different third-party service APIs, and corresponding expected return values are set, such as Figure 3 shown.
[0081] For example, for the login API interface in the URL: / V2 / game / xiaomi / logon, a successful response is established, that is, a successful login response of the simulated channel.
[0082] For example, for the payment API interface in the URL: / game / weixin / payment-requests, a mock interface that returns failure is established, that is, a response that simulates a third-party payment failure.
[0083] Then, according to the above response, the execution of the entire test case is completed and reported. The test case includes multiple first tasks, and the above first tasks have a front-and-back dependency relationship with other multiple test tasks. The test case completes the execution of the entire test case according to the simulated response result of the first task without relying on third-party services.
[0084] Furthermore, the execution reports of all test cases are automatically sent through the communication tool.
[0085] In an embodiment of the present application, in order to realize independent testing of business services that have an interactive relationship with third-party services, when performing automated testing, the client is simulated to initiate a related task request. When the business server receives the request, the HTTP request of the third-party service is intercepted, and the third-party service domain name is replaced with the local test service domain name through hook technology; and in the local test service, different corresponding mock interfaces are configured according to different third-party service APIs, and the corresponding expected return values are set according to the requirements of the test case, thereby realizing the execution of the entire test case without relying on third-party services or making changes to the original business service code.
[0086] Corresponding to the above method embodiment, the present application also provides an embodiment of an automated testing system for a network API, such as Figure 4 As shown, the system includes:
[0087] An automatic deployment unit, used to obtain a program package of a target business service according to a test task, and deploy it to a first server;
[0088] The first test unit is used to trigger the test case in the test service and simulate the client sending a first task request to the target business service;
[0089] The target business service is used to send an HTTP request to a third-party service according to the first task request; and intercept the HTTP request and redirect it to a local test service;
[0090] The local test service is used to receive the redirected HTTP request and return the corresponding expected response according to the API path in the HTTP request to complete the execution of the test case.
[0091] The above is a schematic scheme of an automated testing system for a network API of this embodiment. It should be noted that the technical scheme of the automated testing system for the network API and the technical scheme of the automated testing method for the network API described above are of the same concept, and the details of the technical scheme of the automated testing system for the network API that are not described in detail can be found in the description of the technical scheme of the automated testing method for the network API described above.
[0092] In one embodiment of the present application, a computing device is also provided, including a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor implements the steps of the automated testing method of the network API when executing the instructions.
[0093] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the above-mentioned network API automated testing method belong to the same concept, and the details of the technical scheme of the computing device that are not described in detail can be found in the description of the technical scheme of the above-mentioned network API automated testing method.
[0094] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the steps of the automated testing method for the network API as described above.
[0095] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the automated testing method of the network API described above are of the same concept, and the details not described in detail in the technical scheme of the storage medium can be found in the description of the technical scheme of the automated testing method of the network API described above.
[0096] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0097] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0098] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0099] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0100] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. An automated testing method for a network API, characterized in that: include: Obtain a program package of the target business service according to the test task, and deploy it to the first server; Trigger the test case to simulate the client sending the first task request to the target business service; The target business service sends an HTTP request to the third-party service according to the first task request; The target business service intercepts the HTTP request and redirects it to the local test service; The local test service receives the redirected HTTP request and returns the corresponding expected response according to the API path in the HTTP request to complete the execution of the test case.
2. The method according to claim 1, wherein: The target business service intercepts the HTTP request and redirects it to the local test service, including: The HTTP request is hooked and the HTTP request is modified using the pre-configuration in the configuration file to point to the local test service.
3. The method according to claim 1, wherein: The method further includes: Control the interception and redirection functions of the target business service through a switch.
4. The method according to claim 1, wherein: Returning the corresponding expected response according to the API path in the HTTP request, completing the execution of the test case includes: In the local test service, configure different corresponding mock interfaces according to different third-party service APIs, and set the corresponding expected return values.
5. The method according to claim 1, wherein: The first task includes but is not limited to: a payment request, a registration and login request, and a redemption request.
6. An automated testing system for a network API, characterized in that: include: An automatic deployment unit, used to obtain a program package of a target business service according to a test task, and deploy it to a first server; The first test unit is used to trigger a test case and simulate the client sending a first task request to a target business service; The target business service is used to send an HTTP request to a third-party service according to the first task request; And intercept the HTTP request and redirect it to the local test service; The local test service is used to receive the redirected HTTP request and return the corresponding expected response according to the API path in the HTTP request to complete the execution of the test case.
7. A computing device comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, characterized in that: When the processor executes the instructions, the steps of the method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium storing computer instructions, characterized in that: When the instruction is executed by a processor, the steps of the method described in any one of claims 1 to 5 are implemented.
