Unit test code generation method and device applied to anonymous type instance

By collecting and using data related to anonymous type instances in unit tests, and generating more comprehensive test case templates and method call templates, the problem of low test coverage of anonymous type instances is solved, and the coverage and effectiveness of code tests is improved.

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

Patent Information

Application Number
CN202410973547.3
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

Technical Problem

When handling anonymous type instances, the code test coverage is low and the test effect is poor. It is impossible to effectively collect real test data of internal instances corresponding to anonymous functional forms, resulting in a large amount of code not being covered.

Method used

By collecting data associated with the method being tested and dependent methods from the real test scenario, and data associated with the execution method corresponding to the anonymous type instance generated by the method being tested, the matching test case template and method call template are determined. Fill in templates to generate unit test code, expand the scope of data acquisition, and improve the comprehensiveness and accuracy of test cases.

Benefits of technology

Improves code test coverage and test effectiveness, ensuring that the generated unit test code can still execute the internal logic of the dependent method after it is simulated.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916282A_ABST
    Figure CN118916282A_ABST
Patent Text Reader

Abstract

The invention provides a unit test code generation method and device applied to an anonymous type instance. Relates to the technical field of big data. The method comprises the following steps: respectively acquiring first data and second data from a first business scene according to a corresponding data acquisition mode; determining a test case template matched with the first data and a method calling template matched with an execution method corresponding to the anonymous type instance, and adding the method calling template to a corresponding position in the test case template to obtain a to-be-filled template; and filling the first data and the second data into the to-be-filled template to generate a unit test code. According to the method, the code test coverage rate is increased, and the test effect 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 big data technology, and in particular to a method and device for generating unit test code for anonymous type instances. Background Art

[0002] With the development of unit testing technology and unit test code generation auxiliary technology, in a possible implementation, unit test code can be generated based on unit test code generation technology based on database tables and standard templates. For example, test data can be collected based on the aspect-oriented programming (AOP) technology under the Spring framework, and automatic unit test cases can be written based on the collected test data to generate unit test code, so as to perform unit testing based on the unit test code.

[0003] When unit testing is performed based on unit test code, the tested code needs to be isolated based on Mockito technology. At this time, when the input parameter of the mocked object is an anonymous object type, since the mocked object will not execute the input parameter of the anonymous object type like the real object, it is impossible to collect the real test data of the internal instance corresponding to the anonymous function based on the existing aspect-oriented programming AOP technology, resulting in a large amount of uncovered code in the unit testing process, thereby reducing the code coverage rate in the code testing process and affecting the code testing effect. Summary of the invention

[0004] The present application provides a method and device for generating unit test code for anonymous type instances, so as to solve the problems of low code test coverage and poor test effect existing in the existing unit test technology.

[0005] In a first aspect, the present application provides a unit test code generation method applied to an anonymous type instance, the method comprising:

[0006] According to the corresponding data collection method, first data and second data are collected from the first business scenario respectively; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with the method under test and the dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with the execution method corresponding to the anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates the method called by the dependent method after receiving the anonymous type instance as input parameter data;

[0007] Determine a test case template that matches the first data and a method call template that matches the execution method corresponding to the anonymous type instance, and add the method call template to a corresponding position in the test case template to obtain a template to be filled;

[0008] Fill the first data and the second data into the template to be filled, and generate unit test code.

[0009] In a possible implementation, collecting first data from a first business scenario according to a corresponding data collection method includes:

[0010] Determine a data collection scope; wherein the data collection scope indicates a scope including a method under test that generates an anonymous type instance;

[0011] Within the data collection scope, first data is collected from a first business scenario according to aspect-oriented programming (AOP) technology.

[0012] In a possible implementation, determining the data collection scope includes:

[0013] According to the aspect-oriented programming AOP technology, third data is collected from a second business scenario; wherein the second business scenario indicates a real test scenario different from the first business scenario; and the third data represents data associated with a plurality of tested methods;

[0014] Determine a target method in the second business scenario, whose input parameter type is an anonymous instance type; wherein the target method indicates a dependent method;

[0015] The data collection scope is determined based on the scope of the tested method that depends on the target method.

