Test code generation method and device, equipment and storage medium
Through aspect-oriented programming technology to obtain unit test case data from actual business scenarios, generate unit test code that conforms to complex external object structures, which solves the problem that the existing technology cannot generate Java-type unit test code that does not conform to Spring-Bean object structure types. Improved coverage and quality of unit tests.
Patent Information
- Application Number
- CN202410973542.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2044-07-19
AI Technical Summary
Existing unit test code generation technology cannot generate unit test code of Java type that does not conform to Spring-Bean object structure type, and cannot effectively reflect the business scenarios and changes of complex external objects.
Use aspect-oriented programming (AOP) technology to obtain unit test case data from actual business scenarios, and compare the verification templates based on the predefined target object type and the entry parameter and return value, and generate virtual code that matches the target object type, thereby generating unit tests Code.
It realizes the generation of unit test code for Java type that does not meet the requirements of Java Bean object structure, improves the coverage and quality of unit tests, and can more accurately reflect actual business needs and changes.
Smart Images

Figure CN118916280A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of code processing technology, and in particular to a test code generation method, device, equipment 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 reasoning.
[0004] However, none of the above methods for generating unit test codes provides a solution for generating Java type unit test codes that do not conform to the structural type of Spring-Bean objects. Summary of the invention
[0005] The present application provides a test code generation method, apparatus, device and storage medium to solve the problem in the prior art that it is impossible to generate unit test code corresponding to a complex external object.
[0006] In a first aspect, the present application provides a test code generation method, comprising:
[0007] According to the aspect-oriented programming (AOP) technology, first unit test case data is obtained from an actual business scenario, wherein the first unit test case data includes case data of each object type in the business scenario;
[0008] Determining second unit test case data from the first unit test case data according to a predefined target object type, wherein the target object type is a Java type that does not meet the structural requirements of a Java Bean object;
[0009] Generate virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison and verification template, wherein the input parameter and return value comparison and verification template is determined based on parameter characteristics and attribute characteristics of the method corresponding to the target object type;
[0010] Generate unit test code according to the virtual code corresponding to the target object type.
[0011] In a possible design, determining the second unit test case data from the first unit test case data according to a predefined target object type includes:
[0012] Determine, from the first unit test case data, an input parameter instance of a method corresponding to the target object type according to an object monitoring technique;
[0013] According to the instance object characteristics of the return value of the method corresponding to the target object type, the return value instance is determined from the first unit test case data by using Java reflection technology;
[0014] Among them, the second unit test case data includes: the input parameter instance and the return value instance.
[0015] In a possible design, before generating a virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template, the method further includes:
[0016] Determine a reference comparison verification template according to parameter rules corresponding to parameters in the parameter characteristics of the method corresponding to the target object type;
[0017] Determine a return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type;
[0018] Among them, the input parameter and return value comparison and verification template includes: the input parameter comparison and verification template and the return value comparison and verification template.
[0019] In a possible design, generating virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template includes:
[0020] Determine basic data for generating virtual objects and test cases according to the target object type, the second unit test case data, and the input parameter and return value comparison verification template;
[0021] According to the basic data used to generate virtual objects and test cases, a virtual code corresponding to the target object type is determined.
[0022] In a possible design, generating unit test code according to the virtual code corresponding to the target object type includes:
[0023] The unit test code is generated according to the configuration information and expected behavior in the virtual code.
[0024] In one possible design, the method further includes:
[0025] Get the code to be tested;
[0026] The code to be tested is tested according to the unit test code to obtain a test result of the code to be tested.
[0027] In a second aspect, the present application provides a test code generating device, comprising:
[0028] An acquisition module, used to acquire first unit test case data from an actual business scenario according to aspect-oriented programming (AOP) technology, wherein the first unit test case data includes case data of each object type in the business scenario;
[0029] A determination module, configured to determine second unit test case data from the first unit test case data according to a predefined target object type, wherein the target object type is a Java type that does not meet the structural requirements of a Java Bean object;
[0030] A first generating module is used to generate virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template, wherein the input parameter and return value comparison verification template is determined based on parameter characteristics and attribute characteristics of the method corresponding to the target object type;
[0031] The second generating module is used to generate unit test code according to the virtual code corresponding to the target object type.
[0032] In a possible design, the determining module is specifically used to:
[0033] Determine, from the first unit test case data, an input parameter instance of a method corresponding to the target object type according to an object monitoring technique;
[0034] According to the instance object characteristics of the return value of the method corresponding to the target object type, the return value instance is determined from the first unit test case data by using Java reflection technology;
[0035] Among them, the second unit test case data includes: the input parameter instance and the return value instance.
[0036] In a possible design, before generating the virtual code corresponding to the target object type according to the target object type, the second unit test case data, and the preset input parameter and return value comparison verification template, the determination module is further used to:
[0037] Determine a reference comparison verification template according to parameter rules corresponding to parameters in the parameter characteristics of the method corresponding to the target object type;
[0038] Determine a return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type;
[0039] Among them, the input parameter and return value comparison and verification template includes: the input parameter comparison and verification template and the return value comparison and verification template.
[0040] In a possible design, the first generating module is used to:
[0041] Determine basic data for generating virtual objects and test cases according to the target object type, the second unit test case data, and the input parameter and return value comparison verification template;
[0042] According to the basic data used to generate virtual objects and test cases, a virtual code corresponding to the target object type is determined.
[0043] In a possible design, the second generating module is used to:
[0044] The unit test code is generated according to the configuration information and expected behavior in the virtual code.
[0045] In a possible design, the determining module is further used to:
[0046] Get the code to be tested;
[0047] The code to be tested is tested according to the unit test code to obtain a test result of the code to be tested.
[0048] In a third aspect, an embodiment of the present application provides an electronic device, comprising: at least one processor and a memory; the memory stores computer execution instructions; the at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the test code generation method described in the first aspect and various possible designs of the first aspect.
[0049] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer execution instructions are stored. When a processor executes the computer execution instructions, the test code generation method described in the first aspect and various possible designs of the first aspect is implemented.
[0050] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program. When the computer program is executed by a processor, it implements the test code generation method described in the first aspect and various possible designs of the first aspect.
[0051] The test code generation method, device, equipment and storage medium provided by the present application obtain the first unit test case data from the actual business scenario according to the AOP technology, and the first unit test case data includes the case data of each object type in the business scenario; according to the predefined target object type, the second unit test case data is determined from the first unit test case data, and the target object type is a Java type that does not meet the structural requirements of the Java Bean object; according to the target object type, the second unit test case data, and the preset input parameter and return value comparison verification template, the virtual code corresponding to the target object type is generated, and the input parameter and return value comparison verification template is determined based on the parameter characteristics and attribute characteristics of the method corresponding to the target object type; according to the virtual code corresponding to the target object type, the unit test code is generated. In this technical solution, based on the Java type that does not meet the structural requirements of the Java Bean object, starting from the test cases in the actual business scenario, combined with the corresponding preset input parameter and return value comparison verification template, the unit test code for complex external objects is generated more accurately. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] 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.
[0053] Figure 1 The process of the test code generation method provided in the embodiment of the present application Figure 1 ;
[0054] Figure 2 The process of the test code generation method provided in the embodiment of the present application Figure 2 ;
[0055] Figure 3 The process of the test code generation method provided in the embodiment of the present application Figure 3 ;
[0056] Figure 4 A schematic diagram of the structure of a test code generating device provided in an embodiment of the present application;
[0057] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0058] 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
[0059] 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.
[0060] 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.
[0061] 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.
[0062] Terminology explanation:
[0063] 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. Technical background:
[0065] Unit testing technology and unit test code generation auxiliary technology have made remarkable progress in the testing field.
[0066] The existing generation technologies are mainly divided into the following categories:
[0067] 1. Unit test code generation technology based on database tables and standard templates: By parsing the database table structure and business logic, the corresponding unit test code is automatically generated. 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 unit testing;
[0068] 2. Unit test code generation technology based on generative artificial intelligence dialogue technology: Using natural language processing and machine learning technology, generative artificial intelligence can understand the intentions of testers and generate unit test code that meets the requirements through dialogue. This technology makes the writing of test codes more intelligent and humane.
[0069] However, the above unit test code generation auxiliary technology cannot generate Java type unit test code that does not conform to the structural type of the Spring-Bean object.
[0070] In response to the above problems, the inventors found in the process of studying the unit test code generation method that if Java types that do not meet the structural requirements of Java Bean objects can be pre-defined, and then the virtual (English: Mock) code in the above types is implemented in combination with the input parameter and return value comparison verification templates in the above types, then unit test code of Java types that do not meet the structural requirements of Java Bean objects can be generated. This not only solves the problem of being unable to generate corresponding unit test code, but also can more effectively reflect the complex situations and changes in the actual business system, and improve the overall quality and reliability of unit testing.
[0071] 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.
[0072] Figure 1 The process of the test code generation method provided in the embodiment of the present application Figure 1 .like Figure 1 As shown, the test code generation method includes:
[0073] Step 11: Obtain first unit test case data from an actual business scenario based on AOP technology, where the first unit test case data includes case data of each object type in the business scenario;
[0074] In this step, during the unit test code generation process, in order to ensure that the unit test case data is more in line with the actual business needs, during the application system execution of functional testing, AOP technology is used to obtain the first unit test case data from the actual business scenario in real time.
[0075] Optionally, the technical solution provided in the embodiment of the present application intercepts the Bean object managed by the Spring container in the application system using AOP technology, and uses AOP technology to capture operations and data in actual business scenarios in real time. This interception mechanism not only ensures the real-time and accuracy of the unit test case data, but also enables the generated unit test code to better reflect the actual business needs, thereby improving the coverage and effectiveness of the unit test. In this way, testers can obtain high-quality test data without interfering with normal business logic, further improving the overall quality of unit testing.
[0076] Step 12: Determine the second unit test case data from the first unit test case data according to the predefined target object type;
[0077] Wherein, the target object type is a Java type that does not conform to the structural requirements of a Java Bean object;
[0078] In this step, the predefined target object type involved in the embodiment of the present application is an enumerated Java type, which needs to be added to the custom collection type set after being included in the custom collection scope.
[0079] For example, "co.elastic.clients.elasticsearch.ElasticsearchClient". This type is not a basic Java data type, nor should it be a type that complies with the JavaBean specification and has properties that are simple basic types or standard collection types. The embodiment of the present application extends a customized solution for types that do not comply with the above standards.
[0080] The defined type needs to be a singleton type of Spring, which is responsible for program execution logic rather than data entity objects. For custom collection object types, it is necessary to further clearly list the collection methods of this type.
[0081] For example, the search method and index method of ElasticsearchClient mentioned above are generally limited to the method range required for the Mock operation of the project unit test project. The following takes the search method as an example to describe the subsequent steps of the processing process.
[0082] Step 13: Generate a virtual code corresponding to the target object type according to the target object type, the second unit test case data, and the preset input parameter and return value comparison verification template;
[0083] Among them, the input parameter and return value comparison verification template is determined based on the parameter characteristics and attribute characteristics of the method corresponding to the target object type.
[0084] In this step, the input parameter and return value comparison and verification template includes: an input parameter comparison and verification template, and a return value comparison and verification template; the second unit test case data includes: an input parameter instance, and a return value instance.
[0085] Optionally, set the expected behavior of the simulation object when it receives specific input based on the input example and the input comparison verification template, and set the expected value that the simulation object should return after receiving specific input based on the return value example and the return value comparison verification template.
[0086] Using the generated mock objects and expected behaviors, write unit test cases to verify the virtual code corresponding to the target object. In the test case, call the method of the mock object, pass in the specified parameters, and check whether the return value meets the expectations.
[0087] Use the unit testing framework and tools to execute the written test cases to check whether the virtual code of the target object meets expectations. If it meets expectations, the virtual code corresponding to the target object type is used. If it does not meet expectations, continue to check and adjust.
[0088] Optionally, the implementation of step 13 may be as follows:
[0089] Step 1: Determine the basic data for generating virtual objects and test cases based on the target object type, the second unit test case data, and the input parameter and return value comparison verification template;
[0090] In this implementation, based on the type of the target object, the class or interface of the simulation object to be created can be determined; based on the input parameter instance, the parameters that need to be passed in when calling the target object in the test case can be determined; based on the return value instance, the expected return value after calling the target object in the test case can be determined; based on the input parameter comparison verification template, the parameter comparison required when calling the target object method in the test case can be determined; based on the return value comparison verification template, the verification method for the target object method return value in the test case can be determined.
[0091] Through the above, the basic data for generating virtual objects and test cases can be determined.
[0092] Among them, the input parameter instance can include various types of data, such as integers, strings, lists, etc.; the return value instance can be any type of data returned by the target object method, such as Boolean values, strings, objects, etc.; the input parameter comparison can include verification of parameter type, parameter value range, number of parameters, etc.; the return value comparison can include verification of return value type, return value value range, return value structure, etc.
[0093] Step 2: Determine the virtual code corresponding to the target object type based on the basic data used to generate virtual objects and test cases.
[0094] In this implementation, a mock object is created using tools such as mock or stub according to the target object type; the expected behavior of the mock object when it receives specific input is set according to the input parameter instance and the return value instance; and unit test cases are written using the expected behavior set above to verify the virtual code corresponding to the target object.
[0095] Step 14: Generate unit test code according to the virtual code corresponding to the target object type.
[0096] In this step, a test method is written according to the virtual code corresponding to the target object type to test the function of the target object.
[0097] For example, if the target object is a class, you can write test methods for each method of the class; if the target object is a function, you can directly write a test method for the function.
[0098] Afterwards, according to the virtual code corresponding to the target object type, input data is prepared, including input data under normal circumstances and input data under boundary circumstances, so as to comprehensively test the functions of the target object.
[0099] Then, use the unit testing framework and tools to execute the written test methods, check whether the functions of the target objects meet expectations, analyze the test results, and check whether any tests fail. If so, further debugging and modification of the code is required until all tests pass.
[0100] Optionally, the implementation of step 14 may be as follows: generating unit test code according to the configuration information and expected behavior in the virtual code.
[0101] In this implementation, select a suitable unit test framework based on the development environment and programming language; set up the test environment based on the configuration information in the virtual code; call the method or function of the simulated object based on the expected behavior; use assertion statements (such as assert statements) to verify whether the behavior of the simulated object meets expectations; if the expected behavior includes exception handling, make sure to write test code to verify the behavior of the simulated object under abnormal circumstances; make sure that the written test code is repeatable and can run in different environments, and then generate unit test code.
[0102] Furthermore, after step 14, there may be another implementation as follows: obtaining the code to be tested; testing the code to be tested according to the unit test code, and obtaining the test result of the code to be tested.
[0103] In this implementation, the goal of unit testing is to verify that each unit of the code to be tested (such as function, method, class, etc.) works as expected. Unit testing is usually based on expected input data and output results to verify the correctness and robustness of the code.
[0104] Run the unit test code and observe the test results. The unit testing framework usually displays the execution status of each test case (passed, failed, skipped, etc.) and the specific reason for failure. Through these results, you can quickly understand which parts of the code to be tested have problems or do not achieve expected behavior.
[0105] Based on the test results, the code to be tested is modified and optimized to fix problems found in the unit test code during the testing process.
[0106] Based on the above steps, one implementation is as follows. For the undisclosed contents, please refer to the following embodiments:
[0107] According to the above types of selection and data collection, unit test code rules are formed. This includes reading fixed rule definitions, reading specific types of rules, and filling in templates. The following describes the generation process of a specific code example using the scenario where the search method is called.
[0108] First, define the basic form of unit testing based on the basic framework. Here, the content of the GeneralSearchPortalTest.java file in the attachment is used as a complete test generation template for further explanation.
[0109] First, the embodiment of the present application focuses on the code-assisted generation method of custom complex types with the content of testSearch().
[0110] For custom complex types, the code generation phase is mainly divided into three steps: parameter construction and comparison rule code generation, mock return value object code generation, and mock method parameter and return value rule assignment. There is no strict order between the above steps, and they can appear alternately.
[0111] In the above template: the first thing executed is the generation of the return value object definition code of the search method.
[0112] The generation rules of custom return value objects can include native object assignment rules, mock rule types, or other custom types. In this implementation, the above two types are used interchangeably to complete the final mock return value generation.
[0113] From the template code example, we can see that:
[0114] On the one hand, you can use the Mockito.mock method to generate a mock instance of SearchResponse.
[0115] On the other hand, you can also use the SearchResponse.Builder().build() method to generate a real instance.
[0116] For the Mockito.mock method, the generated mock instance uses the given, when and other methods of mock to assign values to the instance behavior according to the aforementioned return value sampling results.
[0117] Such as in the template code: BDDMockito.given(totalHits.value()).willReturn(1L); and other forms.
[0118] Of course, all the valid methods and judgment bases provided by Mockito can be used here.
[0119] Obviously, the above method can be used to formulate complete recursive definition rules for the mock instance and its sub-instances according to the above return value and its content, and generate the definition code of the unit test according to the rules.
[0120] For real instances, you need to assign properties based on the return value content collected above and the public properties exposed by SearchResponse.Builder(). For example, you can use the following specific method to assign values to the hits method and get the final SearchResponse type instance by calling the builder.build() method.
[0121] Obviously, for other valid attributes of the real instance, corresponding code generation rules and rules for generating the final unit test code based on the collected data can be constructed. And according to the above rules and corresponding data, the corresponding assignment code segment is constructed to obtain the final return value type instance.
[0122] Obviously, for complex types of input parameters, the above two methods can be used to construct them, which will not be repeated here.
[0123] Next are the input parameter matching rules and mock return value assignment rules. On the one hand, for mock methods that are only executed once in the entire call, the any rule in the template can be used to simply define the input parameter matching method.
[0124] Similarly, according to the previous input comparison verification rules, Mockito.argThat can be used to implement detailed comparison of input parameters based on the rules defined by the custom rule template. And finally generate the parameter and return value control code part of the mock object.
[0125] The test code generation method provided by the embodiment of the present application obtains the first unit test case data from the actual business scenario according to the AOP technology, and the first unit test case data includes the case data of each object type in the business scenario; according to the predefined target object type, the second unit test case data is determined from the first unit test case data, and the target object type is a Java type that does not meet the structural requirements of the Java Bean object; according to the target object type, the second unit test case data, and the preset input parameter and return value comparison verification template, the virtual code corresponding to the target object type is generated, and the input parameter and return value comparison verification template is determined based on the parameter characteristics and attribute characteristics of the method corresponding to the target object type; according to the virtual code corresponding to the target object type, the unit test code is generated. In this technical solution, based on the Java type that does not meet the structural requirements of the Java Bean object, starting from the test cases in the actual business scenario, combined with the corresponding preset input parameter and return value comparison verification template, the unit test code for complex external objects is generated more accurately.
[0126] Based on the above embodiments, Figure 2 The process of the test code generation method provided in the embodiment of the present application Figure 2 .like Figure 2 As shown, the implementation of step 12 may include:
[0127] Step 21: Determine the input parameter instance of the method corresponding to the target object type from the first unit test case data according to the object monitoring technology;
[0128] In this step, taking the search method as an example, the input parameters of search are a Function type and a Class type.
[0129] The Class type is the basic input parameter type and will not be described in detail. Function is a method-based extensible type and does not fall within the scope of the basic parameter type definition. Custom collection rules need to be used to define the collection method in detail. According to the parameter definition of the search method, the only apply method of the Function type here receives an input parameter of the SearchRequest.Builder type and returns a return value of the ObjectBuilder type. Finally, by calling the build method of ObjectBuilder, a SearchRequest type instance is obtained to perform subsequent operations.
[0130] According to the object monitoring technology, it can be found that the Function type stores data in the form of specific anonymous parameters during the actual operation process.
[0131] The type of anonymous parameters is determined by the internal parameters of RequestBuilder, and most of them are basic types. If you encounter a non-basic type, you can obviously use the recursive definition method to define the data collection method of the custom type.
[0132] Based on the actual form of the input parameter this time, the actual data parameters of this type can be recorded in the above list. This parameter set will be used in the subsequent execution of the virtual (English: Mock) operation, the input parameter consistency comparison stage.
[0133] Similarly, for any other input parameter type, it is necessary to define or recursively define comparable collection value rules for the input parameters based on the actual characteristics of the input parameters.
[0134] Step 22: According to the instance object characteristics of the return value of the method corresponding to the target object type, the return value instance is determined from the first unit test case data by using Java reflection technology;
[0135] In this step, we continue to take the search method as an example. The return value of the search method is a complex object of the SearchResponse type.
[0136] According to the data characteristics of SearchResponse, the sampled data can be saved in the corresponding format. During the collection process, the memory data of the SearchResponse type instance can be collected through Java reflection technology and saved in the above format.
[0137] For other complex return value types, in specific instance applications, you need to customize independent data saving methods.
[0138] Among them, the second unit test case data includes: input parameter instance and return value instance.
[0139] The above data can be saved in the database in the form of row data, with each unit saving an attribute content.
[0140] The test code generation method provided in the embodiment of the present application determines the input parameter instance of the method corresponding to the target object type from the first unit test case data according to the object monitoring technology, and determines the return value instance from the first unit test case data according to the instance object characteristics of the return value of the method corresponding to the target object type through the Java reflection technology, wherein the second unit test case data includes: the input parameter instance and the return value instance. In this technical solution, based on the object monitoring technology and the Java reflection technology, the unit test case data corresponding to the target object type can be determined more accurately.
[0141] Based on the above embodiments, Figure 3The process of the test code generation method provided in the embodiment of the present application Figure 3 .like Figure 3 As shown, the test code generation method may also include:
[0142] Step 31, determining a reference comparison verification template according to the parameter rules corresponding to the parameters in the parameter characteristics of the method corresponding to the target object type;
[0143] In this step, the input reference comparison verification template is used for mock operations and for input parameter assignment comparison during when and given operations.
[0144] In order to comply with the Java comparison rules, specific equals and other related implementation method types are created. The equals method can be used directly to implement object comparison.
[0145] For general complex parameter types, you need to create custom comparison rules. Taking the input parameters of the search method above as an example, you need to define a standard comparison rule template:
[0146] Optionally, the template includes the following two types of rules:
[0147] Type 1: When the search operation is performed only once within the scope of this test, the Mockito.any(Function.class) and Mockito.eq(Map.class) parameter rules can be used to replace the specific comparison rules.
[0148] Type 2: When the search operation is executed twice within the scope of this test and the parameters are different, you can use the Mockito.argThat method to customize the parameter comparison conditions and rules.
[0149] Step 32: Determine the return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type;
[0150] In this step, the comparison rules of complex return value types are mainly used in the unit test result correctness verification phase, usually in the Assertions.assertEquals(200, servletResponse.getStatus(), phase.
[0151] Taking SearchResponse as an example, its attributes are all comparable attributes and have value comparison characteristics. Therefore, two types of value comparison methods can be specified as examples:
[0152] Method 1: After the value is toString(), perform a full character string match method.
[0153] Method 2: Based on the data collected from the return value collection result of the above process, traverse the attributes in JSON format and the attributes matching the JSON content, use reflection technology or method reading technology to specify the attribute value collection method of SearchResponse one by one, and compare the target value with the actual return value.
[0154] Of course, at this stage, specific developers can define any other legal value comparison method and verification method.
[0155] Among them, the input parameter and return value comparison and verification template includes: an input parameter comparison and verification template and a return value comparison and verification template.
[0156] The test code generation method provided in the embodiment of the present application determines the input reference comparison verification template according to the parameter rules corresponding to the parameters in the parameter characteristics of the method corresponding to the target object type; determines the return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type; wherein the input parameter and return value comparison verification templates include: input reference comparison verification template and return value comparison verification template. In this technical solution, based on string matching or Json format attributes, and parameter rules, the input parameter and return value comparison verification templates can be determined more accurately.
[0157] Figure 4 This is a schematic diagram of the structure of the test code generation device provided in the embodiment of the present application. Figure 4 As shown, the test code generating device comprises:
[0158] An acquisition module 41 is used to acquire first unit test case data from an actual business scenario according to aspect-oriented programming (AOP) technology, wherein the first unit test case data includes case data of each object type in the business scenario;
[0159] A determination module 42, configured to determine second unit test case data from the first unit test case data according to a predefined target object type, wherein the target object type is a Java type that does not meet the structural requirements of a Java Bean object;
[0160] A first generating module 43 is used to generate a virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template, wherein the input parameter and return value comparison verification template is determined based on parameter characteristics and attribute characteristics of the method corresponding to the target object type;
[0161] The second generating module 44 is used to generate unit test code according to the virtual code corresponding to the target object type.
[0162] In a possible design, the determining module 42 is specifically configured to:
[0163] Determine, from the first unit test case data, an input parameter instance of a method corresponding to the target object type according to an object monitoring technique;
[0164] According to the instance object characteristics of the return value of the method corresponding to the target object type, the return value instance is determined from the first unit test case data by using Java reflection technology;
[0165] Among them, the second unit test case data includes: the input parameter instance and the return value instance.
[0166] In a possible design, before generating the virtual code corresponding to the target object type according to the target object type, the second unit test case data, and the preset input parameter and return value comparison verification template, the determination module 42 is further used to:
[0167] Determine a reference comparison verification template according to parameter rules corresponding to parameters in the parameter characteristics of the method corresponding to the target object type;
[0168] Determine a return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type;
[0169] Among them, the input parameter and return value comparison and verification template includes: the input parameter comparison and verification template and the return value comparison and verification template.
[0170] In a possible design, the first generating module 43 is used to:
[0171] Determine basic data for generating virtual objects and test cases according to the target object type, the second unit test case data, and the input parameter and return value comparison verification template;
[0172] According to the basic data used to generate virtual objects and test cases, a virtual code corresponding to the target object type is determined.
[0173] In a possible design, the second generating module 44 is used to:
[0174] The unit test code is generated according to the configuration information and expected behavior in the virtual code.
[0175] In a possible design, the determining module 42 is further configured to:
[0176] Get the code to be tested;
[0177] The code to be tested is tested according to the unit test code to obtain a test result of the code to be tested.
[0178] The test code generation device provided in the embodiment of the present application can be used to execute the technical solution of the test code generation method in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.
[0179] 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. For example, the determination module 42 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 function of the above determination module 42. The implementation of other modules is similar. In addition, these modules can be fully or partially 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.
[0180] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 5 As shown, the electronic device may include: a transceiver 51 , a processor 52 , and a memory 53 .
[0181] A processor 51, a memory 52, and computer program instructions stored in the memory 52 and executable on the processor 51. When the processor 51 executes the computer program instructions, the code processing method provided in any of the foregoing embodiments is implemented.
[0182] Optionally, the above-mentioned components of the electronic device may be connected via a system bus.
[0183] The memory 52 may be a separate storage unit or a storage unit integrated in the processor 51. The number of the processor 51 may be one or more.
[0184] It should be understood that the processor 51 can be a central processing unit (CPU), or other general-purpose processors 51, digital signal processors 51 (DSP), application-specific integrated circuits (ASIC), etc. The general-purpose processor 51 can be a microprocessor 51 or the processor 51 can also be any conventional processor 51, etc. The steps of the method disclosed in the present application can be directly embodied as being executed by the hardware processor 51, or can be executed by a combination of hardware and software modules in the processor 51.
[0185] The system bus may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus may 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 memory 52 may include a random access memory 52 (RAM), and may also include a non-volatile memory 52 (NVM), such as at least one disk storage 52.
[0186] All or part of the steps of the above-mentioned method embodiments can be completed by hardware related to program instructions. The above-mentioned program can be stored in a readable memory 52. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the above-mentioned memory 52 (storage medium) includes: read-only memory 52 (ROM), RAM, flash memory 52, hard disk, solid state hard disk, magnetic tape, floppy disk, optical disc and any combination thereof.
[0187] An embodiment of the present application also provides a chip for executing instructions, which is used to execute the technical solution of the test code generation method in the above embodiment.
[0188] 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 test code generation method of the above embodiment.
[0189] 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, the technical solution of the test code generation method in the above embodiment can be implemented.
[0190] 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.
[0191] 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.
[0192] 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.
[0193] 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.
[0194] 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.
[0195] 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.
[0196] 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.
[0197] 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.
[0198] 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.
[0199] 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.
[0200] 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 test code generation method, characterized in that: include: According to the aspect-oriented programming (AOP) technology, first unit test case data is obtained from an actual business scenario, wherein the first unit test case data includes case data of each object type in the business scenario; Determining second unit test case data from the first unit test case data according to a predefined target object type, wherein the target object type is a Java type that does not meet the structural requirements of a Java Bean object; Generate virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison and verification template, wherein the input parameter and return value comparison and verification template is determined based on parameter characteristics and attribute characteristics of the method corresponding to the target object type; Generate unit test code according to the virtual code corresponding to the target object type.
2. The method according to claim 1, characterized in that: The step of determining the second unit test case data from the first unit test case data according to the predefined target object type includes: Determine, from the first unit test case data, an input parameter instance of a method corresponding to the target object type according to an object monitoring technique; According to the instance object characteristics of the return value of the method corresponding to the target object type, the return value instance is determined from the first unit test case data by using Java reflection technology; Among them, the second unit test case data includes: the input parameter instance and the return value instance.
3. The method according to claim 1, characterized in that Before generating a virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template, the method further includes: Determine a reference comparison verification template according to parameter rules corresponding to parameters in the parameter characteristics of the method corresponding to the target object type; Determine a return value comparison verification template according to the string matching or Json format attributes in the attribute characteristics of the method corresponding to the target object type; Among them, the input parameter and return value comparison and verification template includes: the input parameter comparison and verification template and the return value comparison and verification template.
4. The method according to any one of claims 1 to 3, characterized in that: The generating a virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template includes: Determine basic data for generating virtual objects and test cases according to the target object type, the second unit test case data, and the input parameter and return value comparison verification template; According to the basic data used to generate virtual objects and test cases, a virtual code corresponding to the target object type is determined.
5. The method according to any one of claims 1 to 3, characterized in that: The generating unit test code according to the virtual code corresponding to the target object type includes: The unit test code is generated according to the configuration information and expected behavior in the virtual code.
6. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: Get the code to be tested; The code to be tested is tested according to the unit test code to obtain a test result of the code to be tested.
7. A test code generating device, characterized in that: include: An acquisition module, used to acquire first unit test case data from an actual business scenario according to aspect-oriented programming (AOP) technology, wherein the first unit test case data includes case data of each object type in the business scenario; A determination module, configured to determine second unit test case data from the first unit test case data according to a predefined target object type, wherein the target object type is a Java type that does not meet the structural requirements of a Java Bean object; A first generating module is used to generate virtual code corresponding to the target object type according to the target object type, the second unit test case data, and a preset input parameter and return value comparison verification template, wherein the input parameter and return value comparison verification template is determined based on parameter characteristics and attribute characteristics of the method corresponding to the target object type; The second generating module is used to generate unit test code according to the virtual code corresponding to the target object type.
8. An electronic device, characterized in that: include: 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 method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 6 when executed by a processor.
10. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 6 when being executed by a processor.
Citation Information
Patent Citations
Test method and device and electronic device
CN110008113A
Unit test case generation method and device, equipment and storage medium
CN116541278A
Basic base-based unit test case rapid development method
CN117591414A
Test code generation method and device, electronic equipment and storage medium
CN118093354A
Automatic identification of input values that expose output failures in software object
US20040181713A1
