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

By collecting and generating unit test code, the problem of low code test coverage for anonymous type instances was solved, achieving more comprehensive data collection and more accurate test results, thus improving the coverage and effectiveness of unit tests.

CN118916282BActive Publication Date: 2025-11-21CCB FINTECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410973547.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2025-11-21
Estimated Expiration
2044-07-19

AI Technical Summary

Technical Problem

Existing unit testing techniques suffer from low code test coverage and poor test performance for anonymous type instances. They fail to effectively collect real test data for anonymous object types, resulting in low code test coverage and impacting test effectiveness.

Method used

By collecting data associated with the tested method and its dependent methods in the first business scenario, as well as data associated with the execution methods corresponding to the anonymous type instances generated by the tested method, and combining aspect-oriented programming (AOP) technology and bytecode enhancement technology, unit test code is generated to ensure the comprehensiveness and accuracy of test cases.

Benefits of technology

It improves code test coverage, enhances unit test effectiveness, ensures that the generated unit test code can execute the internal logic of dependent methods, and strengthens the comprehensiveness and accuracy of testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916282B_ABST
    Figure CN118916282B_ABST
Patent Text Reader

Abstract

The application provides a unit test code generation method and device applied to an anonymous type instance. It relates to the technical field of big data. The method comprises the following steps: collecting first data and second data from a first business scene according to corresponding data collection modes; determining a test case template matched with the first data and a method call template matched with an execution method corresponding to the anonymous type instance, and adding the method call 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. The method of the application improves the code test coverage and enhances the test effect.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of big data technology, and in particular to a method and apparatus for generating unit test code for anonymous type instances. Background Technology

[0002] With the development of unit testing technology and unit test code generation assistance technology, one possible implementation is to generate unit test code based on database tables and standard templates. For example, test data can be collected using aspect-oriented programming (AOP) technology under the Spring framework, and automated unit test cases can be written based on the collected test data to generate unit test code, which can then be used for unit testing.

[0003] When performing unit tests based on unit test code, it's necessary to isolate the code under test using Mockito technology. However, when the input parameter of the mocked object is an anonymous object type, the mocked object doesn't execute the anonymous object type input parameter like a real object. Therefore, it's impossible to use existing AOP (Aspect-Oriented Programming) techniques to collect real test data for the internal instances of anonymous functions. This results in a large amount of uncovered code during unit testing, reducing code coverage and impacting the effectiveness of code testing. Summary of the Invention

[0004] This application provides a method and apparatus for generating unit test code for anonymous type instances, in order to solve the problems of low code test coverage and poor test results in existing unit testing technologies.

[0005] Firstly, this application provides a method for generating unit test code for anonymous type instances, 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 the actual test scenario; the first data indicates the data associated with the method under test and the dependent methods on which the method under test depends in the first business scenario; the second data indicates the 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 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 the template to be filled;

[0008] The first data and the second data are used to fill the template to be filled, thereby generating unit test code.

[0009] In one possible implementation, first data is collected from the first business scenario according to the corresponding data collection method, including:

[0010] Determine the data collection scope; wherein, the data collection scope indicates the range in which the tested method that generates anonymous type instances is located;

[0011] Within the data acquisition range, first data is acquired from the first business scenario using Aspect-Oriented Programming (AOP) technology.

[0012] In one possible implementation, determining the data collection range 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; the third data represents data associated with multiple tested methods;

[0014] In the second business scenario, identify the target method whose input parameter type is anonymous instance; wherein, the target method indicates the dependent method;

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

[0016] In one possible implementation, determining the data acquisition range based on the range of the tested method that depends on the target method includes:

[0017] The data collection range is determined based on the unit test scope of the method under test that depends on the target method.

[0018] In one possible implementation, second data is collected from the first business scenario according to the corresponding data collection method, including:

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

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

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

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

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

[0024] Based on the instance capture template and the method execution template, the method call template is determined.

[0025] Secondly, this application provides a unit test code generation apparatus for anonymous type instances, comprising:

[0026] The data acquisition unit is used to acquire first data and second data from a first business scenario according to a corresponding data acquisition 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 methods 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;

[0027] The determining unit is used to 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 the template to be filled.

[0028] A generation unit is used to fill the template to be filled with the first data and the second data to generate unit test code.

