Unit test code generation method and device, equipment and storage medium

Through the tangent-oriented programming technology, dynamically obtain and filter unit test case data, fill in the target template to generate unit test code, solving the problem of the difference between unit test case data and business data, and improving the coverage and accuracy of tests.

CN118916279APending Publication Date: 2024-11-08CCB FINTECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202410973530.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-11-08

AI Technical Summary

Technical Problem

In the existing unit test code generation technology, there are differences between unit test case data and business data in actual business application scenarios, resulting in reduced test accuracy and insufficient coverage.

Method used

Use aspect-oriented programming (AOP) technology to dynamically obtain unit test case data from actual business scenarios, and generate unit test code through predefined filtering rules and target unit test case templates.

Benefits of technology

Ensure that unit test case data is more in line with actual business needs, improve the coverage and accuracy of unit tests, solve the limitations brought about by static data table definition, reflect the complexity and changes of actual business systems, and improve the overall quality of unit tests and reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916279A_ABST
    Figure CN118916279A_ABST
Patent Text Reader

Abstract

The invention provides a unit test code generation method and device, equipment and a storage medium, and relates to the technical field of unit testing. In the scheme, firstly, multiple groups of first unit test case data are obtained from an actual service scene by utilizing an AOP technology, then the obtained multiple groups of first unit test case data are screened according to a predefined screening rule, and multiple groups of second unit test case data are obtained; and finally, filling the multiple groups of second unit test case data into the target unit test case template to generate unit test codes. Through the method, the use case data in the unit test code can better fit the service data in the actual service scene, so that the effectiveness of the unit test is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of unit testing technology, and in particular to a unit testing code generation method, apparatus, device and storage medium. Background Art

[0002] Unit testing plays an indispensable role in the development lifecycle of software systems. However, the process of generating unit test code is extremely challenging.

[0003] At present, unit test code generation technologies include generation technologies based on standard templates, generation technologies based on generative artificial intelligence dialogue technology, and generation technologies based on logical inference.

[0004] However, the use case data that the above unit test code generation technology relies on is different from the business data in the actual business application scenario. Summary of the invention

[0005] The present application provides a unit test code generation method, apparatus, device and storage medium to solve the problem in the prior art that there is a difference between unit test case data and business data in actual business application scenarios.

[0006] In a first aspect, the present application provides a method for generating unit test code, the method comprising:

[0007] According to the aspect-oriented programming (AOP) technology, multiple groups of first unit test case data are obtained from actual business scenarios, wherein each group of first unit test case data includes the type of business request, the calling method, the passed parameters, and the return value;

[0008] According to the predefined screening rules, each group of first unit test case data is screened to obtain the second unit test case data after screening;

[0009] Determine a predefined target unit test case template according to the type in each group of second unit test case data;

[0010] Fill the target unit test case template with the second unit test case data to obtain unit test code.

[0011] In a possible design, the method of filling the target unit test case template with the second unit test case data to obtain the unit test code includes:

[0012] Determine, according to a predefined reflection mechanism, an initial trigger state of a corresponding type in each group of second unit test case data;

[0013] Adding the initial trigger state to each set of second unit test case data to obtain multiple sets of target unit test case data;

[0014] Fill the target unit test case data into the target unit test case template to obtain the unit test code.

[0015] In a possible design, before obtaining multiple sets of first unit test case data from actual business scenarios according to the aspect-oriented programming AOP technology, the method includes:

[0016] Configure the corresponding target unit test case template according to the type of target unit test;

[0017] Define a target unit test case data collection scope, wherein the target unit test case data collection scope includes all Bean objects managed by the Spring container.

[0018] In a possible design, the service request includes: a service entry request, or a service entry request and a service internal request;

[0019] If the service request includes the service entry request and the service internal request, each group of the first unit test case data further includes an attribute dependency relationship, and the attribute dependency relationship is used to represent the dependency relationship between the service entry request and the service internal request.

[0020] In one possible design, the method further includes:

[0021] According to the type of the business request, a method for triggering the execution of the unit test code is determined, wherein the method includes a Hypertext Transfer Protocol HTTP message method and / or a predefined function method.

[0022] In a possible design, the method of determining, according to the type of the business request, a way of triggering the execution of the unit test code includes:

[0023] If the type of the service request is a Hypertext Transfer Protocol (HTTP) request, a filter is used to record a request message and a response message in the HTTP request format;

[0024] Packing and numbering the request message and the response message to obtain the HTTP message, wherein the HTTP message is used to trigger the execution of the unit test code;

[0025] If the type of the business request is a non-Hypertext Transfer Protocol (HTTP) request, the unit test code execution is triggered by the predefined function.

[0026] In one possible design, the predefined screening rule includes at least one of the following screening rules:

[0027] Deduplication filtering rules;

[0028] Sorting and filtering rules;

[0029] A boundary screening rule, wherein the boundary screening rule is used to select first unit test case data having a specific boundary condition;

[0030] The extended screening rule is used to expand and supplement the first unit test case data.

[0031] In a second aspect, the present application provides a unit test code generation device, the device comprising:

[0032] A collection module is used to obtain multiple groups of first unit test case data from actual business scenarios based on aspect-oriented programming (AOP) technology, wherein each group of first unit test case data includes the type of business request, the calling method, the passed parameters, and the return value;

[0033] An analysis module is used to filter each group of first unit test case data according to a predefined filtering rule to obtain filtered second unit test case data;

[0034] The analysis module is further used to determine a predefined target unit test case template according to the type in each group of second unit test case data;

[0035] A template filling module is used to fill the second unit test case data into the target unit test case template to obtain a unit test code.

[0036] In a third aspect, the present application provides an electronic device, comprising: a transceiver, a processor, and a memory communicatively connected to the processor;

[0037] The memory stores computer-executable instructions;

[0038] The processor executes the computer-executable instructions stored in the memory to implement the unit test code generation method as described in any one of the first aspects.

[0039] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the unit test code generation method as described in any one of the first aspects.

[0040] In a fifth aspect, the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the unit test code generation method as described in any one of the first aspects.

[0041] The unit test code generation method, device, equipment and storage medium provided by the present application relate to the field of unit test technology. In this scheme, in order to ensure that the test case data in the unit test code fits the business data in the actual business scenario, the unit test case data is obtained from the actual business scenario mainly through aspect-oriented programming (Aspect Oriented Programming, referred to as AOP) technology. At the same time, in order to ensure the comprehensiveness of the unit test case data, the present application uses predefined screening rules to identify and screen the business data initially obtained from the actual business scenario. On this basis, in order to improve the generation efficiency of the unit test code, the present application directly fills the screened unit test case data into the pre-configured target unit test case template. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0043] Figure 1 A schematic diagram of an application scenario of the unit test code generation method provided in this application;

[0044] Figure 2 A flowchart of a first embodiment of a method for generating unit test code provided in the present application;

[0045] Figure 3 A flowchart of a second embodiment of the unit test code generation method provided in this application;

