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

By automatically generating unit test code, the problem of low generation efficiency and high cost in existing technologies is solved, achieving efficient and accurate unit test code generation and reducing the risk of online incidents.

CN116340155BActive Publication Date: 2026-03-27BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-09
Publication Date
2026-03-27

AI Technical Summary

Technical Problem

Current technologies for generating unit test code are inefficient, and manual writing of such code incurs significant R&D costs and can easily lead to online incidents.

Method used

By acquiring the target function code from the existing code, extracting function and statement information, generating unit test code corresponding to the target function code based on this information, and automatically generating unit test code using a generation model, including visual input sources, generation engine and full package output, automatically mocking downstream requests and cleaning test data.

Benefits of technology

It improves the efficiency of generating unit test code, reduces R&D costs, lowers the risk of online incidents, and ensures the accuracy and coverage of test code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116340155B_ABST
    Figure CN116340155B_ABST
Patent Text Reader

Abstract

The present disclosure provides a method and device for generating unit test code, electronic equipment and storage medium, relating to the technical field of computer, especially to the technical field of data processing, artificial intelligence and the like. The specific implementation scheme is as follows: obtaining target function code in inventory code; extracting function information and statement information from the target function code; generating unit test code corresponding to the target function code based on the function information and the statement information; and performing function test on the target function code based on the unit test code.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, and in particular, to the technical field of data processing, artificial intelligence, etc. BACKGROUND

[0002] In the related art, the business process of the newly added function code is mainly combed by artificial, and the function code is tested by using the way of handwritten unit test code, and when the downstream request is made, the real test environment is directly connected, which not only consumes the research and development cost, but also easily causes online accidents. SUMMARY

[0003] The present disclosure provides a unit test code generation method and device, electronic equipment and storage medium.

[0004] According to an aspect of the present disclosure, a unit test code generation method is provided, comprising: obtaining target function code in inventory code, wherein the target function code is obtained by modifying original function code in the inventory code; extracting function information and statement information from the target function code, wherein the function information at least includes a first input parameter of a function in the target function code, and the statement information at least includes a first conditional statement code in the target function code, and the first input parameter is used to represent a data function implemented by the target function code; generating a unit test code corresponding to the target function code based on the function information and the statement information, wherein a second input parameter of a function in the unit test code matches the first input parameter, the second input parameter is used to represent a number of test cases of the unit test code for testing the target function code, and a second conditional statement code in the unit test code matches the first conditional statement code; and performing a function test on the target function code based on the unit test code.

[0005] According to another aspect of the present disclosure, another unit test code generation method is provided, comprising: obtaining inventory code, wherein the inventory code at least includes target function code, and the target function code is obtained by modifying original function code in the inventory code; inputting the inventory code into a unit test code generation model for analysis and processing to obtain a unit test code corresponding to the target function code, wherein the unit test code generation model is determined based on the above unit test code method.

[0006] According to an aspect of the present disclosure, a unit test code generation apparatus is provided, comprising: a first obtaining unit configured to obtain target function code in inventory code, wherein the target function code is modified from original function code in the inventory code; an extracting unit configured to extract function information and statement information from the target function code, wherein the function information at least comprises a first input parameter of a function in the target function code, and the statement information at least comprises a first conditional statement code in the target function code, and the first input parameter is used to represent a data function implemented by the target function code; a generating unit configured to generate unit test code corresponding to the target function code based on the function information and the statement information, wherein a second input parameter of a function in the unit test code matches the first input parameter, the second input parameter is used to represent a number of test cases of the unit test code for testing the target function code, and a second conditional statement code in the unit test code matches the first conditional statement code; and a testing unit configured to perform function testing on the target function code based on the unit test code.

[0007] According to another aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory connected with the at least one processor in communication; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the unit test code generation method of the embodiments of the present disclosure.

[0008] According to another aspect of the present disclosure, a non-transitory computer readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable a computer to perform the unit test code generation method of the embodiments of the present disclosure.

[0009] According to another aspect of the present disclosure, a computer program product is provided, comprising a computer program, wherein the computer program, when executed by a processor, implements the unit test code generation method of the embodiments of the present disclosure.

[0010] It should be understood that the content described in this part is not intended to identify key or important features of the embodiments of the present disclosure, nor to limit the scope of the present disclosure. Other features of the present disclosure will become apparent through the following description. BRIEF DESCRIPTION OF DRAWINGS

[0011] The accompanying drawings are used to better understand the present scheme, and do not limit the present disclosure. Among them:

[0012] Figure 1 is a flowchart of a unit test code generation method according to an embodiment of the present disclosure;

[0013] FIG. 2(a) is a flowchart of another unit test code generation method according to an embodiment of the present disclosure;

[0014] FIG. 2(b) is a schematic diagram of a computer product for performing a generation method of unit test code according to an embodiment of the present disclosure;

[0015] FIG. 2(c) is a schematic diagram of a human-computer interaction operation interface for performing a generation method of unit test code according to an embodiment of the present disclosure;

[0016] Figure 3 is a schematic diagram of a display interface of a tool for generating unit test code according to an embodiment of the present disclosure;

[0017] Figure 4 is a schematic diagram of a unit test code generation device according to an embodiment of the present disclosure;

[0018] Figure 5 is a schematic diagram of another unit test code generation device according to an embodiment of the present disclosure;

[0019] Figure 6 is a schematic diagram of an electronic device for implementing a generation method of unit test code according to an embodiment of the present disclosure. DETAILED DESCRIPTION

[0020] Exemplary embodiments of the present disclosure are described below with reference to the accompanying drawings, which include various details of the embodiments of the present disclosure to help the understanding of the present disclosure. These should be considered in the context of the overall description and should not be considered limiting in any way. Thus, it will be apparent to one of ordinary skill in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Also, for the sake of brevity and clarity, descriptions of well-known functions and constructions are omitted from the following description.