[0016] In a possible implementation, determining the data collection scope based on the scope of the tested method that depends on the target method includes:

[0017] The data collection scope is determined according to the unit test scope of the tested method that depends on the target method.

[0018] In a possible implementation, collecting second data from the first business scenario according to a corresponding data collection method includes:

[0019] Determine the location of the execution method corresponding to the anonymous type instance;

[0020] According to the bytecode enhancement technology, adding data collection code at the location of the execution method corresponding to the anonymous type instance;

[0021] Based on the data collection code, second data is collected from the first business scenario.

[0022] In a possible implementation, determining a method call template that matches an execution method corresponding to the anonymous type instance includes:

[0023] Determine an instance capture template that matches the anonymous type instance, and a method execution template that matches an execution method corresponding to the anonymous type instance;

[0024] The method call template is determined based on the instance capture template and the method execution template.

[0025] In a second aspect, the present application provides a unit test code generation device applied to an anonymous type instance, comprising:

[0026] A collection unit, configured to collect first data and second data from a first business scenario respectively according to a corresponding data collection method; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with a method under test and a dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with an execution method corresponding to an anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates a method called by the dependent method after receiving the anonymous type instance as input parameter data;

[0027] A determination unit, configured to determine a test case template matching the first data and a method call template matching the execution method corresponding to the anonymous type instance, and add the method call template to a corresponding position in the test case template to obtain a template to be filled;

[0028] A generating unit is used to fill the first data and the second data into the template to be filled, and generate a unit test code.

[0029] In a possible implementation, the collection unit includes a first collection unit, which is used to:

[0030] Determine a data collection scope; wherein the data collection scope indicates a scope including a method under test that generates an anonymous type instance;

[0031] Within the data collection scope, first data is collected from a first business scenario according to aspect-oriented programming (AOP) technology.

[0032] In a possible implementation, the first acquisition unit is used to:

[0033] According to the aspect-oriented programming AOP technology, third data is collected from a second business scenario; wherein the second business scenario indicates a real test scenario different from the first business scenario; and the third data represents data associated with a plurality of tested methods;

[0034] Determine a target method in the second business scenario, whose input parameter type is an anonymous instance type; wherein the target method indicates a dependent method;

[0035] The data collection scope is determined based on the scope of the tested method that depends on the target method.

[0036] In a possible implementation, the first acquisition unit is used to:

[0037] The data collection scope is determined according to the unit test scope of the tested method that depends on the target method.

[0038] In a possible implementation, the collection unit includes a second collection unit, which is used to:

[0039] Determine the location of the execution method corresponding to the anonymous type instance;

[0040] According to the bytecode enhancement technology, adding data collection code at the location of the execution method corresponding to the anonymous type instance;

[0041] Based on the data collection code, second data is collected from the first business scenario.

[0042] In a possible implementation manner, the determining unit is configured to:

[0043] Determine an instance capture template that matches the anonymous type instance, and a method execution template that matches an execution method corresponding to the anonymous type instance;

[0044] The method call template is determined based on the instance capture template and the method execution template.

[0045] In a third aspect, an embodiment of the present application provides a computer 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 unit test code generation method applied to anonymous type instances as described in the first aspect and various possible designs of the first aspect.

[0046] 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, a unit test code generation method applied to an anonymous type instance as described in the first aspect and various possible designs of the first aspect is implemented.

[0047] 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 unit test code generation method applied to anonymous type instances as described in the first aspect and various possible designs of the first aspect.

[0048] The unit test code generation method and device provided by the present application for anonymous type instances can collect the first data and the second data from the first business scenario according to the corresponding data collection method, so as to obtain the data associated with the tested method and the dependent method in the first business scenario, and also obtain the data associated with the execution method corresponding to the anonymous type instance generated by the tested method in the first business scenario, so as to obtain the real test data more comprehensively and accurately, expand the scope of data collection, and thus help to improve the comprehensiveness and accuracy of the test cases generated based on the real test data. After that, the test case template matching the first data and the method call template matching the execution method corresponding to the anonymous type instance can be determined, and the method call template is added to the test case template to obtain the template to be filled, so as to ensure that the template to be filled can realize the call of the execution method corresponding to the anonymous type instance. At this time, after the first data and the second data are filled into the template to be filled, the unit test code can be generated, so that the generated unit test code can still execute the internal logic of the dependent method after the dependent method is simulated, thereby improving the code test coverage and the unit test effect. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] 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.