[0046] Figure 4 A flowchart of a third embodiment of the unit test code generation method provided in this application;

[0047] Figure 5 A flowchart of a fourth embodiment of the method for generating unit test code provided in this application;

[0048] Figure 6 A flowchart of the unit test code generation method provided for this application;

[0049] Figure 7 A schematic diagram of the structure of the first embodiment of the unit test code generation device provided by the present application;

[0050] Figure 8 A schematic diagram of the structure of the second embodiment of the unit test code generation device provided by the present application;

[0051] Fig. 9 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.

[0052] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0053] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0054] In the technical solution of this application, the collection, storage, use, processing, transmission, provision and disclosure of information such as financial data or user data involved shall comply with the provisions of relevant laws and regulations and shall not violate public order and good morals.

[0055] It should be noted that in the embodiments of the present application, certain software, components, models and other existing solutions in the industry may be mentioned, and they should be regarded as exemplary. Their purpose is only to illustrate the feasibility of implementing the technical solution of the present application, but it does not mean that the applicant has or will necessarily use the solution.

[0056] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0057] Terminology explanation:

[0058] Aspect Oriented Programming (AOP): The main purpose of AOP is to extract aspects from the business process. It focuses on a certain step or stage in the process to achieve low coupling isolation between the parts of the logic process.

[0059] Hypertext Transfer Protocol (HTTP): an application layer protocol for distributed, collaborative and hypermedia information systems.

[0060] Unit testing technology and unit test code generation auxiliary technology have made remarkable progress in the testing field.

[0061] Typical unit test code generation technologies include unit test code generation technology based on database tables and standard templates, and unit test code generation technology based on generative artificial intelligence dialogue technology. Among them, the unit test code generation technology based on database tables and standard templates automatically generates corresponding unit test code by parsing the database table structure and business logic. This technology can ensure that the unit test cases are consistent with the actual data structure in the database, thereby improving the accuracy and effectiveness of the unit test. The unit test code generation technology based on generative artificial intelligence dialogue technology uses natural language processing and machine learning technology to enable generative artificial intelligence to understand the intentions of testers and generate unit test code that meets the requirements through dialogue. This technology makes the writing of test code more intelligent and humane.

[0062] However, the unit test case data that the above unit test code generation auxiliary technology relies on is mainly based on the formal definition of static data tables, which is somewhat different from the dynamic business data in actual business application scenarios.

[0063] In view of the above problems, the inventor found in the process of studying the unit test code generation method that if the unit test case data in the generated unit test code all rely on the static data table formal definition, then the problems such as reduced test accuracy and insufficient coverage are likely to occur during the unit test process. Based on this, the inventor considers whether it is possible to monitor the services of the business test process in the process of functional testing of the actual business system, and obtain actual dynamic business data, so that the unit test case data is more in line with actual needs, and the coverage and accuracy of the unit test case data are improved. Specifically, in the process of functional testing of the actual business system, firstly, multiple groups of unit test case data are obtained from the actual business scenario using AOP technology, and then the obtained multiple groups of unit test case data are screened according to predefined screening rules to obtain target unit test case data, and finally the target unit test case data is filled into the target unit test case template, so as to obtain the unit test code. Through this method, it can be ensured that the unit test case data is closer to the actual business needs and the coverage and accuracy of the unit test are improved. At the same time, this method of dynamically acquiring and filtering test case data not only solves the limitations brought by static data table definitions, but also can more effectively reflect the complex situations and changes in actual business systems, and improve the overall quality and reliability of unit testing.

[0064] Figure 1 This is a schematic diagram of an application scenario of the unit test code generation method provided in this application. Figure 1 As shown, the application scenario of the solution provided by the present application includes a unit test code generation system 101 and a target application server 102. Among them, the unit test code generation system 101 is deployed with a configuration module 1011, a collection module 1012, a data analysis module 1013 and a template filling module 1014. The target application server 102 is deployed with a target application system 1021. Figure 1 Only one unit test code generation system 101 and target application server 102 are shown in FIG. 1 , but it should be understood that two or more unit test code generation systems 101 and target application servers 102 may exist.

[0065] After the target application system 1021 deployed on the target application server 102 is successfully started, the acquisition module 1012 deployed in the unit test code generation system 101 first collects the unit test case data generated in the actual business scenario in real time as the first unit test case data. Then the acquisition module 1012 transmits the collected first unit test case data to the data analysis module 1013. The data analysis module 1013 screens the first unit test case data according to the predefined screening rules to obtain the screened second unit test case data. Subsequently, the data analysis module 1013 extracts the predefined target unit test case template from the configuration module 1011 according to the type in the second unit test case data. Finally, the template filling module 1014 fills the second unit test case data into the predefined target unit test case template, thereby generating the final unit test code. Through the above process, the unit test code generation system 101 can obtain and process the test data in the actual business scenario in real time, ensure that the generated unit test code is closer to the actual business needs, and improve the coverage and accuracy of the unit test.

[0066] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0067] based on Figure 1 In the application scenario shown, an embodiment of the present application also provides a unit test code generation method. Figure 2 A flowchart of the first embodiment of the unit test code generation method provided in this application.

[0068] like Figure 2 As shown, the process of the unit test code generation method may include:

[0069] S201: According to the AOP technology, multiple groups of first unit test case data are obtained from actual business scenarios, wherein each group of first unit test case data includes the type of business request, the calling method, the passed parameters and the return value.

[0070] In this step, it is very important to obtain unit test case data during the unit test code generation process. In order to ensure that the unit test case data is more in line with actual business needs, during the process of executing functional tests on the target application system, multiple sets of first unit test case data are obtained in real time from actual business scenarios using AOP technology.

[0071] It is worth noting that this application mainly uses AOP technology to intercept Bean objects managed by Spring containers in the target application system. Among them, the Spring framework is widely used in enterprise-level application development, and one of its core is to achieve modularization and decoupling through dependency injection and AOP technology. Through AOP technology, additional functions such as logging, transaction management, and security checks can be dynamically added to Bean objects managed by Spring containers without modifying business codes. This is a priority set of code types. As the basic framework for Java application design, Spring covers most scenarios of business application code development and is of great significance to realistic unit test code generation.

[0072] In this application, these Bean objects are intercepted using AOP technology, which can capture operations and data in actual business scenarios in real time. This interception mechanism not only ensures the real-time and accuracy of unit test case data, but also enables the generated unit test code to better reflect actual business needs, thereby improving the coverage and effectiveness of unit testing. In this way, testers can obtain high-quality test data without interfering with normal business logic, further improving the overall quality of unit testing.

[0073] Among them, the key elements involved in each set of first unit test case data include but are not limited to the type of business request, the calling method, the passed parameters and the return value.

[0074] Specifically, the type of business request refers to the classification of business operations, such as create, read, and update operations. Obtaining the type of business request helps to classify and organize unit test cases, thereby more comprehensively covering different types of business scenarios.

