Unit test automatic generation method and device, electronic equipment and storage medium
By loading and replacing test templates during software development, generating unit test code, and using the recording and playback plug-in to generate variable values, the problems of low efficiency and insufficient accuracy of traditional unit tests are solved, and efficient and accurate unit tests are achieved generate.
Patent Information
- Application Number
- CN202411527002.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-29
- Publication Date
- 2025-02-07
AI Technical Summary
The traditional unit test generation method leads to a decrease in software development efficiency, and the accuracy of the obtained unit test results needs to be improved.
By loading the basic test template in the target code project, replacing the basic configuration variables into the project configuration variables, and generating unit test code templates; generating unit test variable values based on the recording playback file list of the recording playback plug-in, replacing the target in the unit test code template Template variable value, generate unit test function, obtain unit test code file, and store it in the target code project.
It improves the generation efficiency of unit tests and software development efficiency, while improving the accuracy of unit test results.
Smart Images

Figure CN119396730A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software testing technology, and in particular to a method, device, electronic device and storage medium for automatically generating unit tests. Background Art
[0002] In the software development process, unit testing is the testing of the smallest testable unit in the software. Unit tests are usually written during the development process to verify the correctness of the written code. Unit testing helps improve code quality, reduce debugging time, promote code refactoring, and ensure that modifications do not destroy existing functions. However, in the actual development process, the writing of unit tests takes up a lot of developers' time, resulting in reduced software development efficiency. In addition, due to the different technical capabilities of developers, the quality of the written unit tests is also different, and there are certain errors in the test results of unit tests. Summary of the invention
[0003] The main technical problem solved by the implementation method of the present application is that the traditional unit test generation method leads to reduced software development efficiency, and the accuracy of the obtained unit test results needs to be improved.
[0004] To solve the above technical problems, the first technical solution adopted in the implementation mode of the present application is: to provide a method for automatically generating unit tests, characterized in that it includes: loading a basic test template into a target code project; replacing the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template; generating corresponding unit test variable values according to a preset recording and playback file list of a recording and playback plug-in; using the unit test variable values to replace the corresponding target template variable values in the unit test code template, generating a corresponding unit test function, and obtaining a unit test code file; storing the unit test code file in the corresponding code directory in the target code project.
[0005] Optionally, the step of generating corresponding unit test variable values according to the recording and playback file list of a preset recording and playback plug-in includes: obtaining the recording and playback folder directory of the preset recording and playback plug-in, wherein the open interface of the target code project corresponds one-to-one to the recording and playback folder directory; reading the recorded request file under the recording and playback folder directory; deserializing and parsing the recorded request file to obtain the recorded network request parameters; and using the recorded network request parameters to generate the unit test variable values.
[0006] Optionally, the step of using the unit test variable value to replace the corresponding target template variable value in the unit test code template, generating the corresponding unit test function, and obtaining the unit test code file includes: generating an initial unit test code file corresponding to the recording and playback folder directory according to the unit test code template; traversing the recorded request files under the recording and playback folder directory, and generating the unit test functions corresponding to different recorded request files in the initial unit test code file according to the interface naming string of the open interface; generating the function body code of the unit test function according to the functional code segment in the unit test code template; and using the unit test variable value to replace the corresponding target template variable value in the function body code to obtain the unit test code file.
[0007] Optionally, after the step of storing the unit test code file to the corresponding code directory in the target code project, it also includes: when a unit test call instruction is received, obtaining the unit test code file according to the code directory; according to the unit test call instruction, executing the corresponding unit test function in the unit test code file to obtain a first unit test result; and returning the first unit test result.
[0008] Optionally, after the step of executing the corresponding unit test function in the unit test code file according to the unit test calling instruction to obtain the first unit test result, it also includes: according to the unit test function, obtaining the corresponding recorded request file, parsing the recorded request file, and obtaining the first recorded request result; comparing whether the first unit test result and the first recorded request result are the same; if they are not the same, generating unit test result exception record information.
[0009] Optionally, after the step of executing the corresponding unit test function in the unit test code file according to the unit test calling instruction to obtain the first unit test result, it also includes: according to the unit test function, obtaining the corresponding recorded request file, parsing the recorded request file, and obtaining a second recorded request result; calculating a first similarity between the first unit test result and the second recorded request result; determining whether the first similarity is within a preset similarity threshold range; if not within the preset similarity threshold range, generating unit test result abnormal record information.
[0010] Optionally, after the step of using the unit test variable value to replace the corresponding target template variable value in the function body code to obtain the unit test code file, it also includes: traversing the function body code of the unit test function in the unit test code file; checking whether there is the unit test function with the same function body code; if there is the unit test function with the same function body code, merging the unit test functions with the same function body code to obtain a merged unit test function; setting the different recorded request files corresponding to the unit test function before the merge, and re-corresponding to the merged unit test function.
[0011] To solve the above technical problems, the second technical solution adopted in the implementation mode of the present application is: to provide a unit test automatic generation device, including: a template loading module, used to load the basic test template into the target code project; a single test template module, used to replace the basic configuration variables in the basic test template with the project configuration variables in the target code project, to obtain the unit test code template; a single test variable value module, used to generate corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in; a single test code generation module, used to replace the corresponding target template variable value in the unit test code template with the unit test variable value, to obtain a unit test code file; a single test file storage module, used to store the unit test code file to the corresponding code directory in the target code project.
[0012] To solve the above technical problems, the third technical solution adopted in the implementation mode of the present application is: to provide an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the unit test automatic generation method as described above.
[0013] In order to solve the above technical problems, the fourth technical solution adopted in the implementation mode of the present application is: providing a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores computer-executable instructions. When the computer-executable instructions are executed by an electronic device, the electronic device executes the unit test automatic generation method as described above.
[0014] Different from the related art, the present application obtains a unit test code template by loading a basic test template into a target code project; replacing the basic configuration variables in the basic test template with the project configuration variables in the target code project; generating corresponding unit test variable values according to a preset recording and playback file list of a recording and playback plug-in; using the unit test variable values to replace the corresponding target template variable values in the unit test code template, generating a corresponding unit test function, and obtaining a unit test code file; and storing the unit test code file in the corresponding code directory in the target code project, thereby not only improving the unit test generation efficiency and software development efficiency, but also improving the accuracy of the unit test results. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] One or more embodiments are exemplarily described by corresponding drawings, which do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and the figures in the drawings do not constitute proportional limitations unless otherwise stated.
[0016] Figure 1 It is a schematic diagram of the operating environment of the unit test automatic generation method provided in an embodiment of the present application.
[0017] Figure 2 It is a schematic diagram of the execution flow of the unit test automatic generation method provided in an embodiment of the present application.
[0018] Figure 3 It is a schematic diagram of the execution flow of generating unit test variable values in the unit test automatic generation method provided in an embodiment of the present application.
[0019] Figure 4 It is a schematic diagram of the execution flow of generating a unit test code file in the unit test automatic generation method provided in an embodiment of the present application.
[0020] Figure 5 It is a schematic diagram of the execution flow of obtaining the first unit test result in the unit test automatic generation method provided in an embodiment of the present application.
[0021] Figure 6 It is a schematic diagram of the execution flow of generating abnormal record information of unit test results in the unit test automatic generation method provided in an embodiment of the present application.
[0022] Figure 7 It is a schematic diagram of the execution flow of generating abnormal record information of unit test results in the unit test automatic generation method provided in another embodiment of the present application.
[0023] Figure 8 It is a schematic diagram of the execution flow of merging unit test functions in the unit test automatic generation method provided in an embodiment of the present application.
[0024] Fig. 9 It is a schematic diagram of the system structure of the unit test automatic generation device provided in an embodiment of the present application.
[0025] Fig.10 It is a schematic diagram of the hardware structure of an electronic device for executing the unit test automation generation method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0027] It should be noted that, if there is no conflict, the various features in the embodiments of the present application can be combined with each other, all within the scope of protection of the present application. In addition, although the functional modules are divided in the device schematic diagram and the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in a different order than the module division in the device schematic diagram or the order in the flow chart.
[0028] Unless otherwise defined, all technical and scientific terms used in this specification have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used in the specification of this application are only for the purpose of describing specific embodiments and are not intended to limit this application. The term "and / or" used in this specification includes any and all combinations of one or more of the related listed items.
[0029] To facilitate understanding of this embodiment, firstly, a unit test automatic generation method disclosed in the embodiment of the present application is described in detail. Figure 1 , Figure 1 Schematic diagram of the operating environment of the unit test automatic generation method provided in the embodiment of the present application, such as Figure 1 As shown, the execution subject of the unit test automatic generation method provided in the embodiment of the present application is generally an electronic device with certain computing capabilities, such as a computer device. In some possible implementations, the unit test automatic generation method can be implemented by a processor calling a computer-readable instruction stored in a memory. Figure 1 The computer device in the term can be a server. The server can be an independent server or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms. It can be understood that Figure 1The number of computer devices in the figure is only for reference and can be expanded to any number according to actual needs.
[0030] Please continue reading Figure 2 , Figure 2 is a schematic diagram of the execution flow of the unit test automatic generation method provided in the embodiment of the present application, such as Figure 2 As shown, the process includes the following steps S1 to S5.
[0031] S1. Load the basic test template in the target code project.
[0032] For example, in a software project that mainly uses the Java programming language, you can define a basic template Java file and design the default values of common variables in the template, such as the startup main class package, test class name, startup class, etc.
[0033] S2. Replace the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template.
[0034] For example, after the above-mentioned basic test template is loaded, the default values of the general variables in the basic test template need to be modified to the project configuration variables in the target code project. For example, the default value of the startup main class package in the basic test template may be an empty string. In this case, the value of the startup main class package can be modified to the package name of the corresponding startup main class package in the above-mentioned target code project (for example, startAppClass is the corresponding package name, then the startup main class package can be configured as "startAppClass").
[0035] S3. Generate corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in.
[0036] Among them, the recording and playback plug-in is a plug-in tool integrated into the corresponding software development tool or environment, which can snapshot the interface request and save it as the corresponding recording file. In the target code project, the recording and playback plug-in is started in the form of a microservice. When the external request reaches the microservice, the plug-in will monitor and intercept the woven functions, determine whether the current request is a normal request or a playback request, and if it is a normal request, package the recording Invocation (including the request header, request url, input parameters, etc.), assemble the return value before returning, and save the recording file (serialized file); if it is a playback request (Repeat-TraceId-X is specified in the request header), parse the request input and output parameters according to the recording file, package the mock, and omit some sub-calls to directly return the outer layer result.
[0037] As an optional implementation, please continue to refer to Figure 3 , Figure 3: is a schematic diagram of the execution flow of generating unit test variable values in the unit test automatic generation method provided in the embodiment of the present application, such as Figure 3 As shown, it may include steps S31 to S34.
[0038] S31. Obtain a preset recording and playback folder directory of the recording and playback plug-in, wherein the open interface of the target code project corresponds to the recording and playback folder directory in a one-to-one manner.
[0039] For example, the name of the recording and playback folder can be the name of the above-mentioned open interface. Each different recording and playback folder represents a different open interface mentioned above. Each different recording and playback folder stores the corresponding recorded request file. Multiple requests can be initiated for the same development interface, so there are multiple different recorded request files under the same recording and playback folder.
[0040] S32: Read the recorded request file in the recording and playback folder directory.
[0041] S33. Deserialize and parse the recorded request file to obtain the recorded network request parameters.
[0042] For example, the network request parameter may include {reqUrlFormat}, {indexNum}, {reqUrl}, {index}, etc.
[0043] S34. Generate unit test variable values using the recorded network request parameters.
[0044] Among them, obtaining the recorded network request parameters from the recorded request file to generate the corresponding unit test variable values can solve the source problem of variable values when writing unit tests and improve the efficiency of unit test generation.
[0045] S4. Use the unit test variable value to replace the corresponding target template variable value in the unit test code template, generate the corresponding unit test function, and obtain the unit test code file.
[0046] As an optional implementation, please continue to refer to Figure 4 , Figure 4 : is a schematic diagram of the execution flow of generating a unit test code file in the unit test automatic generation method provided in the embodiment of the present application, such as Figure 4 As shown, it may include steps S41 to S44.
[0047] S41. Generate an initial unit test code file corresponding to the recording and playback folder directory according to the unit test code template.
[0048] Among them, according to the above description of the operating principle of the recording and playback plug-in, each recording and playback folder corresponds to an open interface, and the unit test also focuses on the testing of the development interface, so different initial unit test code files can be generated according to different recording and playback folders.
[0049] S42, traversing the recorded request files in the recording and playback folder directory, and generating unit test functions corresponding to different recorded request files in the initial unit test code file according to the interface naming string of the open interface.
[0050] Among them, according to the above description of the operating principle of the recording and playback plug-in, it can be seen that different recorded request files under the recording and playback folder directory are multiple access requests for the open interface, but in each access request, there may be different request parameters included, and different request return results. Therefore, different corresponding unit test functions can be generated in the initial unit test code file to represent the situation of accessing the above open interface with different request parameters, and the test cases for the development interface can be further enriched, thereby further improving the coverage of unit tests and the accuracy of the tested interfaces.
[0051] S43. Generate function body code of the unit test function according to the functional code segment in the unit test code template.
[0052] S44. Use the unit test variable value to replace the corresponding target template variable value in the function body code to obtain a unit test code file.
[0053] Among them, after using the unit test variable value to replace the corresponding target template variable value in the function body code, the corresponding programming language format check can also be performed (for example, checking whether the variable type of the unit test variable value is the same as the type of the variable value in the target template), which can be a programming language syntax check or a programming language typesetting style check to unify the coding style and improve the readability of the programming code.
[0054] S5. Store the unit test code file to the corresponding code directory in the target code project.
[0055] As an optional implementation, please continue to refer to Figure 5 , Figure 5 : is a schematic diagram of the execution flow of obtaining the first unit test result in the unit test automatic generation method provided in the embodiment of the present application, such as Figure 5 As shown, it may include steps S51 to S53.
[0056] S51. When a unit test calling instruction is received, a unit test code file is obtained according to a code directory.
[0057] S52. According to the unit test calling instruction, execute the corresponding unit test function in the unit test code file to obtain a first unit test result.
[0058] As an optional implementation, please continue to refer to Figure 6 , Figure 6 Schematic diagram of the execution flow of generating abnormal record information of unit test results in the unit test automatic generation method provided in the embodiment of the present application, such as Figure 6 As shown, it may include steps S521 to S523.
[0059] S521. According to the unit test function, obtain a corresponding recorded request file, parse the recorded request file, and obtain a first recorded request result.
[0060] S522: Compare the first unit test result and the first recorded request result to see whether they are the same.
[0061] In step S522, when comparing whether the two results are the same, the comparison may be made on whether the number of fields, field names, field data structures, and values corresponding to the fields in the comparison results are the same.
[0062] S523. If they are not the same, generate unit test result exception record information.
[0063] As an optional implementation, please continue to refer to Figure 7 , Figure 7 FIG. 1 is a schematic diagram of an execution flow of generating abnormal record information of unit test results in a unit test automatic generation method provided in another embodiment of the present application, such as Figure 7 As shown, steps S524 to S527 may be included.
[0064] S524. According to the unit test function, obtain the corresponding recorded request file, parse the recorded request file, and obtain a second recorded request result.
[0065] S525: Calculate a first similarity between the first unit test result and the second recorded request result.
[0066] Among them, a corresponding similarity calculation algorithm can be designed, for example, it can be the sum of the field sub-similarity and the field value sub-similarity, that is, the first aspect is to compare the number of fields and the similarity of the corresponding field names, the ratio of the number of identical field names in the two results, and the second aspect is the similarity of the same field values, the ratio of the field values corresponding to the same fields in the two results. For example, the timestamp, business status code, etc. in the field may not be the same in the two results, but according to business logic and other rules, both results should be correct unit test results.
[0067] S526: Determine whether the first similarity is within a preset similarity threshold range.
[0068] The similarity threshold may be further adjusted according to actual unit test results. For example, in a test environment with a low tolerance, the similarity between the two results may be required to be as high as possible.
[0069] S527: If the similarity is not within the preset threshold range, generate unit test result abnormality record information.
[0070] S53. Return the first unit test result.
[0071] As an optional implementation, please continue to refer to Figure 8 , Figure 8 : is a schematic diagram of the execution flow of merging unit test functions in the unit test automatic generation method provided in the embodiment of the present application, such as Figure 8 As shown, it may include steps S441 to S444.
[0072] S441. Traverse the function body code of the unit test function in the unit test code file.
[0073] S442. Check whether there is a unit test function with the same function body code.
[0074] S443. If there are unit test functions with the same function body code, merge the unit test functions with the same function body code to obtain a merged unit test function.
[0075] S444. Set different recorded request files corresponding to the unit test functions before merging, and re-match them to the unit test functions after merging.
[0076] Among them, merging unit test functions with the same function body code can avoid redundancy of unit test code files. Because there are two or more identical requests in the recorded request file, merging multiple corresponding identical unit test functions at this time not only reduces the number of unit test functions in the unit test code file (reducing the number of functions that can fail in unit tests), but also further reduces the size of the unit test code file and simplifies the structure of the unit test code file, which can not only improve the efficiency of unit test execution, but also reduce the time for error finding after a unit test error.
[0077] The unit test automatic generation method provided in the embodiment of the present application loads a basic test template into a target code project; replaces the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template; generates corresponding unit test variable values according to a preset recording and playback file list of a recording and playback plug-in; uses the unit test variable values to replace the corresponding target template variable values in the unit test code template to generate a corresponding unit test function to obtain a unit test code file; and stores the unit test code file in a corresponding code directory in the target code project, which not only improves the unit test generation efficiency and software development efficiency, but also improves the accuracy of the unit test results.
[0078] Please continue reading Fig. 9 , Fig. 9 is a schematic diagram of the system structure of the unit test automatic generation device provided in the embodiment of the present application, such as Fig. 9 As shown, the device 90 includes: a template loading module 91, a single test template module 92, a single test variable value module 93, a single test code generation module 94 and a single test file storage module 95.
[0079] The template loading module 91 is used to load the basic test template in the target code project;
[0080] The unit test template module 92 is used to replace the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template;
[0081] The unit test variable value module 93 is used to generate corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in;
[0082] The unit test code generation module 94 is used to replace the corresponding target template variable value in the unit test code template with the unit test variable value to obtain a unit test code file;
[0083] The unit test file storage module 95 is used to store the unit test code file in the corresponding code directory in the target code project.
[0084] As an optional implementation, the unit test variable value module 93 is also used to obtain the recording and playback folder directory of the preset recording and playback plug-in, wherein the open interface of the target code project corresponds one-to-one to the recording and playback folder directory; read the recorded request file under the recording and playback folder directory; deserialize and parse the recorded request file to obtain the recorded network request parameters; and use the recorded network request parameters to generate the unit test variable value.
[0085] As an optional implementation, the unit test code generation module 94 is also used to generate an initial unit test code file corresponding to the recording and playback folder directory according to the unit test code template; traverse the recorded request files under the recording and playback folder directory, and generate the unit test functions corresponding to different recorded request files in the initial unit test code file according to the interface naming string of the open interface; generate the function body code of the unit test function according to the functional code segment in the unit test code template; use the unit test variable value to replace the corresponding target template variable value in the function body code to obtain the unit test code file.
[0086] As an optional implementation, the unit test automatic generation device 90 also includes a unit test execution module, which is used to obtain the unit test code file according to the code directory when receiving a unit test call instruction; execute the corresponding unit test function in the unit test code file according to the unit test call instruction to obtain a first unit test result; and return the first unit test result.
[0087] As an optional implementation, the unit test execution module is also used to obtain the corresponding recorded request file according to the unit test function, parse the recorded request file, and obtain a first recorded request result; compare whether the first unit test result and the first recorded request result are the same; if they are not the same, generate unit test result exception record information.
[0088] As an optional implementation, the unit test execution module is also used to obtain the corresponding recorded request file according to the unit test function, parse the recorded request file, and obtain a second recorded request result; calculate a first similarity between the first unit test result and the second recorded request result; determine whether the first similarity is within a preset similarity threshold range; if not within the preset similarity threshold range, generate unit test result abnormal record information.
[0089] As an optional implementation, the unit test code generation module 94 is also used to traverse the function body code of the unit test function in the unit test code file; check whether there are unit test functions with the same function body code; if there are unit test functions with the same function body code, merge the unit test functions with the same function body code to obtain a merged unit test function; set the different recorded request files corresponding to the unit test functions before the merge, and re-match the merged unit test functions.
[0090] It should be noted that the above-mentioned unit test automatic generation device can execute the unit test automatic generation method provided in the embodiment of the present application, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not fully described in the embodiment of the unit test automatic generation device, please refer to the unit test automatic generation method provided in the embodiment of the present application.
[0091] Please continue reading Fig.10 , Fig.10 1 is a schematic diagram of the hardware structure of an electronic device 1000 for executing the unit test automation generation method provided in an embodiment of the present application, such as Fig.10 As shown, the electronic device 1000 includes:
[0092] One or more processors 1010 and memory 1020, Fig.10 A processor 1010 is taken as an example.
[0093] The processor 1010 and the memory 1020 may be connected via a bus or other means. Fig.10 The example of connecting through bus is taken in the following.
[0094] The memory 1020, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as program instructions / modules corresponding to the unit test automation generation method in the embodiment of the present application. The processor 1010 executes various functional applications and data processing of the server by running the non-volatile software programs, instructions and modules stored in the memory 1020, that is, the unit test automation generation method in the above method embodiment is implemented.
[0095] The memory 1020 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required by at least one function; the data storage area may store data created according to the use of the unit test automation generation device, etc. In addition, the memory 1020 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 1020 may optionally include a memory remotely arranged relative to the processor 1010, and these remote memories may be connected to the unit test automation generation device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0096] The one or more modules are stored in the memory 1020, and when executed by the one or more processors 1010, the XXXXX method in any of the above method embodiments is executed, for example, the XXXXX method described above is executed. Figure 2Steps S1 to S5 of the method, Figure 3 Steps S31 to S34 of the method, Figure 4 Steps S41 to S44 of the method, Figure 5 Steps S51 to S53 of the method, Figure 6 Steps S521 to S523 of the method, Figure 7 Steps S524 to S527 of the method, Figure 8 Steps S441 to S444 of the method are implemented Fig. 9 The functions of modules 91-95 in.
[0097] The above-mentioned product can execute the method provided in the embodiment of the present application, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not fully described in this embodiment, please refer to the method provided in the embodiment of the present application.
[0098] The embodiment of the present application provides a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are executed by one or more processors, such as Fig.10 A processor 1010 in the embodiment may enable the one or more processors to execute the unit test automation generation method in any of the above method embodiments, for example, to execute the above described Figure 2 Steps S1 to S5 of the method, Figure 3 Steps S31 to S34 of the method, Figure 4 Steps S41 to S44 of the method, Figure 5 Steps S51 to S53 of the method, Figure 6 Steps S521 to S523 of the method, Figure 7 Steps S524 to S527 of the method, Figure 8 Steps S441 to S444 of the method are implemented Fig. 9 The functions of modules 91-95 in.
[0099] The present application embodiment provides a computer program product, the computer program product includes a computer program stored on a non-volatile computer-readable storage medium, the computer program includes program instructions, when the program instructions are executed by the electronic device, the electronic device is enabled to execute the unit test automation generation method in any of the above method embodiments, for example, execute the above described Figure 2 Steps S1 to S5 of the method, Figure 3 Steps S31 to S34 of the method, Figure 4 Steps S41 to S44 of the method, Figure 5 Steps S51 to S53 of the method, Figure 6 Steps S521 to S523 of the method, Figure 7 Steps S524 to S527 of the method, Figure 8 Steps S441 to S444 of the method are implemented Fig. 9 The functions of modules 91-95 in.
[0100] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0101] Through the description of the above implementation methods, ordinary technicians in this field can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, it can also be implemented by hardware. Ordinary technicians in this field can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0102] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Under the concept of the present application, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present application as described above, which are not provided in detail for the sake of simplicity. Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features can be replaced by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for automatically generating unit tests, characterized in that: include: Load the basic test template in the target code project; Replacing the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template; Generate corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in; Use the unit test variable value to replace the corresponding target template variable value in the unit test code template, generate the corresponding unit test function, and obtain a unit test code file; The unit test code file is stored in a corresponding code directory in the target code project.
2. The method for automatically generating unit tests according to claim 1, characterized in that: The step of generating corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in includes: Obtaining a recording and playback folder directory of the preset recording and playback plug-in, wherein the open interface of the target code project corresponds to the recording and playback folder directory in a one-to-one manner; Read the recorded request file in the recording and playback folder directory; Deserialize and parse the recorded request file to obtain recorded network request parameters; The unit test variable value is generated using the recorded network request parameter.
3. The method for automatically generating unit tests according to claim 2, characterized in that: The step of replacing the corresponding target template variable value in the unit test code template with the unit test variable value, generating the corresponding unit test function, and obtaining the unit test code file includes: Generate an initial unit test code file corresponding to the recording and playback folder directory according to the unit test code template; Traversing the recorded request files in the recording and playback folder directory, and generating the unit test functions corresponding to different recorded request files in the initial unit test code file according to the interface naming string of the open interface; Generate the function body code of the unit test function according to the functional code segment in the unit test code template; The unit test variable value is used to replace the corresponding target template variable value in the function body code to obtain the unit test code file.
4. The method for automatically generating unit tests according to claim 3, characterized in that: After the step of storing the unit test code file in the corresponding code directory in the target code project, the method further includes: When a unit test call instruction is received, obtaining the unit test code file according to the code directory; According to the unit test calling instruction, execute the corresponding unit test function in the unit test code file to obtain a first unit test result; Returns the first unit test result.
5. The method for automatically generating unit tests according to claim 4, characterized in that: After the step of executing the corresponding unit test function in the unit test code file according to the unit test calling instruction to obtain the first unit test result, the method further includes: According to the unit test function, obtaining the corresponding recorded request file, parsing the recorded request file, and obtaining a first recorded request result; Comparing whether the first unit test result and the first recorded request result are the same; If they are not the same, unit test result exception record information is generated.
6. The method for automatically generating unit tests according to claim 4, characterized in that: After the step of executing the corresponding unit test function in the unit test code file according to the unit test calling instruction to obtain the first unit test result, the method further includes: According to the unit test function, obtaining the corresponding recorded request file, parsing the recorded request file, and obtaining a second recorded request result; Calculating a first similarity between the first unit test result and the second recorded request result; Determining whether the first similarity is within a preset similarity threshold range; If it is not within the preset similarity threshold range, then unit test result abnormal record information is generated.
7. The method for automatically generating unit tests according to claim 3, characterized in that: After the step of replacing the corresponding target template variable value in the function body code with the unit test variable value to obtain the unit test code file, the step further includes: Traversing the function body code of the unit test function in the unit test code file; Check whether there is the unit test function with the same function body code; If the unit test functions with the same function body code exist, then the unit test functions with the same function body code are merged to obtain a merged unit test function; Set the different recorded request files corresponding to the unit test functions before merging, and re-match them to the unit test functions after merging.
8. A unit test automatic generation device, characterized in that: include: Template loading module, used to load basic test templates in the target code project; A unit test template module, used to replace the basic configuration variables in the basic test template with the project configuration variables in the target code project to obtain a unit test code template; The unit test variable value module is used to generate corresponding unit test variable values according to the preset recording and playback file list of the recording and playback plug-in; A unit test code generation module, used to replace the corresponding target template variable value in the unit test code template with the unit test variable value to obtain a unit test code file; The unit test file storage module is used to store the unit test code file in the corresponding code directory in the target code project.
9. An electronic device, characterized in that: include: 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, and the instructions are executed by the at least one processor so that the at least one processor can execute the unit test automatic generation method according to any one of claims 1 to 7.
10. A non-volatile computer-readable storage medium, characterized in that: The non-volatile computer-readable storage medium stores computer-executable instructions. When the computer-executable instructions are executed by an electronic device, the electronic device executes the unit test automatic generation method according to any one of claims 1 to 7.