Method, device, processor and storage medium for automatically generating unit tests

By obtaining the differential files in the distributed version control system and positioning methods that require testing, and generating unit tests in batches, the problem of low unit test generation efficiency in the existing technology is solved, and the test coverage and generation efficiency are improved.

CN119782176APending Publication Date: 2025-04-08CHINA CONSTRUCTION BANK +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411934894.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-26
Publication Date
2025-04-08

AI Technical Summary

Technical Problem

The existing automated generation unit test methods can only generate unit tests for a single Java class, and the generation efficiency is inefficient.

Method used

By obtaining the difference files between the master and development branches in a distributed version control system, locate methods that require unit testing and batch generate unit tests based on these methods.

Benefits of technology

Improves the efficiency of unit test generation, ensures that every change point can be tested, improves test coverage, and avoids possible omissions and errors when manually finding and identifying code differences.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119782176A_ABST
    Figure CN119782176A_ABST
Patent Text Reader

Abstract

The embodiments of the present application provide a method, device, processor and storage medium for automatically generating unit tests. The method includes: based on the main branch and development branch in the distributed version control system, obtaining the difference information between the main branch and the development branch; redirecting the difference information to a file to obtain a difference file; parsing the difference file to obtain the line number of the code that has changed between the main branch and the development branch; based on the line number of the code that has changed, locating the code change position corresponding to the line number of the changed code, and then locating the method that has changed corresponding to the code change position; based on the method that has changed, establishing a unit test method list; and traversing the method list to generate unit tests corresponding to each method that has changed. The method is used to generate unit tests in batches to improve the efficiency of unit test generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method, device, processor and storage medium for automatically generating unit tests. Background Art

[0002] Java unit testing focuses on verifying the smallest testable part of a Java application: usually a method, and is a vital part of the software development process. Unit testing aims to ensure the correctness, reliability, and stability of the code. Its background originates from modern software development methodologies such as test-driven development (TDD) and agile development. With the rapid development of the software industry, unit testing has become an important means to improve code quality, reduce maintenance costs, and speed up development progress. In this field, developers use test frameworks such as JUnit and TestNG, combined with simulation libraries such as Mockito and EasyMock, to conduct detailed tests on the code to discover potential defects and lay a solid foundation for subsequent integration testing and system testing.

[0003] The existing automatic unit test generation method generates corresponding unit tests according to Java classes, and can only generate unit tests for a single Java class, resulting in low generation efficiency. Summary of the invention

[0004] The purpose of the embodiments of the present application is to provide a method, device, processor and storage medium for automatically generating unit tests. The method is used to generate unit tests in batches to improve the generation efficiency of unit tests.

[0005] In order to achieve the above-mentioned objectives, the first aspect of the present application provides a method for automatically generating unit tests, including: based on the main branch and the development branch in a distributed version control system, obtaining the difference information between the main branch and the development branch; redirecting the difference information to a file to obtain a difference file; parsing the difference file to obtain the line number of the code where changes occur between the main branch and the development branch; based on the line number of the changed code, locating the code change position corresponding to the line number of the changed code, and then locating the changed method corresponding to the code change position; based on the changed method, establishing a unit test method list, the method list is used to record each of the changed methods; and traversing the method list to generate a unit test corresponding to each of the changed methods.

[0006] In an embodiment of the present application, traversing the method list and generating a unit test corresponding to each method that generates a change includes: for any one of the methods that generates a change in the method list, performing the following operations: calling a pre-selected unit test template; generating a unit test class for the changed method based on the unit test template; and writing the unit test class into a unit test class file.

[0007] In an embodiment of the present application, the method also includes: before writing the unit test class into the unit test class file, checking whether a unit test class file already exists in the specified path; if so, adding the unit test class to the end of the existing unit test class file and saving it; if not, creating a unit test class file and writing the unit test class into the created unit test class file.

[0008] In the embodiment of the present application, the distributed version control system is GIT, and the difference file is obtained through the diff command of GIT.