[0050] Figure 1 A schematic diagram of an application scenario of a unit test code generation method provided in an embodiment of the present application;

[0051] Figure 2 A schematic diagram of a flow chart of a method for generating unit test code for an anonymous type instance provided in an embodiment of the present application;

[0052] Figure 3 A flowchart of another method for generating unit test code for anonymous type instances provided in an embodiment of the present application;

[0053] Figure 4 A schematic diagram of an implementation flow of a method for generating unit test code for an anonymous type instance provided in an embodiment of the present application;

[0054] Figure 5 A schematic diagram of the structure of a unit test code generation device applied to an anonymous type instance provided in an embodiment of the present application;

[0055] Figure 6 A schematic diagram of the structure of another unit test code generation device applied to an anonymous type instance provided in an embodiment of the present application;

[0056] Figure 7 A schematic diagram of the structure of a computer device provided in an embodiment of the present application.

[0057] 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

[0058] 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.

[0059] In the technical solution of this application, the collection, storage, use, processing, transmission, provision and disclosure of various types of test data involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.

[0060] It should be noted that in the embodiments of the present application, certain existing solutions in the industry, such as certain frameworks, software, components, models, etc., may be mentioned. 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.

[0061] See also Figure 1 , Figure 1 A schematic diagram of an application scenario of a unit test code generation method provided in an embodiment of the present application, such as Figure 1 As shown, the application scenario at least includes: a data acquisition module, a data processing module, a configuration module and a template filling module.

[0062] Among them, the data collection module is used to collect real test data based on aspect-oriented programming (AOP) technology under the Spring framework.

[0063] The data processing module is used to parse and process the collected real test data to obtain the test data that needs to be filled. For example, when the amount of collected real data is large, the collected real test data can be deduplicated, or the collected real test data can be grouped to obtain multiple groups of test data that can be filled.

[0064] The configuration module is used to determine the basic scope of unit test coverage and generate the corresponding unit test template.

[0065] The template filling module is used to read the unit test template, that is, the template to be filled, and fill the test data to be filled into the unit test template, and then generate the unit test code.

[0066] When testing the unit test code generated according to the above steps, since the unit test code contains the test code after mocking based on Mockito technology, when the input parameter of the mocked object is an anonymous object type, since the mocked object will not execute the input parameter of the anonymous object type like the real object, it is impossible to collect the real test data of the internal instance corresponding to the anonymous function based on the existing aspect-oriented programming AOP technology, resulting in a large amount of uncovered code in the unit test process, thereby reducing the code test coverage and affecting the code test effect.

[0067] In one example, assume that the existing unit test code is used to test multiple methods, for example, the multiple methods may be the tested method, the dependent method, and the execution method called inside the dependent method. At this time, assume that when the tested method is executed, an anonymous type instance is generated, and the anonymous type instance is transmitted to the dependent method as the input parameter data of the dependent method. At this time, it is necessary to execute the execution method matching the anonymous type instance inside the dependent method to obtain the return value of the dependent method.

[0068] However, during the unit test, the dependent methods that the method under test depends on will be simulated, so that the dependent methods are simulated into mock objects. During the unit test, the internal logic of the mock object cannot be executed, and the return value corresponding to the dependent method cannot be accurately obtained, and the execution method contained in the dependent method cannot be tested, which not only affects the code test coverage, but also affects the accuracy of the code test, and thus affects the code test effect.

[0069] In response to the above technical problems, the present application can combine multiple data collection methods to collect more comprehensive and accurate real test data, and generate unit test code for unit testing based on the collected test data, so as to solve the problems of low code test coverage and poor test effect in existing unit testing technology.

[0070] 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.

[0071] See also Figure 2 , Figure 2 The present invention provides a flow chart of a method for generating unit test code for anonymous type instances. Figure 2 As shown, the unit test code generation method applied to the anonymous type instance includes:

