Method and system for testing application programming interface
Through interface parameter expansion and variable replacement rules, multiple test data sets are constructed to realize fully automated testing of complex APIs, solving the problems of incomplete coverage and high maintenance costs in the existing technology, and improving testing efficiency and scalability.
Patent Information
- Application Number
- CN202410572719.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-10
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2044-05-10
AI Technical Summary
In the prior art, API testing of complex business links has problems such as incomplete coverage and high maintenance costs, and it is difficult to ensure API quality when the project launch cycle is urgent.
By expanding the interface parameters, the test scripts and initial data are generated and loaded, and the preset variable replacement rules are used to replace data, and different test data sets are constructed to simulate the lag and intersection of business processing, thereby realizing fully automated testing and identifying Deep-level problems in system architecture.
It significantly improves the testing coverage efficiency of complex interface business scenarios, reduces the maintenance cost of test script data, shortens the project launch cycle, supports full process expansion, and has better subsequent scalability.
Smart Images

Figure CN118467356B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of interface automation testing, and in particular to a method, system, electronic device, and computer program product for testing an application programming interface. Background Art
[0002] As the pace of enterprise informatization and digital transformation accelerates, online management of the entire project chain is becoming increasingly important, and the customization of business needs (characterized by long business links, many front-end dependencies, and multiple hybrid scenario combinations) is gradually increasing, and the risk of system architecture adjustment is increasing. Therefore, how to ensure the quality of the project's application programming interface (API) under the condition of tight project launch cycle time and no increase in costs has become a major challenge facing enterprises. To meet this challenge, it is necessary to test the API before the project goes online.
[0003] In the prior art, testing of APIs with complex business links has always had problems of incomplete coverage and high maintenance costs.
[0004] Therefore, a new testing scheme needs to be designed to overcome the above problems. Summary of the invention
[0005] The present disclosure provides a method, system, electronic device and computer program product for testing an application programming interface to solve the deficiencies in the prior art.
[0006] The present disclosure provides a method for testing an application programming interface, comprising:
[0007] Generate and load test scripts and initial test data based on the interface parameters of the application programming interface (API) to be tested and preset initialization rules;
[0008] Performing parameter expansion on the initial test data according to pre-configured expansion parameters and a pre-configured expansion strategy to generate a first test data set;
[0009] Using a preset variable replacement rule, replace the test data in the first test data set to generate a second test data set;
[0010] The test script is used to execute the test task by traversing the second test data set to generate a test result.
[0011] The present disclosure also provides a system for testing an application programming interface, comprising:
[0012] The initialization module is configured to: generate and load a test script and initial test data based on interface parameters of the application programming interface API to be tested and preset initialization rules;
[0013] A parameter expansion module is configured to: perform parameter expansion on the initial test data according to pre-configured expansion parameters and pre-configured expansion strategies to generate a first test data set;
[0014] A variable replacement module is configured to: replace the test data in the first test data set by using a preset variable replacement rule to generate a second test data set;
[0015] The test execution module is configured to: utilize the test script to execute the test task by traversing the second test data set to generate a test result.
[0016] The present disclosure also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method for testing an application programming interface as described in any one of the above is implemented.
[0017] The present disclosure also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method for testing an application programming interface as described in any one of the above is implemented.
[0018] The present disclosure also provides a computer program product, including a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above methods for testing an application programming interface.
[0019] By using the method for testing the application programming interface provided by the above embodiment of the present disclosure, firstly, in the actual testing process, different combinations of parameter extensions can be used to simulate the lag and cross-talk of business processing, thereby discovering some problems that cannot be discovered in conventional single-interface testing scenarios, and identifying some deep-seated problems in system architecture. Secondly, in actual business scenarios, different dependent business data can be constructed by using parameter extensions, which can greatly improve the efficiency of process testing and shorten the project launch cycle.
[0020] As described above, the method for testing the application programming interface provided by the above embodiment of the present disclosure, on the one hand, is easy to use, has clear logic, and is highly flexible, which significantly improves the efficiency of covering complex interface business scenarios and reduces the cost of test script data maintenance; on the other hand, it supports the expansion of the entire process and has better subsequent scalability than using existing frameworks or tools. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the technical solutions in the present disclosure or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0022] Figure 1 is a flowchart of a method for testing an application programming interface provided by the present disclosure;
[0023] Figure 2 It is a schematic diagram of an interface test case design for determining a preset interface method template provided by an embodiment of the present disclosure;
[0024] Figure 3 is a schematic diagram of the structure of a system for testing an application programming interface provided by the present disclosure;
[0025] Figure 4 It is a schematic diagram of the structure of an electronic device provided by the present disclosure. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solutions and advantages of the present disclosure clearer, the technical solutions in the present disclosure will be clearly and completely described below in conjunction with the drawings in the present disclosure. Obviously, the described embodiments are part of the embodiments of the present disclosure, rather than all the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0027] Summary of the invention concept:
[0028] After research, the inventors of the present disclosure found that the interface testing solutions in the prior art can basically achieve full automation for simple interfaces; however, for interfaces of complex business links, they lack flexibility and have high usage barriers, resulting in incomplete test coverage and high maintenance costs.
[0029] In response to the technical problems existing in the prior art of "interface testing for complex business links", the inventors of the present invention consider that, in order to ensure the quality of complex services over long links, interface scenario automated testing can be implemented based on interface parameter expansion, thereby minimizing the initial investment and subsequent maintenance costs of interface scenario automation and enhancing the ability of testers in ensuring the quality of product and project interfaces.
[0030] In view of this, the inventors of the present disclosure propose a solution for testing an application programming interface as described in the following embodiment.
[0031] Example:
[0032] The following describes the solution for testing an application programming interface disclosed in the present invention in conjunction with the accompanying drawings.
[0033] Figure 1 1 is a flow chart of a method for testing an application programming interface provided by an exemplary embodiment of the present disclosure. This embodiment can be applied to electronic devices (for example, servers or cloud computing platforms), such as Figure 1 As shown, the method for testing the application programming interface includes the following steps:
[0034] S110 , generating and loading a test script and initial test data based on interface parameters of the application programming interface API to be tested and preset initialization rules.
[0035] Among them, the present disclosure does not limit the interface parameters; for example, it may include but is not limited to interface Swagger data, or data representing information such as the business level, business link / path, etc. associated with the API.
[0036] Among them, Swagger is a standardized and complete framework for generating, describing, calling and visualizing RESTful-style Web services.
[0037] Optionally, while executing the steps of generating and loading the test script and the initial test data, step S110 includes: loading a pre-configured operating environment for testing the API to be tested.
[0038] Among them, how to generate the test script and initial test data based on the "preset initialization rules" will be described below and will not be elaborated here.
[0039] S120: Perform parameter expansion on the initial test data according to pre-configured expansion parameters and pre-configured expansion strategies to generate a first test data set.
[0040] The specific implementation of parameter extension will be described below.
[0041] S130: Using a preset variable replacement rule, replace the test data in the first test data set to generate a second test data set.
[0042] Among them, specific replacement implementation methods will be described in the text.
[0043] S140: Utilize the test script to execute the test task by traversing the second test data set to generate a test result.
[0044] As described above, using the method for testing the application programming interface provided by the above embodiment of the present disclosure, firstly, in the actual testing process, through different combinations of parameter extensions, the lag and cross-reactivity of business processing can be simulated, so that some problems that cannot be found in conventional single-interface test scenarios can be discovered, and some deep-seated problems in system architecture can be identified. Secondly, in actual business scenarios, different dependent business data can be constructed using parameter extensions, which can greatly improve the efficiency of process testing and shorten the project launch cycle. In addition, in the face of multi-environment switching operation verification problems, only the corresponding server configuration, database configuration and a small amount of basic business data need to be modified, supporting more than 95% of the scenarios only maintaining data files, and a small number of complex conversion operations involving interface data require editing scripts, which greatly improves the efficiency of interface automation coverage, reduces the later maintenance investment, and improves the interface use case coverage efficiency by more than 60%.
[0045] In summary, the method for testing the application programming interface provided by the above-mentioned embodiments of the present invention is, on the one hand, easy to use, clear in logic, and highly flexible, which significantly improves the efficiency of covering complex interface business scenarios and reduces the cost of test script data maintenance; on the other hand, it supports the expansion of the entire process and has better subsequent scalability than using existing frameworks or tools.
[0046] exist Figure 1 Based on the embodiment, as an optional implementation, step S110 can be implemented in the following manner:
[0047] Step 1) parse the interface parameters of the API to be tested to obtain module information and interface feature data associated with the API to be tested.
[0048] Among them, the associated module information may include but is not limited to hierarchical business data, business links, etc.; the interface feature data may include but is not limited to data request method, request parameters, response status code, etc.
[0049] Step 2), based on the module information and interface feature data associated with the API to be tested, generate initial test data corresponding to the API to be tested using a preset test data template; and generate a test script corresponding to the API to be tested using a preset interface method template.
[0050] Among them, the types of the preset test data templates may at least include: load test, stress test, large data volume test, network performance test, server performance test, fatigue test and other types.
[0051] The preset interface method template may be, for example, Figure 2 The interface test cases shown are designed and determined. Specifically, it can include at least the following aspects of testing:
[0052] 1) Field uniqueness check; for example, the username field cannot be repeated when inserting data. Send two requests and check the second return result.
[0053] 2) Related to business logic, token is empty or wrong, reverse use case;
[0054] 3) Parameter value range, parameter default value range, string length, and reverse use cases;
[0055] 4) Arbitrary combination of optional parameters, positive use cases;
[0056] 5) Cover all parameters and positive use cases;
[0057] 6) Cover all required parameters and positive use cases;
[0058] 7) A required parameter is empty, reverse use case;
[0059] 8) Required parameter data type error, data value error, reverse use case;
[0060] 9) Pass one more parameter, pass one less parameter, and reverse the use case.
[0061] exist Figure 1 , 2 Based on the embodiment, as an optional implementation, step S120 can be implemented in the following manner:
[0062] Step i) recursively traverses the pre-configured extended parameters, identifies the extended parameter path, and obtains the extended parameter dictionary.
[0063] The pre-configured extended parameters include intra-interface parameters and inter-interface parameters.
[0064] As an optional example, the extended parameter may be preceded by a "*" as an identifier for easy identification.
[0065] Step ii) uses the extended parameter dictionary and the pre-configured extended strategy to perform parameter expansion on the initial test data to generate a first test data set.
[0066] The pre-configured extension strategy is determined according to the business association relationship between the interface parameters of the API to be tested.
[0067] The mapping relationships involved in the pre-configured expansion strategy include one-to-one and many-to-many.
[0068] To facilitate understanding of the implementation, as an optional example, the extended parameter dictionary may include: {'*a':[1,2,3], 'b':{'m':{'*c':['fgg','6hf','abc']}, 'f':[4,5,7]}}; after performing parameter expansion in step ii), {'a':[1,2,3], 'bmc':['fgg','6hf','abc']} may be obtained; that is, the first test data set. Wherein, when the mapping relationship is one-to-one, 3 sets of data may be obtained; when the mapping relationship is many-to-many, 9 sets of data may be obtained.
[0069] It should be noted that when the above step S120 is used to perform parameter expansion, the generated scene data group can be stored in the shared memory, and the current memory scene data is written to the file during operation, and the file is automatically loaded during operation. The reason for this design is to be consistent with the original interface operation rules and avoid repeated loading of extended class objects. When a scene interface is completed, the scene data file can be deleted. The decorator method @get_params is used in the test script to obtain the corresponding test data from the memory through the class name and interface method name, thereby realizing the separation of script and data, which is convenient for script reuse and data expansion.
[0070] exist Figure 1 , 2 Based on the embodiment, as an optional implementation, the "preset variable replacement rule" in step S120 may include:
[0071] A matching replacement pattern is determined according to the type of the interface parameter of the API to be tested.
[0072] Wherein, the replacement mode includes a static mode and a dynamic mode;
[0073] The types of the interface parameters include public variable types, interface input types, return data types, and run method generation types.
[0074] Among them, the common variable can be identified by the symbol "%common%", the input between interfaces can be identified by the symbol "#method.constant", the returned data can be identified by the symbol "@method.result", and the running method generation can be identified by the symbol "${__object.method(*args,**kwargs)}".
[0075] Among them, public variables belong to static replacement; input, return data and running method generation between interfaces belong to dynamic replacement.
[0076] By utilizing the preset variable replacement rules, when faced with multi-environment switching operation verification problems, it is only necessary to modify the corresponding server configuration, database configuration and a small amount of basic business data. It supports more than 95% of scenarios where only data files are maintained, and a small amount of complex conversion operations involving interface data require editing scripts, which greatly improves the efficiency of interface automation coverage, reduces subsequent maintenance investment, and improves the interface use case coverage efficiency by more than 60%.
[0077] exist Figure 1 , 2 Based on the embodiment, as an optional implementation, step S130 may be implemented in the following manner: test data belonging to a common variable type in the first test data set is replaced by using a static pattern to generate a second test data set.
[0078] exist Figure 1 , 2 Based on the embodiment, as an optional implementation, step S140 can be implemented in the following manner:
[0079] First, the test data of the second test data set that belongs to the interface input type, return data type and running method generation type is replaced by using the dynamic pattern to obtain a real-time test data set; secondly, the test data in the real-time test data set is parsed to obtain the parsed value; finally, the test is executed by using the preset test method and the parsed value to generate the test result.
[0080] The preset test method may include a combination test based on the file directory, file, and method level.
[0081] As described above, using the method for testing the application programming interface provided by the above embodiment of the present disclosure, firstly, in the actual testing process, through different combinations of parameter extensions, the lag and cross-reactivity of business processing can be simulated, so that some problems that cannot be found in conventional single-interface test scenarios can be discovered, and some deep-seated problems in system architecture can be identified. Secondly, in actual business scenarios, different dependent business data can be constructed using parameter extensions, which can greatly improve the efficiency of process testing and shorten the project launch cycle. In addition, in the face of multi-environment switching operation verification problems, only the corresponding server configuration, database configuration and a small amount of basic business data need to be modified, supporting more than 95% of the scenarios only maintaining data files, and a small number of complex conversion operations involving interface data require editing scripts, which greatly improves the efficiency of interface automation coverage, reduces the later maintenance investment, and improves the interface use case coverage efficiency by more than 60%.
[0082] In summary, the method for testing the application programming interface provided by the above-mentioned embodiments of the present invention is, on the one hand, easy to use, clear in logic, and highly flexible, which significantly improves the efficiency of covering complex interface business scenarios and reduces the cost of test script data maintenance; on the other hand, it supports the expansion of the entire process and has better subsequent scalability than using existing frameworks or tools.
[0083] The following is a description of a system for testing an application programming interface provided by the present disclosure. The system for testing an application programming interface described below and the method for testing an application programming interface described above can refer to each other.
[0084] Figure 3 FIG. 1 is a schematic diagram of a system for testing an application programming interface provided by an exemplary embodiment of the present disclosure. Figure 3 As shown, the system for testing an application programming interface includes: an initialization module 310, configured to: generate and load a test script and initial test data based on interface parameters of an application programming interface API to be tested and preset initialization rules; a parameter extension module 320, configured to: perform parameter extension on the initial test data according to pre-configured extension parameters and a pre-configured extension strategy to generate a first test data set; a variable replacement module 330, configured to: use preset variable replacement rules to replace the test data in the first test data set to generate a second test data set; a test execution module 340, configured to: use the test script to perform the test task by traversing the second test data set to generate a test result.
[0085] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute a method for testing an application programming interface, the method comprising: generating and loading a test script and initial test data based on the interface parameters of the application programming interface API to be tested and a preset initialization rule; performing parameter expansion on the initial test data according to pre-configured expansion parameters and a pre-configured expansion strategy to generate a first test data set; replacing the test data in the first test data set using a preset variable replacement rule to generate a second test data set; and performing a test task by traversing the second test data set using the test script to generate a test result.
[0086] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present disclosure is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0087] On the other hand, the present disclosure also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the method for testing the application programming interface provided by the above-mentioned methods, and the method includes: based on the interface parameters of the application programming interface API to be tested and preset initialization rules, generating and loading a test script and initial test data; according to pre-configured extension parameters and pre-configured extension strategies, performing parameter expansion on the initial test data to generate a first test data set; using preset variable replacement rules, replacing the test data in the first test data set to generate a second test data set; using the test script to traverse the second test data set to perform the test task and generate a test result.
[0088] On the other hand, the present disclosure also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the method for testing an application programming interface provided by the above-mentioned methods, the method comprising: generating and loading a test script and initial test data based on interface parameters of the application programming interface API to be tested and preset initialization rules; performing parameter expansion on the initial test data according to pre-configured extension parameters and a pre-configured extension strategy to generate a first test data set; replacing the test data in the first test data set using preset variable replacement rules to generate a second test data set; and generating a test result by traversing the second test data set using the test script to perform the test task.
[0089] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0090] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0091] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present disclosure, rather than to limit them. Although the present disclosure has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present disclosure.
Claims
1. A method for testing an application programming interface, characterized in that: include: Generate and load test scripts and initial test data based on the interface parameters of the application programming interface (API) to be tested and preset initialization rules; Performing parameter expansion on the initial test data according to pre-configured expansion parameters and a pre-configured expansion strategy to generate a first test data set; Using a preset variable replacement rule, replace the test data in the first test data set to generate a second test data set; Using the test script, executing the test task by traversing the second test data set, and generating a test result; The generating of the test script and the initial test data based on the interface parameters of the application programming interface API to be tested and the preset initialization rules includes: Obtaining module information and interface feature data associated with the API to be tested by parsing interface parameters of the API to be tested; Based on the module information and interface feature data associated with the API to be tested, using a preset test data template to generate initial test data corresponding to the API to be tested; and using a preset interface method template to generate a test script corresponding to the API to be tested; Performing parameter expansion on the initial test data according to the pre-configured expansion parameters and the pre-configured expansion strategy to generate a first test data set includes: By recursively traversing the pre-configured extended parameters, identifying the extended parameter path, and obtaining an extended parameter dictionary; wherein the pre-configured extended parameters include intra-interface parameters and inter-interface parameters; Using the extended parameter dictionary and the pre-configured extension strategy, perform parameter extension on the initial test data to generate a first test data set; Wherein, the pre-configured extension strategy is determined according to the business association relationship between the interface parameters of the API to be tested; The mapping relationships involved in the pre-configured expansion strategy include one-to-one and many-to-many; The preset variable replacement rules include: Determine a matching replacement mode according to the type of the interface parameter of the API to be tested; wherein the replacement mode includes a static mode and a dynamic mode; The types of the interface parameters include public variable types, interface input types, return data types, and run method generation types; The step of replacing the test data in the first test data set by using a preset variable replacement rule to generate a second test data set includes: Replacing the test data of the public variable type in the first test data set with a static pattern to generate a second test data set; Using the test script to perform the test task by traversing the second test data set to generate a test result includes: The test data of the interface input type, the return data type and the running method generation type in the second test data set are replaced by using the dynamic mode to obtain a real-time test data set; Parsing the test data in the real-time test data set to obtain a parsed value; Utilize the preset test method and the analysis value to execute the test and generate the test result; wherein the preset test method includes a combination test based on the file directory, file, and method level.
2. The method according to claim 1, characterized in that While executing the steps of generating and loading the test script and the initial test data, the method further includes: A pre-configured operating environment for testing the API to be tested is loaded.
3. A system for testing an application programming interface, characterized in that: Implementing the method for testing an application programming interface as claimed in any one of claims 1 to 2, comprising: The initialization module is configured to: generate and load a test script and initial test data based on interface parameters of the application programming interface API to be tested and preset initialization rules; A parameter expansion module is configured to: perform parameter expansion on the initial test data according to pre-configured expansion parameters and pre-configured expansion strategies to generate a first test data set; A variable replacement module is configured to: replace the test data in the first test data set by using a preset variable replacement rule to generate a second test data set; The test execution module is configured to: utilize the test script to execute the test task by traversing the second test data set to generate a test result.
4. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the method for testing an application programming interface as claimed in any one of claims 1 to 2 is implemented.
5. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for testing an application programming interface according to any one of claims 1 to 2 is implemented.
Citation Information
Patent Citations
Extended finite state machine (EFSM) model-based Web service case generation method and system
CN106708739A
Automated testing system and method of test case of automatic expanding interface
CN107203473A
Parameterized test script-based automatic test method and system
CN107943689A
Interface test data and test script generation method, terminal equipment and storage medium
CN113886232A
Interface test case generation method, system and device and storage medium
CN116701215A