[0009] In an embodiment of the present application, the difference file includes: the name of the file that is different between the main branch and the development branch; the line number of the code where the difference file is changed; and the change content corresponding to the line number of the code where the change is generated.

[0010] In the embodiment of the present application, the unit test template includes: Junit4, Junit5 or TestNG.

[0011] The second aspect of the present application provides an apparatus for automatically generating unit tests, comprising: a first processing module, for obtaining difference information between a main branch and a development branch in a distributed version control system, and redirecting the difference information to a file to obtain a difference file; a second processing module, for parsing the difference file, and obtaining the line number of the code where changes occur between the main branch and the development branch; a third processing module, for locating the code change position corresponding to the line number of the changed code based on the line number of the changed code, and then locating the method for generating changes corresponding to the code change position; a fourth processing module, for establishing a unit test method list based on the method for generating changes, the method list being used to record each method for generating changes; and a fifth processing module, for traversing the method list, and generating a unit test corresponding to each method for generating changes.

[0012] In an embodiment of the present application, traversing the method list and generating a unit test corresponding to each method that generates a change includes: for any one of the methods that generates a change in the method list, performing the following operations: calling a pre-selected unit test template; generating a unit test class for the changed method based on the unit test template; and writing the unit test class into a unit test class file.

[0013] In an embodiment of the present application, the fifth processing module is also used to: before writing the unit test class into the unit test class file, check whether a unit test class file already exists in the specified path; if so, add the unit test class to the end of the existing unit test class file and save it; if not, create a unit test class file and write the unit test class into the created unit test class file.

[0014] A third aspect of the present application provides a processor configured to execute the above-mentioned method of automatically generating unit tests.

[0015] A fourth aspect of the present application provides a machine-readable storage medium having instructions stored thereon, which, when executed by a processor, configure the processor to execute the above-mentioned method for automatically generating unit tests.

[0016] A fifth aspect of the present application provides a computer program product, comprising a computer program, which, when executed by a processor, implements any one of the methods for automatically generating unit tests.

[0017] Through the above technical solution, before generating unit tests, the difference files between the main branch and the development branch in the distributed version control system are first obtained. The difference files contain the difference information between different code branches. Then, the required unit test parts are located according to the difference information, and unit tests are generated in batches through the difference files. This method of automatically generating unit tests in batches not only improves the efficiency of unit test generation, but also helps to avoid omissions and errors that may occur when manually finding and identifying code differences, ensuring that each change point can be tested and improving test coverage.

[0018] Other features and advantages of the embodiments of the present application will be described in detail in the subsequent specific implementation section. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] The accompanying drawings are used to provide a further understanding of the embodiments of the present application and constitute a part of the specification. Together with the following specific implementations, they are used to explain the embodiments of the present application, but do not constitute a limitation on the embodiments of the present application. In the accompanying drawings:

[0020] Figure 1A schematic diagram of a process flow of a method for automatically generating unit tests according to an embodiment of the present application is shown;

[0021] Figure 2 The structure block diagram of the device for automatically generating unit tests according to an embodiment of the present application is schematically shown;

[0022] Figure 3 The internal structure diagram of a computer device according to an embodiment of the present application is schematically shown.

[0023] Description of Reference Numerals

[0024] 100- means for automatically generating unit tests;

[0025] 101-first processing module;

[0026] 102-second processing module;

[0027] 103-third processing module;

[0028] 104- fourth processing module;

[0029] 105-the fifth processing module. DETAILED DESCRIPTION

[0030] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the specific implementation methods described herein are only used to illustrate and explain the embodiments of the present application, and are not used to limit the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0031] It should be noted that if the embodiments of the present application involve directional indications (such as up, down, left, right, front, back...), the directional indications are only used to explain the relative position relationship, movement status, etc. between the components under a certain specific posture (as shown in the accompanying drawings). If the specific posture changes, the directional indication will also change accordingly.