[0072] S201. Collect first data and second data from a first business scenario respectively according to a corresponding data collection method.

[0073] The first business scenario indicates a real test scenario. For example, the first business scenario may indicate a scenario in which a test is performed in a business function test environment or an interface test environment.

[0074] The first data indicates data associated with the method under test and the dependent method on which the method under test depends in the first business scenario. For example, the first data may at least include the following data: the type of the method under test, the input data of the method under test, the return value of the method under test, the type of the dependent method, the input parameter data of the dependent method, the return value of the dependent method, etc. Alternatively, the first data may also include data that needs to be configured separately during the execution of the method under test and the dependent method. The specific content included in the first data is not limited here, and it is subject to what can be implemented.

[0075] The second data indicates data associated with the execution method corresponding to the anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates the method called by the dependent method after receiving the anonymous type instance as input parameter data. For example, the second data may include but is not limited to the following data: the type of the execution method, the name of the execution method, the type declaration of the input parameter data of the execution method, the input parameter data of the execution method, the type declaration of the return value of the execution method, and the return value of the execution method.

[0076] In one example, the first data and the second data may also include abnormal data captured according to the corresponding data collection method, etc. The specific content included in the collected first data and second data is not limited here, and is based on meeting actual needs.

[0077] In one example, data collection methods may include but are not limited to the following methods: aspect-oriented programming AOP technology, bytecode enhancement technology, Java reflection technology, etc.

[0078] S202, determine a test case template that matches the first data and a method call template that matches the execution method corresponding to the anonymous type instance, and add the method call template to the corresponding position in the test case template to obtain a template to be filled.

[0079] In one example, a test case template and a corresponding method call template can be generated based on a unit test framework and the collected first data, wherein the unit test framework can be a framework such as JUnit, TestNG, etc. The specific type of the unit test framework is not limited here, and is subject to implementation.

[0080] In one example, a method call template is used to execute a template of an execution method corresponding to an anonymous type instance according to an anonymous type instance received by a dependent function.

[0081] In one example, the method call template may be added to a corresponding position before the execution dependent method in the test case template.

[0082] S203, filling the first data and the second data into the template to be filled, and generating unit test code.

[0083] In one example, before the first data and the second data are filled into the template to be filled, the first data and the second data may be screened. For example, the first data and the second data may be deduplicated to avoid generating duplicate test cases and causing certain waste. Alternatively, the first data and the second data may be expanded to obtain richer test data, thereby making the obtained test cases richer and more diverse.

[0084] It can be seen from the above description that the embodiment of the present application can collect the first data and the second data from the first business scenario according to the corresponding data collection method, so that the data associated with the method under test and the dependent method in the first business scenario can be obtained, and the data associated with the execution method corresponding to the anonymous type instance generated by the method under test in the first business scenario can also be obtained, so that the real test data can be obtained more comprehensively and accurately, and the data collection scope is expanded, which helps to improve the comprehensiveness and accuracy of the test cases generated based on the real test data. After that, the test case template matching the first data and the method call template matching the execution method corresponding to the anonymous type instance can be determined, and the instance capture template and the method call template are added to the test case template to obtain the template to be filled, so that it can be ensured that the template to be filled can realize the call of the execution method corresponding to the anonymous type instance. At this time, after the first data and the second data are filled into the template to be filled, the unit test code can be generated, so that the generated unit test code can still execute the internal logic of the dependent method after the dependent method is simulated, thereby improving the code test coverage and the test effect.

[0085] See also Figure 3 , Figure 3 A flowchart of another method for generating unit test code for anonymous type instances provided in an embodiment of the present application. Figure 3 As shown, the unit test code generation method applied to the anonymous type instance includes:

[0086] S301. Collect third data from the second business scenario according to aspect-oriented programming (AOP) technology.

[0087] The second business scenario indicates a real test scenario different from the first business scenario; and the third data represents data associated with multiple test methods.

[0088] Here, the second business scenario and the first business scenario indicate different real test scenarios. For example, the second business scenario can be understood as the scenario of performing a business test or an interface test for the first time, and the first business scenario can be understood as the scenario of performing a business test or an interface test for the second time.