[0021] A generation method of unit test code according to an embodiment of the present disclosure is described below.

[0022] Figure 1 is a flowchart of a generation method of unit test code according to an embodiment of the present disclosure, as shown in Figure 1 the method can include the following steps:

[0023] In step S102, the target function code in the inventory code is obtained.

[0024] In the technical solution provided by the step S102 of the present disclosure, the inventory code can be an inventory business code, and the inventory code can include all the functional codes to be detected in the entire test project, such as original functional codes and target functional codes obtained by modifying the original functional codes, wherein the target functional codes can be codes obtained by modifying the original functional codes according to the research and development requirements of the research and development project by the research and development personnel. It should be noted that the type of the target functional code is not specifically limited here, and any newly added functional code in the inventory code is within the protection scope of the embodiments of the present disclosure, and is not listed one by one here.

[0025] In this embodiment, the abstract syntax tree of the inventory code can be scanned to determine the functional code of the inventory code that is not covered by the unit test code, and the functional code is determined as the target functional code.

[0026] It should be noted that the above method is only one preferred implementation for obtaining the target functional code from the inventory code, and the method for obtaining the target functional code from the inventory code is not specifically limited here, and any method and process for obtaining the target functional code from the inventory code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0027] In step S104, function information and statement information are extracted from the target functional code.

[0028] In the technical solution provided by the step S104 of the present disclosure, after the abstract syntax tree of the inventory code is scanned to obtain the target functional code, the target functional code can be further analyzed, and the function information of the function in the target functional code and the statement information of the target functional code are extracted, wherein the function information can include the first input parameter of the function in the target functional code and the branch logic inside the function, and the statement information can include the first conditional statement code in the target functional code, the first input parameter can be used to represent the data function implemented by the target functional code, and the first conditional statement code can be a conditional statement in the target functional code.

[0029] It should be noted that the method for extracting the function information and the statement information from the target functional code is not specifically limited here, and any method and process for extracting the function information and the statement information from the target functional code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0030] Optionally, the first input parameter of the function in the target functional code also needs to match the input parameter of the original functional code in the inventory code, and the first conditional statement code of the target functional code also needs to match the conditional statement of the original functional code, so as to achieve the technical effect that the modified target functional code can normally run.

[0031] In step S106, the unit test code corresponding to the target function code is generated based on the function information and the statement information.

[0032] In the technical solution provided in step S106 of the present disclosure, the unit test code corresponding to the target function code can be constructed according to the function information and the statement information extracted in step S104, wherein the second input parameter of the function in the unit test code matches the first input parameter of the target function code, the second input parameter can be used to represent the number of test cases of the unit test code for testing the target function code, and the second conditional statement code in the unit test code matches the first conditional statement code.

[0033] Optionally, the second input parameter of the unit test code also needs to match the original unit test code corresponding to the original target function code in the inventory code, and the second conditional statement code of the unit test code also needs to match the conditional statement of the original unit test code, so as to achieve the technical effect that the generated unit test code can run normally.

[0034] Optionally, in the embodiment of the present disclosure, the unit test code corresponding to the target function code can also be directly generated through a preset template, wherein the preset template can be a code template set in advance for the basic function code, or the preset template can be a code template constructed by the R&D personnel according to their own business situation, combined with internal framework, dependency and public library, which is only applicable to their own business. The type of the preset template is not limited here.

[0035] Optionally, when generating the unit test code corresponding to the target function code according to the function information and the statement information, the basic test code applicable to the target function code can be generated through the preset template first, and then the unit test code can be obtained by modifying the basic test code.

[0036] It should be noted that the above content is only one preferred embodiment of generating the unit test code according to the function information and the statement information, and the method of generating the unit test code according to the function information and the statement information is not limited here. Any method and process for generating the unit test code according to the function information and the statement information are within the protection scope of the embodiments of the present disclosure, and will not be listed one by one here.

[0037] In step S108, the target function code is functionally tested based on the unit test code.

[0038] In the technical solution provided in step S108 of the present disclosure, the unit test code can be checked, and after it is determined that the unit test code does not contain error code, the unit test code is used to functionally test the target function code.

[0039] Optionally, after the function test on the target function code, the unit test code can also be cached to improve the test efficiency of the function test on the target function code.

[0040] Through the steps S102 to S108, the target function code in the inventory code is obtained, the function information and the statement information are extracted from the target function code, the unit test code corresponding to the target function code is generated based on the function information and the statement information, and the function test on the target function code is performed based on the unit test code. That is, in the embodiment of the present disclosure, the inventory code is scanned first, and the target function code in the inventory code is obtained, then the unit test code corresponding to the target function code is generated according to the function information and the statement information in the target function code, and finally the function test on the target function code is performed by using the unit test code, so as to automatically generate the unit test code, thereby solving the technical problem of low efficiency of generating the unit test code, and achieving the technical effect of improving the efficiency of generating the unit test code.

[0041] The above method of the embodiment will be further described in detail.

[0042] As an optional implementation, in step S106, the unit test code corresponding to the target function code is generated based on the function information and the statement information, including: based on the function information and the statement information, the target function code is partitioned according to the function category to obtain the test group code template corresponding to the partition result of the target function code; and the unit test code is generated based on the test group code template.

[0043] In the embodiment, the partition result of the target function code can include a function area, a business area, a data operation area, etc., and the test group code template can be the unit test code for testing the code of the above function area, business area, and data operation area. Here, only for illustration, the partition result of the target function code is not specifically limited.