[0032] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present application, the descriptions of "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or suggesting their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but they must be based on the ability of ordinary technicians in the field to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.

[0033] The acquisition, transmission, storage, use, and processing of data in the technical solution of this application are in compliance with the relevant provisions of national laws and regulations. In addition, it should be noted that in the embodiments of this application, some existing solutions in the industry such as certain software, components, and models may be mentioned, which should be considered as exemplary, and their purpose is only to illustrate the feasibility of the implementation of the technical solution of this application, but it does not mean that the applicant has or will necessarily use the solution.

[0034] Figure 1 The flowchart of the method for automatically generating unit tests according to an embodiment of the present application is schematically shown. Figure 1 As shown, in one embodiment of the present application, a method for automatically generating unit tests is provided, comprising the following steps.

[0035] Step S101: Based on a main branch and a development branch in a distributed version control system, obtain difference information between the main branch and the development branch.

[0036] Branching is one of the core features of distributed version control systems, allowing developers to work on multiple development lines at the same time without interfering with each other. The main branch is usually called master or main, and the development branch is usually called develop. The main branch is the core branch in a distributed version control system, containing code that has been tested, stable, and ready for release, while the development branch contains newly developed new features and improvements. In this step, the difference information between the main branch and the development branch is obtained, so that the unit test part that needs to be performed can be located based on the difference information.

[0037] Step S102: redirecting and outputting the difference information to a file to obtain a difference file.

[0038] It is understandable that after comparing the main branch and the development branch, the difference information obtained is directly displayed on the terminal or command line interface. When the terminal session ends or the command line interface is closed, the difference information obtained will be directly lost. In addition, if the output content exceeds the display range of the terminal and is not scrolled and saved, the excess information may also be lost. After obtaining the difference information, the present invention outputs the difference information to a file for subsequent analysis.

[0039] Specifically, when redirecting the difference information to a file, a standard output redirection operator is used to redirect the output to a file, thereby obtaining a difference file.

[0040] Step S103: Parse the difference file to obtain the line numbers of the codes that have been changed between the main branch and the development branch.

[0041] The difference file contains the line number range of the files with differences between the main branch and the development branch. By parsing the difference file, you can accurately obtain which methods have been changed and use it to generate corresponding method-level unit tests.

[0042] Step S104: Based on the line number of the changed code, locate the code change position corresponding to the line number of the changed code, and then locate the changed method corresponding to the code change position.

[0043] Step S105: Based on the method that generates the change, a unit test method list methodList is established, and the method list methodList is used to record each method that generates the change.

[0044] After locating the changed method corresponding to the code change position, a unit test method list is established based on all the changed methods, called methodList, as the basis for subsequent unit testing. In the prior art, it is necessary to manually search and select the methods that need to generate unit tests, while the present invention establishes a method list methodList before generating unit tests, and lists all methods that need to be unit tested in advance. The method list methodList is established as a pre-process for unit testing, and can be processed and saved at any time after the code is changed and before the unit test is officially carried out, avoiding the inefficiency and duplication of work caused by searching and generating at the same time in the prior art.

[0045] Step S106: traverse the method list methodList and generate a unit test corresponding to each of the changed methods.

[0046] The existing method for automatically generating unit tests includes the following steps: selecting a Java class for which a unit test needs to be generated; calling a corresponding unit test template; generating a unit test file path; and writing the unit test content into a corresponding file. This method for automatically producing unit tests can only generate unit tests for a single Java class, and the generation efficiency is low. The method for automatically generating unit tests proposed in the present invention first obtains the difference files between different code branches based on a distributed version control system before generating unit tests, and then generates unit tests in batches based on the difference files, thereby improving the generation efficiency of unit tests. At the same time, the difference files contain the differences in all positions between the main branch and the development branch. The unit tests are generated in batches through the difference files, which avoids omissions and errors that may occur when manually searching and identifying code differences, ensures that each change point can be tested, and improves the test coverage.