[0089] At this time, the third data collected in the second business scenario includes data associated with all the tested methods during the execution process.

[0090] S302: Determine a target method in the second business scenario whose input parameter type is an anonymous instance type, wherein the target method indicates a dependent method.

[0091] In one example, when the input parameter type is identified as an anonymous instance type, in a possible implementation, the anonymous instance type can be identified by automatically identifying the characteristics of the anonymous instance type (e.g., specific characters associated with the anonymous type instance, such as the anonymous type instance declaration format, the anonymous type instance assignment character "->", etc.), thereby determining the target method. For example, the anonymous instance type can be automatically identified based on the abstract syntax tree, or the anonymous instance type can be automatically identified based on the bytecode analysis technology, thereby determining the target method. The automatic identification method is not limited here, and is subject to what can be implemented.

[0092] In another possible implementation, the anonymous example type can be identified by manual identification, and then the target method whose input parameter type is the anonymous instance type can be determined. For example, the following scenario can be manually identified to determine the target method: an anonymous type instance is generated by the method under test, and the anonymous type instance is passed to a dependent method on which the method under test depends, and the dependent method triggers execution.

[0093] In an example, if there are many target methods whose input parameter types are anonymous type instances, the relevant information of the target methods whose input parameter types are anonymous type instances can be aggregated first to obtain the aggregated target methods.

[0094] S303: Determine a data collection scope based on a scope of the tested method that depends on the target method.

[0095] The data collection scope indicates the scope of the method under test that generates the anonymous type instance. At this time, the data collection scope can be determined according to the unit test scope of the method under test that depends on the target method. For example, the unit test scope of the method under test that depends on the target method can be the class where the method under test is located, or it can be the package where the method under test is located. The unit test scope of the method under test that depends on the target method is not limited here.

[0096] In another possible implementation, the data collection scope may be determined based on a code coverage report obtained in the second business scenario (e.g., a code coverage report obtained based on Jacoco). For example, the data collection scope may be determined based on code segments not covered in the code coverage report.

[0097] This implementation method can determine the uncovered code segments in the first real test scenario, and determine the data collection scope based on the uncovered code segments, so as to collect data in a targeted manner, which is helpful to further improve the generated unit test code.

[0098] S304: Within the data collection scope, first data is collected from the first business scenario according to aspect-oriented programming (AOP) technology.

[0099] This implementation manner can avoid collecting a lot of irrelevant data by predetermining the data collection range and then collecting the first data based on the determined data collection range. This not only narrows the collection range of the first data and improves the collection efficiency of the first data, but also reduces the data processing volume and improves the efficiency of processing the first data.

[0100] In one example, after collecting the first data from the first business scenario, the first data may be stored for subsequent use. At this time, the first data may be stored according to the data format of each data, so as to ensure accurate use of the first data.

[0101] It should be noted here that, in addition to collecting the first data and the third data according to the aspect-oriented programming AOP technology, the first data and the third data may also be collected according to the Java reflection technology, and the comparison here is not limited.

[0102] S305: Determine the location of the execution method corresponding to the anonymous type instance.

[0103] S306. According to the bytecode enhancement technology, add data collection code at the location of the execution method corresponding to the anonymous type instance.

[0104] S307. Based on the data collection code, collect second data from the first business scenario.

[0105] In one example, after collecting the second data from the first business scenario, the second data may be stored according to the data format corresponding to the collected second data. The data format corresponding to the second data may indicate at least the following information: the data type of the second data, the data type of each sub-data included in the second data, the arrangement order of each sub-data included in the second data, the data value of each sub-data included in the second data, etc.

[0106] In this implementation, bytecode enhancement technology can be used to collect data that cannot be collected by aspect-oriented programming AOP technology, thereby making up for the data collection defects. At the same time, by adding matching data collection code at the corresponding position to collect the second data, data collection can be performed more specifically, avoiding the phenomenon of repeated collection, and improving the collection quality of real test data.

[0107] S308. Determine a test case template that matches the first data, an instance capture template that matches the anonymous type instance, and a method execution template that matches the execution method corresponding to the anonymous type instance, and add the instance capture template and the method execution template to the corresponding positions in the test case template to obtain a template to be filled.