[0044] In the embodiment, the target function code can be partitioned according to the function category based on the function information and the statement information, then the test group code template corresponding to the function code in the partition result is obtained according to the partition result, and finally the obtained test group code template is assembled to obtain the unit test code corresponding to the target function code, so as to automatically generate the unit test code, thereby achieving the technical effect of improving the efficiency of generating the unit test code.

[0045] It should be noted that the above is only one preferred embodiment of generating the unit test code according to the function information and the statement information, and the method of generating the unit test code according to the function information and the statement information is not specifically limited here, and any method and process for generating the unit test code according to the function information and the statement information are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0046] As an optional embodiment, the unit test code is generated based on the test group code template, including: modifying at least one test group code in the test group code template based on the first input parameter to obtain the unit test code.

[0047] In this embodiment, the first input parameter can be used to represent the data function of the target function code implementation, and at least one test group code in the test group code template can be modified according to the first input parameter to obtain the unit test code corresponding to the target function code, so as to achieve the purpose of automatically generating the unit test code through the preset template, thereby realizing the technical effect of improving the efficiency of generating the unit test code, wherein the test group code can be a test control group (Case).

[0048] For example, when modifying at least one test group code in the test group code template, the unit test code corresponding to the target function code can be generated by covering the parameter value of the function in the test group code. It should be noted that the method of modifying the test group code is not specifically limited here, and any method and process for generating the unit test code corresponding to the target function code by modifying the test group code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0049] As an optional embodiment, in step S102, the target function code in the inventory code is obtained, including: scanning the syntax tree of the inventory code to determine the logical branches of the functions in the inventory code; and extracting the target function code from the inventory code according to the logical branches.

[0050] In this embodiment, when the target function code in the inventory code is obtained, the logical branches of the functions in the inventory code can be obtained by scanning the syntax tree of the inventory code, and then the reference relationship of the target function code in the inventory code is determined according to the logical branches of the functions, and the target function code is extracted from the inventory code according to the reference relationship, so as to achieve the technical effect of improving the efficiency of extracting the target function code from the inventory code, wherein the syntax tree can be an abstract syntax tree (Abstract Syntax Tree, AST).

[0051] It should be noted that the above is only one preferred embodiment of extracting target function code from the obtained stock code, and the method of extracting target function code from the obtained stock code is not specifically limited here, and any method and process for extracting target function code from the obtained stock code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0052] As an optional embodiment, the method further comprises: after the function test on the target function code based on the unit test code, covering the original unit test code corresponding to the original function code in the stock code based on the unit test code, wherein the original unit test code is used for function test on the original function code.

[0053] In this embodiment, after the function test on the target function code based on the unit test code, the original unit test code corresponding to the original function code in the stock code can be covered using the unit test code, so as to achieve the technical effect of timely updating the stock code according to the target function code, wherein the original unit test code can be used for function test on the original function code.

[0054] As an optional embodiment, the method further comprises: before the function test on the target function code based on the unit test code, performing static code analysis on the unit test code and the target function code, determining the first matching result of the second input parameter and the first input parameter, and determining the second matching result of the first conditional statement and the second conditional statement code; based on the first matching result and the second matching result, determining whether the unit test code contains error code.

[0055] In this embodiment, before the function test on the target function code based on the unit test code, the unit test code and the target function code can also be subjected to static code analysis by the AST static code checking method to obtain the first matching result of the second input parameter and the first input parameter, and the second matching result of the first conditional statement and the second conditional statement code, and then the unit test code is checked according to the obtained first matching result and second matching result to determine whether the unit test code contains error code, so as to achieve the purpose of checking the generated unit test code, and further realize the technical effect of improving the accuracy of the generated unit test code, wherein the error code can be an error code in the unit test code, used to represent that the unit test code cannot be used for testing the target function code.

[0056] Optionally, before the function test on the target function code based on the unit test code, the code insertion point of the target function code and the unit test code can also be checked by using the above-mentioned AST static code checking method, so as to achieve the technical effect of ensuring the accuracy of the target function code and the unit test code.

[0057] It should be noted that the above method of performing static code analysis on the unit test code and the target function code is a preferred embodiment, and any method and process for performing static code analysis on the unit test code and the target function code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0058] As an optional embodiment, based on the first matching result and the second matching result, it is determined whether the unit test code contains error code, comprising: in response to the first matching result being that the second input parameter is not successfully matched with the first input parameter, and / or the second matching result being that the first conditional statement code is not successfully matched with the second conditional statement code, it is determined that the unit test code and / or the target function code contains error code.

[0059] In this embodiment, after the AST static code checking method is used to perform static code analysis on the unit test code and the target function code, if one of the first matching result and the second matching result is an unsuccessful matching result, it can be determined that the unit test code and / or the target function code contains error code, and the error code needs to be modified until the unit test code and the target function code do not contain error code, so as to achieve the technical effect of ensuring the accuracy of the unit test code and the target function code.

[0060] As an optional embodiment, the method further comprises: obtaining the assertion code of the unit test code before performing function test on the target function code based on the unit test code.

[0061] In this embodiment, before performing function test on the target function code based on the unit test code, the assertion code of the unit test code can also be obtained from the assertion code template, and then the unit test code is debugged through the assertion code, so as to achieve the technical effect of performing regular check on the output parameter of the test code, wherein the assertion code can be dynamically generated assertion code or directly written assertion code.

[0062] It should be noted that the generation method of the assertion code is not limited here, and any method and process for generating the assertion code are within the protection scope of the embodiments of the present disclosure, and are not listed one by one here.

[0063] Next, another method for generating unit test code according to an embodiment of the present disclosure will be introduced.

[0064] FIG. 2(a) is a flowchart of another method for generating unit test code according to an embodiment of the present disclosure, as shown in FIG. 2(a), the method can include the following steps:

[0065] In step S202, the inventory code is acquired.