[0047] According to a specific implementation, traversing the method list methodList and generating a unit test corresponding to each of the changed methods includes: for any one of the changed methods in the method list methodList, performing the following operations: calling a pre-selected unit test template; generating a unit test class for the changed method based on the unit test template; and writing the unit test class into a unit test class file.

[0048] Among them, for the Java language, common unit test templates include: Junit4, Junit5 or TestNG, etc. For different templates, the classes that need to be imported and the methods of use are also different. During the specific implementation, the template can be selected according to the actual situation.

[0049] It should be noted that generating a unit test class for a changed method is a technology known to ordinary technicians in the relevant field, and it can be appropriately selected during implementation, and the present invention will not discuss this in detail.

[0050] According to a specific implementation, the method also includes: before writing the unit test class into the unit test class file, checking whether a unit test class file already exists in the specified path; if so, adding the unit test class to the end of the existing unit test class file and saving it; if not, creating a unit test class file and writing the unit test class into the created unit test class file.

[0051] It is understandable that a unit test file contains one or more unit test classes. Each time a unit test class is generated, the generated unit test class needs to be written into the unit test class file. If no unit test class has been generated, there is no unit test class file. In this case, after the unit test class is generated, the generated unit test class is written into a new unit test class file. If there is already a unit test class, then the unit test class file already exists in the corresponding path. In this case, after the new unit test class is generated, it is added to the end of the existing unit test class file without having to create a new unit test class file. By checking whether a unit test class file already exists in the specified path, repeated creation of identical or redundant test class files is avoided.

[0052] It should be noted that when creating unit test class files, naming standards should be maintained to avoid errors when writing unit test classes into unit test class files and to avoid increasing coordination costs among team members.

[0053] According to a specific implementation, the distributed version control system is GIT.

[0054] Among them, GIT is a commonly used distributed version control system in the field. In specific implementation, according to actual conditions, other types of distributed version control systems may also be selected, such as Mercurial, Bazaar, and Fossil, etc., but the present invention is not limited thereto.

[0055] When the selected distributed version control system is GIT, the difference file is obtained through the diff command of GIT.

[0056] When using the diff command to redirect the difference information to a file, it is achieved by adding the redirection operator ">" after the command. The name of the difference file is after the operator, which is a custom name. Since the difference file needs to be parsed in the subsequent steps, a complete file path can be specified after the operator. The generated difference file is saved in this path for easy management and accurate retrieval during parsing. In addition, for the development of large projects, it is generally required for a multi-person team to complete it, and different team members are responsible for different parts. After the difference file is generated, it can be saved to a shared server for different team members to obtain.

[0057] According to a specific implementation, the difference file includes: the name of the file that is different between the main branch and the development branch; the line number of the code where the difference file is changed; and the change content corresponding to the line number of the code where the change is generated.

[0058] The changes include deletions or additions, which are displayed in text form. The added lines start with "+" and the deleted lines start with "-". For example, -line 2 in the difference file means that the second line is deleted, and +line3added means that a new line is added.

[0059] In specific implementation, the names of the files with differences between the main branch and the development branch contained in the difference file can be used to locate the corresponding files with differences, and then the line numbers of the changed code can be used to locate the corresponding methods in the corresponding files, so as to accurately locate each method that has changed. After locating all the methods that have changed, unit test classes can be generated in batches to improve generation efficiency.

[0060] The solution proposed in the present invention introduces GIT version differences. Each time the code version is changed, unit tests for the changed code can be more conveniently generated based on the original main branch and the changed development branch, overcoming the inconvenience of the existing technology that requires selecting and generating unit tests one by one each time; by generating unit tests in batches, the generation efficiency is greatly improved.

[0061] Figure 1 FIG. 1 is a flow chart of a method for automatically generating unit tests in one embodiment. It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.

[0062] Figure 2 The structure block diagram of the apparatus for automatically generating unit tests according to an embodiment of the present application is schematically shown. Figure 2 As shown, an embodiment of the present application provides a device for automatically generating unit tests, and the device 100 for automatically generating unit tests includes the following modules.