[0075] The called method refers to the specific method that is actually called in the business operation. By recording the called method, the execution path of the business logic can be accurately reproduced, ensuring that the unit test case can accurately simulate the actual business operation.

[0076] Passed parameters refer to the input parameters passed when calling a method. By recording the passed parameters, you can ensure that the unit test cases can accurately simulate the actual input conditions.

[0077] The return value refers to the output result or return value after the method is executed. Recording the return value helps to verify whether the execution of the method meets expectations, thereby judging the correctness of the business logic. The return value can be a simple data type or a complex object or data structure. Among them, the return value includes normal values ​​and abnormal values. Normal values ​​refer to the expected results returned by the method under normal execution. These values ​​usually indicate that the business operation is successfully completed and meets the expectations of the business logic. Abnormal values ​​refer to the results returned when the method encounters an error or abnormal situation during execution. These values ​​usually indicate that the business operation failed to complete successfully and the error or exception needs to be handled.

[0078] Capturing these key elements in real time through AOP technology can lay the foundation for generating high-quality unit test case data. These data not only truly reflect the operations in actual business scenarios, but also ensure that the generated unit test code has high coverage and high accuracy, thereby improving the overall quality and reliability of unit testing.

[0079] In a possible implementation, the service request includes a service entry request, or a service entry request and a service internal request.

[0080] If the service request includes a service entry request and a service internal request, each set of first unit test case data also includes an attribute dependency relationship, and the attribute dependency relationship is used to represent the dependency relationship between the service entry request and the service internal request.

[0081] Specifically, during the functional testing process, the business requests involved include business entry requests or business entry requests and business internal requests.

[0082] Among them, the business entry request refers to the request directly initiated by the tester, which is the first step to enter the target application system. For example, when the tester clicks a button in the application, the login request sent is a business entry request.

[0083] Internal business requests refer to the process where a business entry request enters the target application system, which may trigger a series of operations and data processing within the system. These operations and processing will further generate internal requests to complete the entire business process. For example, after a login request is received, the target application system may need to verify the user's credentials, which will involve an internal request to query the database.

[0084] Therefore, the functional testing process involves not only business entry requests, but also business internal requests triggered by business entry requests to ensure the correctness and completeness of the entire business process.

[0085] If the business request includes a business entry request and a business internal request, then each set of first unit test case data also includes attribute dependency.

[0086] Among them, the attribute dependency is used to represent the dependency between the business entry request and the business internal request. Specifically, the test data of the business internal request depends on the data provided by the business entry request. Therefore, when obtaining unit test case data, you need to pay attention to this dependency to ensure that all possible situations can be covered in the subsequent unit test code.

[0087] It is worth noting that business requests are relative and are not necessarily HTTP requests that Java programs generally involve, but all requests configured as unit test case target objects and methods, including HTTP requests and general requests. At the same time, the same type and method combination can be the internal request of the first test case and the entry request of the second test case at the same time.

[0088] For example, in a functional test case, the business request is http: / / host / M1, and the get method of the type M1 instance implements the business logic of the business request, and calls the user method of type M2 to implement internal functions. The user method of M2 calls the data method of M3 to finally obtain data. M1, M2, and M3 are all unit test case target types. Then, in the above functional test case, three unit test cases will be decomposed, namely, http request case: http: / / host / M1, case test target M1 type get method, dependent object M2 type user method; Java method call 1: case test target M2 type, user method, dependent object M3 type data method; Java method call 2: case test target M3 type, data method, no dependent object.

[0089] In particular, for specific codeless dynamically generated type instances, type exclusion operations can be performed. For example, the above M3 type is assumed to be the Mapper type object of Mybatis, which is an interface type and has no actual testable implementation code. In this case, it can be used as an excluded type and the test case of Java method call 2 can be ignored.

[0090] The method provided in this embodiment covers the dependency between business entry requests and business internal requests in unit test cases, can better cover the functions and interactions of various aspects of the target application system, and improve the comprehensiveness of unit testing.

[0091] S202: Filter each group of first unit test case data according to a predefined filtering rule to obtain filtered second unit test case data.

[0092] In this step, according to step S201, after obtaining multiple groups of first unit test case data from actual business scenarios according to AOP technology, each group of first unit test case data is filtered according to predefined filtering rules to obtain second unit test case data after filtering.

[0093] The reason for performing the screening is that the data volume of the multiple groups of first unit test cases is large and complex, and the efficiency and pertinence of the unit test need to be ensured.

[0094] When predefining screening rules, the main factors to consider include but are not limited to coverage, boundary conditions, exception handling, and performance testing. Specifically, coverage refers to selecting test cases that can cover more code paths to improve the comprehensiveness of the test. Boundary conditions refer to giving priority to test cases that can test system boundary conditions, such as maximum values, minimum values, and null values. Exception handling refers to selecting test cases that can test the system's exception handling capabilities to ensure the stability of the system under abnormal circumstances. Performance testing refers to selecting test cases that can test system performance, such as high concurrency and large data volume scenarios.

[0095] By screening each set of first unit test case data, redundant testing can be effectively reduced, test efficiency can be improved, and key functions and scenarios can be ensured to be fully verified.

[0096] Optionally, before screening multiple groups of first unit test case data, the data can be automatically classified. This classification process aims to group the first unit test case data according to specific standards or attributes for better understanding and processing. In this process, test cases can be classified according to different standards, such as the type of business request, functional module, application scenario, complexity level or priority. By analyzing each test case and assigning it to the corresponding classification group, a group of test cases with similar characteristics or attributes can be obtained. Such classification results help to better organize and process test case data and provide a more accurate basis for subsequent screening and template selection.

[0097] S203: Determine a predefined target unit test case template according to the type of business request in each group of second unit test case data.

[0098] In this step, according to step S202, multiple groups of first unit test case data are screened to obtain the second unit test case data after screening. Next, it is necessary to determine the predefined target unit test case template based on the type of business request in each group of second unit test case data.

[0099] Specifically, the present application predefines corresponding unit test case templates according to different business request types. These templates contain standard test structures to ensure the comprehensiveness and consistency of the test. Optionally, each unit test case template not only includes the preconditions for the unit test, but also lists in detail the specific test steps, expected prediction results, and post-steps. The design purpose of these templates is to standardize the test process, make the test more systematic and standardized, thereby improving the quality and efficiency of the test and laying the foundation for the generation of subsequent unit test code.

[0100] Therefore, for each group of second unit test case data, the target unit test case template corresponding to the business request type can be accurately located. In this way, it can be ensured that each test case can be tested according to the predefined standards, thereby improving the coverage and accuracy of the test. In addition, this templated testing method can also reduce manual errors and ensure the reliability and consistency of the test results.

[0101] S204: Fill the second unit test case data into the target unit test case template to obtain the unit test code.

[0102] In this step, according to step S203, after determining the predefined target unit test case template according to the type of business request in each group of second unit test case data, the second unit test case data is filled into the target unit test case template to generate specific unit test code.

