Unit testing processing methods, devices, equipment, media and products
Automatically create unit test frameworks and methods by listening to code save events, solving the problems of low unit test efficiency and high labor costs in the existing technology, and achieving efficient unit test execution.
Patent Information
- Application Number
- CN202411555295.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-04
- Publication Date
- 2025-04-01
- Estimated Expiration
- 2044-11-04
AI Technical Summary
In the prior art, unit tests are inefficient in execution and high labor costs, and developers need to manually create frameworks and unit test methods related to unit tests.
By listening to the code saving events implemented by the user, the target code package is automatically obtained and the root node directory of the business module is generated according to its directory structure, the test subdirectory and unit test classes of the candidate service class are created, and the unit test methods for the target business module are generated.
It realizes automatic creation of unit test-related frameworks and unit test methods without manual intervention by developers, reducing labor costs and improving the execution efficiency of unit tests.
Smart Images

Figure CN119046177B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a unit test processing method, device, equipment, medium and product. Background Art
[0002] In order to better ensure the robustness of the software, corresponding unit tests will be built for business scenario functions when the software program is written. Unit testing, also known as module testing, is a test work to verify the correctness of program modules. Program modules are the smallest testable components of software. A program module is a single program, function or process, etc.
[0003] The existing technology usually requires developers to manually create a unit test-related framework and unit test methods before executing unit tests, which not only has high labor costs but also affects the execution efficiency of unit tests. Summary of the invention
[0004] The present invention provides a unit test processing method, device, equipment, medium and product to solve the problem that the prior art relies on developers to manually create a unit test-related framework and unit test method, which is costly and affects the efficiency of unit test execution.
[0005] According to one aspect of the present invention, a method for processing a unit test is provided, comprising:
[0006] When a code saving event implemented by a user is monitored, a target code package corresponding to the code saving event is obtained, and a root node directory of a target business module is generated according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located;
[0007] Determine at least one candidate service class included in the code in the target code package, and create a test subdirectory corresponding to each candidate service class in the root node directory;
[0008] Creating candidate unit test classes corresponding to each candidate service class in each test subdirectory, and creating candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are method names corresponding to the public service class methods;
[0009] Determine the candidate service class methods included in each of the candidate service classes, and determine the candidate method body associated with each of the candidate service classes from the method bodies according to the candidate method type to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name;
[0010] According to each of the candidate unit test method names and the target method body associated with each of the candidate unit test method names, a target unit test method for the target business module is generated in each of the candidate unit test classes.
[0011] According to another aspect of the present invention, there is provided a unit test processing device, comprising:
[0012] A root node directory generation module is used to obtain a target code package corresponding to the code saving event when monitoring a code saving event implemented by a user, and generate a root node directory of a target business module according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located;
[0013] A test subdirectory creation module, used to determine at least one candidate service class contained in the code in the target code package, and create test subdirectories corresponding to each candidate service class in the root node directory;
[0014] A method name creation module, used to create candidate unit test classes corresponding to each candidate service class in each test subdirectory, and create candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are method names corresponding to the public service class methods;
[0015] A target method body determination module is used to determine the candidate service class methods contained in each of the candidate service classes, and determine the candidate method body associated with each of the candidate service classes from the method bodies according to the candidate method type to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name;
[0016] The target unit test method generation module is used to generate a target unit test method for the target business module in each candidate unit test class according to each candidate unit test method name and a target method body associated with each candidate unit test method name.
[0017] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:
[0018] at least one processor; and
[0019] a memory communicatively connected to the at least one processor; wherein,
[0020] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the unit test processing method described in any one of the present invention.
[0021] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement any unit test processing method of the present invention when executed.
[0022] According to another aspect of the present invention, a computer program product is provided, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any one of the unit test processing methods of the present invention.
[0023] The present invention obtains the target code package corresponding to the code saving event when monitoring the code saving event implemented by the user, and generates the root node directory of the target business module according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located; determines at least one candidate service class contained in the code of the target code package, and creates test subdirectories corresponding to each candidate service class in the root node directory; creates candidate unit test classes corresponding to each candidate service class in each test subdirectory, and creates candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are the method names corresponding to the public service class methods; determines the candidate service class methods contained in each candidate service class, and According to the candidate method type to which each candidate service class method belongs, and the association relationship between the method type and the method body, the candidate method body associated with each candidate service class is determined from the method body as the target method body associated with the corresponding candidate unit test method name; according to each candidate unit test method name and the target method body associated with each candidate unit test method name, a target unit test method for the target business module is generated in each candidate unit test class, thereby achieving the effect of automatically creating a unit test-related framework and unit test method for the target business module when a code save event is monitored, without the need for manual intervention by developers, reducing labor costs while speeding up the efficiency of creating unit test-related frameworks and unit test methods, further ensuring the execution efficiency of unit tests.
[0024] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0026] Figure 1 A flowchart of a unit test processing method provided in Embodiment 1 of the present invention;
[0027] Figure 2 A flowchart of a unit test processing method provided in Embodiment 2 of the present invention;
[0028] Figure 3 A schematic diagram of the structure of a unit test processing device provided in Embodiment 3 of the present invention;
[0029] Figure 4 It is a structural schematic diagram of an electronic device for implementing the unit test processing method of an embodiment of the present invention. DETAILED DESCRIPTION
[0030] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0031] It should be noted that the terms "candidate", "target", "first", "second", "auxiliary", "random", "current", "to be supervised", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0032] Embodiment 1
[0033] Figure 1This is a flowchart of a method for processing unit tests provided in the first embodiment of the present invention. This embodiment is applicable to the case of automatically constructing a unit test-related framework and a unit test method. The method can be executed by a unit test processing device, which can be implemented in the form of hardware and / or software. Figure 1 As shown, the method includes:
[0034] S101. When a code saving event implemented by a user is monitored, a target code package corresponding to the code saving event is obtained, and a root node directory of a target business module is generated according to a directory structure of the target code package.
[0035] The code save event refers to the code save event implemented for the target code package when the user completes the code editing of the target code package to save all the codes in the edited target code package. The target code package refers to the code package constructed by the user based on the code edited according to business needs. The target code package adopts the storage method of tree directory, which is similar to the folder in the file system, and is used to facilitate the search and use of classes. The tree directory in the target code package is called the directory structure of the target code package.
[0036] A business module refers to a program module built for one or more business scenario functions. A program module is the smallest measurable component of the software. A program module is a single program, function, or process, etc. The target business module is the business module where the target code package is located.
[0037] In one embodiment, a monitoring plug-in is pre-developed in the code editing software, for example, including but not limited to the IDEA monitoring plug-in, etc. The monitoring plug-in monitors the code saving event of the user in the code editing software in real time, and when the user implements the code saving event in the code editing software, the target code package corresponding to the current code saving event is obtained from the code editing software.
[0038] In view of the fact that current software development all adopts the MVC code structure, the code structure of the target code package is inferred, and the target business module where the target code package is located is determined based on the inferred structure of the code structure. That is, according to the business scenario function targeted by the target code package, the associated business module is determined as the target business module.
[0039] Perform directory structure analysis on the target code package to determine the directory structure of the target code package, and generate the root node directory of the target business module based on the directory structure of the target code package. Optionally, the directory structure of the target code package is directly used as the root node directory of the target business module. The root node directory is also called the root directory, which refers to the root directory in the directory structure corresponding to the target business module.
[0040] S102: Determine at least one candidate service class included in the code in the target code package, and create a test subdirectory corresponding to each candidate service class in the root node directory.
[0041] Among them, the service class is also called the Service class, which is usually a Java class marked as "Service" and is responsible for processing the complex business logic of the program. It helps to separate the different levels of the program, thereby improving the maintainability and testability of the code. It can be understood that the candidate service class refers to all Java classes marked as "Service" in the target code package. The test subdirectory is also called the test subdirectory, which is used to store unit test class java files.
[0042] In one implementation, the Java classes included in the target code package are annotated and traversed to determine the annotation information corresponding to each Java class, and at least one Java class with the annotation information "Service" is used as a candidate service class. According to the candidate service class names corresponding to each candidate service class, test subdirectories corresponding to each candidate service class are created in the root node directory.
[0043] S103. Create candidate unit test classes corresponding to each candidate service class in each test subdirectory, and create candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class.
[0044] Among them, the unit test class refers to the test class that checks and verifies the smallest testable unit in the program (target business module). The public method name is the method name corresponding to the public service class method. The public service class method refers to the service class method that is in a public state and can be called by the public at will. The service class method refers to the method defined in the service class.
[0045] In one embodiment, according to the candidate service class names corresponding to the candidate service classes and the preset unit test class identifiers, candidate unit test classes corresponding to the candidate service classes are created in each test subdirectory. Further, the public service class methods defined in each candidate service class are determined, and the method names corresponding to the public service class methods are determined as the public method names. Further, according to the public method names, corresponding candidate unit test method names with the same names are created in each candidate unit test class.
[0046] Exemplarily, assuming that the public service class method defined in candidate service class A is service class method 1, and assuming that the method name corresponding to service class method 1 is "aabbcc", a candidate unit test method name named "aabbcc" is created in the candidate unit test class corresponding to candidate service class A.
[0047] S104. Determine the candidate service class methods included in each candidate service class, and determine the candidate method body associated with each candidate service class from the method body based on the candidate method type to which each candidate service class method belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name.
[0048] Among them, the candidate service class method is the method included (defined) in the candidate service class. The candidate service class method is usually used to encapsulate business logic and handle specific functions or tasks. The candidate service class defines a series of candidate service class methods to perform specific operations and return the results to the caller.
[0049] The candidate method type refers to the method type corresponding to the candidate service class method. In programming, the method body refers to a code block that encapsulates a method to implement a specific function, which is used to define and implement the function of a method. In this embodiment, the method body is a code block that implements the detection function. The association between the method type and the method body is pre-constructed and maintained. The association reflects which method body can be used to detect the correctness of the candidate service class method of which method type. For example, assuming that there is an association between method body A and method type B, it means that method body A can be used to detect the correctness of the candidate service class method of method type B.
[0050] In one embodiment, the candidate service class methods contained in each candidate service class are determined, and based on the association relationship between the method type and the method body, the candidate method body associated with each candidate service class is determined, and then the candidate method body associated with each candidate service class is used as the target method body associated with the candidate unit test method name corresponding to each candidate service class.
[0051] Exemplarily, assuming that according to the association relationship between method type and method body, candidate method body A is determined to have an association relationship with candidate method type B, candidate method body A is used as the target method body associated with the candidate unit test method name corresponding to candidate method type B.
[0052] S105 . Generate a target unit test method for a target business module in each candidate unit test class according to each candidate unit test method name and a target method body associated with each candidate unit test method name.
[0053] In one implementation, the target method body associated with each candidate unit test method name is combined with each candidate unit test method name to generate a target unit test method for the target business module in each candidate unit test class.
[0054] The embodiment of the present invention obtains the target code package corresponding to the code saving event when monitoring the code saving event implemented by the user, and generates the root node directory of the target business module according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located; determines at least one candidate service class contained in the code in the target code package, and creates test subdirectories corresponding to each candidate service class in the root node directory; creates candidate unit test classes corresponding to each candidate service class in each test subdirectory, and creates candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are the method names corresponding to the public service class methods; determines the candidate service class methods contained in each candidate service class , and according to the candidate method type to which each candidate service class method belongs, and the association relationship between the method type and the method body, the candidate method body associated with each candidate service class is determined from the method body as the target method body associated with the corresponding candidate unit test method name; according to each candidate unit test method name and the target method body associated with each candidate unit test method name, a target unit test method for the target business module is generated in each candidate unit test class, so that when the code save event is monitored, the unit test-related framework and unit test method for the target business module are automatically created, without the need for manual intervention by developers, which reduces labor costs while speeding up the efficiency of creating unit test-related frameworks and unit test methods, further ensuring the execution efficiency of unit tests.
[0055] Embodiment 2
[0056] Figure 2 This is a flowchart of a unit test processing method provided in the second embodiment of the present invention. This embodiment further optimizes and expands the above embodiment and can be combined with the above optional implementation methods. Figure 2 As shown, the method includes:
[0057] S201. When a code saving event implemented by a user is monitored, a target code package corresponding to the code saving event is obtained, and a root node directory of a target business module is generated according to a directory structure of the target code package.
[0058] S202. Determine at least one candidate service class contained in the code in the target code package, determine the candidate service class names corresponding to each candidate service class, and create test subdirectories corresponding to each candidate service class in the root node directory according to the candidate service class names corresponding to each candidate service class.
[0059] Exemplarily, assuming that the candidate service class name corresponding to any candidate service class is "abcd", a test subdirectory corresponding to the candidate service class is created in the root node directory and named "abcd".
[0060] By determining the candidate service class name corresponding to each candidate service class, and creating a test subdirectory corresponding to each candidate service class in the root node directory according to the candidate service class name corresponding to each candidate service class, the unique correspondence between the test subdirectory and the candidate service class is ensured.
[0061] S203. Combine the candidate service class names corresponding to each candidate service class with the preset unit test class identifier to generate candidate unit test class identifiers corresponding to each candidate service class; create candidate unit test classes corresponding to each candidate service class in each test subdirectory according to the candidate unit test class identifiers corresponding to each candidate service class.
[0062] Exemplarily, assuming that the candidate service class name corresponding to any candidate service class is "abcd" and the preset unit test class identifier is "test", "abcd" and "test" are combined to generate the candidate unit test class identifier "abcdtest" corresponding to the candidate service class, and a candidate unit test class named "abcdtest" is created in the test subdirectory corresponding to the candidate service class.
[0063] By combining the candidate service class names corresponding to each candidate service class with the preset unit test class identifiers, candidate unit test class identifiers corresponding to each candidate service class are generated; according to the candidate unit test class identifiers corresponding to each candidate service class, candidate unit test classes corresponding to each candidate service class are created in each test subdirectory, thereby ensuring the unique correspondence between the candidate service class and the candidate unit test class.
[0064] S204. Create candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class.
[0065] S205. Determine the candidate service class methods included in each candidate service class, and determine the method body associated with the candidate method type to which any candidate service class method belongs, based on the association relationship between the method type and the method body, as an auxiliary method body; use the auxiliary method body as the candidate method body associated with the candidate service class to which the candidate service class method belongs, and as the target method body associated with the corresponding candidate unit test method name.
[0066] Exemplarily, assuming that the candidate method type to which any candidate service class method belongs is "method type X", and assuming that in the association relationship between the method type and the method body, the method body associated with "method type X" is "method body Y", then "method body Y" is used as an auxiliary method body, and "method body Y" is further used as a candidate method body associated with "method type X", and as the target method body associated with the candidate unit test method name corresponding to "method type X".
[0067] Optionally, this embodiment divides the candidate method types into "add", "delete", "modify" and "check", and pre-associates corresponding method bodies with various candidate method types for unit testing.
[0068] For example, the candidate method type "add" is associated with method body 1, and the functions of method body 1 include: querying the database based on the object of the added method to see whether it exists and whether the saved value is equal, and calling the delete method of the service class to query the data again to see whether it exists, thereby verifying the correctness of the method.
[0069] The candidate method type "delete" is associated with method body 2, and the functions of method body 2 include: calling the delete method of the service class, querying the data again to see if it exists, and verifying the correctness of the method.
[0070] The candidate method type "change" is associated with method body 3, and the functions of method body 3 include: changing some attributes with random numbers, calling the add method to save, querying the data again to see if it exists, and verifying the correctness of the method.
[0071] The candidate method type "check" is associated with method body 4, and the functions of method body 4 include: querying data based on different values of parameters, whether it exists, and verifying the correctness of the method.
[0072] By determining the method body associated with the candidate method type to which any candidate service class method belongs as an auxiliary method body based on the association relationship between the method type and the method body; the auxiliary method body is used as the candidate method body associated with the candidate service class to which the candidate service class method belongs. Since the association relationship between the method type and the method body is set in advance, the accuracy and adaptability of the candidate method body associated with the candidate service class that is finally determined is guaranteed, thereby further ensuring the accuracy of the unit test.
[0073] S206 . Generate a target unit test method for a target business module in each candidate unit test class according to each candidate unit test method name and a target method body associated with each candidate unit test method name.
[0074] Optionally, after generating the root node directory of the target business module according to the directory structure of the target code package, the method further includes:
[0075] A1. Create a test resource subdirectory in the root node directory, and determine at least one candidate entity class contained in the code in the target code package.
[0076] The test resource subdirectory refers to the directory used to store resource files needed during the test. An entity class is also called an ENTITY class, which is usually a Java class marked as "ENTITY". It usually maps to a table in a database. It contains various attributes, each attribute corresponds to a field in the table. In addition, an entity class can also contain business methods. It can be understood that a candidate entity class refers to all Java classes marked as "ENTITY" in the target code package.
[0077] In one implementation, a test resource subdirectory is created in the root node directory, and the Java classes contained in the target code package are annotated and traversed to determine the annotation information corresponding to each Java class, and at least one Java class with annotation information "ENTITY" is selected as a candidate entity class.
[0078] B1. Create candidate template classes corresponding to each candidate entity class in the test resource subdirectory, and create candidate simulation objects corresponding to each candidate entity class in each candidate template class.
[0079] In order to improve development efficiency, entity classes are usually encapsulated into a template class for direct calling in the project, that is, any candidate entity class uniquely corresponds to one candidate template class. Simulation objects, also known as mock objects, are virtual objects corresponding to each generated candidate entity class, which facilitates the execution of unit tests, that is, any candidate entity class uniquely corresponds to one candidate simulation object.
[0080] In one embodiment, the candidate entity class names corresponding to each candidate entity class are combined with the preset template class identifier to generate the candidate template class identifiers corresponding to each candidate entity class; according to the candidate template class identifiers corresponding to each candidate service class, the candidate template classes corresponding to each candidate entity class are created in the test resource subdirectory. Exemplarily, assuming that the candidate entity class name corresponding to any candidate entity class is "abcd" and the preset template class identifier is "Template", then "abcd" and "Template" are combined to generate the candidate template class identifier "abcdTemplate" corresponding to the candidate entity class, and a candidate template class named "abcdTemplate" is created in the test resource subdirectory.
[0081] Furthermore, candidate simulation objects corresponding to each candidate entity class are created in each candidate template class.
[0082] C1. Create a test context class in the root node directory, and write the candidate simulation data values corresponding to each candidate simulation object into the entity class attributes of the test context class through the initialization method to initialize the entity class attributes.
[0083] The test context class refers to an "open" test environment that provides a program, which can simulate the program context and perform more fine-grained testing. You can use the @SpringBootTest annotation to create a program context, which starts the complete program context and loads all configurations. The initialization method refers to the method used to initialize the entity class attributes of the test context class. Candidate mock data values, also known as mock data, are a type of mock data used to simulate the data of a real data source, with similar data structures and fields to achieve the purpose of unit testing.
[0084] In one implementation, a test context class is created in the root node directory, and the generated initialization method is called to write the candidate simulation data values corresponding to each candidate simulation object into the entity class attributes of the test context class to initialize the entity class attributes.
[0085] A test resource subdirectory is created in the root node directory, and at least one candidate entity class contained in the code in the target code package is determined; candidate template classes corresponding to each candidate entity class are created in the test resource subdirectory, and candidate simulation objects corresponding to each candidate entity class are created in each candidate template class; a test context class is created in the root node directory, and candidate simulation data values corresponding to each candidate simulation object are written into the entity class attributes of the test context class through an initialization method for initializing the entity class attributes, thereby achieving the effect of initializing the entity class attributes of the test context class, which is conducive to ensuring the orderly execution of unit tests.
[0086] Optionally, the candidate simulation data values corresponding to each candidate simulation object are determined in the following manner:
[0087] Obtain at least one optional simulation data value corresponding to any candidate entity class in the simulation data value database; randomly select an optional simulation data value as a random simulation data value, and use the random simulation data value as a candidate simulation data value of the candidate simulation object corresponding to the candidate entity class.
[0088] For example, assuming that in the simulation data value database, the optional simulation data values corresponding to the candidate entity class A include "simulated data value 1", "simulated data value 2" and "simulated data value 3", and assuming that "simulated data value 2" is randomly drawn, "simulated data value 2" is used as the random simulation data value, and "simulated data value 2" is used as the candidate simulation data value of the candidate simulation object corresponding to the candidate entity class A.
[0089] By obtaining at least one optional simulation data value corresponding to any candidate entity class in the simulation data value database; randomly extracting an optional simulation data value as the random simulation data value, and using the random simulation data value as the candidate simulation data value of the candidate simulation object corresponding to the candidate entity class, the execution speed of the process is accelerated while ensuring that the candidate simulation data value can be obtained, and because the optional simulation data value is randomly extracted as the candidate simulation data value, the data content of the candidate simulation data value is enriched.
[0090] Optionally, after creating the test context class in the root directory, also include:
[0091] C11. According to the association relationship between the entity class and the service class, determine the candidate service class associated with any candidate entity class as the target service class.
[0092] Among them, the association relationship between the entity class and the service class reflects which entity class can call the service class method of which service class.
[0093] For example, assuming that in the association relationship between entity class and service class, the service class associated with candidate entity class A is "candidate service class B", then "candidate service class B" is used as the target service class associated with candidate entity class A, that is, candidate entity class A can call the candidate service class method of "candidate service class B".
[0094] C12. Create a data call method corresponding to the target service class in the test context class, and use the candidate entity class as a parameter of the data call method, so that the candidate entity class can call the target service class method contained in the target service class through the data call method.
[0095] In one embodiment, a data call method corresponding to the target service class is created in the test context class, and the candidate entity class is injected into the data call method as a parameter of the data call method, so that the candidate entity class can call the target service class method contained in the target service class through the data call method.
[0096] By determining the candidate service class associated with any candidate entity class as the target service class based on the association relationship between the entity class and the service class, creating a data call method corresponding to the target service class in the test context class, and using the candidate entity class as a parameter of the data call method, the candidate entity class can call the target service class method contained in the target service class through the data call method, ensuring that the candidate entity class can call the service class method normally and orderly, which is further conducive to the orderly execution of unit tests.
[0097] Optionally, after creating candidate unit test classes corresponding to each candidate service class in each test subdirectory, the following is further included:
[0098] Add the first pending supervision mark to each candidate unit test class.
[0099] The first identification to be supervised is used to determine whether the executed unit test class needs supervision when unit testing the target business module. The first identification to be supervised includes but is not limited to "SpringBootTest" or "RunWith".
[0100] Optionally, after generating a target unit test method for a target business module in each candidate unit test class, the method further includes:
[0101] Add a second pending supervision flag to each target unit test method.
[0102] The second pending supervision mark is used to determine whether the executed unit test method needs supervision when unit testing the target business module. The second pending supervision mark includes but is not limited to "NG-UT" and the like.
[0103] By adding a first to-be-supervised flag to each candidate unit test class and a second to-be-supervised flag to each target unit test method, it is helpful to supervise the unit test class and unit test method when unit testing the target business module to ensure the orderly execution of the unit test.
[0104] Optionally, also include:
[0105] A2. When performing unit testing on a target business module, determine a first identifier corresponding to the current unit test class being executed.
[0106] B2. When the first identifier is the first identifier to be supervised, determine the second identifier corresponding to each unit test method in the current unit test class.
[0107] In one embodiment, when unit testing the target business module, the first identifier corresponding to the current unit test class currently being executed is determined. When the first identifier is the first to-be-supervised identifier, it indicates that the current unit test class is a unit test class that requires supervision, and then the second identifier corresponding to each unit test method in the current unit test class is further determined.
[0108] C2. The unit test method with the second identifier as the second to-be-supervised identifier is used as the to-be-supervised unit test method, and when the execution status of each to-be-supervised unit test method is execution completed, the target simulation data value in the entity class attribute of the test context class is cleared.
[0109] In one embodiment, the unit test method with the second identifier as the second to-be-supervised identifier in the current unit test class is used as the to-be-supervised unit test method, and the execution status of each to-be-supervised unit test method is determined in real time. When the execution status of each to-be-supervised unit test method is completed, the target simulation data value in the entity class attribute of the test context class is cleared.
[0110] Among them, the target simulation data value is the candidate simulation data value corresponding to the target simulation object, the target simulation object is the candidate simulation object corresponding to the target entity class, the target entity class is the candidate entity class associated with the current service class, and the current service class is the candidate service class corresponding to the current unit test class.
[0111] Exemplarily, assuming that the candidate service class corresponding to the current unit test class is "candidate service class C", then "candidate service class C" is used as the current service class; assuming that the candidate entity class associated with "candidate service class C" is "candidate entity class D", then "candidate entity class D" is used as the target entity class; assuming that the candidate simulation object corresponding to "candidate entity class D" is "candidate simulation object D", then "candidate simulation object D" is used as the target simulation object; assuming that the candidate simulation data value corresponding to "candidate simulation object D" is "candidate simulation data value X", then "candidate simulation data value X" is used as the target simulation data value, and the "candidate simulation data value X" in the entity class attributes of the test context class is cleared.
[0112] By determining the first identifier corresponding to the current unit test class being executed when unit testing the target business module; when the first identifier is the first identifier to be supervised, determining the second identifier corresponding to each unit test method in the current unit test class; using the unit test method with the second identifier as the second identifier to be supervised as the unit test method to be supervised, and when the execution status of each unit test method to be supervised is execution completed, clearing the target simulation data value in the entity class attribute of the test context class, so that after the unit test method that needs to be supervised is executed, the relevant simulation data value in the entity class attribute of the test context class is cleared in time, which is conducive to reassigning values and performing unit testing again, thereby ensuring the repeatability of the unit test.
[0113] Embodiment 3
[0114] Figure 3 This is a schematic diagram of the structure of a unit test processing device provided in Embodiment 3 of the present invention. Figure 3 As shown, the device comprises:
[0115] The root node directory generating module 31 is used to obtain the target code package corresponding to the code saving event when monitoring the code saving event implemented by the user, and generate the root node directory of the target business module according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located;
[0116] A test subdirectory creation module 32, used to determine at least one candidate service class contained in the code of the target code package, and create a test subdirectory corresponding to each candidate service class in the root node directory;
[0117] The method name creation module 33 is used to create candidate unit test classes corresponding to each candidate service class in each test subdirectory, and create candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are method names corresponding to the public service class methods;
[0118] The target method body determination module 34 is used to determine the candidate service class methods contained in each of the candidate service classes, and determine the candidate method body associated with each of the candidate service classes from the method bodies according to the candidate method type to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name;
[0119] The target unit test method generation module 35 is used to generate a target unit test method for the target business module in each candidate unit test class according to each candidate unit test method name and a target method body associated with each candidate unit test method name.
[0120] Optionally, the test subdirectory creation module 32 is specifically used to:
[0121] Determine the candidate service class names corresponding to the candidate service classes, and create test subdirectories corresponding to the candidate service classes in the root node directory according to the candidate service class names corresponding to the candidate service classes.
[0122] Optionally, the method name creation module 33 is specifically used for:
[0123] Combining the candidate service class names corresponding to the candidate service classes with the preset unit test class identifiers to generate candidate unit test class identifiers corresponding to the candidate service classes;
[0124] According to the candidate unit test class identifiers respectively corresponding to the candidate service classes, candidate unit test classes respectively corresponding to the candidate service classes are created in the test sub-directories.
[0125] Optionally, the target method body determination module 34 is specifically used to:
[0126] According to the association relationship between the method type and the method body, determine the method body associated with the candidate method type to which any of the candidate service class methods belongs as the auxiliary method body;
[0127] The auxiliary method body is used as a candidate method body associated with the candidate service class to which the candidate service class method belongs.
[0128] Optionally, the device further includes an entity class attribute initialization module, which is specifically used to:
[0129] Creating a test resource subdirectory in the root node directory, and determining at least one candidate entity class included in the code in the target code package;
[0130] Creating candidate template classes corresponding to the candidate entity classes in the test resource subdirectory, and creating candidate simulation objects corresponding to the candidate entity classes in the candidate template classes;
[0131] A test context class is created in the root node directory, and the candidate simulation data values corresponding to each of the candidate simulation objects are written into the entity class attributes of the test context class through an initialization method to initialize the entity class attributes.
[0132] Optionally, the candidate simulation data values corresponding to each of the candidate simulation objects are determined in the following manner:
[0133] Acquire at least one optional simulation data value corresponding to any candidate entity class in a simulation data value database;
[0134] A piece of the optional simulation data value is randomly selected as a random simulation data value, and the random simulation data value is used as the candidate simulation data value of the candidate simulation object corresponding to the candidate entity class.
[0135] Optionally, the device further includes a parameter setting module, specifically configured to:
[0136] According to the association relationship between the entity class and the service class, determining the candidate service class associated with any of the candidate entity classes as the target service class;
[0137] A data calling method corresponding to the target service class is created in the test context class, and the candidate entity class is used as a parameter of the data calling method, so that the candidate entity class can call the target service class method contained in the target service class through the data calling method.
[0138] Optionally, the device further includes a first identification adding module, which is specifically configured to:
[0139] Adding a first to-be-supervised flag to each of the candidate unit test classes; wherein the first to-be-supervised flag is used to determine whether the executed unit test class needs to be supervised when performing unit testing on the target business module;
[0140] Optionally, the device further includes a second identification adding module, which is specifically configured to:
[0141] A second to-be-supervised flag is added to each of the target unit test methods; wherein the second to-be-supervised flag is used to determine whether the executed unit test method needs to be supervised when the target business module is unit tested.
[0142] Optionally, the device further includes a simulation data value clearing module, specifically configured to:
[0143] When unit testing the target business module, determining a first identifier corresponding to a current unit test class currently being executed;
[0144] When the first identifier is the first identifier to be supervised, determining a second identifier corresponding to each unit test method in the current unit test class;
[0145] The unit test method whose second identifier is the second identifier to be supervised is used as the unit test method to be supervised, and when the execution status of each unit test method to be supervised is execution completed, the target simulation data value in the entity class attribute of the test context class is cleared;
[0146] Among them, the target simulation data value is the candidate simulation data value corresponding to the target simulation object, the target simulation object is the candidate simulation object corresponding to the target entity class, the target entity class is the candidate entity class associated with the current service class, and the current service class is the candidate service class corresponding to the current unit test class.
[0147] The unit test processing device provided in the embodiment of the present invention can execute the unit test processing method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0148] Embodiment 4
[0149] Figure 4A schematic diagram of an electronic device 40 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.
[0150] like Figure 4 As shown, the electronic device 40 includes at least one processor 41, and a memory connected to the at least one processor 41, such as a read-only memory (ROM) 42, a random access memory (RAM) 43, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 41 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 42 or the computer program loaded from the storage unit 48 to the random access memory (RAM) 43. In the RAM 43, various programs and data required for the operation of the electronic device 40 can also be stored. The processor 41, the ROM 42, and the RAM 43 are connected to each other through a bus 44. An input / output (I / O) interface 45 is also connected to the bus 44.
[0151] A number of components in the electronic device 40 are connected to the I / O interface 45, including: an input unit 46, such as a keyboard, a mouse, etc.; an output unit 47, such as various types of displays, speakers, etc.; a storage unit 48, such as a disk, an optical disk, etc.; and a communication unit 49, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 49 allows the electronic device 40 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0152] The processor 41 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 41 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 41 executes the various methods and processes described above, such as the processing method of the unit test.
[0153] In some embodiments, the processing method of the unit test can be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 48. In some embodiments, part or all of the computer program can be loaded and / or installed on the electronic device 40 via the ROM 42 and / or the communication unit 49. When the computer program is loaded into the RAM 43 and executed by the processor 41, one or more steps of the processing method of the unit test described above can be performed. Alternatively, in other embodiments, the processor 41 can be configured to execute the processing method of the unit test by any other appropriate means (for example, by means of firmware).
[0154] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0155] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0156] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, device, or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0157] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0158] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0159] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0160] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0161] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A unit test processing method, characterized in that: include: When a code saving event implemented by a user is monitored, a target code package corresponding to the code saving event is obtained, and a root node directory of a target business module is generated according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located; Determine at least one candidate service class included in the code in the target code package, and create a test subdirectory corresponding to each candidate service class in the root node directory; Creating candidate unit test classes corresponding to each candidate service class in each test subdirectory, and creating candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are method names corresponding to the public service class methods; Determine the candidate service class methods included in each of the candidate service classes, and determine the candidate method body associated with each of the candidate service classes from the method bodies according to the candidate method type to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name; According to each of the candidate unit test method names and the target method body associated with each of the candidate unit test method names, a target unit test method for the target business module is generated in each of the candidate unit test classes.
2. The method according to claim 1, characterized in that The step of creating a test subdirectory corresponding to each candidate service class in the root node directory includes: Determine the candidate service class name corresponding to each of the candidate service classes, and create a test subdirectory corresponding to each of the candidate service classes in the root node directory according to the candidate service class name corresponding to each of the candidate service classes; The step of creating candidate unit test classes corresponding to the candidate service classes in the test sub-directories includes: Combining the candidate service class names corresponding to the candidate service classes with the preset unit test class identifiers to generate candidate unit test class identifiers corresponding to the candidate service classes; According to the candidate unit test class identifiers respectively corresponding to the candidate service classes, candidate unit test classes respectively corresponding to the candidate service classes are created in the test sub-directories.
3. The method according to claim 1, characterized in that The determining, from the method bodies, the candidate method bodies associated with each of the candidate service classes according to the candidate method types to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, comprises: According to the association relationship between the method type and the method body, determine the method body associated with the candidate method type to which any of the candidate service class methods belongs as the auxiliary method body; The auxiliary method body is used as a candidate method body associated with the candidate service class to which the candidate service class method belongs.
4. The method according to claim 1, after generating the root node directory of the target business module according to the directory structure of the target code package, further comprises: Creating a test resource subdirectory in the root node directory, and determining at least one candidate entity class included in the code in the target code package; Creating candidate template classes corresponding to the candidate entity classes in the test resource subdirectory, and creating candidate simulation objects corresponding to the candidate entity classes in the candidate template classes; A test context class is created in the root node directory, and the candidate simulation data values corresponding to each of the candidate simulation objects are written into the entity class attributes of the test context class through an initialization method to initialize the entity class attributes.
5. The method according to claim 4, characterized in that The candidate simulation data values corresponding to each of the candidate simulation objects are determined in the following manner: Acquire at least one optional simulation data value corresponding to any candidate entity class in a simulation data value database; A piece of the optional simulation data value is randomly selected as a random simulation data value, and the random simulation data value is used as the candidate simulation data value of the candidate simulation object corresponding to the candidate entity class.
6. The method according to claim 4, after creating the test context class in the root node directory, further comprising: According to the association relationship between the entity class and the service class, determining the candidate service class associated with any of the candidate entity classes as the target service class; A data calling method corresponding to the target service class is created in the test context class, and the candidate entity class is used as a parameter of the data calling method, so that the candidate entity class can call the target service class method contained in the target service class through the data calling method.
7. The method according to claim 4, after creating the candidate unit test classes corresponding to the candidate service classes in the test sub-directories, further comprising: Adding a first to-be-supervised flag to each of the candidate unit test classes; wherein the first to-be-supervised flag is used to determine whether the executed unit test class needs to be supervised when performing unit testing on the target business module; After generating the target unit test method for the target business module in each of the candidate unit test classes, the method further includes: A second to-be-supervised flag is added to each of the target unit test methods; wherein the second to-be-supervised flag is used to determine whether the executed unit test method needs to be supervised when the target business module is unit tested.
8. The method according to claim 7, further comprising: When unit testing the target business module, determining a first identifier corresponding to a current unit test class currently being executed; When the first identifier is the first identifier to be supervised, determining a second identifier corresponding to each unit test method in the current unit test class; The unit test method whose second identifier is the second identifier to be supervised is used as the unit test method to be supervised, and when the execution status of each unit test method to be supervised is execution completed, the target simulation data value in the entity class attribute of the test context class is cleared; Among them, the target simulation data value is the candidate simulation data value corresponding to the target simulation object, the target simulation object is the candidate simulation object corresponding to the target entity class, the target entity class is the candidate entity class associated with the current service class, and the current service class is the candidate service class corresponding to the current unit test class.
9. A unit test processing device, characterized in that: include: A root node directory generation module is used to obtain a target code package corresponding to the code saving event when monitoring a code saving event implemented by a user, and generate a root node directory of a target business module according to the directory structure of the target code package; wherein the target business module is the business module where the target code package is located; A test subdirectory creation module, used to determine at least one candidate service class contained in the code in the target code package, and create test subdirectories corresponding to each candidate service class in the root node directory; A method name creation module, used to create candidate unit test classes corresponding to each candidate service class in each test subdirectory, and create candidate unit test method names in each candidate unit test class according to the public method names corresponding to each candidate service class; wherein the public method names are method names corresponding to the public service class methods; A target method body determination module is used to determine the candidate service class methods contained in each of the candidate service classes, and determine the candidate method body associated with each of the candidate service classes from the method bodies according to the candidate method type to which each of the candidate service class methods belongs and the association relationship between the method type and the method body, as the target method body associated with the corresponding candidate unit test method name; The target unit test method generation module is used to generate a target unit test method for the target business module in each candidate unit test class according to each candidate unit test method name and a target method body associated with each candidate unit test method name.
10. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the unit test processing method according to any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to execute the unit test processing method according to any one of claims 1 to 8.
12. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the unit test processing method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Automatic unit test realizing method and device
CN104407973A
Code testing method and device
CN104809071A