[0063] The first processing module 101 is used to obtain difference information between the main branch and the development branch based on the main branch and the development branch in the distributed version control system, and redirect the difference information to a file to obtain a difference file.

[0064] The second processing module 102 is used to parse the difference file and obtain the line number of the code that has been changed between the main branch and the development branch.

[0065] The third processing module 103 is used to locate the code change position corresponding to the line number of the changed code based on the line number of the changed code, and then locate the change method corresponding to the code change position.

[0066] The fourth processing module 104 is used to create a unit test method list methodList based on the method that generates the change, and the method list methodList is used to record each method that generates the change.

[0067] The fifth processing module 105 is used to traverse the method list methodList and generate a unit test corresponding to each of the changed methods.

[0068] According to a specific implementation, traversing the method list methodList and generating a unit test corresponding to each of the changed methods includes: for any one of the changed methods in the method list methodList, performing the following operations: calling a pre-selected unit test template; generating a unit test class for the changed method based on the unit test template; and writing the unit test class into a unit test class file.

[0069] According to a specific implementation, the fifth processing module 105 is also used to: before writing the unit test class into the unit test class file, check whether a unit test class file already exists in the specified path; if so, add the unit test class to the end of the existing unit test class file and save it; if not, create a unit test class file and write the unit test class into the created unit test class file.

[0070] According to a specific implementation, the distributed version control system is GIT, and the difference file is obtained through the diff command of GIT.

[0071] According to a specific implementation, the difference file includes: the name of the file that is different between the main branch and the development branch; the line number of the code where the difference file is changed; and the change content corresponding to the line number of the code where the change is generated.

[0072] According to a specific implementation, the unit test template includes: Junit4, Junit5 or TestNG.

[0073] The device 100 for automatically generating unit tests includes a processor and a memory. The first processing module 101, the second processing module 102, the third processing module 103, the fourth processing module 104 and the fifth processing module 105 are all stored in the memory as program units, and the processor executes the above program modules stored in the memory to implement corresponding functions.

[0074] The processor includes a kernel, which retrieves the corresponding program unit from the memory. One or more kernels can be set, and the method of automatically generating unit tests is realized by adjusting kernel parameters.

[0075] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.

[0076] An embodiment of the present application provides a storage medium on which a program is stored. When the program is executed by a processor, the method for automatically generating unit tests is implemented.

[0077] An embodiment of the present application provides a processor, which is used to run a program, wherein the method for automatically generating unit tests is executed when the program is running.

[0078] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 3 As shown. The computer device includes a processor A01, a network interface A02, a display screen A04, an input device A05 and a memory (not shown in the figure) connected through a system bus. Among them, the processor A01 of the computer device is used to provide computing and control capabilities. The memory of the computer device includes an internal memory A03 and a non-volatile storage medium A06. The non-volatile storage medium A06 stores an operating system B01 and a computer program B02. The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 in the non-volatile storage medium A06. The network interface A02 of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor A01, a method for automatically generating unit tests is implemented. The display screen A04 of the computer device can be a liquid crystal display or an electronic ink display, and the input device A05 of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.

[0079] Those skilled in the art will understand that Figure 3The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0080] In one embodiment, the apparatus 100 for automatically generating unit tests provided by the present application can be implemented in the form of a computer program. The computer program can be used in Figure 3 The computer device is run on the computer device shown. The memory of the computer device can store various program modules constituting the apparatus 100 for automatically generating unit tests, for example, Figure 2 The first processing module 101, the second processing module 102, the third processing module 103, the fourth processing module 104 and the fifth processing module 105 are shown. The computer program composed of each program module enables the processor to execute the steps of the method for automatically generating unit tests in each embodiment of the present application described in this specification.