[0029] In one possible implementation, the acquisition unit includes a first acquisition unit, used for:

[0030] Determine the data collection scope; wherein, the data collection scope indicates the range in which the tested method that generates anonymous type instances is located;

[0031] Within the data acquisition range, first data is acquired from the first business scenario using Aspect-Oriented Programming (AOP) technology.

[0032] In one possible implementation, the first acquisition unit is used for:

[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; the third data represents data associated with multiple tested methods;

[0034] In the second business scenario, identify the target method whose input parameter type is anonymous instance; wherein, the target method indicates the dependent method;

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

[0036] In one possible implementation, the first acquisition unit is used for:

[0037] The data collection range is determined based on the unit test scope of the method under test that depends on the target method.

[0038] In one possible implementation, the acquisition unit includes a second acquisition unit for:

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

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

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

[0042] In one possible implementation, the determining unit is used for:

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

[0044] Based on the instance capture template and the method execution template, the method call template is determined.

[0045] Thirdly, embodiments of this application provide a computer device, including: 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, causing the at least one processor to execute the unit test code generation method for anonymous type instances as described in the first aspect and various possible designs of the first aspect.

[0046] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions. When a processor executes the computer-executable instructions, it implements the unit test code generation method for anonymous type instances as described in the first aspect and various possible designs of the first aspect.

[0047] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the unit test code generation method for 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 apparatus for anonymous type instances provided in this application can collect first data and second data from a first business scenario according to the corresponding data collection method. This allows for the acquisition of data associated with the tested method and dependent methods in the first business scenario, as well as data associated with the execution method corresponding to the anonymous type instance generated by the tested method. This enables more comprehensive and accurate acquisition of real test data, expanding the data collection scope and thus improving the comprehensiveness and accuracy of test cases generated based on this real test data. Subsequently, a test case template matching the first data and a method call template matching the execution method corresponding to the anonymous type instance can be determined and added to the test case template to obtain a template to be filled. This ensures that the template to be filled can call the execution method corresponding to the anonymous type instance. After filling the template with the first and second data, unit test code can be generated. This allows the generated unit test code to execute the internal logic of the dependent methods even after the dependent methods have been simulated, thereby improving code test coverage and enhancing unit test effectiveness. Attached Figure Description

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

[0050] Figure 1 This is a schematic diagram illustrating an application scenario of a unit test code generation method provided in an embodiment of this application;

[0051] Figure 2 A flowchart illustrating a unit test code generation method for anonymous type instances provided in an embodiment of this application;

[0052] Figure 3 A flowchart illustrating another unit test code generation method for anonymous type instances provided in this application embodiment;

[0053] Figure 4 A schematic diagram illustrating the implementation process of a unit test code generation method for anonymous type instances provided in this application embodiment;

[0054] Figure 5 A schematic diagram of a unit test code generation device for anonymous type instances provided in an embodiment of this application;

[0055] Figure 6 A schematic diagram of another unit test code generation device for anonymous type instances provided in this application embodiment;

[0056] Figure 7 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application.

[0057] The accompanying drawings have illustrated specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to specific embodiments. Detailed Implementation

[0058] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0059] The technical solutions of this application involve the collection, storage, use, processing, transmission, provision, and disclosure of various types of test data, all of which comply with relevant laws and regulations and do not violate public order and good morals.

[0060] It should be noted that in the embodiments of this application, certain existing solutions in the industry, such as frameworks, software, components, and models, may be mentioned. These should be considered as exemplary and are intended only to illustrate the feasibility of implementing the technical solution of this application. However, they do not mean that the applicant has used or necessarily used the solution.

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

[0062] The data acquisition 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 there is a large amount of collected real test data, the collected real test data can be deduplicated, or the collected real test data can be grouped to obtain multiple sets of test data that can be filled.

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

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

[0066] When testing the unit test code generated according to the above steps, because this unit test code includes test code that has been mocked using Mockito technology, when the input parameter of the mocked object is an anonymous object type, the mocked object does not execute the input parameter of the anonymous object type like a real object. Therefore, it is impossible to collect real test data of the internal instance corresponding to the anonymous function based on existing aspect-oriented programming (AOP) technology. This results in a large amount of uncovered code during the unit test process, thereby reducing code test coverage and affecting the code test effect.