[0066] In the technical solution provided in step S202, the inventory code can be acquired, and the inventory code can be an inventory service code. The inventory code can include all functional codes to be detected in the entire test project, such as original functional codes and target functional codes obtained by modifying the original functional codes. The target functional codes can be codes obtained by modifying the original functional codes according to the research and development requirements of the research and development project.

[0067] It should be noted that the type of the target functional code is not specifically limited here, and any newly added functional code in the inventory code is within the protection scope of the embodiments of the present disclosure, and will not be described here.

[0068] In step S204, the inventory code is input into the unit test code generation model for analysis and processing to obtain the unit test code corresponding to the target functional code.

[0069] In the technical solution provided in step S204, the unit test code generation model can be a code production tool for automatically generating the unit test code. The unit test code generation model can generate the unit test code corresponding to the target functional code by using the unit test code generation method described above. Figure 1

[0070] In this embodiment, the inventory code can be input into the unit test code generation model, and then the inventory code is analyzed and processed by the unit test code generation model to generate the unit test code corresponding to the target functional code.

[0071] Optionally, the unit test code generation model can include at least a visual input source, a generation engine, and an output package. The visual input source can be used to input the inventory code. The generation engine can be used to determine the target functional code in the inventory code and generate the unit test code corresponding to the target functional code. The output package can be used to output the generated unit test code. It should be noted that the specific manifestation of the unit test code generation model is not limited here, and any unit test code generation model generated by using the unit test code generation method in the embodiments of the present disclosure is within the protection scope of the embodiments of the present disclosure, and will not be enumerated here.

[0072] ​Fig. 2(b) is a schematic diagram of a computer product for executing the generation method of the unit test code according to an embodiment of the present disclosure. As shown in Fig. 2(b), the above steps S202 to S204 can be executed by a computing device. The inventory code is input into the computing device. The computing device analyzes and processes the inventory code based on the generation model of the unit test code, and outputs the unit test code corresponding to the target function code in the inventory code, so as to improve the efficiency of generating the unit test code.

[0073] Fig. 2(c) is a schematic diagram of a human-computer interaction operation interface for executing the generation method of the unit test code according to an embodiment of the present disclosure. As shown in Fig. 2(c), the inventory code 202 can be input by triggering the control 201 on the operation interface, and the unit test code 203 corresponding to the target function code in the inventory code can be obtained.

[0074] It should be noted that the computer product and the human-computer interaction operation interface for executing the generation method of the unit test code are not specifically limited here. Any computer product and human-computer interaction operation interface for executing the generation method of the unit test code are within the protection scope of the present disclosure, and will not be illustrated one by one here.

[0075] Through the above steps S202 to S204, the inventory code is obtained, the inventory code is input into the generation model of the unit test code for analysis and processing, and the unit test code corresponding to the target function code is obtained, so as to solve the technical problem of low efficiency of generating the unit test code, and achieve the technical effect of improving the efficiency of generating the unit test code.

[0076] The generation method of the unit test code according to an embodiment of the present disclosure will be further introduced below in combination with a preferred embodiment.

[0077] At present, in order to reduce the online bug rate, the unit test needs to be written in advance before the product goes online. This not only improves the code quality, but also makes the code design better, and even greatly reduces the time spent on debugging.

[0078] When writing unit tests, not only is it necessary to construct the single test body code of each newly added function to cover all branches as much as possible, but it is also necessary to avoid false positives caused by unstable downstream test environments when testing downstream services. If there is no downstream test environment, it is also necessary to find a way to Mock (simulate) the stub data to bypass, and when constructing databases and caches, since most microservices currently have a general framework that has encapsulated databases and caches at the bottom layer, although this is convenient for business use, it is not flexible, resulting in an increase in the workload of Mock stub data. In addition, it is necessary to clean up all test data written after the single test is completed to avoid the situation that dirty data generated by multiple single tests causes inaccurate single test assertions.

[0079] In the related art, due to the business scenario and the many upstream and downstream dependencies, it mainly relies on manual writing of unit tests to test the logic branch coverage, that is, by manually sorting out the business processes of the newly added functions, constantly running the coverage script, and checking the uncovered branches. When requesting downstream, in order to reduce costs, it is almost directly connected to the real test environment. However, manually writing single tests will consume a lot of research and development costs, and directly connecting to the downstream environment will also have many unstable factors. If there is no test data in the downstream, it is necessary to re-construct, so that the instability of the downstream service will also cause the unit test assertion to fail, and if the address connected directly is filled in incorrectly as online, it may also cause online accidents.

[0080] However, in the embodiments of the present disclosure, a tool for quickly generating unit test codes of all newly added methods is provided, and the tool can quickly Mock downstream requests, databases, and cache operations, and the tool also has the ability to automatically clean up test data, which can free up the efforts of researchers and developers in writing unit tests, thereby achieving the technical effect of improving the efficiency of generating unit test codes.

[0081] Figure 3 is a schematic diagram of a display interface of a tool for generating unit test codes according to an embodiment of the present disclosure, as Figure 3 shown, the tool mainly includes a visual input source 301, a generation engine 302, and a whole package output && incremental output 303, wherein the visual input source 301 can include an Application Programming Interface (API) service registration 3011, a database 3012, a downstream dependency 3013, and an offline task 3014, the generation engine 302 can include a Schema constructor 3021, a rule binding processor 3022, a generator 3023, and a local scaffold 3024, and the whole package output && incremental output 303 can include an output package 3031.