[0081] Figure 3 The computer device shown can be Figure 2 The first processing module 101 in the device for automatically generating unit tests shown executes step S101 and step S102, the second processing module 102 executes step S103, the third processing module 103 executes step S104, the fourth processing module 104 executes step S105, and the fifth processing module 105 executes step S106.

[0082] An embodiment of the present application provides a device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, the steps in the method for automatically generating unit tests in each embodiment of the present application are implemented.

[0083] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program for initializing the steps in the method for automatically generating unit tests in various embodiments of the present application.

[0084] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0085] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0086] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0087] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0088] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0089] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0090] Computer readable media include permanent and non-permanent, removable and non-removable media, and can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0091] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0092] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.

Claims

1. A method for automatically generating unit tests, characterized in that: The method comprises: Based on the main branch and the development branch in the distributed version control system, obtaining difference information between the main branch and the development branch; Redirecting the difference information to a file to obtain a difference file; Parse the difference file to obtain the line number of the code that has been changed between the main branch and the development branch; Based on the line number of the changed code, locate the code change position corresponding to the line number of the changed code, and then locate the changed method corresponding to the code change position; Based on the method of generating changes, establishing a unit test method list, the method list is used to record each method of generating changes; and The method list is traversed to generate a unit test corresponding to each of the methods that have been changed.

2. The method according to claim 1, characterized in that Traversing the method list to generate a unit test corresponding to each of the methods that generate changes includes: for any one of the methods that generate changes in the method list, performing the following operations: Invoke the pre-selected unit test template; Based on the unit test template, generating a unit test class for the changed method; and Write the unit test class into a unit test class file.

3. The method according to claim 2, characterized in that The method further comprises: Before writing the unit test class into the unit test class file, checking whether the unit test class file already exists in the specified path; If it exists, the unit test class is added to the end of the existing unit test class file and saved; if it does not exist, a unit test class file is created and the unit test class is written into the created unit test class file.

4. The method according to claim 1, characterized in that: The distributed version control system is GIT, and the difference file is obtained through the diff command of GIT.

5. The method according to claim 1, characterized in that The difference files include: the names of the files that differ between the master branch and the development branch; The line number of the code where the difference file produces the change; and The changed content corresponding to the line number of the code where the change occurs.

6. The method according to claim 2, characterized in that The unit test templates include: Junit4, Junit5 or TestNG.

7. A device for automatically generating unit tests, characterized in that: The device comprises: A first processing module is used to obtain difference information between the main branch and the development branch based on the main branch and the development branch in the distributed version control system, and redirect the difference information to a file to obtain a difference file; A second processing module is used to parse the difference file and obtain the line number of the code that has been changed between the main branch and the development branch; A third processing module is used to locate the code change position corresponding to the line number of the changed code based on the line number of the changed code, and then locate the change method corresponding to the code change position; A fourth processing module is used to establish a unit test method list based on the method of generating changes, wherein the method list is used to record each method of generating changes; and The fifth processing module is used to traverse the method list and generate a unit test corresponding to each of the methods that have been changed.

8. The device according to claim 7, characterized in that Traversing the method list to generate a unit test corresponding to each of the methods that generate changes includes: for any one of the methods that generate changes in the method list, performing the following operations: Invoke the pre-selected unit test template; Based on the unit test template, generating a unit test class for the changed method; and Write the unit test class into a unit test class file.

9. The device according to claim 8, characterized in that The fifth processing module is also used for: Before writing the unit test class into the unit test class file, checking whether the unit test class file already exists in the specified path; If it exists, the unit test class is added to the end of the existing unit test class file and saved; if it does not exist, a unit test class file is created and the unit test class is written into the created unit test class file.

10. A processor, characterized in that: The method is configured to execute the method for automatically generating unit tests according to any one of claims 1 to 6.

11. A machine-readable storage medium having instructions stored thereon, characterized in that: When the instruction is executed by a processor, the processor is configured to execute the method for automatically generating unit tests according to any one of claims 1 to 6.

12. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the computer program implements the method for automatically generating unit tests according to any one of claims 1 to 6.