A unit test code generation method and apparatus
Patent Information
- Application Number
- CN202310268282.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-15
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2043-03-15
AI Technical Summary
但与此同时,相关技术提供的单元测试代码生成工具中,生成单元测试代码的参数都是默认值,在执行单元测试之前仍然需要编码人员修改代码,导致存在单元测试代码的可维护性能较低,单元测试效果较差,单元测试代码的生成效率较低,用户体验差等技术问题
[0034]上述发明中的一个实施例具有如下优点或有益效果:因为采用响应于用户发起的单元测试代码生成请求,确定生成请求指示的单元测试对象,获取单元测试对象对应的测试参数的数据类型;其中,单元测试对象包括Java的类或方法;接收用户在单元测试配置页面输入的参数配置信息,根据参数配置信息、单元测试对象和数据类型配置单元测试所对应的单元测试参数;其中,参数配置信息中指示了单元测试对象对应的外部方法调用信息;根据单元测试参数对单元测试模板进行参数填充和模板渲染,得到程序结构接口对象,根据程序结构接口技术对程序接口对象进行解析处理,得到待生成的单元测试代码对应的Java对象,并根据Java对象反向生成单元测试代码的技术手段,所以克服了相关技术中,由于生成单元测试代码的参数都是默认值,在执行单元测试之前仍然需要编码人员修改代码,导致单元测试代码的可维护性能较低,单元测试效果较差,单元测试代码的生成效率较低,用户体验差的技术问题,进而达到能够基于直接根据用户指示的单元测试对象,在可视化的单元配置页面中配置相应的单元测试参数,再结合单元测试模板反向生成单元测试代码,提高单元测试代码的生成效率,提升单元测试代码的可维护性能,提高单元测试代码生成的自动化程度,提升单元测试效果和用户体验的技术效果。
Smart Images