[0082] Optionally, the interface service registration 3011 can include basic information 30111, request parameter setting 30112, and response parameter setting 30113, wherein the basic information 30111 can include route setting, method path setting, permission setting, and interface description, the request parameter setting 30112 can include business multi-scene parameter assembly (business in the middle), query (Query) parameter, form parameter, image file (Raw) parameter, header (Header) parameter, and binding parameter verification rule, and the response parameter setting 30113 can include business multi-scene parameter assembly (business in the middle), file (JavaScript Object Notation, abbreviated as Json) import, template writing, and error code binding.

[0083] Optionally, the database 3012 can include automatic pulling database (Database, abbreviated as DB) information, automatic pulling table information, and mapping table relationship; the downstream dependency 3013 can include service basic information, service parameter information, and service configuration; and the offline task 3014 can include task type, task data source structure, and task configuration.

[0084] Optionally, the generator 3023 can include interface service filter file unit test subject generation, interface service business entry file unit test subject generation, service business layer code unit test subject generation, database operation unit test subject generation, downstream dependency encapsulation unit test subject generation, business configuration operation function unit test subject generation, downstream service simulation test data construction, database scheme construction, simulation test data construction, and cache simulation test data construction; and the local scaffolding 3024 can include incremental synchronization, code difference (Difference, abbreviated as diff), code backup, dependency update, plug-in integration, and scaffolding update.

[0085] Optionally, the output package 3031 can include interface service filter file unit test subject, interface service business entry file unit test subject, service business layer code unit test subject, database operation unit test subject, downstream dependency encapsulation unit test code, business configuration operation function unit test code, downstream service simulation test data configuration file, database scheme construction, simulation test data file, and cache simulation test data file.

[0086] It should be noted that the above content is only a preferred example, and the content displayed on the display interface of the tool for generating unit test code is not specifically limited herein, and any content displayed on the display interface of the tool for generating unit test code is within the protection scope of the embodiments of the present disclosure, which will not be enumerated and described herein.

[0087] Optionally, the above-mentioned visual input source can generate a business function description and code production tool for the R&D personnel to generate a whole package project, which can be used to describe the business through related interface definitions, downstream dependency definitions, data storage definitions, cache definitions, offline and online task definitions, etc. During the definition process, the R&D personnel can manually fill in part of the content according to each description capability, which is used to name and define related dependency parameters, check the related capabilities, and then the system dynamically produces the corresponding function code and the single test code for the function according to the above description capability.

[0088] In this embodiment, the template of the required capability in the cloud native service can be quickly input through the above-mentioned visual input source, and then the final implementation code is processed by the generation engine, and the single test main function of all business entry definitions is automatically generated, and various case test parameters are automatically assembled by the R&D, and the business only needs to fill in the parameter values of the coverage case to generate the final test code. At the same time, the schema code for building a database and a table can also be automatically generated in the local according to the database and cache template filled by the business. Before the single test is run, a local small database and a remote dictionary service (Remote Dictionary Server, referred to as Redis) are constructed in the memory, Mock data can be added by the business, and all can be destroyed after the memory is released at the end of the single test running.

[0089] The method for automatically generating unit test code by the tool is further introduced below.

[0090] Firstly, the single test main body of all function area codes can be converted into a general template by a template engine (Free Marker), wherein the general template can be a template preset in advance according to the basic function, for example, a template dynamically generated by the input and output parameters of the single test type and dependency and the assertion related to the parameters on the R&D platform, wherein the single test type can include functional single test, business layer single test, data layer single test, tool class single test and downstream request single test, etc. The general template can also be a code template suitable for only the business of the self built by the R&D personnel according to the business situation, combined with the internal framework, dependency, public library of the team, for example, the R&D personnel also configure the template generated by the custom.

[0091] Secondly, all single test codes of the function area can be generated according to the above-mentioned template, for example, functional single test, business (Service) layer single test and data access object (Data Access Object, referred to as Dao) layer single test.

[0092] The method of generating all unit test codes of a functional area through a template can include: first, the functional area can be divided according to the capabilities selected and added by the developer on the development platform, for example, a functional area, a business area, and a data operation area; then, the functional code and the corresponding unit test code of each area are dynamically generated according to the data input by the developer on the platform combined with the template; and finally, the insertion points of the functional code and the unit test code generated by the AST static code are checked according to the input and output parameters of the functional code and the unit test code, so that the technical effects that the developer can develop the core business code while creating test cases, Mock downstream data, storage data, cache data, and adding assertion expressions in the platform are achieved, and the local unit test code can be dynamically updated after the code created by the developer is saved in the platform, and the local unit test code can be directly run to view the test effect.

[0093] Optionally, the functions not covered by unit tests can be scanned by AST analysis, the function parameter list type information is extracted, the unit test body and the initialization test case are constructed, the branch logic and conditional statements inside the function are extracted to construct test cases covering all branches, other functions dependent on the function are extracted and the dependent function behaviors are analyzed, for example, downstream requests, database operations, and distributed cache operations, because these dependent remote access behaviors are prone to instability and affect test results due to downstream service exceptions or network exceptions when running unit test coverage, therefore, such information can be collected and displayed through an inherited Integrated Development Environment (IDE) plug-in for business development to select to construct Mock stub data locally. Finally, the return value list of the function is scanned and extracted, the several forms of return value returned according to the context business logic are analyzed, the error code is extracted, and then the code segment of the basic assertion is dynamically generated. Because the scanning and execution of the entire project package is relatively slow, a local caching strategy is implemented in the scaffold layer, the entire package is scanned and cached locally for the first time, and only incremental scanning is performed subsequently.

[0094] Finally, the parameter structure body specific to each functional area is assembled, and the parameter values of the coverage cases are filled in to generate the final test code.

[0095] It should be noted that the parameter definition of each functional area is completed on the platform, so the parameter structure is known before the code is generated, but if the developer modifies the parameter structure locally after generating the code on the platform, the local scaffold will also scan and ask whether to correct the coverage to the platform specification.