[0067] In one example, suppose the existing unit test code is used to test multiple methods, such as the method under test, dependent methods, and execution methods called internally by the dependent methods. Now, suppose that when the method under test executes, it generates an anonymous type instance and transmits this anonymous type instance as an input parameter to the dependent method. Then, the execution method matching the anonymous type instance needs to be executed internally within the dependent method to obtain the return value of the dependent method.

[0068] However, during unit testing, the dependent methods of the method under test are mocked, turning them into mock objects. During unit testing, the internal logic of these mock objects cannot be executed, making it impossible to accurately obtain the return values ​​of the dependent methods or test the execution methods contained within them. This not only affects code test coverage but also the accuracy of code tests, ultimately impacting the effectiveness of the code tests.

[0069] To address the aforementioned technical issues, this application combines multiple data collection methods to collect more comprehensive and accurate real test data. Based on the collected test data, unit test code is generated for unit testing, thereby solving the problems of low code test coverage and poor test results in existing unit testing technologies.

[0070] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.

[0071] See Figure 2 , Figure 2 This is a flowchart illustrating a unit test code generation method for anonymous type instances provided in an embodiment of this application. Figure 2 As shown, this method for generating unit test code for anonymous type instances includes:

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

[0073] The first business scenario refers to a real-world testing scenario. For example, the first business scenario could refer to a scenario in which tests are performed in a business function testing environment or an interface testing environment.

[0074] The first data refers to the data associated with the method under test and its dependent methods in the first business scenario. For example, the first data may include at least the following: 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 methods, the input parameter data of the dependent methods, and the return value of the dependent methods. Alternatively, the first data may also include data that the method under test and its dependent methods need to be configured separately during execution. The specific content of the first data is not limited here, but is subject to what is feasible.

[0075] The second data indicates the 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 invoked 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: 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, etc.

[0076] In one example, the first and second data may also include abnormal data captured according to the corresponding data collection method. The specific content of the collected first and second data is not limited here, and shall be determined according to actual needs.

[0077] In one example, data acquisition methods may include, but are not limited to, the following: Aspect-Oriented Programming (AOP) technology, bytecode enhancement technology, Java reflection technology, etc.

[0078] S202. 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 the template to be filled.

[0079] In one example, test case templates and corresponding method call templates can be generated based on the unit testing framework and the first data collected. The unit testing framework can be a framework such as JUnit or TestNG. The specific type of unit testing framework is not limited here, as long as it can be implemented.

[0080] In one example, a method invocation template is used to execute the template corresponding to the anonymous type instance based on the anonymous type instance received by the dependent function.

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

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

[0083] In one example, before filling the template with the first and second data, the first and second data can be filtered. For instance, duplicate data can be removed to avoid generating repetitive test cases and causing waste. Alternatively, the first and second data can be expanded to obtain richer test data, thus making the resulting test cases more diverse.

[0084] As described above, this application embodiment can collect first data and second data from the first business scenario according to the corresponding data collection method. This allows for the acquisition of data associated with the tested method and dependent methods in the first business scenario, as well as data associated with the execution method corresponding to the anonymous type instance generated by the tested method. This enables more comprehensive and accurate acquisition of real test data, expanding the data collection scope and improving the comprehensiveness and accuracy of test cases generated based on the real test data. Subsequently, a test case template matching the first data and a method call template matching the execution method corresponding to the anonymous type instance can be determined. The instance capture template and method call template are added to the test case template to obtain the template to be filled. This ensures that the template to be filled can call the execution method corresponding to the anonymous type instance. After filling the template with the first and second data, unit test code can be generated. This allows the generated unit test code to execute the internal logic of the dependent method even after the dependent method has been simulated, thereby improving code test coverage and test effectiveness.

[0085] See Figure 3 , Figure 3 This is a flowchart illustrating another method for generating unit test code for anonymous type instances, provided as an embodiment of this application. Figure 3 As shown, this method for generating unit test code for anonymous type instances includes:

[0086] S301. Based on Aspect-Oriented Programming (AOP) technology, collect third data from the second business scenario.

[0087] Among them, the second business scenario indicates a real test scenario that is different from the first business scenario; the third data represents data associated with multiple test methods.

[0088] Here, the second business scenario and the first business scenario refer to different real-world test scenarios. For example, the second business scenario can be understood as the scenario in which business testing or interface testing is performed for the first time, while the first business scenario can be understood as the scenario in which business testing or interface testing is performed for the second time.

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