[0103] Specifically, after the unit test case template applicable to the specific service request type is determined, the filtered second unit test case data are filled into corresponding positions of the template one by one.

[0104] In this way, specific information in the second unit test case data, such as the type of business request, the called method, the passed parameters and the return value, etc., will be embedded in the placeholder corresponding to the target unit test case template.

[0105] Specifically, during the filling process, these placeholders will be replaced by actual test case data. For example, if the business request type is "user login", the method called is "login()", the parameters passed are the user name and password, and the expected return value is a message of successful login, then the placeholders in the template will be replaced with the corresponding specific values.

[0106] After the above filling process, all the placeholders in the template will be replaced with specific test case data, thus generating complete unit test code. These codes can be used directly to execute tests and verify the correctness and stability of the system under various business request types.

[0107] The unit test code generation method provided by the embodiment of the present application, based on actual business scenarios, adopts AOP technology to dynamically obtain actual business data as unit test case data, effectively improving the fit and coverage of unit test case data with actual business scenarios. At the same time, the unit test case data is screened using predefined screening rules to ensure the quality of the unit test case data. On this basis, the second unit test case data after screening is filled into the predefined target unit test case template, thereby efficiently generating standardized unit test code. This method not only improves the efficiency and accuracy of unit testing, but also provides a reliable basis for subsequent test execution and result analysis.

[0108] Next, the specific implementation process of step S204 will be discussed in detail. Figure 3 This is a flow chart of the second embodiment of the unit test code generation method provided by this application. Figure 3 As shown, based on the above embodiment, the second unit test case data is filled into the target unit test case template to obtain the unit test code. The process of the unit test code generation method may include:

[0109] S301: Determine an initial trigger state of a corresponding type in each group of second unit test case data according to a predefined reflection mechanism.

[0110] In this step, after determining the predefined target unit test case template according to the type of business request in each set of second unit test case data, the initial trigger state of the corresponding type in each set of second unit test case data is determined according to the predefined reflection mechanism. The initial trigger state refers to the initial value or state of a method or field at the beginning of the unit test.

[0111] Specifically, reflection is a technology that can dynamically obtain class information and operate class properties and methods at runtime. In unit testing, reflection can be used to dynamically call methods, set properties, and obtain the state of objects. In this way, different types of business requests can be handled more flexibly and unit test cases can accurately reflect the actual business logic.

[0112] For example, the target application system is an e-commerce management system, and the types of business requests include user login, product query, and order processing. For each type of business request, there are corresponding unit test case data and predefined target unit test case templates. When the type of business request is user login, the reflection mechanism can dynamically call the user login method and set the initial state to the non-logged-in state.

[0113] S302: Add the initial trigger state to each set of second unit test case data to obtain multiple sets of target unit test case data.

[0114] In this step, according to step S301, based on the predefined reflection mechanism, after determining the initial trigger state of the corresponding type in each group of second unit test case data, the initial trigger state is added to each group of second unit test case data, thereby obtaining multiple groups of target unit test case data.

[0115] Specifically, the purpose of adding the initial trigger state to each set of second unit test case data is to ensure that each unit test case has a clear and consistent initial state when it is executed. These target unit test case data are preprocessed and contain all necessary initial state settings, thereby ensuring that the test case can accurately simulate the real operation scenario when it is executed. In this way, the reliability and repeatability of subsequent unit tests can be ensured, and the deviation of test results caused by inconsistent initial states can be avoided.

[0116] Optionally, the initial trigger state of each group of second unit test cases can also be obtained by passing the initial state value as a parameter to the unit test case.

[0117] S303: Fill the target unit test case data into the target unit test case template to obtain the unit test code.

[0118] In this step, after obtaining multiple groups of target unit test case data according to step S302, these target unit test case data are filled into the target unit test case template, thereby generating the final unit test code.

[0119] The target unit test case data contains all the inputs, expected outputs, and initial state settings required for the unit test. The target unit test case template contains the framework and necessary logic of the unit test code to ensure that each unit test case can be executed in a unified environment.

[0120] By filling the target unit test case data into the target unit test case template, complete unit test code can be quickly generated without manually writing the details of each unit test case.

[0121] Optionally, after filling the target unit test case data into the target unit test case template, the existing code generation tool can be applied to automatically generate unit test code related to the target unit test case data and the target unit test case template. Among them, the ruoyi-vue-pro code generation tool provided by the Yudao organization can automatically generate complete unit test code based on the provided target unit test case data and target unit test case template. Through the code generation tool, the tester does not need to manually write the unit test code, thereby improving the test efficiency and code quality.

[0122] The unit test code generation method provided by the embodiment of the present application can determine the initial trigger state of the corresponding type in each group of the second unit test case data through a predefined reflection mechanism. In order to generate multiple groups of target unit test case data, these initial trigger states need to be added to each group of the second unit test case data. This process ensures that each group of test cases has the correct initial state before execution, thereby executing the test logic more accurately. By adding the initial trigger state, the accuracy and consistency of the unit test can be ensured, thereby improving the efficiency of the unit test.

[0123] Figure 4 This is a flow chart of the third embodiment of the unit test code generation method provided by this application. Figure 4 As shown, based on any of the above embodiments, according to the AOP technology, before obtaining multiple groups of first unit test case data from the actual business scenario, the process of the unit test code generation method also includes:

[0124] S401: According to the type of the target unit test, configure a corresponding target unit test case template.

[0125] In this step, before obtaining multiple groups of first unit test case data from actual business scenarios according to the AOP technology, it is necessary to pre-configure the corresponding target unit test case template.

[0126] Among them, when configuring the target unit test case template, the basis relied on is the type of the target unit test. Specifically, in the process of creating the target unit test case template, it is necessary to configure it according to the types of different business requests. Common business request types include but are not limited to query business requests, create business requests, update business requests, etc.

[0127] Optionally, the target unit test case template includes the unit test case number, description, input, expected output, checkpoint, precondition, postcondition, test method, actual result, etc. Among them, the unit test case number is used to uniquely identify each unit test case, and the description is a brief description of the unit test case, explaining the goal and expected result of the unit test. The input is the given input data or parameters used to trigger the execution of the unit under test. The expected output is the expected unit execution result for the given input. The checkpoint is the condition for verifying the expected output, and the checkpoint can be a comparison of state variables, the capture of exceptions, etc. The precondition is the condition or setting that needs to be met before running the unit test case. The postcondition is the cleanup operation or state recovery required after running the unit test case. The test method is the step that describes how to execute the test case. The actual result mainly records the actual result of each unit test step.

[0128] It is worth noting that the content in the target unit test case template can be adjusted and expanded according to actual conditions to meet the needs of unit testing. Specifically, the target unit test case template has scalability and flexibility, and it can be expanded and customized as needed to adapt to different test needs. The target unit test case template can process Java's basic types (such as int, float, boolean, etc.), String type, Date type, BigDecimal type and other system-defined basic types according to conventional rules, and also supports standard JavaBean types based on these basic types. JavaBean is a Java class that conforms to specific specifications, usually with private properties, public getter and setter methods. At the same time, the target unit test case template can automatically identify and process these types of attributes without additional manual intervention. For the identification and comparison of the type range covered by the template and other extended types, each specific implementation can be customized and extended on the existing basis, that is, the template can be expanded to support more types or custom types according to the specific needs of the test. This application does not impose special restrictions on the types supported by the template and the extension of the types.

