A unit test case generation method, device, electronic device and storage medium
The automatic generation of unit tests using the EvoSuite and Gradle plugin, enhanced for Android support, addresses the challenges of low unit testing popularity by reducing manual effort and improving test efficiency and reliability.
Patent Information
- Application Number
- CN202410184201.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-19
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2044-02-19
AI Technical Summary
The low popularity of unit testing in software development is due to the high barrier and cumbersome process of writing unit tests, leading to a low willingness among developers to perform unit testing.
A method and system that automatically generates unit test cases using the EvoSuite framework, triggered by code changes, and integrates with the Gradle plugin to ensure successful execution and generation of unit tests, enhanced by merging the Robolectric framework to support Android environments and configuring attributes for optimal test data generation.
This approach reduces the manual effort in writing and maintaining unit tests, enhances test case generation efficiency, and improves test coverage and reliability by generating tests that meet actual requirements.
Smart Images

Figure CN117971687B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software development, and more particularly, to a method, apparatus, electronic device, and storage medium for generating unit test cases. Background Art
[0002] Unit testing is used to check and verify the smallest testable units in software. Unit testing is usually carried out after the code is written, and the test cases are written and executed by developers. However, since writing unit tests has a certain threshold and the steps for writing unit tests are relatively cumbersome, the popularity of unit testing is not high, and developers are less willing to write unit tests. Summary of the Invention
[0003] In view of this, an object of the embodiments of the present invention is to provide a method, apparatus, electronic device, and storage medium for generating unit test cases to at least partially improve the above problems.
[0004] To achieve the above object, the technical solutions adopted in the embodiments of the present invention are as follows:
[0005] In a first aspect, an embodiment of the present invention provides a method for generating unit test cases, the method comprising:
[0006] When it is detected that there is a change in the module code, determine whether there is a unit test case in the module code; the unit test case is generated by the EvoSuite framework;
[0007] If so, execute the unit test case and detect whether the unit test case is successfully executed;
[0008] If the execution fails, detect whether the module code is normal;
[0009] If the module code is normal, generate unit test cases by the EvoSuite framework through a Gradle plugin; the Gradle plugin is used to trigger the EvoSuite framework to run.
[0010] Optionally, before generating the unit test cases, the method further comprises:
[0011] Merge the Robolectric framework into the EvoSuite framework.
[0012] Optionally, the step of merging the Robolectric framework into the EvoSuite framework comprises:
[0013] Set the running container of the EvoSuite framework to RobolectricTestRunner; the running container is used to generate and execute the unit test cases;
[0014] Merge the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class;
[0015] Configure the pass-through attributes, which are used to pass the attributes required by the Robolectric framework to the EvoSuite framework.
[0016] Optionally, the step of merging the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class includes:
[0017] Obtain the second loading class; the second loading class includes at least one second loading item;
[0018] Merge the second loading items into the first loading class in sequence to obtain the third loading class; the first loading class includes at least one first loading item, the third loading class includes at least two third loading items, and the third loading items include all the first loading items and all the second loading items;
[0019] Remove the conflicting third loading items in the third loading class.
[0020] Optionally, the method further includes:
[0021] Configure the pre-properties, which are used to add the same execution method before multiple unit test cases when generating the unit test cases.
[0022] Set the character range of the random data in the generated unit test cases so that the generated test data meets the actual requirements.
[0023] Optionally, the Gradle plugin includes a use case generation task and a use case detection task. The step of generating unit test cases by the EvoSuite framework through the Gradle plugin includes:
[0024] According to the module code, call the use case generation task to generate unit test cases to be detected;
[0025] Call the use case detection task, execute the unit test cases to be detected, and remove the unit test cases to be detected with execution exceptions to obtain the unit test cases.
[0026] Optionally, the step of calling the use case generation task to generate unit test cases to be detected according to the module code includes:
[0027] Compile the module code to obtain a compiled file;
[0028] Pass the compiled file to the EvoSuite framework, and the EvoSuite framework generates unit test cases to be detected.
[0029] In a second aspect, an embodiment of the present invention provides a unit test case generation device, which includes:
[0030] A code change detection unit, configured to determine whether there are unit test cases in the module code when it detects that the module code has changed; the unit test cases are generated by the EvoSuite framework;
[0031] A use case execution unit, configured to, if so, execute the unit test cases and detect whether the unit test cases are executed successfully;
[0032] A code logic detection unit, configured to, if the execution fails, detect whether the module code is normal;
[0033] A use case generation unit, configured to, if the module code is normal, generate unit test cases by the EvoSuite framework through a Gradle plugin; the Gradle plugin is used to trigger the EvoSuite framework to run.
[0034] In a third aspect, an embodiment of the present invention provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the method described in any one of the above is implemented.
[0035] In a fourth aspect, an embodiment of the present invention provides a storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method described in any one of the above is implemented.
[0036] A unit test case generation method, device, electronic device, and storage medium provided by an embodiment of the present invention automatically generate unit test cases when detecting a change in module code, reducing the workload of developers manually writing and maintaining test cases, configuring preconditions to make the test cases more convenient, and setting the character range of random data in the generated unit test cases, so that the generated test data meets the actual requirements.
[0037] To make the above objects, features, and advantages of the present application more obvious and understandable, the following specifically enumerates preferred embodiments and, in conjunction with the accompanying drawings, provides a detailed description as follows. Description of the Drawings
[0038] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as limiting the scope. For those of ordinary skill in the art, without creative efforts, other related drawings can also be obtained based on these drawings.
[0039] Figure 1 Schematic structural block diagram of an electronic device provided by an embodiment of the present invention;
[0040] Figure 2 Schematic flow chart of a unit test case generation method provided by an embodiment of the present invention;
[0041] Figure 3 Schematic flow chart of integrating the Robolectric framework into the EvoSuite framework provided by an embodiment of the present invention;
[0042] Figure 4 Another schematic flow chart of integrating the Robolectric framework into the EvoSuite framework provided by an embodiment of the present invention;
[0043] Figure 5 Another schematic flow chart of integrating the Robolectric framework into the EvoSuite framework provided by an embodiment of the present invention;
[0044] Figure 6 Another schematic flow chart of a unit test case generation method provided by an embodiment of the present invention;
[0045] Figure 7 Schematic diagram of a unit test case generation device provided by an embodiment of the present invention.
[0046] Icons: 100 - electronic device; 101 - memory; 102 - communication interface; 103 - processor; 104 - bus; 300 - unit test case generation device; 310 - code change detection unit; 320 - use case execution unit; 330 - code logic detection unit; 340 - use case generation unit. Detailed implementation manners
[0047] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. Usually, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.
[0048] Accordingly, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely represents selected embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the scope of protection of the present invention.
[0049] It should be noted that like reference numerals and letters denote like items in the following drawings. Therefore, once an item is defined in one drawing, it does not require further definition and explanation in subsequent drawings. At the same time, in the description of the present invention, terms such as "first", "second", etc. are only used for differential description and cannot be construed as indicating or implying relative importance.
[0050] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or also includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising a..." does not exclude the presence of additional identical elements in the process, method, article or device comprising the element.
[0051] Unit testing is used to check and verify the smallest testable units in software. Unit testing is usually carried out after the code is written, and the test cases are written and executed by developers. However, since there is a certain threshold for writing unit tests and the steps for writing unit tests are relatively cumbersome, the popularity of unit testing is not high, and the willingness of developers to write unit tests is low.
[0052] Based on the above situation, the embodiments of the present invention provide a method, device, electronic device and storage medium for generating unit test cases. When it is detected that the module code has changed, it is judged whether there are unit test cases in the module code. If so, the unit test cases are executed. If the execution of the unit test cases fails, it is detected whether the module code is normal. If the module code is normal, unit test cases are generated by the EvoSuite framework through a Gradle plugin, thereby being able to automatically generate unit test cases and reducing the workload of developers for manually writing and maintaining test cases.
[0053] To implement the process steps and functions of the various examples of the present invention, please refer to Figure 1 , Figure 1A schematic structural block diagram of an electronic device provided by an embodiment of the present invention. The electronic device 100 includes a memory 101 and a processor 103, and the memory 101 and the processor 103 are directly or indirectly electrically connected to each other to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses 104 or signal lines. The memory 101 can be used to store software programs and modules, and the processor 103 can execute various functional applications and data processing by executing the software programs and modules stored in the memory 101.
[0054] The electronic device 100 can be, but is not limited to, a personal computer (PC), a server, a distributed computer, and so on. It can be understood that the electronic device 100 is not limited to a physical server, but can also be a virtual machine on a physical server, a virtual machine built on a cloud platform, or other computers that can provide the same functions as the server or virtual machine. The operating system of the electronic device 100 can be, but is not limited to, the Windows system, the Linux system, and so on.
[0055] Among them, the memory 101 can be, but is not limited to, a random access memory (RAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), and so on.
[0056] The communication connection between the electronic device 100 and an external device is realized through at least one communication interface 102 (which can be wired or wireless).
[0057] The processor 103 may be an integrated circuit chip with the ability to process signals. In the implementation process, the steps of the embodiments of the present invention can be completed by the integrated logic circuit in the hardware of the processor 103 or the instructions in the form of software. The processor 103 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0058] It can be understood that Figure 1 The structure shown is only for illustration, and the electronic device 100 may also include more or fewer components than those shown Figure 1 in it, or have a different configuration from that shown Figure 1 in it. Figure 1 Each component shown in it can be implemented by hardware, software, or a combination thereof.
[0059] Next, an exemplary description is provided for the unit test case generation method provided by the present invention. Specifically, Figure 2 is a schematic flowchart of a unit test case generation method provided by an embodiment of the present invention. Refer to Figure 2 , the execution subject of this method can be the above-mentioned Figure 1 shown electronic device 100, and this method includes the following steps as Figure 2 described:
[0060] Step 210: When it is detected that the module code has changed, determine whether there are unit test cases in the module code; among them, the unit test cases are generated by the EvoSuite framework. If so, execute step 220; if not, execute step 240.
[0061] Step 220: Execute the unit test cases and detect whether the unit test cases are executed successfully. If not, execute step 230; if so, execute step 240.
[0062] Step 230: Detect whether the module code is normal. If it is normal, execute step 240; if it is not normal, hand it over to the developer to modify the module code.
[0063] Step 240: Generate unit test cases by the EvoSuite framework through the Gradle plugin. Among them, the Gradle plugin is used to trigger the EvoSuite framework to run.
[0064] This unit test case generation method is integrated into the project's static scanning system. The static scanning system is a way to perform potential error analysis without running the code. After this unit test case generation method is integrated into the static scanning system, when the code is submitted, it will detect whether there are changes in the module code, thereby triggering a judgment on whether unit test cases need to be generated.
[0065] Among them, the module code refers to the business code of each business module in a software project.
[0066] If there are already unit test cases in the code, first execute these unit test cases, which can detect whether the updated code affects the original code logic. If the unit test cases fail to execute, hand it over to the developer to detect the code. If the failure is caused by a logical change, generate unit test cases by the EvoSuite framework through the Gradle plugin. If the failure is caused by a problem with the code itself, after the developer modifies the code, resubmit the code and execute step 210.
[0067] Only using the EvoSuite framework to generate unit test cases requires manually starting the relevant services in the command line and entering commands to generate unit test cases. Through the Gradle plugin, the steps of generating unit test cases can be automatically performed after the code is submitted.
[0068] This method detects changes in the module code. When it detects changes in the module code, it judges whether there are unit test cases in the module code. If so, execute the unit test cases. If the unit test cases fail to execute, detect whether the module code is normal. If the module code is normal, generate unit test cases by the EvoSuite framework through the Gradle plugin, thereby enabling the automatic generation of unit test cases and reducing the workload of developers manually writing and maintaining test cases.
[0069] Since the EvoSuite framework, although based on Java, does not support Android-related framework apis and cannot directly use the EvoSuite framework to generate Android-related unit test cases. In order to generate unit test cases in the Android environment, before generating unit test cases, the above method further includes:
[0070] Merge the Robolectric framework into the EvoSuite framework.
[0071] Among them, the Robolectric framework simulates various behaviors of the Android system API on a common Java virtual machine, so as to achieve a framework that can execute Android-related unit test logic on a common Java virtual machine.
[0072] Specifically, referring to Figure 3 , Figure 3 FIG. is a schematic flow chart of integrating the Robolectric framework into the EvoSuite framework provided by an embodiment of the present invention. The steps of integrating the Robolectric framework into the EvoSuite framework may include:
[0073] Step 410: Set the running container of the EvoSuite framework to RobolectricTestRunner.
[0074] Among them, RobolectricTestRunner is a key component in the Robolectric framework. It is a customized test running container for executing Android unit tests on a Java virtual machine. This running container is used to generate and execute unit test cases.
[0075] Step 420: Merge the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class.
[0076] Step 430: Configure pass-through attributes. Among them, the pass-through attributes are used to pass the attributes required by the Robolectric framework to the EvoSuite framework.
[0077] First, replace the original running container of the EvoSuite framework with the running container RobolectricTestRunner that can execute Android unit tests on a Java virtual machine.
[0078] Both the EvoSuite framework and the Robolectric framework have their own loading classes. When merging the two frameworks, the two loading classes need to be merged so that the synthesized framework can automatically load the components required by the EvoSuite framework and the Robolectric framework.
[0079] When integrating the Robolectric framework into the EvoSuite framework, there is only one input port for the framework, and the parameters that can be received are all those required by the EvoSuite framework. In order to pass the attributes required by the Robolectric framework, pass-through attributes are configured. The pass-through attributes are used to receive the attributes required by the Robolectric framework and pass the attributes required by the Robolectric framework to the Robolectric framework.
[0080] To enable the smooth merging of the first loading class and the second loading class without conflicts, so that the framework can finally load the components required by the EvoSuite framework and the Robolectric framework, refer to Figure 4 , Figure 4 FIG. 420 is another process schematic diagram of integrating the Robolectric framework into the EvoSuite framework provided by an embodiment of the present invention. Step 420 may include:
[0081] Step 421: Obtain the second loading class. The second loading class includes at least one second loading item.
[0082] Step 422: Sequentially merge the second loading items into the first loading class to obtain a third loading class.
[0083] The first loading class includes at least one first loading item, the third loading class includes at least two third loading items, and the third loading item includes all the first loading items and the second loading items.
[0084] Step 423: Remove the conflicting third loading items in the third loading class.
[0085] The Robolectric framework is not native source code but a compiled package, and its internal source code cannot be directly viewed. Therefore, to obtain the second loading class of the Robolectric framework, the second loading class can be obtained through reflection. It is necessary to obtain it through getClassLoader, but getClassLoader is protected. The getClassLoader method accepts a Class object as a parameter and uses reflection to call the getClassLoader(Class) method in the ClassLoader class. This method is usually protected, so we need to allow access by setAccessible(true). Then, we call this getClassLoader method through the invoke method and pass in the target class as a parameter to obtain the second loading class.
[0086] Generally, the loading classes of the EvoSuite framework and the Robolectric framework both include multiple loading items. Add the loading items of the Robolectric framework into the first loading class of the EvoSuite framework in sequence to obtain a third loading class. The third loading class includes at least two third loading items. The third loading item includes all the loading items of the EvoSuite framework and the loading items of the Robolectric framework. However, there may be conflicting loading items in the third loading item, and the conflicting loading items need to be removed to ensure that there are no problems caused by repeated loading.
[0087] In order to make the unit test cases generated by the merged EvoSuite framework more reasonable and concise, referring to Figure 5 , Figure 5 FIG.
[0088] Step 440: Configure preconditions. Among them, the preconditions are used to add the same execution method before multiple unit test cases when generating unit test cases.
[0089] Step 450: Set the character range of the random data in the generated unit test cases. So that the generated test data meets the actual requirements.
[0090] The original EvoSuite framework does not have preconditions. In the generated test cases, there is no setup method. If the same method is used in multiple test methods, it will cause the same code in each test method, resulting in code redundancy. Therefore, preconditions can be added to the EvoSuite framework to allow the addition of @Before-related setup method execution initialization logic in the generated unit tests. Exemplarily, the generated test cases are as follows:
[0091]
[0092] Before the execution of test1 and test2 in the ExampleTest class, setup will be executed. Setup can be used to initialize the test object and set up the test environment. It will be called before each test method and can share objects and environments among multiple test methods. This can improve the code reusability and maintainability and reduce the generation of duplicate code; ensure the consistency of the test environment. By executing the same initialization code before each test method, it can ensure that each test method runs under the same environmental conditions, which helps to ensure the reliability of the test results; reduce the test execution time.
[0093] For step 450, setting the character range can improve the robustness of the test and avoid the problem that the generated test data includes some special symbols, resulting in abnormal operation of the test cases. Specifically, the character range can be set to a-z and A-Z.
[0094] Furthermore, the EvoSuite framework itself is not compatible with Java 9+. To enable it to run on Java 9+, when the EvoSuite framework starts a Java service, it makes the server support the higher version of Java so that it can be used in the higher version of Java.
[0095] In a possible implementation, to generate unit test cases more smoothly, the Gradle plugin can include a test case generation task and a test case detection task. See Figure 6 , Figure 6 which is another flowchart of a unit test case generation method provided by an embodiment of the present invention. Step 240 may include:
[0096] Step 241: Call the test case generation task according to the module code to generate the unit test cases to be detected.
[0097] Step 242: Call the test case detection task, execute the unit test cases to be detected, and eliminate the unit test cases to be detected with execution exceptions to obtain the unit test cases.
[0098] For step 241, it may include:
[0099] Compile the module code to obtain a compiled file.
[0100] Pass the compiled file to the EvoSuite framework, and the EvoSuite framework generates the unit test cases to be detected.
[0101] The test case generation task will first execute the corresponding compilation task of the business module to obtain a compiled file, then take out the compiled file, and use it as an input parameter to pass to the EvoSuite framework to generate the corresponding unit test cases.
[0102] For step 242, the test case detection task will repeatedly execute the unit test cases generated by the EvoSuite framework and try to correct and eliminate the test cases with execution exceptions among them, and finally output the unit test cases that can be successfully executed.
[0103] In practical applications, through the above method, the data for generating unit test cases of each business module in a project is as follows:
[0104] module testCase cov branchCov time autotestbase 230 52% 81% 795s hardcoder 21 28% 34% 220s hybrid-base 107 18% 29% 1421s kpersistent 390 24% 21% 1728s listframe-main 1126 28% 25% 5970s mvp 71 12% 5% 2027s oakweb 163 12% 10% 1587s player 28 42% 25% 391s prophet 13 12% 16% 345s protocol 256 43% 35% 1062s
[0105] Among them, module represents the business module, testCase represents the number of unit test cases, cov represents the coverage rate, branchCov represents the logical branch coverage rate, and time represents the generation time.
[0106] An exemplary description is given below for the device capable of executing the above example process steps. Specifically, Figure 7 is a schematic diagram of a unit test case generation device provided in an embodiment of the present invention. Refer to Figure 7 , the unit test case generation device 300 includes a code change detection unit 310, a use case execution unit 320, a code logic detection unit 330, and a use case generation unit 340:
[0107] The code change detection unit 310 is configured to determine whether there is a unit test case in the module code when it detects that the module code has changed; the unit test case is generated by the EvoSuite framework.
[0108] The use case execution unit 320 is configured to, if so, execute the unit test case and detect whether the unit test case is executed successfully.
[0109] The code logic detection unit 330 is configured to, if the execution fails, detect whether the module code is normal.
[0110] The use case generation unit 340 is configured to, if the module code is normal, generate unit test cases by the EvoSuite framework through the Gradle plugin; the Gradle plugin is used to trigger the EvoSuite framework to run.
[0111] In summary, a unit test case generation method, device, electronic device, and storage medium provided in an embodiment of the present invention can automatically determine whether there are unit test cases by detecting changes in the module code and automatically execute these test cases, greatly improving the degree of test automation and reducing the workload of manual operations; the unit test cases generated by the EvoSuite framework can cover various execution paths of the module code, thereby improving the accuracy and coverage of the test. At the same time, if the test case execution fails, it will also detect whether the module code is normal, further ensuring the reliability of the test result; by triggering the EvoSuite framework to run through the Gradle plugin, it can be easily integrated into the existing development process with strong flexibility; by configuring the pre-properties and pass-through properties, the same execution method can be added when generating unit test cases and the properties required by the Robolectric framework can be passed to the EvoSuite framework, thereby improving the test efficiency; setting the character range of the random data in the generated unit test cases can make the generated test data more in line with the actual needs, further improving the test efficiency.
[0112] In the embodiments provided by the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architectures, functions, and operations of devices, methods, and computer program products according to multiple embodiments of the present invention. In this regard, each block in the flowchart or block diagram may represent a module, a program segment, or a part of code, and a module, a program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks can actually be executed substantially in parallel, and they can sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, as well as the combination of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or actions, or can be implemented by a combination of dedicated hardware and computer instructions.
[0113] In addition, the functional modules in each embodiment of the present invention can be integrated together to form an independent part, or each module can exist alone, or two or more modules can be integrated to form an independent part.
[0114] If the function is implemented in the form of a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a computer-readable storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods in each embodiment of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs that can store program codes.
[0115] The above are only the preferred embodiments of the present invention and are not used to limit the present invention. For those skilled in the art, the present invention can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
[0116] For those skilled in the art, it is obvious that the present invention is not limited to the details of the above-described exemplary embodiments, and the present invention can be implemented in other specific forms without departing from the spirit or basic characteristics of the present invention. Therefore, from any point of view, the embodiments should be regarded as exemplary and non-restrictive. The scope of the present invention is defined by the appended claims rather than the above description. Therefore, all changes falling within the meaning and scope of the equivalent elements of the claims are intended to be embraced within the present invention. Any reference signs in the claims should not be construed as limiting the claims involved.
Claims
1. A unit test case generation method, characterized in that, The method comprises: When a change in the module code is detected, determine whether there is a unit test case in the module code; the unit test case is generated by the EvoSuite framework; If yes, execute the unit test case to detect whether the unit test case is executed successfully; If the execution fails, check whether the module code is normal; If the module code is normal, the EvoSuite framework generates unit test cases through the Gradle plug-in, including: calling the case generation task of the Gradle plug-in according to the module code to generate unit test cases to be detected; calling the case detection task of the Gradle plug-in to execute the unit test cases to be detected, and eliminating the unit test cases to be detected with abnormal execution to obtain the unit test cases; the Gradle plug-in is used to trigger the operation of the EvoSuite framework; Among them, before generating unit test cases, the running container of the EvoSuite framework is set to RobolectricTestRunner; the running container is used to generate and execute the unit test cases; the second loading class of the Robolectric framework is merged into the first loading class of the EvoSuite framework to obtain the third loading class; the transparent transmission attribute is configured, and the transparent transmission attribute is used to accept the attributes required by the Robolectric framework and transparently transmit the attributes required by the Robolectric framework to the Robolectric framework.
2. The method according to claim 1, wherein The step of merging the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class comprises: Acquire the second loading class; the second loading class includes at least one second loading item; Merging the second loading items into the first loading class in sequence to obtain the third loading class; the first loading class includes at least one first loading item, the third loading class includes at least two third loading items, and the third loading items include all the first loading items and all the second loading items; The third loading item that has conflicts in the third loading class is removed.
3. The method according to claim 1, characterized in that, The method further comprises: Configure a pre-attribute, wherein the pre-attribute is used to add the same execution method before multiple unit test cases when generating the unit test case; Set the character range for generating random data in unit test cases to make the generated test data meet actual requirements.
4. The method according to claim 1, characterized in that, The step of calling the use case generation task of the Gradle plug-in according to the module code to generate the unit test case to be detected includes: Compile the module code to obtain a compiled file; The compiled file is passed to the EvoSuite framework, and the EvoSuite framework generates unit test cases to be detected.
5. A unit test case generation device, characterized in that, The device comprises: A code change detection unit, used to determine whether there are unit test cases in the module code when a change is detected in the module code; the unit test cases are generated by the EvoSuite framework; A use case execution unit, which is used to execute the unit test case if so, and detect whether the unit test case is successfully executed; A code logic detection unit, which is used to detect whether the module code is normal if the execution fails; A use case generation unit, which is used to generate unit test cases by the EvoSuite framework through the Gradle plugin if the module code is normal, including: calling the use case generation task of the Gradle plugin according to the module code to generate unit test cases to be detected; calling the use case detection task of the Gradle plugin to execute the unit test cases to be detected, and eliminating the unit test cases to be detected with abnormal execution to obtain the unit test cases; the Gradle plugin is used to trigger the EvoSuite framework to run; before generating unit test cases, it is also used to: set the running container of the EvoSuite framework to RobolectricTestRunner; the running container is used to generate and execute the unit test cases; merge the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class; configure the pass-through attributes, and the pass-through attributes are used to receive the attributes required by the Robolectric framework and pass the attributes required by the Robolectric framework to the Robolectric framework.
6. An electronic device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the program, the method according to any one of claims 1 to 4 is implemented.
7. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by the processor, the method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Test case generation method and device, electronic equipment and storage medium
CN116028330A
Test report generation method and device, equipment and storage medium
CN116185874A