Figure CN116107908B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for generating unit test code. Background Technology
[0002] Unit testing primarily targets the most basic unit code of a system. Developers need to perform targeted testing on the corresponding code blocks based on the code logic. Although unit testing can improve code quality, developing unit test code is relatively cumbersome and time-consuming.
[0003] To address the aforementioned issues, a backend low-code unit testing plugin was designed to generate unit test code and provide advanced unit testing features, such as mock data configuration management, multi-case (instance) management, and support for custom unit test expressions. However, the unit test code generation tools provided by these technologies use default values for the generated unit test code. This necessitates code modification by programmers before execution, resulting in low maintainability, poor test performance, low generation efficiency, and a poor user experience. Summary of the Invention
[0004] In view of this, embodiments of the present invention provide a unit test code generation method and apparatus, which can directly configure corresponding unit test parameters in a visual unit configuration page based on the unit test object indicated by the user, and then generate unit test code in reverse by combining unit test templates. This improves the generation efficiency of unit test code, enhances the maintainability of unit test code, increases the automation of unit test code generation, and improves unit test efficiency and user experience.
[0005] To achieve the above objectives, according to one aspect of the present invention, a unit test code generation method is provided, comprising:
[0006] In response to a user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods;
[0007] It receives parameter configuration information input by the user on the unit test configuration page, and configures the unit test parameters corresponding to the unit test according to the parameter configuration information, unit test object, and data type; among them, the parameter configuration information indicates the external method call information corresponding to the unit test object;
[0008] The unit test template is populated and rendered based on the unit test parameters to obtain the program structure interface object. The program structure interface object is then parsed and processed according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated. The unit test code is then generated in reverse from the Java object.
[0009] Furthermore, unit test parameters include input parameters, simulation parameters, and output parameters; the steps for configuring the unit test parameters also include:
[0010] Retrieve Java file data for online business operations; the Java file data includes Java method information corresponding to the online business operations and external method call information corresponding to the Java methods.
[0011] The target Java method corresponding to the online business is determined by matching the similarity between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data.
[0012] Configure unit test parameters based on the external method call information corresponding to the target Java method.
[0013] Furthermore, the steps to determine the target Java method corresponding to the online business based on similarity matching between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data include:
[0014] From the Java method information in the Java file data, select Java methods whose parameter data types are consistent with the data types of the test parameters corresponding to the unit test object, and use them as candidate Java methods;
[0015] Calculate the similarity between the code block of the Java method corresponding to the unit test object and the code block of the candidate Java method, and determine the candidate Java method with the highest code block similarity as the target Java method.
[0016] Furthermore, the main steps for obtaining Java file data for online business operations include:
[0017] By implementing data tracking in online business processes using software development kits, bytecode can be intercepted before Java files are loaded in online business processes, thereby obtaining Java file data from online business processes.
[0018] Furthermore, after the step of obtaining Java file data for online business, the following steps are also included:
[0019] Perform data cleaning on Java files.
[0020] Furthermore, unit test parameters also include test instance parameters, which indicate different combinations of input parameters, simulation parameters, and output parameters.
[0021] According to another aspect of the present invention, a unit test code generation apparatus is provided, comprising:
[0022] The acquisition module is used to respond to a user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods;
[0023] The configuration module is used to receive parameter configuration information input by the user on the unit test configuration page, and configure the unit test parameters corresponding to the unit test according to the parameter configuration information, unit test object and data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object;
[0024] The unit test code generation module is used to populate and render unit test templates based on unit test parameters to obtain a program structure interface object. The program structure interface object is then parsed and processed according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated. Finally, the unit test code is generated from the Java object.
[0025] Furthermore, unit test parameters include input parameters, simulation parameters, and output parameters; the configuration module is also used for:
[0026] Retrieve Java file data for online business operations; the Java file data includes Java method information corresponding to the online business operations and external method call information corresponding to the Java methods.
[0027] The target Java method corresponding to the online business is determined by matching the similarity between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data.
[0028] Configure unit test parameters based on the external method call information corresponding to the target Java method.
[0029] According to another aspect of the present invention, an electronic device for generating unit test code is provided, comprising:
[0030] One or more processors;
[0031] Storage device for storing one or more programs.
[0032] When one or more programs are executed by one or more processors, the one or more processors implement any of the unit test code generation methods described above.
[0033] According to another aspect of the present invention, a computer-readable medium is provided having a computer program stored thereon, which, when executed by a processor, implements any of the unit test code generation methods described above.
[0034] One embodiment of the above invention has the following advantages or beneficial effects: Because it responds to a user-initiated unit test code generation request, it determines the unit test object indicated by the generation request and obtains the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods; it receives parameter configuration information input by the user on the unit test configuration page, and configures the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; it fills in the unit test template with parameters and renders the template according to the unit test parameters to obtain a program structure interface object, and parses the program interface object according to program structure interface technology to obtain the unit test code to be generated. This technology uses Java objects to generate unit test code, overcoming the problems of low maintainability, poor test performance, low generation efficiency, and poor user experience associated with related technologies. These technologies rely on default values for unit test parameters, requiring code modification before execution, leading to user-instructed unit test objects and the ability to generate unit test code from templates. This improves generation efficiency, maintainability, automation, test effectiveness, and user experience.
[0035] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0036] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:
[0037] Figure 1 This is a schematic diagram of the main flow of a unit test code generation method provided according to an embodiment of the present invention;
[0038] Figure 2a This is a schematic diagram of the main flow of a unit test code generation method provided according to another embodiment of the present invention;
[0039] Figure 2b yes Figure 2a The diagram shows the unit test page in the unit test code generation method.
[0040] Figure 3 This is a schematic diagram of the main modules of the unit test code generation device provided according to an embodiment of the present invention;
[0041] Figure 4 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;
[0042] Figure 5 This is a schematic diagram of the structure of a computer system suitable for implementing terminal devices or servers of the present invention. Detailed Implementation
[0043] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0044] It should be noted that the technical solutions disclosed herein, including the collection, gathering, updating, analysis, processing, use, transmission, and storage of user personal information related to online data collection, all comply with relevant laws and regulations, are used for legitimate purposes, and do not violate public order and good morals. Necessary measures are taken to prevent unauthorized access to user personal information data and to safeguard user personal information security, network security, and national security.
[0045] Figure 1 This is a schematic diagram of the main flow of a unit test code generation method provided according to an embodiment of the present invention; as shown below. Figure 1 As shown, the unit test code generation method provided in this embodiment of the invention mainly includes:
[0046] Step S101: In response to the unit test code generation request initiated by the user, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods.
[0047] Unit testing, in computer programming, is the testing process that verifies the correctness of program modules (the smallest units of software design). The meaning of "unit" in unit testing generally depends on the specific context. For example, in C, a unit refers to a function; in Java, it refers to a class; and in graphical software, it can refer to a window or a menu. In general, a unit is the smallest functional module defined by the programmer and subject to testing. The unit test objects designed in the unit test code generation method provided in this embodiment of the invention are Java classes or methods.
[0048] Specifically, the unit test code generation method provided in this embodiment of the invention is applied to an automatic unit test code generation plugin. This plugin is developed based on the IntelliJ IDEA (a Java integrated development environment) platform. The plugin can be installed on the IDEA software in the form of a zip package (zip file format is a file format for data compression and document storage) or a jar package (class archive files; jar files are used not only for compression and distribution but also for deploying and packaging libraries, components, and plugin programs, and can be directly used by compilers and other tools). A visual page is provided for users to initiate unit test code generation requests specifying the unit test objects. Unit test parameters can also be configured based on this visual page to facilitate subsequent unit test code generation. Based on the visual page, multiple Java classes or methods can be provided in the menu bar for users to select, thereby initiating a unit test code generation request.
[0049] Step S102: Receive parameter configuration information input by the user on the unit test configuration page, and configure the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object.
[0050] The above steps provide a method for configuring unit test parameters according to embodiments of the present invention. Based on the unit test object and the data type corresponding to the parameters, users can directly customize parameter configuration information in the unit test configuration interface to complete the configuration operation of unit test parameters. Subsequently, unit test code is generated directly based on the unit test parameters and the unit test template. Through the above settings, based on a visual page, the user experience is improved, the efficiency of unit test parameter configuration is increased, and thus the overall efficiency of unit test code generation is improved.
[0051] Further, according to embodiments of the present invention, the unit test parameters include input parameters, simulation parameters, and output parameters; the step of configuring the unit test parameters corresponding to the unit test further includes:
[0052] Retrieve Java file data for online business operations; the Java file data includes Java method information corresponding to the online business operations and external method call information corresponding to the Java methods.
[0053] The target Java method corresponding to the online business is determined by matching the similarity between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data.
[0054] Configure unit test parameters based on the external method call information corresponding to the target Java method.
[0055] The above steps provide another way to configure unit test parameters according to the present invention. First, real data of the online environment (i.e., Java file data of the above online business) is obtained. Then, the parameters most suitable for the current unit test object are determined by similarity matching, and the parameters are used as unit configuration parameters. Through the above settings, the unit test parameters corresponding to the generated unit test code are more in line with the real business scenario, thereby improving the unit test effect.
[0056] Preferably, according to an embodiment of the present invention, the step of determining the target Java method corresponding to the online business by performing similarity matching between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data includes:
[0057] From the Java method information in the Java file data, select Java methods whose parameter data types are consistent with the data types of the test parameters corresponding to the unit test object, and use them as candidate Java methods;
[0058] Calculate the similarity between the code block of the Java method corresponding to the unit test object and the code block of the candidate Java method, and determine the candidate Java method with the highest code block similarity as the target Java method.
[0059] In configuring unit test parameters based on Java file data from online business, it is preferable to select one or more Java methods with consistent parameter data types from the Java file data of the online business as candidate Java methods. This is a necessary condition for configuring unit test parameters based on online business data; that is, the data types of the parameters involved in both must be consistent. Then, the similarity between the code blocks of the candidate Java methods and the Java methods corresponding to the current unit test object is calculated. According to a specific embodiment of the present invention, the candidate Java method with the highest similarity can be directly selected as the target Java method, or several candidate Java methods with high similarity (the specific value is determined according to the actual situation) can be selected as the target Java method. Finally, the unit test parameters are configured using the external method call information corresponding to the target Java method. The external method call information includes the parameters and return value of the external method call. Further, the parameters of the external method call can be used as input parameters in the configured unit test parameters, and the return value can be used as mock parameters (output parameters) in the configured unit test parameters. The corresponding output parameters are the expected results determined according to the return value type of the unit test method.
[0060] The steps to determine the code block of a Java method can be implemented using PSI (Program Structure Interface) technology. Specifically, PSI describes and maps Java code through Java objects.
[0061] Optionally, according to embodiments of the present invention, the steps for obtaining Java file data of online services mainly include:
[0062] By implementing data tracking in online business processes using software development kits, bytecode can be intercepted before Java files are loaded in online business processes, thereby obtaining Java file data from online business processes.
[0063] Specifically, according to embodiments of the present invention, data tracking is performed using an SDK (Software Development Kit). For example, a JAR file can be provided via JavaAgent (a Java agent that can monitor or redefine services running in an application). This intercepts and modifies bytecode before loading Java files, thereby recording important information such as the Java method names, parameters, and return values of online business processes. Furthermore, ELK (Elasticsearch + Logstash + Kibana, a log analysis system) can be used to achieve the functions of collecting, transmitting, storing, analyzing, and visualizing online tracking data.
[0064] By obtaining the Java file data of online business through the above settings, unit test parameters that are more in line with real business scenarios can be configured to improve the unit test effect.
[0065] For example, according to an embodiment of the present invention, after the step of obtaining Java file data of online business, the above method further includes:
[0066] Perform data cleaning on Java files.
[0067] Data cleaning using Java files helps to configure unit test parameters more quickly, improves the accuracy of unit test parameter configuration, and increases the efficiency of unit test code generation.
[0068] Furthermore, according to embodiments of the present invention, the above-mentioned unit test parameters also include test instance parameters, which indicate different combinations of input parameters, simulation parameters, and output parameters.
[0069] By configuring test case parameters, input parameters, mock parameters, and output parameters are organized into a unified whole. Each case represents a generated unit test method. By generating a complete unit test code according to the configured cases, the application scenarios of unit test code are expanded, and the efficiency of unit test code generation is improved.
[0070] Optionally, a case center can be set up to save all historically generated unit test parameter data, which will facilitate subsequent modification and maintenance of the unit test code.
[0071] Step S103: Fill in the unit test template with parameters and render the template according to the unit test parameters to obtain the program structure interface object. Parse the program interface object according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated, and generate the unit test code in reverse from the Java object.
[0072] Specifically, the unit test template corresponds to the selected unit test object, which constructs the structure of the unit test code. The template is filled with parameters and rendered by the unit tests configured above to obtain the program structure interface object (PSI class), which includes one or more PSI methods (external method calls). The implementation of the methods is executed by code blocks. Therefore, by parsing the program interface object through PSI technology, that is, parsing the code structure and external method calls, the Java object to be generated as the unit test code can be obtained. Then, the Java object is reverse-engineered through PSI to generate the code file, that is, to generate the unit test code.
[0073] According to the technical solution of this embodiment of the invention, in response to a user-initiated unit test code generation request, the unit test object indicated by the generation request is determined, and the data type of the test parameters corresponding to the unit test object is obtained; wherein, the unit test object includes Java classes or methods; the parameter configuration information input by the user on the unit test configuration page is received, and the unit test parameters corresponding to the unit test are configured according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; the unit test template is filled with parameters and rendered according to the unit test parameters to obtain a program structure interface object, and the program interface object is parsed according to the program structure interface technology to obtain the Java code corresponding to the unit test code to be generated. This technique utilizes Java objects to generate unit test code from them. This overcomes the limitations of related technologies where default values are used to generate unit test code, requiring code modification before execution. This results in low maintainability, poor test performance, low generation efficiency, and a poor user experience. The technique allows users to configure unit test parameters directly based on user-defined unit test objects within a visual configuration page. Combined with unit test templates, it generates unit test code from scratch, improving generation efficiency, maintainability, automation, and overall test effectiveness and user experience.
[0074] Figure 2a This is a schematic diagram of the main flow of a unit test code generation method provided according to yet another embodiment of the present invention; as shown Figure 2a As shown, the unit test code generation method provided in this embodiment of the invention mainly includes:
[0075] Step S201: Data tracking is performed on the online business according to the software development kit to intercept bytecode before the online business loads Java files and obtain Java file data of the online business; wherein, Java file data includes Java method information corresponding to the online business and external method call information corresponding to the Java methods.
[0076] Specifically, according to embodiments of the present invention, data tracking is performed using an SDK (Software Development Kit). For example, a JAR file can be provided via JavaAgent (a Java agent that can monitor or redefine services running in an application). This intercepts and modifies bytecode before loading Java files, thereby recording important information such as the Java method names, parameters, and return values of online business processes. Furthermore, ELK (Elasticsearch + Logstash + Kibana, a log analysis system) can be used to achieve the functions of collecting, transmitting, storing, analyzing, and visualizing online tracking data.
[0077] By obtaining the Java file data of online business through the above settings, unit test parameters that are more in line with real business scenarios can be configured to improve the unit test effect.
[0078] For example, according to an embodiment of the present invention, after the step of obtaining Java file data of online business, the above method further includes:
[0079] Perform data cleaning on Java files.
[0080] Data cleaning using Java files helps to configure unit test parameters more quickly, improves the accuracy of unit test parameter configuration, and increases the efficiency of unit test code generation.
[0081] Step S202: In response to the user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods.
[0082] Specifically, the unit test code generation method provided in this embodiment of the invention is applied to an automatic unit test code generation plugin. This plugin is developed based on the IntelliJ IDEA (a Java integrated development environment) platform. The plugin can be installed on the IDEA software in the form of a zip package (zip file format is a file format for data compression and document storage) or a jar package (class archive files; jar files are used not only for compression and distribution but also for deploying and packaging libraries, components, and plugin programs, and can be directly used by compilers and other tools). A visual page is provided for users to initiate unit test code generation requests specifying the unit test objects. Unit test parameters can also be configured based on this visual page to facilitate subsequent unit test code generation. Based on the visual page, multiple Java classes or methods can be provided in the menu bar for users to select, thereby initiating a unit test code generation request.
[0083] Step S203: Select Java methods whose parameter data types are consistent with the data types of the test parameters corresponding to the unit test object from the Java method information in the Java file data, and use them as candidate Java methods.
[0084] Step S204: Calculate the similarity between the code block of the Java method corresponding to the unit test object and the code block of the candidate Java method, and determine the candidate Java method with the highest code block similarity as the target Java method.
[0085] In configuring unit test parameters based on Java file data from online business, it is preferable to select one or more Java methods with consistent parameter data types from the Java file data of the online business as candidate Java methods. This is a necessary condition for configuring unit test parameters based on online business data; that is, the data types of the parameters involved in both must be consistent. Then, the similarity between the code blocks of the candidate Java methods and the Java methods corresponding to the current unit test object is calculated. According to a specific embodiment of the present invention, the candidate Java method with the highest similarity can be directly selected as the target Java method, or several candidate Java methods with high similarity (the specific value is determined according to the actual situation) can be selected as the target Java method. Finally, the unit test parameters are configured using the external method call information corresponding to the target Java method. The external method call information includes the parameters and return value of the external method call. Further, the parameters of the external method call can be used as input parameters in the configured unit test parameters, and the return value can be used as mock parameters (output parameters) in the configured unit test parameters. The corresponding output parameters are the expected results determined according to the return value type of the unit test method.
[0086] The steps to determine the code block of a Java method can be implemented using PSI (Program Structure Interface) technology. Specifically, PSI describes and maps Java code through Java objects.
[0087] Step S205: Configure unit test parameters based on the external method call information corresponding to the target Java method.
[0088] Configure unit test parameters based on the external method call information corresponding to the target Java method. The external method call information includes the parameters and return value of the external method call. Furthermore, the parameters of the external method call can be used as input parameters in the configured unit test parameters, and the return value can be used as mock parameters (output parameters) in the configured unit test parameters. The corresponding output parameters are the expected results determined based on the return value type of the unit test method.
[0089] The above steps provide a way to configure unit test parameters according to an embodiment of the present invention. First, real data of the online environment (i.e., Java file data of the above online business) is obtained. Then, the parameters most suitable for the current unit test object are determined by similarity matching, and the parameters are used as unit configuration parameters. Through the above settings, the unit test parameters corresponding to the generated unit test code are more in line with the real business scenario, thereby improving the unit test effect.
[0090] Furthermore, Figure 2b This diagram illustrates a unit test page in the unit test code generation method provided in an embodiment of the present invention; as shown. Figure 2b As shown in the embodiment of the present invention, another method for configuring unit test parameters is also provided, the specific steps of which include: directly receiving parameter configuration information input by the user on the unit test configuration page, and configuring the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object.
[0091] The above steps provide a method for configuring unit test parameters according to embodiments of the present invention. Based on the unit test object and the data type corresponding to the parameters, users can directly customize parameter configuration information in the unit test configuration interface to complete the configuration operation of unit test parameters. Subsequently, unit test code is generated directly based on the unit test parameters and the unit test template. Through the above settings, based on a visual page, the user experience is improved, the efficiency of unit test parameter configuration is increased, and thus the overall efficiency of unit test code generation is improved.
[0092] For example, to allow users to directly customize unit test parameters based on the unit test configuration page, a specific implementation of this invention provides a configuration example, such as if the code corresponding to the object being unit tested is as follows:
[0093]
[0094] Based on the unit test object selected by the user, the data type of the corresponding test parameter (i.e., the data type of the corresponding input, mock, and output parameters) can be obtained, and the external method call information corresponding to the unit test object (i.e., the data that needs to be mocked) can be determined.
[0095] Secondly, based on the selected Java method (i.e., the unit test object), the number and types of parameters are inferred, and then the input parameters are configured, as shown in the following example:
[0096] arg0 10 a1 arg1 11 b1 arg0 random a2 arg1 2*$arg0 b2
[0097] Secondly, based on the selected Java method (i.e., the unit test object), scan the external method call information inside the method. This external method call information contains the parameter data that needs to be mocked, as shown in the following example:
[0098]
[0099] Furthermore, based on the return value type of the selected method (i.e., the unit test object), the expected result of the input return value (i.e., the mock parameter) is determined as the output parameter, as shown in the following example:
[0100] 20 r1 9 r2
[0101] Alternatively, multiple unit test scenarios can be determined by combining the above input parameters, mock parameters, and output parameters, i.e., configuring case parameters, as shown in the following example:
[0102] a1 b1 r1 a2 b2 r2
[0103] Through the above steps, the unit test parameters for the user-selected unit test object have been configured. Then, combined with the unit test module, unit test code is generated using PSI technology. An example of the unit test code generated for the object being tested in the above example is shown below:
[0104]
[0105]
[0106] Furthermore, according to embodiments of the present invention, the above-mentioned unit test parameters also include test instance parameters, which indicate different combinations of input parameters, simulation parameters, and output parameters.
[0107] By configuring test case parameters, input parameters, mock parameters, and output parameters are organized into a unified whole. Each case represents a generated unit test method. By generating a complete unit test code according to the configured cases, the application scenarios of unit test code are expanded, and the efficiency of unit test code generation is improved.
[0108] Optionally, a case center can be set up to save all historically generated unit test parameter data, which will facilitate subsequent modification and maintenance of the unit test code.
[0109] Step S206: Fill in the unit test template with parameters and render the template according to the unit test parameters to obtain the program structure interface object. Parse the program interface object according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated, and generate the unit test code in reverse from the Java object.
[0110] Specifically, the unit test template corresponds to the selected unit test object, which constructs the structure of the unit test code. The template is filled with parameters and rendered by the unit tests configured above to obtain the program structure interface object (PSI class), which includes one or more PSI methods (external method calls). The implementation of the methods is executed by code blocks. Therefore, by parsing the program interface object through PSI technology, that is, parsing the code structure and external method calls, the Java object to be generated as the unit test code can be obtained. Then, the Java object is reverse-engineered through PSI to generate the code file, that is, to generate the unit test code.
[0111] According to the technical solution of this embodiment of the invention, in response to a user-initiated unit test code generation request, the unit test object indicated by the generation request is determined, and the data type of the test parameters corresponding to the unit test object is obtained; wherein, the unit test object includes Java classes or methods; the parameter configuration information input by the user on the unit test configuration page is received, and the unit test parameters corresponding to the unit test are configured according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; the unit test template is filled with parameters and rendered according to the unit test parameters to obtain a program structure interface object, and the program interface object is parsed according to the program structure interface technology to obtain the Java code corresponding to the unit test code to be generated. This technique utilizes Java objects to generate unit test code from them. This overcomes the limitations of related technologies where default values are used to generate unit test code, requiring code modification before execution. This results in low maintainability, poor test performance, low generation efficiency, and a poor user experience. The technique allows users to configure unit test parameters directly based on user-defined unit test objects within a visual configuration page. Combined with unit test templates, it generates unit test code from scratch, improving generation efficiency, maintainability, automation, and overall test effectiveness and user experience.
[0112] Figure 3 This is a schematic diagram of the main modules of the unit test code generation apparatus provided according to an embodiment of the present invention; as shown. Figure 3As shown, the unit test code generation device 300 provided in this embodiment of the invention mainly includes:
[0113] The acquisition module 301 is used to respond to a user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods.
[0114] Specifically, the unit test code generation method provided in this embodiment of the invention is applied to a unit test code automatic generation plugin. This plugin is installed on the IDEA software and provides a visual page for users to initiate unit test code generation requests specifying unit test objects. Users can also configure unit test parameters based on this visual page to facilitate subsequent unit test code generation. Based on the visual page, multiple Java classes or methods can be provided in the menu bar for users to select, thereby initiating a unit test code generation request.
[0115] The configuration module 302 is used to receive parameter configuration information input by the user on the unit test configuration page, and configure the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object.
[0116] The above settings provide a method for configuring unit test parameters according to embodiments of the present invention. Based on the unit test object and the data type corresponding to the parameters, users can directly customize parameter configuration information in a visual unit test configuration interface to complete the configuration operation of unit test parameters. Subsequently, unit test code is generated directly based on the unit test parameters and the unit test template. This setting, based on a visual page, improves the user experience, increases the efficiency of unit test parameter configuration, and thus improves the overall efficiency of unit test code generation.
[0117] Furthermore, according to an embodiment of the present invention, the unit test parameters include input parameters, simulation parameters, and output parameters; the configuration module 302 is also used for:
[0118] Retrieve Java file data for online business operations; the Java file data includes Java method information corresponding to the online business operations and external method call information corresponding to the Java methods.
[0119] The target Java method corresponding to the online business is determined by matching the similarity between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data.
[0120] Configure unit test parameters based on the external method call information corresponding to the target Java method.
[0121] The above settings provide another way to configure unit test parameters according to the present invention. First, real data of the online environment (i.e., Java file data of the above online business) is obtained. Then, the parameters most suitable for the current unit test object are determined by similarity matching, and the parameters are used as unit configuration parameters. This makes the unit test parameters corresponding to the generated unit test code more consistent with the real business scenario and improves the unit test effect.
[0122] Preferably, according to an embodiment of the present invention, the configuration module 302 is further configured to:
[0123] From the Java method information in the Java file data, select Java methods whose parameter data types are consistent with the data types of the test parameters corresponding to the unit test object, and use them as candidate Java methods;
[0124] Calculate the similarity between the code block of the Java method corresponding to the unit test object and the code block of the candidate Java method, and determine the candidate Java method with the highest code block similarity as the target Java method.
[0125] In configuring unit test parameters based on Java file data from online business, it is preferable to select one or more Java methods with consistent parameter data types from the Java file data of the online business as candidate Java methods. This is a necessary condition for configuring unit test parameters based on online business data; that is, the data types of the parameters involved in both must be consistent. Then, the similarity between the code blocks of the candidate Java methods and the Java methods corresponding to the current unit test object is calculated. According to a specific embodiment of the present invention, the candidate Java method with the highest similarity can be directly selected as the target Java method, or several candidate Java methods with high similarity (the specific value is determined according to the actual situation) can be selected as the target Java method. Finally, the unit test parameters are configured using the external method call information corresponding to the target Java method. The external method call information includes the parameters and return value of the external method call. Further, the parameters of the external method call can be used as input parameters in the configured unit test parameters, and the return value can be used as mock parameters (output parameters) in the configured unit test parameters. The corresponding output parameters are the expected results determined according to the return value type of the unit test method.
[0126] The steps to determine the code block of a Java method can be implemented using PSI (Program Structure Interface) technology. Specifically, PSI describes and maps Java code through Java objects.
[0127] Optionally, according to an embodiment of the present invention, the configuration module 302 is further configured to:
[0128] By implementing data tracking in online business processes using software development kits, bytecode can be intercepted before Java files are loaded in online business processes, thereby obtaining Java file data from online business processes.
[0129] Specifically, according to embodiments of the present invention, a JAR package can be provided via JavaAgent to intercept and modify bytecode before loading Java files, thereby recording important information such as Java method names, method parameters, and method return values for online business processes. Furthermore, ELK (Elasticsearch + Logstash + Kibana, a log analysis system) can be used to achieve the functions of collecting, transmitting, storing, analyzing, and visualizing online event tracking data.
[0130] By obtaining the Java file data of online business through the above settings, unit test parameters that are more in line with real business scenarios can be configured to improve the unit test effect.
[0131] For example, according to an embodiment of the present invention, the unit test code generation apparatus 300 further includes a data cleaning module. After the step of obtaining Java file data of online business, the data cleaning module is used to:
[0132] Perform data cleaning on Java files.
[0133] Data cleaning using Java files helps to configure unit test parameters more quickly, improves the accuracy of unit test parameter configuration, and increases the efficiency of unit test code generation.
[0134] Furthermore, according to embodiments of the present invention, the above-mentioned unit test parameters also include test instance parameters, which indicate different combinations of input parameters, simulation parameters, and output parameters.
[0135] By configuring test case parameters, input parameters, mock parameters, and output parameters are organized into a unified whole. Each case represents a generated unit test method. By generating a complete unit test code according to the configured cases, the application scenarios of unit test code are expanded, and the efficiency of unit test code generation is improved.
[0136] Optionally, a case center can be set up to save all historically generated unit test parameter data, which will facilitate subsequent modification and maintenance of the unit test code.
[0137] The unit test code generation module 303 is used to fill in the unit test template with parameters and render the template according to the unit test parameters to obtain the program structure interface object. The program structure interface object is parsed and processed according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated, and the unit test code is generated in reverse from the Java object.
[0138] Specifically, the unit test template corresponds to the selected unit test object, which constructs the structure of the unit test code. The template is filled with parameters and rendered by the unit tests configured above to obtain the program structure interface object (PSI class), which includes one or more PSI methods (external method calls). The implementation of the methods is executed by code blocks. Therefore, by parsing the program interface object through PSI technology, that is, parsing the code structure and external method calls, the Java object to be generated as the unit test code can be obtained. Then, the Java object is reverse-engineered through PSI to generate the code file, that is, to generate the unit test code.
[0139] According to the technical solution of this embodiment of the invention, in response to a user-initiated unit test code generation request, the unit test object indicated by the generation request is determined, and the data type of the test parameters corresponding to the unit test object is obtained; wherein, the unit test object includes Java classes or methods; the parameter configuration information input by the user on the unit test configuration page is received, and the unit test parameters corresponding to the unit test are configured according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; the unit test template is filled with parameters and rendered according to the unit test parameters to obtain a program structure interface object, and the program interface object is parsed according to the program structure interface technology to obtain the Java code corresponding to the unit test code to be generated. This technique utilizes Java objects to generate unit test code from them. This overcomes the limitations of related technologies where default values are used to generate unit test code, requiring code modification before execution. This results in low maintainability, poor test performance, low generation efficiency, and a poor user experience. The technique allows users to configure unit test parameters directly based on user-defined unit test objects within a visual configuration page. Combined with unit test templates, it generates unit test code from scratch, improving generation efficiency, maintainability, automation, and overall test effectiveness and user experience.
[0140] Figure 4 An exemplary system architecture 400 is shown that can be applied to the unit test code generation method or unit test code generation apparatus of the present invention.
[0141] like Figure 4 As shown, system architecture 400 may include terminal devices 401, 402, and 403, a network 404, and a server 405. Network 404 serves as the medium for providing communication links between terminal devices 401, 402, and 403 and server 405. Network 404 may include various connection types, such as wired or wireless communication links, or fiber optic cables, etc.
[0142] Users can use terminal devices 401, 402, and 403 to interact with server 405 via network 404 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 401, 402, and 403, such as unit testing components, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).
[0143] Terminal devices 401, 402, and 403 can be various electronic devices with displays that support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0144] Server 405 can be a server that provides various services, such as servers that allow users to utilize terminal devices 401, 402, and 403 (for example only). This server can analyze and process received data such as unit test code generation requests, and feed back the processing results (such as generated unit test code) to the terminal devices.
[0145] It should be noted that the unit test code generation method provided in the embodiments of the present invention is generally executed by server 405, and correspondingly, the unit test code generation device is generally set in server 405.
[0146] It should be understood that Figure 4 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0147] The following is for reference. Figure 5 It shows a schematic diagram of the structure of a computer system 500 suitable for implementing terminal devices or servers of the present invention. Figure 5 The terminal device or server shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of the present invention.
[0148] like Figure 5As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 502 or programs loaded from storage section 508 into random access memory (RAM) 503. The RAM 503 also stores various programs and data required for the operation of the system 500. The CPU 501, ROM 502, and RAM 503 are interconnected via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0149] The following components are connected to I / O interface 505: an input section 506 including a keyboard, mouse, etc.; an output section 507 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network interface card such as a LAN card, modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to I / O interface 505 as needed. A removable medium 511, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 510 as needed so that computer programs read from it can be installed into storage section 508 as needed.
[0150] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 509, and / or installed from removable medium 511. When the computer program is executed by central processing unit (CPU) 501, it performs the functions defined above in the system of this invention.
[0151] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0152] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0153] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be located in a processor; for example, a processor can be described as including an acquisition module, a configuration module, and a unit test code generation module. The names of these modules do not necessarily limit the module itself. For example, the acquisition module can also be described as "used to respond to a user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and acquire the data type of the test parameters corresponding to the unit test object; wherein the unit test object includes modules of Java classes or methods."
[0154] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, which, when executed by the device, cause the device to include: responding to a user-initiated unit test code generation request, determining the unit test object indicated by the generation request, and obtaining the data type of the test parameters corresponding to the unit test object; wherein the unit test object includes Java classes or methods; receiving parameter configuration information input by the user on a unit test configuration page, configuring the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object, and the data type; wherein the parameter configuration information indicates the external method call information corresponding to the unit test object; filling parameters and rendering the unit test template according to the unit test parameters to obtain a program structure interface object, parsing the program interface object according to program structure interface technology to obtain the Java object corresponding to the unit test code to be generated, and generating the unit test code from the Java object.
[0155] According to the technical solution of this embodiment of the invention, in response to a user-initiated unit test code generation request, the unit test object indicated by the generation request is determined, and the data type of the test parameters corresponding to the unit test object is obtained; wherein, the unit test object includes Java classes or methods; the parameter configuration information input by the user on the unit test configuration page is received, and the unit test parameters corresponding to the unit test are configured according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; the unit test template is filled with parameters and rendered according to the unit test parameters to obtain a program structure interface object, and the program interface object is parsed according to the program structure interface technology to obtain the Java code corresponding to the unit test code to be generated. This technique utilizes Java objects to generate unit test code from them. This overcomes the limitations of related technologies where default values are used to generate unit test code, requiring code modification before execution. This results in low maintainability, poor test performance, low generation efficiency, and a poor user experience. The technique allows users to configure unit test parameters directly based on user-defined unit test objects within a visual configuration page. Combined with unit test templates, it generates unit test code from scratch, improving generation efficiency, maintainability, automation, and overall test effectiveness and user experience.
[0156] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A method for generating unit test code, characterized in that, include: In response to a user-initiated unit test code generation request, the unit test object indicated by the generation request is determined, and the data type of the test parameters corresponding to the unit test object is obtained; wherein, the unit test object includes Java classes or methods; The system receives parameter configuration information input by the user on the unit test configuration page, and configures the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; The unit test template is populated and rendered according to the unit test parameters to obtain a program structure interface object. The program structure interface object is parsed according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated. The unit test code is then generated in reverse from the Java object. The steps for configuring the unit test parameters corresponding to the unit test also include: Obtain Java file data for online services; wherein, the Java file data includes Java method information corresponding to the online services and external method call information corresponding to the Java methods; The target Java method corresponding to the online business is determined by performing similarity matching between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data. Configure the unit test parameters based on the external method call information corresponding to the target Java method.
2. The unit test code generation method according to claim 1, characterized in that, The unit test parameters include input parameters, simulation parameters, and output parameters.
3. The unit test code generation method according to claim 2, characterized in that, The step of determining the target Java method corresponding to the online business by performing similarity matching between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data includes: From the Java method information in the Java file data, select Java methods whose parameter data types are consistent with the data types of the test parameters corresponding to the unit test object, and use them as candidate Java methods; Calculate the similarity between the code block of the Java method corresponding to the unit test object and the code block of the candidate Java method, and determine the candidate Java method with the highest code block similarity as the target Java method.
4. The unit test code generation method according to claim 2, characterized in that, The steps for obtaining Java file data from online business mainly include: Data tracking is performed on online services using a software development kit to intercept bytecode before the online services load Java files and obtain the Java file data of the online services.
5. The unit test code generation method according to claim 2, characterized in that, Following the step of obtaining the Java file data of the online service, the method further includes: The Java file data is then cleaned.
6. The unit test code generation method according to claim 2, characterized in that, The unit test parameters also include test instance parameters, which indicate different combinations of input parameters, simulation parameters, and output parameters.
7. A unit test code generation device, characterized in that, include: The acquisition module is used to respond to a user-initiated unit test code generation request, determine the unit test object indicated by the generation request, and obtain the data type of the test parameters corresponding to the unit test object; wherein, the unit test object includes Java classes or methods; The configuration module is used to receive parameter configuration information input by the user on the unit test configuration page, and configure the unit test parameters corresponding to the unit test according to the parameter configuration information, the unit test object, and the data type; wherein, the parameter configuration information indicates the external method call information corresponding to the unit test object; The unit test code generation module is used to fill in the unit test template with parameters and render the template according to the unit test parameters to obtain a program structure interface object. The program structure interface object is parsed according to the program structure interface technology to obtain the Java object corresponding to the unit test code to be generated, and the unit test code is generated in reverse from the Java object. The configuration module is also used for: Obtain Java file data for online services; wherein, the Java file data includes Java method information corresponding to the online services and external method call information corresponding to the Java methods; The target Java method corresponding to the online business is determined by performing similarity matching between the Java method corresponding to the unit test object and the Java method information corresponding to the online business in the Java file data. Configure the unit test parameters based on the external method call information corresponding to the target Java method.
8. The unit test code generation apparatus according to claim 7, characterized in that, The unit test parameters include input parameters, simulation parameters, and output parameters.
9. An electronic device for generating unit test code, characterized in that, include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-6.
10. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Testing method and testing device
CN113076253A
Test analysis method and device, computer equipment and storage medium
CN114840410A
Unit test code rapid generation method and system and computer equipment
CN115576844A