[0129] S402: Define a target unit test case data collection scope, wherein the target unit test case data collection scope includes all Bean objects managed by the Spring container.

[0130] In this step, before obtaining multiple sets of first unit test case data from actual business scenarios based on AOP technology, in addition to pre-configuring the corresponding target unit test case template, it is also necessary to define the target unit test case data collection scope to ensure that the unit test case data captured from the actual business scenario is comprehensive and relevant.

[0131] When performing target unit testing, the target unit test case data collection scope includes all bean objects managed by the Spring container. This means that during the test process, all bean objects registered and managed in the Spring container will be considered and used. The Spring container is not only responsible for the creation and configuration of beans, but also manages their life cycle. Therefore, all bean objects managed by the Spring container may participate in the execution of business logic, so they need to be included in the scope of data collection.

[0132] Specifically, in order to ensure the comprehensiveness and accuracy of subsequent unit tests, every bean object managed by the Spring container must be taken into account. These bean objects may include service layer components, data access objects, controllers, and other auxiliary classes. Since these bean objects may depend on and interact with each other during the operation of the application, including them in unit tests can better simulate the actual operating environment.

[0133] The unit test code generation method provided by the embodiment of the present application, by configuring the corresponding target unit test case template, can standardize the writing process of the unit test case, ensure the consistency and maintainability of the unit test case, thereby improving the efficiency and quality of the unit test work. By defining the target unit test case data collection scope, it can be ensured that the data obtained from the actual business scene is comprehensive and accurate, which lays the foundation for the automatic generation and execution of subsequent unit test cases. At the same time, this also helps to ensure that the target unit test case can fully cover all aspects of business logic, and improves the effectiveness and reliability of unit testing.

[0134] In a possible implementation, a method for triggering the execution of the unit test code may be determined according to the type of the service request, wherein the method includes an HTTP message method and / or a predefined function method.

[0135] Specifically, when a unit test is performed, a method for triggering the execution of the unit test code needs to be determined in advance. When determining the execution method of a unit test code, the basis relied on is the type of business request.

[0136] The unit test code execution method includes but is not limited to HTTP message method and / or predefined function method.

[0137] HTTP message mode means that for some business requests, especially those related to network communication, the execution of unit test code can be triggered by simulating sending HTTP requests. This means that the unit test code can simulate the client sending HTTP requests to the server to simulate the actual business scenario and verify whether the system behavior meets expectations.

[0138] The predefined function method means that for certain business requests, the execution of the unit test code can be triggered by calling a predefined function or method. This means that the unit test code can directly call the functions or methods that have been defined in the system to simulate business requests and perform corresponding tests.

[0139] It is worth noting that the HTTP message method can also be replaced by a predefined function method.

[0140] The unit test code generation method provided in this embodiment mainly provides a method for triggering the execution of unit test code. Specifically, selecting the method for triggering the execution of unit test code according to the type of business request helps to ensure that the unit test covers different types of business scenarios, thereby improving the comprehensiveness and effectiveness of the unit test.

[0141] Figure 5 This is a flow chart of the fourth embodiment of the unit test code generation method provided by this application. Figure 5 As shown, based on any of the above embodiments, according to the type of business request, the method for triggering the execution of the unit test code is determined, and the process of the unit test code generation method includes:

[0142] S501: If the type of the service request is an HTTP request, a filter is used to record a request message and a response message in the HTTP request format.

[0143] In this step, when the AOP technology is applied to obtain unit test case data, if the type of the business request is an HTTP request, a filter is used to record the request message and the response message in the HTTP request format.

[0144] A filter is a technology that can intercept and process HTTP requests and responses. It is defined in detail in the Java Servlet specification and will not be described here.

[0145] By setting up specific filters to capture all incoming and outgoing HTTP requests and responses, and record their contents in a format that is easy to understand and analyze. Doing so will allow you to track detailed information about the request, including the request header, request parameters, and request body content, as well as the corresponding response status code, response header, and response body data. The recorded information can be used for data preparation and result verification of unit test cases. By recording HTTP requests and responses through filters, you can better understand the behavior of the system and improve the reliability and stability of the system.

[0146] S502: Pack and number the request message and the response message to obtain an HTTP message, wherein the HTTP message is used to trigger the execution of the unit test code.

[0147] In this step, based on step S501, after obtaining the request message and the response message in the HTTP request format, the request message and the response message are packaged and numbered to obtain the HTTP message.

[0148] Specifically, in order to manage and organize request messages and response messages, they can be packaged into HTTP message objects and each HTTP message object can be assigned a unique number, which helps to track and reference these messages. Packaging refers to associating each group of request messages and response messages to form a complete HTTP interaction record. Numbering refers to assigning a unique identifier to each group of HTTP interaction records.

[0149] In the unit testing phase, these HTTP messages can be used to simulate actual HTTP requests, thereby triggering the execution of business logic. Specifically, according to the type of business request, the pre-stored HTTP message is read, and according to the information in the HTTP message, a simulated request is initiated to the unit to be tested. After the unit to be tested generates the corresponding response, it is compared with the expected response message to verify the correctness of the unit to be tested.

[0150] S503: If the type of the business request is non-HTTP request, trigger the execution of the unit test code through a predefined function.

[0151] In this step, when the type of the business request is non-HTTP request, the unit test code execution can be triggered by a predefined function.

[0152] Specifically, in some systems, business requests may not be transmitted through the HTTP protocol, but through other communication methods, such as message queues, remote procedure calls, and database operations. In this case, predefined functions provide a standardized and flexible way to trigger the execution of unit test code.

[0153] Predefined functions are specific functions used to simulate actual business requests in a unit test environment. These functions can represent the interfaces or methods that the system will call when it is actually running. Predefined functions will generate and send corresponding request data to simulate real business request behavior. At the same time, calling predefined functions will cause the execution of related business logic within the system, thereby testing its response and processing capabilities.

[0154] During the unit testing phase, the corresponding predefined function will be called according to the type of business request, and specific parameters will be passed in to simulate various business scenarios, thereby triggering the execution of the unit test code.

[0155] The unit test code generation method provided in the embodiment of the present application mainly provides two ways of executing unit test code. Specifically, the request message and the response message are packaged and numbered to generate HTTP messages for triggering the execution of the unit test code, which can accurately simulate and verify the processing of HTTP requests, and can also improve the automation and reliability of the test. The predefined function method can effectively simulate various business request scenarios, verify the correctness and stability of the unit to be tested under different conditions, thereby improving the efficiency of unit testing and enhancing the coverage and accuracy of unit testing.