[0108] In one example, the instance capture module indicates the execution process of capturing the anonymous type instance generated by the method under test; the method execution template indicates the execution logic corresponding to the execution method.

[0109] In one example, the location of the dependent method in the test case template can be determined, so that the instance capture template is added to the location of the dependent method in the test case template, so that after the dependent method receives the input parameter data, that is, the anonymous type instance, the anonymous type instance can be captured according to the instance capture template. At this time, the captured anonymous type instance can be saved in the anonymous type container.

[0110] In one example, the embodiment of the present application can add a method execution template after the instance capture template, so that after capturing the anonymous type instance, the execution logic of the execution method called inside the dependent method can be executed according to the method execution template, thereby obtaining a more accurate return value. For example, the method execution template can perform the following process: read the anonymous type instance from the anonymous type container, and determine the method type of the execution method corresponding to the anonymous type instance; then, according to the method type of the execution method, determine the execution logic of the execution method, thereby executing the execution logic and obtaining a return value.

[0111] In one example, the template to be filled may also include a data validation template. For example, the data validation template may be used to validate the return value of the execution method, or the input parameter data of the execution method. Alternatively, the data validation template may also validate the input parameter data and return value of the dependent method (or the method being tested), etc. There is no limitation on the data that can be validated by the data validation template, which is subject to actual needs.

[0112] In one example, the template to be filled may also include a simulation processing template, so that the method on which the method under test depends can be simulated based on the Mockito technology.

[0113] S309: Fill the first data and the second data into the template to be filled, and generate unit test code.

[0114] In one example, after generating the unit test code, the unit test process can be performed on the method under test that generates the anonymous instance type based on the unit test code to obtain the unit test result.

[0115] In specific implementation, the dependent functions included in the unit test code can be simulated based on the simulation processing template to obtain a mock object.

[0116] After that, you can execute the method under test and pass the anonymous type instance generated by the method under test to the mock object.

[0117] Next, the anonymous type instance can be captured according to the instance capture template, and the anonymous type instance can be stored in the anonymous type container.

[0118] Next, the method execution template can read the anonymous type instance from the anonymous type container, and then determine the parent type corresponding to the anonymous type instance (that is, the type of the execution method) and the data type of the input parameter data of the execution method, thereby determining the execution logic of the anonymous type instance and executing it to obtain the return value of the execution method.

[0119] After that, based on the return value of the executed method, the return value of the dependent method is determined to obtain the unit test result.

[0120] This implementation method can determine the type of the execution method by identifying the type of the anonymous type instance, thereby determining the execution logic of the execution method, thereby realizing the execution of the method called inside the mock object, and improving the code test coverage and test effect.

[0121] The following is an introduction to the implementation process of the unit test code generation method for anonymous type instances provided in an embodiment of the present application through a specific flow chart.

[0122] See also Figure 4 , Figure 4 A schematic diagram of an implementation flow of a method for generating unit test code for an anonymous type instance provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, the third data can be collected from the second business scenario based on the aspect-oriented programming AOP technology. Then, based on the third data, the following scenario is determined: the method under test generates an anonymous type instance, and passes the anonymous type instance to the dependent method, thereby determining the target method whose input parameter data is the anonymous type instance. After that, the data collection scope can be determined based on the method under test that depends on the target method.

[0123] Afterwards, the first data can be collected from the first business scenario according to the aspect-oriented programming AOP technology (or Java reflection technology) within the data collection scope, and the second data can be collected from the first business scenario according to the bytecode enhancement technology.

[0124] It should be noted here that in the process of collecting data, exceptions can also be captured, and after the exceptions are captured, the exception data is recorded.

[0125] Next, a test case template matching the first data can be generated according to the unit test framework, and an instance capture template matching the anonymous type instance and a method execution template matching the execution method corresponding to the anonymous type instance can be determined. Then, the instance capture template and the method execution template are added to the corresponding positions in the test case template to obtain a template to be filled.

[0126] Finally, the first data and the second data are filled into the template to be filled to obtain the unit test code. At this point, after receiving the initiated service test request, the unit test process can be performed according to the generated unit test code.