[0096] In this embodiment, the local scaffolding uses a structured query language (SQL) engine that can parse standard SQL and optimize queries, which provides a simple interface to allow custom table data source implementations, provides a server implementation compatible with the protocol (MySQL), and can be used to build a database and table in memory and fill in mock data.

[0097] In this embodiment, simple assertion statements can also be set and converted into single test assertion code, for example, the platform defines several commonly used assertion expressions for regular checks on output parameters, and developers can dynamically generate local assertion code after platform settings or directly write locally to achieve the technical effect of facilitating the use of assertion functions.

[0098] In this embodiment, the single test body of all function area codes is first converted into a general template through a template engine, then all single test codes of the function area are generated through the template, and finally the parameter structure body specific to each function area is assembled and the parameter values covering the case are filled in to generate the final test code, thereby solving the technical problem of low efficiency of generating unit test code and achieving the technical effect of improving the efficiency of generating unit test code.

[0099] The disclosure also provides a unit test code generation method and device. Figure 1 The unit test code generation method of the unit test code generation device of the embodiment shown.

[0100] Figure 4 is a schematic diagram of a unit test code generation device according to an embodiment of the disclosure, as Figure 4 The unit test code generation device 400 can include a first acquisition unit 401, an extraction unit 402, a generation unit 403, and a test unit 404.

[0101] The first acquisition unit 401 is configured to acquire target function code in the inventory code, wherein the target function code is modified from the original function code in the inventory code.

[0102] The extraction unit 402 is configured to extract function information and statement information from the target function code, wherein the function information at least includes a first input parameter of a function in the target function code, and the statement information at least includes a first conditional statement code in the target function code, and the first input parameter is used to represent a data function implemented by the target function code.

[0103] The generating unit 403 is configured to generate the unit test code corresponding to the target function code based on the function information and the statement information, wherein the second input parameter of the function in the unit test code matches the first input parameter, the second input parameter is used to represent the number of test cases of the unit test code for testing the target function code, and the second conditional statement code in the unit test code matches the first conditional statement code.

[0104] The testing unit 404 is configured to perform the function test on the target function code based on the unit test code.

[0105] Optionally, the generating unit 403 includes a partition module configured to partition the target function code according to the function category based on the function information and the statement information, to obtain a test group code template corresponding to the partition result of the target function code, wherein the test group code template includes at least the test group code corresponding to each function partition in the partition result; and a generating module configured to generate the unit test code based on the test group code template.

[0106] Optionally, the generating module includes a modifying submodule configured to modify at least one test group code in the test group code template based on the first input parameter, to obtain the unit test code.

[0107] Optionally, the first obtaining unit 401 includes a scanning module configured to scan the syntax tree of the inventory code to determine the logical branch of the function in the inventory code; and an extracting module configured to extract the target function code from the inventory code according to the logical branch.

[0108] Optionally, the apparatus further includes a covering unit configured to cover the original unit test code corresponding to the original function code in the inventory code based on the unit test code after performing the function test on the target function code based on the unit test code, wherein the original unit test code is used to perform the function test on the original function code.

[0109] Optionally, the apparatus further includes an analyzing unit configured to perform the static code analysis on the unit test code and the target function code before performing the function test on the target function code based on the unit test code, to determine the first matching result of the second input parameter and the first input parameter, and to determine the second matching result of the first conditional statement and the second conditional statement code; and a determining unit configured to determine whether the unit test code includes error code based on the first matching result and the second matching result, wherein the error code is used to at least represent that the unit test code cannot be used to test the target function code.

[0110] Optionally, the determining unit comprises a determining module configured to determine that the unit test code and / or the target function code contains error code in response to the first matching result being that the second input parameter is not successfully matched with the first input parameter and / or the second matching result being that the first conditional statement code is not successfully matched with the second conditional statement code.

[0111] Optionally, the apparatus further comprises a second obtaining unit configured to obtain assertion code of the unit test code, wherein the assertion code is extracted from an assertion code template, and the assertion code template comprises at least the assertion code; and a debugging unit configured to debug the unit test code based on the assertion code.

[0112] In the unit test code generation apparatus according to the embodiments of the present disclosure, the first obtaining unit is configured to obtain target function code in inventory code, wherein the target function code is modified from original function code in the inventory code; the extracting unit is configured to extract function information and statement information from the target function code, wherein the function information at least comprises a first input parameter of a function in the target function code, and the statement information at least comprises a first conditional statement code in the target function code, and the first input parameter is used to represent a data function implemented by the target function code; the generating unit is configured to generate unit test code corresponding to the target function code based on the function information and the statement information, wherein a second input parameter of a function in the unit test code is matched with the first input parameter, the second input parameter is used to represent a number of test cases of the unit test code for testing the target function code, and a second conditional statement code in the unit test code is matched with the first conditional statement code; and the testing unit is configured to perform function testing on the target function code based on the unit test code, thereby solving the technical problem of low efficiency of generating the unit test code and achieving the technical effect of improving the efficiency of generating the unit test code.

[0113] The embodiments of the present disclosure further provide a unit test code generation apparatus for performing the unit test code generation method shown in FIG. 2.

[0114] Figure 5 is a schematic diagram of another unit test code generation apparatus according to the embodiments of the present disclosure, as shown in Figure 5 The unit test code generation apparatus 500 can comprise a third obtaining unit 501 and a processing unit 502.

[0115] The third obtaining unit 501 is configured to obtain inventory code, wherein the inventory code at least comprises target function code, and the target function code is modified from original function code in the inventory code.

[0116] The processing unit 502 is configured to input the inventory code into a unit test code generation model for analysis and processing to obtain unit test code corresponding to the target function code.