[0156] In one possible implementation, the predefined filtering rules include at least one of the following filtering rules: deduplication filtering rules; sorting filtering rules; boundary filtering rules, which are used to select first unit test case data with specific boundary conditions; and extension filtering rules, which are used to expand and supplement the first unit test case data.

[0157] Specifically, when screening multiple groups of first unit test case data, the predefined screening rules applied include but are not limited to deduplication screening rules, sorting screening rules, boundary screening rules and extension screening rules.

[0158] Among them, the deduplication filtering rule is used to remove duplicate first unit test case data. Among the multiple sets of first unit test case data obtained from actual business scenarios using AOP technology, there may be multiple sets of test cases with the same input and expected output, and these duplicate data have no effect on the unit test results. Therefore, the deduplication filtering rule can be used to exclude these duplicate test cases to simplify the testing process and improve efficiency.

[0159] Sorting and filtering rules are used to sort the first unit test case data in order to better organize and manage the data. By specifying sorting rules, test cases can be sorted according to a certain attribute (such as case name, input parameters, etc.), making them easier to find, compare and understand.

[0160] Boundary filtering rules are used to select the first unit test case data with specific boundary conditions. When performing unit testing, it is usually necessary to verify the system's ability to handle various boundary conditions, such as minimum values, maximum values, critical values, etc. Boundary filtering rules can help select test cases that contain these boundary conditions to ensure the correctness and stability of the system in extreme cases.

[0161] Extended filter rules are used to expand and supplement test case data. In actual unit testing, you may find that some test cases are not comprehensive enough to cover all scenarios. Extended filter rules can automatically generate or supplement additional test cases according to specific requirements to enhance the coverage and quality of the test.

[0162] Optionally, the predefined screening rules may also include feature screening rules, random screening rules, dependency screening rules, etc. Feature screening rules refer to screening test cases based on certain specific features or tags. For example, screening is performed based on the priority, module, function point, etc. of the test case in order to focus on testing a certain function or feature.

[0163] The random screening rule refers to randomly extracting the first unit test case data or selecting a certain number of random first unit test case data. This method helps to sample in large-scale first unit test case data and avoid the high cost of executing all test cases.

[0164] Dependency filtering rules are based on the dependencies between test cases. For test cases that need to be executed in sequence, ensure that their dependencies are met. At the same time, you can also exclude dependent test cases that are not related to the current test target.

[0165] The unit test code generation method provided in this embodiment mainly provides predefined screening rules. By applying these rules, the test case data can be optimized and managed to improve the efficiency and accuracy of the test. At the same time, by selecting appropriate screening rules, the coverage and depth of the test can also be enhanced, thereby better verifying the correctness and robustness of the system.

[0166] Figure 6 The following is a flow chart of the unit test code generation method provided in this application. Figure 6 As shown, the process of the unit test code generation method involves users, target applications, AOP capture modules, data analysis modules, template filling modules and configuration modules. The main stages include acquisition stage, resource exhaustion request stage, connection resource release stage and template filling stage. The process specifically includes:

[0167] S601: Start;

[0168] S602: Initiate a service request test;

[0169] In this step, the user, i.e. the tester, initiates a service request test to the target application through the test entry.

[0170] S603: receiving a user request;

[0171] In this step, the target application server where the target application is located responds to the service request test sent by the test entry and receives the entry parameters.

[0172] S604: intercept the request and record the requested method and target parameters;

[0173] In this step, after the target application server responds to the service request test, the AOP capture module deployed in the target application server captures the type, method and entry parameters of the entry request in the form of AOP interception.

[0174] S605: Execute entry request;

[0175] In this step, the target application continues to execute business logic and initiates a request for an internal type instance, where the internal type should be a Bean object managed by the Spring container.

[0176] S606: Intercept the internal service request method and record the target parameters;

[0177] In this step, the AOP capture module deployed in the target application server captures the type, method and entry parameters of the internal request in the form of AOP interception, and determines the attribute dependency between the type instance of the internal request and the type of the entry request based on the Java call stack, that is, whether the upper-level request is an entry request.

[0178] S607: Execute the internal request and return the result or throw an exception;

[0179] In this step, the target internal request executes a specific method and returns a result or throws an exception.

[0180] S608: intercept the return of the internal request and record the return result or exception;

[0181] In this step, the AOP capture module intercepts the return of the internal request in the form of AOP interception and records the return result or exception.

[0182] S609: Execute the subsequent processing of the entry request and generate results;

[0183] In this step, the entry request continues to be returned, the subsequent business logic is completed, and the result is returned.

[0184] S6010: intercept the return of the entry request and record the return result or exception;

[0185] In this step, the AOP capture module intercepts the return of the entry request in the form of AOP interception and records the return result or exception.

[0186] S6011: receiving test results;

[0187] S6012: Pack the captured information and send it to the data analysis module;

[0188] In this step, when the AOP capture module completes the capture, the captured information is packaged and sent to the data analysis module.

[0189] S6013: receiving the captured information and saving it;

[0190] In this step, the data analysis module receives and saves the capture information sent by the AOP capture module.

[0191] S6014: Start parsing the collection results;

[0192] In this step, the data analysis module parses and processes the captured information to obtain template filling data.

[0193] S6015: Read the ingress interface type information, including type and method;

[0194] In this step, the data analysis module reads the entry interface type information, including type and method, to form the target type and target method to be tested.

[0195] S6016: Read internal request information based on the call stack, including the type and method being called;

[0196] In this step, the data analysis module reads the internal type and corresponding method set obtained based on the call stack, which should be the instance object managed by SpringBeanFactory. Based on the above set, the type set and the method set to be defined of Mock are obtained.

[0197] S6017: Based on the call stack, determine whether the two are in an attribute dependency relationship. If so, record the relationship.

[0198] In this step, the data analysis module determines whether there is an attribute dependency relationship between types based on the call stack, and if so, records the relationship.

[0199] S6018: Record the parameters and return values ​​of the entry request as the input parameters and result judgment objects of the test case;

[0200] In this step, the data analysis module records the parameters and return values ​​of the entry request based on the captured information as the input parameters and result judgment objects of the test case.

[0201] S6019: Record the type, method, parameters, and return value of the internal request as a candidate object for Mock;

[0202] In this step, the data analysis module records the type, method, parameters, and return value of the internal request based on the captured information as a candidate object for the Mock.

[0203] S6020: Save the above information as candidate use cases and number them;

[0204] In this step, the data analysis module saves the above information as candidate use cases and numbers them.

[0205] S6021: Group the use cases based on the entry request information, and group the use cases with the same entry request type and method into the same group;

[0206] In this step, the data analysis module groups the use cases based on the entry request information, and all use cases corresponding to the same type and method combination are grouped together.

[0207] S6022: compare multiple candidate use cases in the same group and select the final use cases according to the specified rules;

[0208] In this step, after the data analysis module completes grouping of the use cases, it compares multiple candidate use cases in each group and screens and determines the use cases that are finally adopted according to the specified rules.