[0090] S302. Determine the target method in the second business scenario whose input parameter type is anonymous instance. The target method indicates the dependent method.

[0091] In one example, when identifying an input parameter type as an anonymous instance type, one possible implementation is to automatically identify the anonymous instance type by recognizing its characteristics (e.g., specific characters associated with anonymous instance types, such as the anonymous instance declaration format, the anonymous instance assignment character "->"), and thus determine the target method. For example, the anonymous instance type can be automatically identified based on the abstract syntax tree, or it can be automatically identified based on bytecode analysis techniques. The method of automatic identification is not limited here; the key is what can be implemented.

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

[0093] In one example, if there are many target methods whose input parameter type is anonymous type instance, the relevant information of the target methods whose input parameter type is anonymous type instance can be summarized first to obtain the summarized target methods.

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

[0095] The data collection scope indicates the scope of the tested methods that generate anonymous type instances. In this case, the data collection scope can be determined based on the unit test scope of the tested methods that depend on the target method. For example, the unit test scope of the tested methods that depend on the target method can be the class in which the tested method is located, or it can be the package in which the tested method is located; there is no limitation on the unit test scope of the tested methods that depend on the target method.

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

[0097] This implementation method can identify uncovered code segments in the first real-world test scenario and determine the data collection scope based on these uncovered code segments, thereby collecting data in a targeted manner and helping to further improve the generated unit test code.

[0098] S304. Within the data acquisition range, collect the first data from the first business scenario using Aspect-Oriented Programming (AOP) technology.

[0099] This implementation method can avoid collecting too much irrelevant data by pre-determining the data collection range and then collecting the first data according to 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 amount of data processing 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 can be stored for later use. At this point, it can be stored according to the data format of each piece of first data, thus ensuring accurate use of the first data.

[0101] It should be noted that, in addition to collecting the first and third data using AOP (Aspect-Oriented Programming) technology, the first and third data can also be collected using Java reflection technology. This comparison is not limited.

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

[0103] S306. Based on 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 acquisition code, collect the second data from the first business scenario.

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

[0106] In this implementation, bytecode enhancement technology can be used to collect data that cannot be collected by aspect-oriented programming (AOP) technology, thus compensating for data collection deficiencies. Simultaneously, by adding matching data collection code at corresponding locations to collect secondary data, data collection can be more targeted, avoiding duplicate collection and improving the quality of real test data collection.

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

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

[0109] In one example, the location of the dependent method in the test case template can be determined. The instance capture template can then be added to that location within the test case template. This allows the dependent method to capture the anonymous type instance based on the instance capture template after receiving the input parameter data (i.e., the anonymous type instance). The captured anonymous type instance can then be saved to an anonymous type container.

[0110] In one example, this embodiment of the application can add a method execution template after the instance capture template. This allows the execution logic of the methods called within the dependent methods to be executed according to the method execution template after capturing anonymous type instances, thus obtaining a more accurate return value. For example, the method execution template can perform the following process: read anonymous type instances from the anonymous type container and determine the method type of the execution method corresponding to the anonymous type instance; then, based on the method type of the execution method, determine the execution logic of the execution method, execute the execution logic, and obtain a return value.

[0111] In one example, the template to be populated may also contain a data validation template. For instance, this data validation template can be used to validate the return value of the executed method, or it can validate the input parameters of the executed method. Alternatively, the data validation template can also validate the input parameters and return values ​​of dependent methods (or the method under test). Here, there is no limitation on the data that the data validation template can validate, as long as it meets the actual needs.

[0112] In one example, the template to be filled may also include a mock-up template, which allows the methods relied upon by the method under test to be mocked based on the Mockito technique.

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

[0114] In one example, after generating unit test code, you can also perform unit test processing on the tested method that generates an anonymous instance type based on the unit test code to obtain the unit test results.

[0115] In practice, you can first use a mocking template to simulate the dependency functions contained in the unit test code to obtain mock objects.

[0116] After that, the method under test can be executed, and the anonymous type instance generated by the method under test can be passed to the mock object.

[0117] Next, based on the instance capture template, anonymous type instances can be captured and stored in the anonymous type container.