[0117] In the unit test code generation apparatus provided by the embodiment of the present disclosure, the third obtaining unit is configured to obtain inventory code, wherein the inventory code at least includes target function code, and the target function code is obtained by modifying original function code in the inventory code; and the processing unit is configured to input the inventory code into a generation model of unit test code for analysis and processing, to obtain unit test code corresponding to the target function code, thereby solving the technical problem of low efficiency of generating unit test code, and achieving the technical effect of improving the efficiency of generating unit test code.

[0118] In the technical solution of the present disclosure, the acquisition, storage and application of user personal information comply with relevant laws and regulations and do not violate public order and good customs.

[0119] According to the embodiments of the present disclosure, the present disclosure further provides an electronic device, a readable storage medium and a computer program product.

[0120] Figure 6 is a schematic diagram of an electronic device for implementing the unit test code generation method of the embodiments of the present disclosure. The electronic device is intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device can also represent various forms of mobile devices, such as personal digital processors, cellular telephones, smart phones, wearable devices, and other similar computing devices. The components shown here, their connections, and their functions, as well as their implementation, are merely examples and are not intended to limit the implementations described and / or claimed herein.

[0121] As shown in Figure 6 The device 600 includes a computing unit 601 that can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 602 or a computer program loaded into a random access memory (RAM) 603 from a storage unit 608. In the RAM 603, various programs and data required for the operation of the device 600 can also be stored. The computing unit 601, the ROM 602, and the RAM 603 are connected to each other through a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0122] A number of components in the device 600 are connected to the I / O interface 605, including: an input unit 606, such as a keyboard, a mouse, etc.; an output unit 607, such as various types of displays, speakers, etc.; a storage unit 608, such as a magnetic disk, an optical disk, etc.; and a communication unit 609, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 609 allows the device 600 to exchange information / data with other devices through computer networks, such as the Internet, and / or various telecommunication networks.

[0123] The computing unit 601 can be various general and / or special purpose processing components with processing and computing capabilities. Some examples of the computing unit 601 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The computing unit 601 performs various methods and processes described above, such as the generation method of unit test code. For example, in some embodiments, the generation method of unit test code can be implemented as a computer software program, which is tangibly embodied in a machine-readable medium, such as the storage unit 608. In some embodiments, part or all of the computer program can be loaded and / or installed onto the device 600 via the ROM 602 and / or the communication unit 609. When the computer program is loaded onto the RAM 603 and executed by the computing unit 601, one or more steps of the generation method of unit test code described above can be performed. Alternatively, in other embodiments, the computing unit 601 can be configured to perform the generation method of unit test code by any other appropriate means, such as by means of firmware.

[0124] The various implementations of the systems and techniques described above herein can be realized in a digital electronic circuit system, an integrated circuit system, a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), an application specific standard product (ASSP), a system on a chip system (SOC), a complex programmable logic device (CPLD), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include implementation in one or more computer programs that are executable and / or interpretable on a programmable system including at least one programmable processor, which can be special or general purpose, coupled to receive data and instructions from, and to transmit data and instructions to, a storage system, at least one input device, and at least one output device.

[0125] Program code for carrying out methods of the present disclosure can be written in any combination of one or more programming languages. The program code can be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the program code, when executed by the processor or controller, produces the functions / operations specified in the flowcharts and / or block diagrams. The program code can be executed entirely on a machine, partially on a machine, partially on a machine as a standalone software package, or entirely on a remote machine or server.

[0126] In the context of the present disclosure, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium will include one or more lines of electrical connections, portable computer disks, hard disk drives, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or Flash memory), optical fibers, portable compact disc read-only memories (CD-ROMs), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0127] To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user and a keyboard and a pointing device (e.g., a mouse or a trackball) by which the user can provide input to the computer. Other kinds of devices can be used to provide for interaction with a user as well; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form, including acoustic, speech, or tactile input.