[0209] Specifically, the data analysis module sorts and counts the input parameters of each grouped test case and filters the test cases according to specific rules.

[0210] It is worth noting that the rules here include all feasible rules that can be used for use case screening, and the following are only exemplary rules.

[0211] First, sort and deduplicate the test case data to obtain a deduplicated test case set. Then count the test case sets. If the number of sets is less than or equal to the preset number threshold, the parameters in the test case set are regarded as enumerated parameters, and all deduplicated test case data are used as the final test case data. If the number of sets is greater than the preset number threshold, the parameters in the test case set are regarded as general parameters, and the parameters need to be further summarized and selected.

[0212] The selection can be based on specific extension rules or boundary rules. Specifically, an optional boundary rule is to read the specific attribute value of the parameter to form an attribute value set. If the attribute value is sortable, the boundary value of the set is selected and the middle value is selected according to a certain ratio. If the attribute is an enumerable type, all enumeration values ​​are included in the optional parameter range. If the attribute is not sortable, it is used as an alternative attribute.

[0213] Based on the matrix formed by parameter attribute characteristics, the selection of use case data generally requires priority coverage of all enumeration values ​​of all enumeration attributes, and then coverage of the boundary values ​​and random intermediate value distribution of all sortable attributes. On this basis, differentiated selection is performed for parameters with unsortable and non-enumerable attributes.

[0214] Based on the above rules, a complete set of unit test cases can be formed.

[0215] S6023: Receive a list of final candidate use cases;

[0216] In this step, the template filling module receives the final candidate use case list determined by the data analysis module.

[0217] S6024: Read use case data from the list in sequence;

[0218] In this step, the template filling module reads the data of each unit test case in the collection in turn.

[0219] S6025: Based on the use case target type, return a predefined template of the specified type;

[0220] In this step, the template filling module reads the predefined test case template according to the type of the test target object of the test case.

[0221] S6026: Fill in the template according to the rules and generate the use case file;

[0222] In this step, the template filling module fills the target unit test case template with the unit test case data according to the rules, thereby generating a test case file.

[0223] In the process of filling the unit test case data into the target unit test case template, the filling logic of the unit test case data needs to be adapted to the predefined target unit test case template. The following will be explained by taking the HTTP request template as an example, where the HTTP request template is applied to the HTTP request processing type modified by @Controller or @RestController.

[0224] First, fill in the fixed content part of the target unit test case template, which includes import statements for dependent types, such as importing required Java classes and libraries.

[0225] Next, based on the test target type, create a test type name with the same package name as the target type. The test type name consists of the target type name plus the suffix "Test" and a sequence number. For example, if the test target type is "UserController", the test type name can be "UserControllerTest1".

[0226] Then, define the name of the test main method according to the test target method and replace the placeholder in the target unit test case template. A feasible way is to capitalize the first letter of the method being tested and add the prefix "test". For example, if the method being tested is "getUser", the test main method can be named "testGetUser".

[0227] According to the test target type, define a field annotated with "@InjectMocks". For example, if the target type is "UserController", you can define a "@InjectMocks UserControlleruserController" field, where "userController" can be any legal identifier name.

[0228] Define fields annotated with "@Mock" based on the internal dependency type. For example, if the internal dependency type is "UserService", you can define a "@Mock UserService userService" field. Here, "userService" is usually defined as an identifier that is the same as the type name, but with the first letter lowercase. Other rules can also be used, which are not limited here.

[0229] Generate a behavior statement based on the internal dependency method and its input parameter. For example, if the internal dependency method is "getUser" and the input parameter is "1", then a behavior statement can be generated to indicate that an expected result is returned when "getUser(1)" is called. The behavior statement type is relatively complex and needs to be handled as an extensible support logic. For example, for a boolean input parameter, a behavior statement should be generated to indicate that true is returned when a method is called. The return value part of the behavior statement can indicate that an expected result is returned. For abnormal results, a behavior statement can be generated to indicate that a specific exception is thrown.

[0230] It is worth noting that not every parameter object or return target object can be automatically instantiated. When you encounter a situation where automatic instantiation is not possible, you can create an empty instantiation factory method and mark it as a to-do item for the developer to fill in. At the same time, you should rely on reflection technology as detailed as possible to automatically generate attribute value documentation comments for the target object. For example, generate a behavior statement indicating that an exception is thrown, and mark the to-do item in the factory method to prompt the developer to improve the exception instantiation logic.

[0231] In the type header comment of the generated target file, describe the necessary information and use case completion status, whether it contains to-do items, etc.

[0232] Finally, the filling results are output to the specified target directory and stored according to Java type storage rules.

[0233] S6027: End.

[0234] The unit test code generation method provided in the embodiment of the present application is aimed at the commonly used Spring architecture application, utilizes the monitoring and collection results of the functional testing process to form unit test case data, and generates test code files according to rules, which effectively improves the generation efficiency of this type of test case files and plays a significant role in the popularization of unit test cases.

[0235] Figure 7 This is a schematic diagram of the structure of the first embodiment of the unit test code generation device provided by this application. Figure 7 As shown, the unit test code generating device 700 includes:

[0236] The acquisition module 701 is used to obtain multiple groups of first unit test case data from actual business scenarios according to the AOP technology, wherein each group of first unit test case data includes the type of business request, the calling method, the passed parameters and the return value;

[0237] An analysis module 702 is used to filter each group of first unit test case data according to a predefined filtering rule to obtain filtered second unit test case data;

[0238] The analysis module 702 is further used to determine a predefined target unit test case template according to the type in each group of second unit test case data;

[0239] The template filling module 703 is used to fill the second unit test case data into the target unit test case template to obtain the unit test code.

[0240] Optionally, the analysis module 702 is further configured to:

[0241] Determine, according to a predefined reflection mechanism, an initial trigger state of a corresponding type in each group of second unit test case data;

[0242] The initial trigger state is added to each set of second unit test case data to obtain multiple sets of target unit test case data.

[0243] Optionally, the template filling module 703 is further used to fill the target unit test case data into the target unit test case template to obtain the unit test code.

[0244] Optionally, the business request includes: a business entry request, or a business entry request and a business internal request; if the business request includes a business entry request and a business internal request, each set of first unit test case data also includes attribute dependency, and the attribute dependency is used to represent the dependency between the business entry request and the business internal request.

[0245] Optionally, the analysis module 702 is further configured to:

[0246] According to the type of business request, a method for triggering the execution of the unit test code is determined, wherein the method includes a Hypertext Transfer Protocol HTTP message method and / or a predefined function method.

[0247] Optionally, the analysis module 702 is further configured to:

[0248] If the type of the business request is a Hypertext Transfer Protocol (HTTP) request, a filter is used to record the request message and response message in the HTTP request format;

[0249] The request message and the response message are packaged and numbered to obtain an HTTP message, wherein the HTTP message is used to trigger the execution of the unit test code;