[0118] Next, the method execution template can read the anonymous type instance from the anonymous type container, then determine the parent type (i.e. the type of the method to be executed) of the anonymous type instance and the data type of the input parameters of the method to be executed, thereby determining the execution logic of the anonymous type instance, executing it, and obtaining the return value of the method to be executed.

[0119] Then, based on the return value of the executed method, the return values ​​of the dependent methods are determined, thus obtaining the unit test results.

[0120] This implementation method can determine the type of the method to be executed by identifying the type of the anonymous type instance, thereby determining the execution logic of the method to be executed, thus realizing the execution of the methods called internally by the mock object, improving code test coverage and test effectiveness.

[0121] The following is a detailed flowchart illustrating the implementation process of the unit test code generation method for anonymous type instances provided in this application.

[0122] See Figure 4 , Figure 4 This application provides an implementation flowchart of a unit test code generation method for anonymous type instances, as illustrated in the embodiments of this application. Figure 4 As shown, third data can be collected from the second business scenario first, using 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 this anonymous type instance to the dependent method, thus determining the target method whose input parameter data is the anonymous type instance. Afterwards, the data collection scope can be determined based on the method under test that depends on this target method.

[0123] Then, within the data acquisition range, first data can be collected from the first business scenario using aspect-oriented programming (AOP) technology (or Java reflection technology), and second data can be collected from the first business scenario using bytecode enhancement technology.

[0124] It should be noted that during the data collection process, anomalies can also be captured, and the anomaly data can be recorded after an anomaly is captured.

[0125] Next, based on the unit testing framework, test case templates matching the first data can be generated, and instance capture templates matching anonymous type instances and method execution templates matching the execution methods corresponding to anonymous type instances can be determined. Then, the instance capture templates and method execution templates are added to the corresponding positions in the test case templates to obtain the templates to be populated.

[0126] Finally, the first and second data are filled into the template to be filled, resulting in the unit test code. At this point, after receiving an initiated business test request, unit testing can be performed based on the generated unit test code.

[0127] See Figure 5 , Figure 5 This is a schematic diagram of a unit test code generation device for anonymous type instances provided in an embodiment of this application. Figure 5 As shown, the unit test code generation device 500 applied to anonymous type instances includes:

[0128] The data acquisition unit 501 is used to acquire first data and second data from the first business scenario according to the corresponding data acquisition method; wherein, the first business scenario indicates the actual test scenario; the first data indicates the data associated with the method under test and the dependent methods on which the method under test depends in the first business scenario; the second data indicates the 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 determining 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 the template to be filled.

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

[0131] See Figure 6 , Figure 6 This is a schematic diagram of another unit test code generation device for anonymous type instances provided in an embodiment of this application. Figure 6 As shown, the unit test code generation device 600 applied to anonymous type instances includes:

[0132] The data acquisition unit 601 is used to acquire first data and second data from the first business scenario according to the corresponding data acquisition method; wherein, the first business scenario indicates the actual test scenario; the first data indicates the data associated with the method under test and the dependent methods on which the method under test depends in the first business scenario; the second data indicates the 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] The determining unit 602 is used to determine the test case template that matches the first data, the instance capture template that matches the anonymous type instance, and the 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 the template to be filled.

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

[0135] In one possible implementation, the acquisition unit 601 includes a first acquisition unit 6011, used for:

[0136] Determine the data collection scope; whereby the data collection scope indicates the range of the tested method that generates anonymous type instances;

[0137] Within the data collection scope, the first data is collected from the first business scenario using Aspect-Oriented Programming (AOP) technology.

[0138] In one possible implementation, the first acquisition unit 6011 is used for:

[0139] Based on Aspect-Oriented Programming (AOP) technology, third data is collected from the second business scenario; wherein, the second business scenario indicates a real test scenario that is different from the first business scenario; the third data represents data associated with multiple tested methods;

[0140] In the second business scenario, identify the target method whose input parameter type is anonymous instance; where the target method indicates the dependent method.

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

[0142] In one possible implementation, the first acquisition unit 6011 is used for:

[0143] The data collection scope is determined based on the unit test scope of the method under test that depends on the target method.

[0144] In one possible implementation, the acquisition unit 601 includes a second acquisition unit 6012, used for:

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

[0146] Based on 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 acquisition code, second data is collected from the first business scenario.

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

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

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

