A method and system for automated API performance testing
By developing an API testing tool, the communication and script generation of Python and Java tools are realized automatically, and the problem of inefficient API testing and API performance testing is solved, improving testing efficiency and reducing errors.
Patent Information
- Application Number
- CN202210800005.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-08
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2042-07-08
AI Technical Summary
In the prior art, since API testing and API performance testing use different languages and tools, the test script needs to be written repeatedly, which is inefficient and prone to unexpected errors.
By developing an API testing tool, using Python to create and parse the source code of Java tool Jmeter, it realizes the two-way automatic generation of API testing and performance testing scripts, and uses Python to communicate with Java tools to encapsulate and split scripts to adapt to data in different formats.
Improves the efficiency of API performance testing, realizes seamless communication between Python and Java tools and automated script generation, and reduces manual conversion errors.
Smart Images

Figure CN115237759B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and particularly to a method and system for automated API performance testing. Background Art
[0002] API (Application Programming Interface) is a set of predefined functions, which aims to provide the ability for an application and a developer to access a set of routines based on a certain software or hardware, without the need to access the source code or understand the details of the internal working mechanism. During the operation of an application, it is usually necessary to test the API interfaces provided by it.
[0003] Currently, WEB application API testing mostly relies on the flexible and convenient Python language to complete, while API performance testing mostly relies on tools developed in Java or C because of their high efficiency and accurate test results. Since the principles of API testing and API performance testing are the same, but the languages and tools are different, the testing efficiency is low when conducting tests, the test scripts need to be rewritten repeatedly, and many unexpected errors may occur during the manual conversion and writing process due to the high complexity of the API. Summary of the Invention
[0004] The purpose of the present invention is to provide a method and system for automated API performance testing, enabling Python tools and Java tools to communicate, and capable of automatically generating API test scripts and API performance test scripts bidirectionally, so as to solve the problems raised in the above technical background.
[0005] To achieve the above purpose, the present invention adopts the following technical solutions:
[0006] The first aspect of the present application provides a method for automated API performance testing, including:
[0007] S1, creating an API test tool using Python, the API test tool being configured to receive raw data in different formats from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry, combine different test cases, and perform automated testing;
[0008] S2, parsing the source code and scripts of the open-source Java tool Jmeter, converting the scripts into XML files, analyzing and obtaining the data processing and code logic of each XML partition, and classifying the scripts and part of the Java source code using Python, encapsulating various functions into classes or methods that can be called independently and have their own parameters;
[0009] S3. Use the API testing tool in step S1 to perform low-level processing of data and business logic on Jmeter. After the processing is completed, perform script data processing and generation to generate an API performance testing script suitable for the Jmeter tool.
[0010] S4. Use the API testing tool to open the API externally, receive the test script of Jmeter, convert the received test script of Jmeter into an XML file, split the XML file, match and compare it with the data in steps S2 and S3. After the comparison is completed, according to different parameters for calling the API, generate an API testing script suitable for the API testing tool in the data storage area of the API testing tool.
[0011] Preferably, in step S1, the raw data in different formats includes, but is not limited to, JSON data, XML data, Excel data, and Mysql data.
[0012] Preferably, the API testing tool includes:
[0013] A data extraction module, configured to encapsulate a general data extraction method for various types of raw data, and extract various types of target data according to different input parameters;
[0014] An APIBase module, configured to provide a base entry according to the re-encapsulation of the python low-level code, and this base entry is used to receive test parameters and return target data;
[0015] A test execution module, configured to automatically call the base entry according to the extracted target data, combine different test cases, and execute automated testing;
[0016] A test report writing module, configured to return test reports in different data formats according to the type of input parameters.
[0017] Preferably, in step S2, analyzing and obtaining the data processing and code logic of the XML file includes: analyzing the business logic described by the XML language, and classifying and summarizing the formats of various data combinations and the storage locations and functions of the Java code in the script.
[0018] Preferably, in step S3, the API testing tool performs low-level processing of data and business logic on Jmeter, including, but not limited to, API request elements, Java code blocks, and Jmeter tool parameters.
[0019] Preferably, step S3 specifically includes the following steps:
[0020] S31. Obtain the script information of the API test tool, where the script information includes: script identifier and script data. Among them, the script identifier is used to identify the attributes and change information of the script.
[0021] S32. Split the script of the API test tool according to the script identifier.
[0022] After the splitting is completed, map the split scripts to the classes or methods encapsulated in step S2 one by one according to the script identifier. After the matching is completed, perform script combination processing according to the script rules of the Jmeter tool to generate an API performance test script applicable to the Jmeter tool.
[0023] More preferably, in step S32, according to the script identifier, the script of the API test tool is split into default request processing, pre- and post-processors, post-data extractors, timers, assertions, and request body parameterization.
[0024] More preferably, in step S33, the combined script includes, but is not limited to, thread group basic settings, default request processing components, timers or random data generators, pre- and post-beanshell processors, assertion settings, post-extractors, and loop processors.
[0025] Preferably, in step S4, the generation of the API test script applicable to the API test tool includes, but is not limited to: basic request group, request details, pre-processing and post-extraction, and reusable data blocks.
[0026] The second aspect of the present application discloses a system for automated API performance testing, including:
[0027] An API test tool, configured to receive raw data in different formats from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry to combine different test cases and execute automated testing;
[0028] An encapsulation module, configured to parse the source code and scripts of the open-source Java tool Jmeter, convert the scripts into XML files, analyze and obtain the data processing and code logic of each XML partition, and use python to classify the scripts and part of the Java source code, and encapsulate various functions into classes or methods that can be called independently and have their own parameters;
[0029] A first conversion module, configured to use the API test tool to perform low-level processing of data and business logic on Jmeter. After the processing is completed, perform script data processing and generation to generate an API performance test script applicable to the Jmeter tool.
[0030] The second conversion module is configured to open APIs externally using the API testing tool, receive the test script of Jmeter, convert the received test script of Jmeter into an XML file, split the XML file, match and compare it with the data of the encapsulation module and the first conversion module. After the comparison is completed, according to different parameters for calling the API, an API test script applicable to the API testing tool is generated in the data storage area of the API testing tool.
[0031] More preferably, the API testing tool includes:
[0032] A data extraction module configured to encapsulate a common data extraction method for various types of raw data and extract various types of target data according to different input parameters;
[0033] An APIBase module configured to provide a base entry according to the re-encapsulation of the underlying python code, and this base entry is used to receive test parameters and return target data;
[0034] A test execution module configured to automatically call the base entry according to the extracted target data, combine different test cases, and execute automated tests;
[0035] A test report writing module configured to return test reports with different data formats according to the type of input parameters.
[0036] Compared with the prior art, the technical solution of the present invention has the following beneficial effects:
[0037] The present application provides a method and system for automated API performance testing, enabling communication between python tools and Java tools, and capable of automatically generating API test scripts and API performance test scripts bidirectionally, improving the efficiency of API performance testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The accompanying drawings constituting a part of this application are used to provide a further understanding of this application. The schematic embodiments of this application and their descriptions are used to explain this application and do not constitute an improper limitation to this application. In the drawings:
[0039] Figure 1 is a flowchart of a method for automated API performance testing according to a preferred embodiment of the present invention;
[0040] Figure 2 is a block diagram of the functional unit composition of a system for automated API performance testing according to a preferred embodiment of the present invention;
[0041] Figure 3It is a schematic diagram of the execution process of a system for automated API performance testing. Detailed implementation manners
[0042] The present invention provides a method and a system for automated API performance testing. To make the objectives, technical solutions and effects of the present invention clearer and more definite, the following further describes the present invention in detail with reference to the accompanying drawings and by way of examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0043] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and do not necessarily need to describe a specific order or sequence. It should be understood that such data used in appropriate cases can be interchanged. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device comprising a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0044] Embodiment 1:
[0045] Figure 1 It is a schematic flowchart of a method for automated API performance testing according to a preferred embodiment of the present invention.
[0046] As Figure 1 shown, a method for automated API performance testing specifically includes the following steps:
[0047] Step S1, develop an API testing tool using Python. This API testing tool has the characteristics of high cohesion and low coupling and can adapt to various different formats of data. For example, in this embodiment, the API testing tool is applicable to JSON data, XML data, Excel data, and Mysql data. The API testing tool is configured to receive different formats of raw data from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry, combine different test cases, and perform automated testing.
[0048] Specifically, the API testing tool includes the following modules:
[0049] A data extraction module, which is configured to encapsulate a general data extraction method for various types of raw data and can extract various types of required data according to different input parameters;
[0050] The APIBase module is configured to provide a base entry according to the re - encapsulation of the underlying Python code. This base entry is used to receive test parameters and return the required data.
[0051] The test execution module is configured to automatically call the base entry according to the extracted target data, combine different test cases, and execute automated tests.
[0052] The test report writing module is configured to return test reports in various different data formats according to the type of input parameters.
[0053] Step S2: Parse the source code and scripts of the open - source Java tool Jmeter, convert the scripts into XML files, and analyze to obtain its data processing and code logic.
[0054] The specific steps are as follows:
[0055] S21: Convert the Jmeter scripts into XML files, analyze what business logic each XML partition processes respectively, classify and summarize the formats of various data combinations, and the storage locations and functions of the Java code in the scripts.
[0056] S22: Use Python to classify the scripts and part of the Java source code, encapsulate various functions into classes or methods that can be called independently and have their own parameters.
[0057] Step S3: Use the developed API test tool to perform low - level processing of data and business logic on Jmeter, including, but not limited to, API request elements, JAVA code blocks, Jmeter tool parameters, etc. After processing, perform script data processing and generation, and an API performance test script applicable to the Jmeter tool can be generated.
[0058] The specific steps are as follows:
[0059] S31: Obtain the script information of the API test tool. The script information includes: script identifier, script data, where the script identifier is used to identify the attributes and change information of the script.
[0060] S32: Split the script of the API test tool according to the script identifier. The script can be split into default request processing, pre - and post - processors, post - data extractors, timers, assertions, request body parameterization, etc.
[0061] In S33, after the splitting is completed, the split scripts are mapped one by one to the classes or methods encapsulated in step S2 according to the script identifiers. After the matching is completed, according to the script rules of the Jmeter tool, script combination processing is performed to generate an API performance test script applicable to the Jmeter tool. Among them, the combined script includes, but is not limited to, basic thread group settings, default request processing components, timers or random data generators, pre- and post-processor beanshells, assertion settings, post-extractors, and loop processors.
[0062] In step S4, the API test tool is used to open the API externally, receive the test script of Jmeter, convert the received Jmeter test script into an XML file, split the XML file, and match and compare it with the data in steps S2 and S3. After the comparison is completed, according to different parameters for calling the API, an API test script applicable to the API test tool is generated in the data storage area of the API test tool. The generated API test script includes, but is not limited to: basic request groups, request details, pre-processing and post-extract, and reusable data blocks.
[0063] Among them, after splitting the XML file, the following data can be obtained:
[0064] (1) Request address and request header information;
[0065] (2) Request type and request message;
[0066] (3) Parameterized extraction and usage strategy;
[0067] (4) Layout of the test strategy (i.e., the order of interface requests, the number of requests for the interface, the waiting time for requests, the rendezvous points for interface requests, etc.);
[0068] (5) Other tools (such as: timers, counters, function expressions, loop controllers, etc.).
[0069] After obtaining the above data, according to the disassembly and encapsulation of the script in steps S2 and S3, these data can be reversely input into the API test tool in step S1. The API test tool calls different receiving interfaces according to the input data types to process the data, so as to save these data as different types of original data described in step S1.
[0070] Based on the above steps S1 - S4, two-way conversion and generation of test scripts between the API test tool and Jmeter can be performed.
[0071] Embodiment 2:
[0072] This embodiment provides a design example of a system for automated API performance testing. The operating system is mainly LINUX, the development languages are Python and Java, and the open-source tool Jmeter based on JAVA is also used. The core part of this system includes:
[0073] 1) An API testing tool with high cohesion and low coupling, which can adapt to various data formats;
[0074] 2) A file format conversion tool that can convert data formats such as JSON, XML, JMX, XLSX, and DBData to each other;
[0075] 3) A core script converter tool that can mutually convert the script files of the API testing tool and Jmeter according to the test execution strategy and parameters. Among them, the test execution strategy refers to the design strategy of the test script. For example, when performing performance testing, the number of loads, on which terminal to test, the selection of terminal performance, the startup logic of load users, etc.; Common parameters include: the format of the converted script, the performance test strategy settings for converting the API testing tool to Jmeter, such as the number of concurrent users, the concurrent growth strategy, whether to enable assertions, the script running time, the script startup strategy, the number of loops, etc.;
[0076] 4) An executor that can configure the automatic execution of tests of the API testing tool and directly generate the test script of Jmeter after the test is completed to execute the test of the Jmeter tool.
[0077] Figure 2 It is a block diagram of the functional units of a system for automated API performance testing according to a preferred embodiment of the present invention.
[0078] Refer to Figure 2 As shown, a system for automated API performance testing includes:
[0079] The API testing tool 100 is configured to receive raw data in different formats from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry to combine different test cases and execute automated tests;
[0080] The encapsulation module 200 is configured to parse the source code and scripts of the open-source Java tool Jmeter, convert the scripts into XML files, analyze and obtain the data processing and code logic of each XML partition, and use python to classify the scripts and part of the Java source code, and encapsulate various functions into classes or methods that can be called independently and have their own parameters;
[0081] The first conversion module 300 is configured to use the API test tool to perform underlying processing of data and business logic on Jmeter. After the processing is completed, script data processing and generation are performed to generate an API performance test script applicable to the Jmeter tool.
[0082] The second conversion module 400 is configured to use the API test tool to open the API externally, receive the test script of Jmeter, convert the received test script of Jmeter into an XML file, split the XML file, and perform matching and comparison with the data of the encapsulation module 200 and the first conversion module 300. After the comparison is completed, according to different parameters for calling the API, an API test script applicable to the API test tool is generated in the data storage area of the API test tool.
[0083] Among them, the modules included in the developed API test tool can refer to the description in the foregoing method embodiments and will not be elaborated herein.
[0084] Refer to Figure 3 , when the system for automated API performance testing runs, its usage steps are as follows:
[0085] 1) Interface test data is saved in Table A of the database. Table A stores requirement data such as the paths of various APIs, interface bodies, parameterized dependency data, assertions, etc. Performance test strategies are saved in the associated Table P, and the data includes the number of users, pressurization method, timer range, random number range, Java code of beanshell, etc.
[0086] 2) Call the API through the ID of the input parameter, extract the corresponding data in Table A, and convert it into an interface test script, which can be regarded as a collection of multiple pieces of data, and execute the encapsulated apibase class.
[0087] 3) Input parameters through the method provided by the API test tool: interface test script (data in Table A after being transformed by the tool) and performance test strategy (data in Table P), match each interface attribute in the interface test script with the encapsulated Jmeter script, and match the encapsulated reuse template according to the performance test strategy of the input parameter.
[0088] 4) After the matching is completed, assemble the API interface performance test script XML, change the XML to JMX, and the interface performance test script conversion is completed.
[0089] 5) Execute the test according to the test script, and the test report is generated by the test tool.
[0090] In summary, the present application provides a method and system for automated API performance testing, enabling communication between Python tools and Java tools, and capable of automatically generating API test scripts and API performance test scripts bidirectionally, thereby improving the efficiency of API performance testing.
[0091] The specific embodiments of the present invention have been described in detail above, but they are only examples, and the present invention is not limited to the specific embodiments described above. For those skilled in the art, any equivalent modifications and substitutions made to the present invention are also within the scope of the present invention. Therefore, all equivalent transformations and modifications made without departing from the spirit and scope of the present invention should be covered within the scope of the present invention.
Claims
1. A method for automated API performance testing, characterized in that, Including: S1. Create an API test tool using Python. The API test tool is configured to receive raw data in different formats from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry, combine different test cases, and execute automated testing; S2. Parse the source code and scripts of the open-source Java tool Jmeter, convert the scripts into XML files, analyze and obtain the data processing and code logic of each XML partition, and classify the scripts and some Java source code using Python, encapsulate various functions, and encapsulate them into classes or methods that can be called independently and have their own parameters; S3. Use the API test tool in step S1 to perform low-level processing of data and business logic for Jmeter. After the processing is completed, perform script data processing and generation to generate an API performance test script applicable to the Jmeter tool; S4. Open the API externally using the API test tool, receive the test script of Jmeter, convert the received test script of Jmeter into an XML file, and split the XML file, match and compare it with the data in steps S2 and S3. After the comparison is completed, according to different parameters for calling the API, generate an API test script applicable to the API test tool in the data storage area of the API test tool.
2. The method for automated API performance testing according to claim 1, wherein, In step S1, the raw data in different formats includes JSON data, XML data, Excel data, and Mysql data.
3. The method for automated API performance testing according to claim 1, wherein The API test tool includes: A data extraction module, which is configured to encapsulate a general data extraction method for various types of raw data and extract various types of target data according to different input parameters; An API Base module, which is configured to provide a base entry according to the re-encapsulation of the Python underlying code. This base entry is used to receive test parameters and return target data; A test execution module, which is configured to automatically call the base entry according to the extracted target data, combine different test cases, and execute automated testing; A test report writing module, which is configured to return test reports in different data formats according to the type of input parameters.
4. A method for automated API performance testing according to claim 1, characterized in that, In step S2, analyzing and obtaining the data processing and code logic of the XML file includes: analyzing the business logic described in the XML language, and classifying and summarizing the formats of various data combinations and the storage locations and functions of Java code in the script.
5. The method for automated API performance testing according to claim 1, wherein Step S3 specifically includes the following steps: S31. Obtain the script information of the API test tool. The script information includes: script identifier, script data, where the script identifier is used to identify the attributes and change information of the script; S32. Split the script of the API test tool according to the script identifier. In S33, after the splitting is completed, the split scripts are made to correspond one by one with the classes or methods encapsulated in step S2 according to the script identifiers. After the matching is completed, according to the script rules of the Jmeter tool, script combination processing is performed to generate an API performance test script applicable to the Jmeter tool.
6. The method for automated API performance testing according to claim 5, wherein In step S32, according to the script identifier, the script of the API test tool is split into default request processing, pre- and post-processors, post-data extractors, timers, assertions, and request body parameterization.
7. A method for automated API performance testing according to claim 5, characterized in that, The combined script includes thread group basic settings, default request processing components, timers or random data generators, pre- and post-beanshell processors, assertion settings, post-extractors, and loop processors.
8. A method for automated API performance testing according to claim 1, characterized in that, In step S4, the generation of an API test script applicable to the API test tool includes: basic request groups, request details, pre-processing and post-extraction, and reusable data blocks.
9. A system for automated API performance testing, characterized in that, Including: An API test tool, configured to receive raw data in different formats from at least one data source, extract target data associated with the target system from the raw data, and automatically call the base entry to combine different test cases and execute automated tests; An encapsulation module, configured to parse the source code and scripts of the open-source Java tool Jmeter, convert the scripts into XML files, analyze and obtain the data processing and code logic of each XML partition, and use python to classify the scripts and some Java source code, and encapsulate various functions into classes or methods that can be called separately and have their own parameters; A first conversion module, configured to use the API test tool to perform low-level processing of data and business logic on Jmeter. After the processing is completed, script data processing and generation are performed to generate an API performance test script applicable to the Jmeter tool; A second conversion module, configured to use the API test tool to open the API externally, receive the test script of Jmeter, convert the received test script of Jmeter into an XML file, split the XML file, match and compare it with the data of the encapsulation module and the first conversion module. After the comparison is completed, according to different parameters for calling the API, an API test script applicable to the API test tool is generated in the data storage area of the API test tool.
10. The system for automated API performance testing according to claim 9, characterized in that, The API test tool includes: A data extraction module, configured to encapsulate a general data extraction method for various types of raw data and extract various types of target data according to different input parameters; An APIBase module, configured to provide a base entry according to the re-encapsulation of the python underlying code, and this base entry is used to receive test parameters and return target data; A test execution module, configured to automatically call the base entry according to the extracted target data, combine different test cases, and execute automated tests; A test report writing module, configured to return test reports in different data formats according to the type of input parameters.
Citation Information
Patent Citations
An automatic interface testing method based on Jmeter
CN109597761A
Method for automatically generating performance test script based on interface test tool
CN110427331A