[0250] If the type of the business request is not a Hypertext Transfer Protocol (HTTP) request, the unit test code execution is triggered by a predefined function.

[0251] Optionally, the predefined filtering rules include at least one of the following filtering rules: deduplication filtering rules; sorting filtering rules; boundary filtering rules, which are used to select first unit test case data with specific boundary conditions; and extended filtering rules, which are used to expand and supplement the first unit test case data.

[0252] The unit test code generation device provided in this embodiment can be used to execute the unit test code generation method in any of the aforementioned method embodiments. Its implementation principle and technical effects are similar and will not be repeated here.

[0253] Figure 8 This is a schematic diagram of the structure of the second embodiment of the unit test code generation device provided by this application. Figure 8 As shown, the unit test code generating device 700 also includes:

[0254] Configuration module 704, configured to configure a corresponding target unit test case template according to the type of the target unit test;

[0255] Optionally, the configuration module 704 is further used to: define a target unit test case data collection scope, wherein the target unit test case data collection scope includes all Bean objects managed by the Spring container.

[0256] The unit test code generation device provided in this embodiment can be used to execute the unit test code generation method in any of the aforementioned method embodiments. Its implementation principle and technical effects are similar and will not be repeated here.

[0257] It should be noted that it should be understood that the division of the various modules of the above device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. And these modules can all be implemented in the form of software called by processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware. Each module can be a separately established processing element, or it can be integrated in a chip of the above device. In addition, it can also be stored in the memory of the above device in the form of program code, and called and executed by a processing element of the above device. The functions of each module. In addition, all or part of these modules can be integrated together, or they can be implemented independently. The processing element here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each module above can be completed by an integrated logic circuit of hardware in the processor element or instructions in the form of software.

[0258] Fig. 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Fig. 9 As shown, the electronic device may include: a transceiver 900 , a processor 901 , and a memory 902 .

[0259] The processor 901 executes the computer execution instructions stored in the memory, so that the processor 901 executes the scheme in the above embodiment. The processor 901 can be a general-purpose processor, including a central processing unit CPU, a network processor (network processor, NP), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.

[0260] The memory 902 is connected to the processor 901 via a system bus and completes communication between them. The memory 902 is used to store computer program instructions.

[0261] The transceiver 900 may be used to obtain unit test case data and send service requests.

[0262] The system bus can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The transceiver is used to realize the communication between the database access device and other computers (such as clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM) and may also include non-volatile memory.

[0263] The electronic device provided in the embodiment of the present application may be the unit test code generation system of the above embodiment.

[0264] An embodiment of the present application also provides a chip for executing instructions, which is used to execute the technical solution of the unit test code generation method in the above embodiment.

[0265] An embodiment of the present application also provides a computer-readable storage medium, in which computer instructions are stored. When the computer instructions are executed on a computer, the computer executes the technical solution of the unit test code generation method of the above embodiment.

[0266] An embodiment of the present application also provides a computer program product, which includes a computer program stored in a computer-readable storage medium. At least one processor can read the computer program from the computer-readable storage medium. When at least one processor executes the computer program, it can implement the technical solution of the unit test code generation method in the above embodiment.

[0267] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.

[0268] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.

[0269] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.

[0270] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the methods of various embodiments of the present application.

[0271] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.

[0272] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.

[0273] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.

[0274] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.

[0275] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic control unit or a main control device.

[0276] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.

[0277] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application 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 replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for generating unit test code, characterized in that: The method comprises: According to the aspect-oriented programming (AOP) technology, multiple groups of first unit test case data are obtained from actual business scenarios, wherein each group of first unit test case data includes the type of business request, the calling method, the passed parameters, and the return value; According to the predefined screening rules, each group of first unit test case data is screened to obtain the second unit test case data after screening; Determine a predefined target unit test case template according to the type in each group of second unit test case data; Fill the target unit test case template with the second unit test case data to obtain unit test code.

2. The method according to claim 1, characterized in that The method of filling the target unit test case template with the second unit test case data to obtain the unit test code comprises: Determine, according to a predefined reflection mechanism, an initial trigger state of a corresponding type in each group of second unit test case data; Adding the initial trigger state to each set of second unit test case data to obtain multiple sets of target unit test case data; Fill the target unit test case data into the target unit test case template to obtain the unit test code.

3. The method according to claim 1, characterized in that Before obtaining multiple sets of first unit test case data from actual business scenarios according to the aspect-oriented programming AOP technology, the method includes: Configure the corresponding target unit test case template according to the type of target unit test; Define a target unit test case data collection scope, wherein the target unit test case data collection scope includes all Bean objects managed by the Spring container.

4. The method according to any one of claims 1 to 3, characterized in that: The service request includes: a service entry request, or a service entry request and a service internal request; If the service request includes the service entry request and the service internal request, each group of the first unit test case data further includes an attribute dependency relationship, and the attribute dependency relationship is used to represent the dependency relationship between the service entry request and the service internal request.

5. The method according to claim 4, characterized in that The method further comprises: According to the type of the business request, a method for triggering the execution of the unit test code is determined, wherein the method includes a Hypertext Transfer Protocol HTTP message method and / or a predefined function method.

6. The method according to claim 5, characterized in that The method of determining, according to the type of the business request, a way of triggering the execution of the unit test code comprises: If the type of the service request is a Hypertext Transfer Protocol (HTTP) request, a filter is used to record a request message and a response message in the HTTP request format; Packing and numbering the request message and the response message to obtain the HTTP message, wherein the HTTP message is used to trigger the execution of the unit test code; If the type of the business request is a non-Hypertext Transfer Protocol (HTTP) request, the unit test code execution is triggered by the predefined function.

7. The method according to claim 1, characterized in that The predefined screening rules include at least one of the following screening rules: Deduplication filtering rules; Sorting and filtering rules; A boundary screening rule, wherein the boundary screening rule is used to select first unit test case data having a specific boundary condition; Extended screening rules, where the extended screening rules are used to extend and supplement the first unit test case data.

8. A unit test code generation device, characterized in that: The device comprises: The acquisition module is used to obtain multiple sets of first unit test case data from actual business scenarios based on aspect-oriented programming (AOP) technology, wherein each set of first unit test case data includes the type of business request, the calling method, the passed parameters, and the return value; An analysis module is used to filter each group of first unit test case data according to a predefined filtering rule to obtain filtered second unit test case data; The analysis module is further used to determine a predefined target unit test case template according to the type in each group of second unit test case data; A template filling module is used to fill the second unit test case data into the target unit test case template to obtain a unit test code.

9. An electronic device, characterized in that: include: A transceiver, a processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the unit test code generation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the unit test code generation method according to any one of claims 1 to 7 when executed by a processor.

11. A computer program product, characterized in that The invention comprises a computer program, which implements the unit test code generation method according to any one of claims 1 to 7 when being executed by a processor.

Citation Information

Cited By

  • Function test method and device for Internet of Things platform, and medium

    CN121357051A

  • A function test method, device and medium of an internet of things platform

    CN121357051B