[0128] The systems and techniques described here can be implemented in a computing system that includes a back end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front end component (e.g., a user computer having a graphical user interface or a Web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back end, middleware, or front end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network (LAN), a wide area network (WAN), and the Internet.

[0129] The computer system can include clients and servers. This relationship can be. The servers are typically remote from the clients with the interactions between them occurring over a communication network. The relationship between client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other. The servers can be cloud servers, servers of a distributed system, or servers incorporating blockchain.

[0130] It should be understood that the steps shown in the various forms above can be reordered, added to, or deleted from. For example, the steps recited in the present disclosure can be performed in parallel, in series, or in a different order, as long as the desired results of the technology disclosed in the present disclosure are achieved, which is not limited herein.

[0131] The specific implementation described above does not constitute a limitation of the protection scope of the present disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent replacements, and improvements within the spirit and principles of the present disclosure should be included in the protection scope of the present disclosure.

Claims

1. A method of generating unit test code, characterized by, The method comprises the following steps: acquiring target function code in inventory code, wherein the target function code is modified from original function code in the inventory code; extracting function information and statement information from the target function code, wherein the function information at least includes a first input parameter of a function in the target function code, and the statement information at least includes a first conditional statement code in the target function code, and the first input parameter is used to represent a data function implemented by the target function code; generating unit test code corresponding to the target function code by a preset template based on the function information and the statement information, wherein a second input parameter of a function in the unit test code matches the first input parameter, the second input parameter is used to represent a number of test cases of the unit test code for testing the target function code, the second input parameter matches original unit test code corresponding to the original target function code in the inventory code, and a second conditional statement code in the unit test code matches the first conditional statement code; performing function test on the target function code based on the unit test code; wherein acquiring the target function code in the inventory code comprises scanning a syntax tree of the inventory code to determine a logical branch of a function in the inventory code, and extracting the target function code from the inventory code according to the logical branch; extracting the target function code from the inventory code according to the logical branch comprises determining a reference relationship of the target function code in the inventory code based on the logical branch, and extracting the target function code from the inventory code based on the reference relationship.

2. The method of claim 1, wherein, Generating the unit test code corresponding to the target function code based on the function information and the statement information comprises: partitioning the target function code according to a function category based on the function information and the statement information to obtain a test group code template corresponding to a partition result of the target function code, wherein the test group code template at least includes test group code corresponding to each function partition in the partition result; generating the unit test code based on the test group code template.

3. The method of claim 2, wherein, Generating the unit test code based on the test group code template comprises: modifying at least one test group code in the test group code template based on the first input parameter to obtain the unit test code.

4. The method of claim 1, wherein, After performing the function test on the target function code based on the unit test code, the method further comprises: covering original unit test code corresponding to the original function code in the inventory code based on the unit test code, wherein the original unit test code is used to perform function test on the original function code.

5. The method of claim 1, wherein, Before performing the function test on the target function code based on the unit test code, the method further comprises: Static code analysis is performed on the unit test code and the target function code to determine the first matching result between the second input parameter and the first input parameter, and to determine the second matching result between the first conditional statement and the second conditional statement code. Based on the first matching result and the second matching result, it is determined whether the unit test code contains error code, wherein the error code is at least used to characterize that the unit test code cannot be used to test the target function code.

6. The method of claim 5, wherein, Based on the first matching result and the second matching result, determining whether the unit test code contains error code includes: In response to the first matching result being that the second input parameter and the first input parameter did not match successfully, and / or the second matching result being that the first conditional statement and the second conditional statement code did not match successfully, it is determined that the unit test code and / or the target function code contain error codes.

7. The method of claim 1, wherein, Before performing functional testing on the target functional code based on the unit test code, the method further includes: Obtain the assertion code of the unit test code, wherein the assertion code is extracted from the assertion code template, and the assertion code template includes at least the assertion code; The unit test code is debugged based on the assertion code.

8. A method of generating unit test code, characterized by, include: Obtain existing code, wherein the existing code includes at least target function code, and the target function code is obtained by modifying the original function code in the existing code; The existing code is input into the unit test code generation model for analysis and processing to obtain unit test code corresponding to the target function code, wherein the unit test code generation model is determined based on the method described in any one of claims 1 to 7.

9. An apparatus for generating unit test code, characterized by include: The first acquisition unit is used to acquire the target function code in the existing code, wherein the target function code is obtained by modifying the original function code in the existing code; An extraction unit is used to extract function information and statement information from the target function code, wherein the function information includes at least a first input parameter of the function in the target function code, and the statement information includes at least a first conditional statement code in the target function code, and the first input parameter is used to characterize the data function implemented by the target function code; A generation unit is used to generate unit test code corresponding to the target function code based on the function information and the statement information through a preset template. In the unit test code, the second input parameter of the function matches the first input parameter. The second input parameter is used to characterize the number of test cases used by the unit test code to test the target function code. The second input parameter matches the original unit test code corresponding to the original target function code in the existing code. The second conditional statement code in the unit test code matches the first conditional statement code. A test unit is used to perform functional tests on the target function code based on the unit test code. The first acquisition unit includes: a scanning module, used to scan the syntax tree of the existing code to determine the logical branches of functions in the existing code; and an extraction module, used to determine the reference relationship of the target function code in the existing code according to the logical branches; and to extract the target function code from the existing code based on the reference relationship.

10. The apparatus of claim 9, wherein, The generation unit includes: The partitioning module is used to partition the target function code according to function category based on the function information and the statement information, and obtain a test group code template corresponding to the partitioning result of the target function code, wherein the test group code template includes at least the test group code corresponding to each function partition in the partitioning result; The generation module is used to generate the unit test code based on the test group code template.

11. The apparatus of claim 10, wherein, The generation module includes: The modification submodule is used to modify at least one of the test group codes in the test group code template based on the first input parameters to obtain the unit test code.

12. The apparatus of claim 9, wherein, The device further includes: A coverage unit is used to cover the original unit test code corresponding to the original functional code in the existing code after performing functional testing on the target functional code based on the unit test code, wherein the original unit test code is used to perform functional testing on the original functional code.

13. The apparatus of claim 9, wherein, The device further includes: The analysis unit is configured to perform static code analysis on the unit test code and the target function code before performing functional testing on the target function code based on the unit test code, to determine a first matching result between the second input parameter and the first input parameter, and to determine a second matching result between the first conditional statement and the second conditional statement code. A determining unit is configured to determine, based on the first matching result and the second matching result, whether the unit test code contains an error code, wherein the error code is at least used to characterize that the unit test code cannot be used to test the target function code.

14. The apparatus of claim 13, wherein, The defined unit includes: The determination module is configured to determine, in response to the first matching result being that the second input parameter and the first input parameter did not match successfully, and / or the second matching result being that the first conditional statement and the second conditional statement code did not match successfully, that the unit test code and / or the target function code contain error codes.

15. The apparatus of claim 9, wherein, The device further includes: The second acquisition unit is used to acquire the assertion code of the unit test code before performing functional testing on the target function code based on the unit test code, wherein the assertion code is extracted from the assertion code template, and the assertion code template includes at least the assertion code; A debugging unit is used to debug the unit test code based on the assertion code.

16. An electronic device comprising: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-7.

17. A non-transitory computer readable storage medium having stored thereon computer instructions, wherein, The computer instructions are used to cause the computer to perform the method according to any one of claims 1-7.

18. A computer program product comprising a computer program that, when executed by a processor, implements the method according to any one of claims 1-7.

Citation Information

Patent Citations

  • Processing method and device of code files

    CN107305527A

  • Unit test method and device, electronic equipment and storage medium

    CN113051163A