[0127] See also Figure 5 , Figure 5 A schematic diagram of the structure of a unit test code generation device for an anonymous type instance provided in an embodiment of the present application. Figure 5 As shown, the unit test code generation device 500 applied to an anonymous type instance includes:

[0128] The collection unit 501 is used to collect first data and second data from the first business scenario according to the corresponding data collection method; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with the method under test and the dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with the execution method corresponding to the anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates the method called by the dependent method after receiving the anonymous type instance as input parameter data.

[0129] The determination unit 502 is used to determine the test case template that matches the first data and the method call template that matches the execution method corresponding to the anonymous type instance, and add the method call template to the corresponding position in the test case template to obtain a template to be filled.

[0130] The generating unit 503 is used to fill the first data and the second data into the template to be filled, and generate the unit test code.

[0131] See also Figure 6 , Figure 6 A schematic diagram of the structure of another unit test code generation device for anonymous type instances provided in an embodiment of the present application. Figure 6 As shown, the unit test code generation device 600 applied to the anonymous type instance includes:

[0132] The collection unit 601 is used to collect first data and second data from a first business scenario according to a corresponding data collection method; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with the method under test and the dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with the execution method corresponding to the anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates the method called by the dependent method after receiving the anonymous type instance as input parameter data.

[0133] A determination unit 602 is used to determine a test case template that matches the first data, an instance capture template that matches the anonymous type instance, and a method execution template that matches the execution method corresponding to the anonymous type instance, and add the instance capture template and the method execution template to corresponding positions in the test case template to obtain a template to be filled.

[0134] The generating unit 603 is used to fill the first data and the second data into the template to be filled, and generate the unit test code.

[0135] In a possible implementation, the collection unit 601 includes a first collection unit 6011, which is used to:

[0136] Determine a data collection scope; wherein the data collection scope indicates a scope including a method under test that generates an anonymous type instance;

[0137] Within the data collection scope, first data is collected from the first business scenario according to aspect-oriented programming (AOP) technology.

[0138] In a possible implementation, the first acquisition unit 6011 is used to:

[0139] According to the aspect-oriented programming AOP technology, third data is collected from the second business scenario; wherein the second business scenario indicates a real test scenario different from the first business scenario; and the third data represents data associated with a plurality of tested methods;

[0140] Determine a target method in the second business scenario, where the input parameter type is an anonymous instance type; wherein the target method indicates a dependent method;

[0141] Determine the scope of data collection based on the scope of the method under test that depends on the target method.

[0142] In a possible implementation, the first acquisition unit 6011 is used to:

[0143] Determine the data collection scope based on the unit test scope of the tested method that depends on the target method.

[0144] In a possible implementation, the collection unit 601 includes a second collection unit 6012, which is used to:

[0145] Determine the location of the execution method corresponding to the anonymous type instance;

[0146] According to the bytecode enhancement technology, data collection code is added at the location of the execution method corresponding to the anonymous type instance;

[0147] Based on the data collection code, second data is collected from the first business scenario.

[0148] In a possible implementation, the determining unit 602 is configured to:

[0149] Determine an instance capture template that matches the anonymous type instance and a method execution template that matches the execution method corresponding to the anonymous type instance;

[0150] A method call template is determined based on the instance capture template and the method execution template.

[0151] The unit test code generation device applied to anonymous type instances provided in the embodiments of the present application can be used to execute the technical solution of the unit test code generation method applied to anonymous type instances in the above embodiments. Its implementation principle and technical effects are similar and will not be repeated here.

[0152] 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 physically separated. Moreover, these modules can be all implemented in the form of software calling through processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software calling through processing elements, and some modules can be implemented in the form of hardware.

[0153] join Figure 7 , Figure 7 This is a schematic diagram of the structure of a computer device provided in an embodiment of the present application. Figure 7 As shown, the computer device may include: a transceiver 701 , a processor 702 , and a memory 703 .

[0154] The memory 703 stores computer-executable instructions; the processor 702 executes the computer-executable instructions stored in the memory, so that the processor 702 executes the solution in the above embodiment. The processor 702 can be a general-purpose processor, including a central processing unit CPU, a 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.

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