[0151] The unit test code generation apparatus for anonymous type instances provided in this application embodiment can be used to execute the technical solution of the unit test code generation method for anonymous type instances in the above embodiment. Its implementation principle and technical effect are similar, and will not be described again here.

[0152] It should be noted that the division of the various modules in the above device is merely a logical functional division. In actual implementation, they can be fully or partially integrated into a single physical entity, or they can be physically separated. Furthermore, these modules can be implemented entirely in software through processing element calls; they can be implemented entirely in hardware; or some modules can be implemented by processing element calls to software, while others are implemented in hardware.

[0153] join Figure 7 , Figure 7 This is a schematic diagram of the structure of a computer device provided in an embodiment of this 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, causing the processor 702 to execute the scheme in the above embodiments. 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 gate or transistor logic devices, or discrete hardware components.

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

[0156] Transceiver 701 can be used to obtain the task to be run and its configuration information.

[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 address bus, data bus, control bus, etc. For ease of representation, only one thick line is used in the diagram, but this does not indicate that there is only one bus or one type of bus. Transceivers are used to enable communication between database access devices and other computers (e.g., clients, read-write libraries, and read-only libraries). Memory may include random access memory (RAM) and may also include non-volatile memory.

[0158] The computer device provided in this application embodiment can be the terminal device described in the above embodiments.

[0159] This application also provides a computer-readable storage medium storing computer-executable instructions. When the computer-executable instructions are executed on a computer, the computer executes the technical solution of the unit test code generation method for anonymous type instances described above.

[0160] This 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 the 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 embodiments.

[0161] In the several embodiments provided in this 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 merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or modules, and may be electrical, mechanical, or other forms.

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

[0163] Furthermore, the functional modules in the various embodiments of this 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 unit composed of the above modules can be implemented in hardware or in the form of hardware plus software functional units.

[0164] The integrated modules described above, implemented as software functional modules, can be stored in a computer-readable storage medium. These software functional modules, stored in a storage medium, include several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute some steps of the methods of the various embodiments of this application.

[0165] It should be understood that the aforementioned processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. A general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly manifested as being executed by a hardware processor, or executed by a combination of hardware and software modules within the processor.

[0166] The memory may include high-speed RAM, and may also include non-volatile storage (NVM), such as at least one disk storage device, and may also be a USB flash drive, external hard drive, read-only memory, disk or optical disc, etc.

[0167] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.

[0168] The aforementioned storage medium can be implemented from 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 storage, flash memory, magnetic disk, or optical disk. The storage medium can be any available medium accessible to general-purpose or special-purpose computers.

[0169] An exemplary storage medium is coupled to a processor, enabling the processor to read information from and write information to the storage medium. Alternatively, the storage medium can be an integral part of the processor. The processor and storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and storage medium can exist as discrete components in an electronic control unit or main control device.

[0170] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.

[0171] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.

Claims

1. A method for generating unit test code for anonymous type instances, characterized in that, The method includes: 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 the actual test scenario; the first data indicates the data associated with the method under test and the dependent methods on which the method under test depends in the first business scenario; the second data indicates the 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 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 the template to be filled; The first data and the second data are used to fill the template to be filled, thereby generating unit test code.

2. The unit test code generation method according to claim 1, characterized in that, Based on the corresponding data collection method, collect the first data from the first business scenario, including: Determine the data collection scope; wherein, the data collection scope indicates the range in which the tested method that generates anonymous type instances is located; Within the data acquisition range, first data is acquired from the first business scenario using Aspect-Oriented Programming (AOP) technology.

3. The unit test code generation method according to claim 2, 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; the third data represents data associated with multiple tested methods; In the second business scenario, identify the target method whose input parameter type is anonymous instance; wherein, the target method indicates the dependent method; The data collection range is determined based on the range of the tested method that depends on the target method.

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

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

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

7. A unit test code generation device for anonymous type instances, characterized in that, The device includes: The data acquisition unit is used to acquire first data and second data from a first business scenario according to a corresponding data acquisition 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 methods 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; The determining unit is used to 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 the template to be filled. A generation unit is used to fill the template to be filled with the first data and the second data to generate unit test code.

8. A computer device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1-6.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1-6.

10. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1-6.

Citation Information

Patent Citations

  • Apparatus and method for unit test of code

    KR1020160044305A

  • Optimizing code generation system with input data integration

    US20100218165A1