[0156] The transceiver 701 may be used to obtain tasks to be executed and configuration information of the tasks to be executed.

[0157] 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.

[0158] The computer device provided in the embodiment of the present application may be the terminal device of the above-mentioned embodiment.

[0159] An embodiment of the present application also provides a computer-readable storage medium, which stores computer execution instructions. When the computer execution instructions are executed on a computer, the computer executes the technical solution of the unit test code generation method applied to anonymous type instances in the above embodiment.

[0160] 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 applied to anonymous type instances in the above embodiment.

[0161] 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.

[0162] 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.

[0163] 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.

[0164] 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.

[0165] 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.

[0166] 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.

[0167] 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.

[0168] 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.

[0169] 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.

[0170] 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.

[0171] 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 unit test code generation method for anonymous type instances, It is characterized in that The method comprises: According to the corresponding data collection method, first data and second data are collected from the first business scenario respectively; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with the method under test and the dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with the execution method corresponding to the anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates the method called by the dependent method after receiving the anonymous type instance as input parameter data; Determine a test case template that matches the first data and a method call template that matches the execution method corresponding to the anonymous type instance, and add the method call template to a corresponding position in the test case template to obtain a template to be filled; Fill the first data and the second data into the template to be filled, and generate unit test code.

2. The unit test code generation method according to claim 1, It is characterized in that Collecting first data from the first business scenario according to the corresponding data collection method includes: Determine a data collection scope; wherein the data collection scope indicates a scope including a method under test that generates an anonymous type instance; Within the data collection scope, first data is collected from a first business scenario according to aspect-oriented programming (AOP) technology.

3. The unit test code generation method according to claim 2, It is characterized in that Determine the scope of data collection, including: According to the aspect-oriented programming AOP technology, third data is collected from a second business scenario; wherein the second business scenario indicates a real test scenario different from the first business scenario; and the third data represents data associated with a plurality of tested methods; Determine a target method in the second business scenario, whose input parameter type is an anonymous instance type; wherein the target method indicates a dependent method; The data collection scope is determined based on the scope of the tested method that depends on the target method.

4. The unit test code generation method according to claim 3, It is characterized in that Determining the data collection scope based on the scope of the tested method that depends on the target method includes: The data collection scope is determined according to the unit test scope of the tested method that depends on the target method.

5. The unit test code generation method according to claim 1, It is characterized in that Collecting second data from the first business scenario according to the corresponding data collection method includes: Determine the location of the execution method corresponding to the anonymous type instance; According to the bytecode enhancement technology, adding data collection code at the location of the execution method corresponding to the anonymous type instance; Based on the data collection code, second data is collected from the first business scenario.

6. The unit test code generation method according to any one of claims 1 to 5, It is characterized in that Determining a method call template that matches the execution method corresponding to the anonymous type instance, including: Determine an instance capture template that matches the anonymous type instance, and a method execution template that matches an execution method corresponding to the anonymous type instance; The method call template is determined based on the instance capture template and the method execution template.

7. A unit test code generation device applied to anonymous type instances, It is characterized in that The device comprises: A collection unit, configured to collect first data and second data from a first business scenario respectively according to a corresponding data collection method; wherein the first business scenario indicates a real test scenario; the first data indicates data associated with a method under test and a dependent method on which the method under test depends in the first business scenario; the second data indicates data associated with an execution method corresponding to an anonymous type instance generated by the method under test; the execution method corresponding to the anonymous type instance indicates a method called by the dependent method after receiving the anonymous type instance as input parameter data; A determination unit, configured to determine a test case template matching the first data and a method call template matching the execution method corresponding to the anonymous type instance, and add the method call template to a corresponding position in the test case template to obtain a template to be filled; A generating unit is used to fill the first data and the second data into the template to be filled, and generate a unit test code.

8. A computer device, It is 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, It is 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, It is 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

  • Automatic interface case generation method and system

    CN114020630A

  • Unit test code generation method and device

    CN116107908A

  • Test code generation method and device, equipment and storage medium

    CN117349155A

  • Apparatus and method for unit test of code

    KR1020160044305A

  • Optimizing code generation system with input data integration

    US20100218165A1