CN117971687A - Unit test case generation method and device, electronic equipment and storage medium - Google Patents

Unit test case generation method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN117971687A
CN117971687A CN202410184201.5A CN202410184201A CN117971687A CN 117971687 A CN117971687 A CN 117971687A CN 202410184201 A CN202410184201 A CN 202410184201A CN 117971687 A CN117971687 A CN 117971687A
Authority
CN
China
Prior art keywords
unit
evosuite
unit test
test case
framework
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410184201.5A
Other languages
Chinese (zh)
Inventor
梁铭锐
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guangzhou Huya Technology Co Ltd
Original Assignee
Guangzhou Huya Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guangzhou Huya Technology Co Ltd filed Critical Guangzhou Huya Technology Co Ltd
Priority to CN202410184201.5A priority Critical patent/CN117971687A/en
Publication of CN117971687A publication Critical patent/CN117971687A/en
Pending legal-status Critical Current

Links

Landscapes

  • Test And Diagnosis Of Digital Computers (AREA)

Abstract

The embodiment of the invention provides a method and a device for generating a unit test case, electronic equipment and a storage medium, and relates to the field of software development. When the change of the module code is detected, judging whether the module code has the unit test cases, if so, executing the unit test cases, if the unit test cases fail to be executed, detecting whether the module code is normal, and if the module code is normal, generating the unit test cases by EvoSuite frames through Gradle plug-ins, thereby automatically generating the unit test cases, ensuring the code quality and reducing the workload of manually writing and maintaining the test cases by developers.

Description

Unit test case generation method and device, electronic equipment and storage medium
Technical Field
The present invention relates to the field of software development, and in particular, to a method and apparatus for generating a unit test case, an electronic device, and a storage medium.
Background
The unit test is used for checking and verifying the smallest testable unit in the software, the unit test is usually carried out after the code writing is completed, and a developer writes and executes test cases, but because the unit test writing has a certain threshold, and the writing of the unit test steps is complicated, the popularity of the unit test is not high, and the writing unit test will of the developer is lower.
Disclosure of Invention
In view of the above, an object of an embodiment of the present invention is to provide a method, an apparatus, an electronic device and a storage medium for generating unit test cases, so as to at least partially improve the above-mentioned problems.
In order to achieve the above object, the technical scheme adopted by the embodiment of the invention is as follows:
In a first aspect, an embodiment of the present invention provides a method for generating a unit test case, where the method includes:
When the change of the module code is detected, judging whether the module code has a unit test case or not; the unit test cases are generated by EvoSuite frames;
if yes, executing the unit test case, and detecting whether the unit test case is executed successfully or not;
if the execution fails, detecting whether the module code is normal;
If the module code is normal, generating a unit test case by the EvoSuite framework through a Gradle plug-in; the Gradle plug-in is used for triggering the EvoSuite framework to run.
Optionally, before generating the unit test case, the method further comprises:
robolectric frames are incorporated into the EvoSuite frames.
Optionally, the step of incorporating Robolectric the frame into the EvoSuite frame includes:
Setting the running container of the EvoSuite frame to RobolectricTestRunner; the operation container is used for generating and executing the unit test cases;
Merging the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class;
and configuring a transparent attribute, wherein the transparent attribute is used for transmitting the attribute required by the Robolectric framework to the EvoSuite framework.
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:
acquiring the second loading class; the second load class includes at least one second load item;
Sequentially merging the second loading items into the first loading class to obtain the third loading class; the first load class includes at least one first load item, the third load class includes at least two third load items, the third load items include all the first load items and all the second load items;
and rejecting the third load item with conflict in the third load class.
Optionally, the method further comprises:
Configuring a pre-attribute, wherein the pre-attribute is used for adding the same execution method before a plurality of unit test cases when the unit test cases are generated;
Setting a character range of random data in the test case of the generating unit so as to enable the generated test data to meet the actual requirements.
Optionally, the Gradle plugin includes a case generation task and a case detection task, and the step of generating the unit test case by the EvoSuite framework through the Gradle plugin includes:
According to the module code, calling the case generation task to generate a unit to be detected test case;
and calling the case detection task, executing the unit to be detected test case, and eliminating the unit to be detected test case with abnormal execution to obtain the unit test case.
Optionally, the step of calling the case generation task according to the module code to generate the test case of the unit to be detected includes:
compiling the module codes to obtain compiled files;
and transmitting the compiled file to the EvoSuite framework, and generating a unit to be detected test case by the EvoSuite framework.
In a second aspect, an embodiment of the present invention provides a unit test case generating device, where the device includes:
The code change detection unit is used for judging whether a unit test case exists in the module code or not when the change of the module code is detected; the unit test cases are generated by EvoSuite frames;
the case execution unit is used for executing the unit test case if yes, and detecting whether the unit test case is successfully executed;
The code logic detection unit is used for detecting whether the module code is normal or not if the execution fails;
The case generation unit is used for generating a unit test case from the EvoSuite framework through a Gradle plug-in if the module code is normal; the Gradle plug-in is used for triggering the EvoSuite framework to run.
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, the processor implementing the method of any one of the above when executing the program.
In a fourth aspect, embodiments of the present invention provide a storage medium having stored thereon a computer program which, when executed by a processor, implements a method as described in any of the preceding claims.
According to the method, the device, the electronic equipment and the storage medium for generating the unit test cases, when the module code is detected to be changed, the unit test cases are automatically generated, the workload of manually writing and maintaining the test cases by a developer is reduced, the front-end attribute is configured to enable the test cases to be simpler and more convenient, the character range of random data in the unit test cases is set, and the generated test data can meet the actual requirements.
In order to make the above objects, features and advantages of the present application more comprehensible, preferred embodiments accompanied with figures are described in detail below.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed in the embodiments will be briefly described below, it being understood that the following drawings only illustrate some embodiments of the present invention and therefore should not be considered as limiting the scope, and other related drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a schematic block diagram of an electronic device according to an embodiment of the present invention;
FIG. 2 is a schematic flow chart of a method for generating unit test cases according to an embodiment of the present invention;
FIG. 3 is a schematic flow chart of a Robolectric-frame assembly and EvoSuite-frame assembly according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of another process for integrating Robolectric frames into EvoSuite frames according to one embodiment of the present invention;
FIG. 5 is a schematic view of another process for assembling Robolectric frames and EvoSuite frames according to one embodiment of the present invention;
FIG. 6 is another flow chart of a method for generating unit test cases according to an embodiment of the present invention;
FIG. 7 is a schematic diagram of a unit test case generating device according to an embodiment of the present invention.
Icon: 100-an electronic device; 101-memory; 102-a communication interface; 103-a processor; 104-a bus; 300-unit test case generating device; 310-a code change detection unit; 320—use case execution unit; 330-a code logic detection unit; 340-use case generation unit.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are some embodiments of the present invention, but not all embodiments of the present invention. The components of the embodiments of the present invention generally described and illustrated in the figures herein may be arranged and designed in a wide variety of different configurations.
Thus, the following detailed description of the embodiments of the invention, as presented in the figures, is not intended to limit the scope of the invention, as claimed, but is merely representative of selected embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
It should be noted that: like reference numerals and letters denote like items in the following figures, and thus once an item is defined in one figure, no further definition or explanation thereof is necessary in the following figures. Meanwhile, in the description of the present invention, the terms "first", "second", and the like are used only to distinguish the description, and are not to be construed as indicating or implying relative importance.
It is noted that relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The unit test is used for checking and verifying the smallest testable unit in the software, the unit test is usually carried out after the code writing is completed, and a developer writes and executes test cases, but because the unit test writing has a certain threshold, and the writing of the unit test steps is complicated, the popularity of the unit test is not high, and the writing unit test will of the developer is lower.
Based on the above situation, the embodiment of the invention provides a method, a device, an electronic device and a storage medium for generating unit test cases, when a change of a module code is detected, whether the module code has the unit test cases or not is judged, if so, the unit test cases are executed, if the unit test cases fail to be executed, whether the module code is normal or not is detected, if the module code is normal, the unit test cases are generated by the EvoSuite framework through a Gradle plug-in, so that the unit test cases can be automatically generated, and the workload of manually writing and maintaining the test cases by developers is reduced.
In order to implement the flow steps and functions of the examples of the present invention, please refer to fig. 1, and fig. 1 is a schematic block diagram of an electronic device according to an embodiment of the present invention. The electronic device 100 comprises a memory 101 and a processor 103, the memory 101 and the processor 103 being electrically connected to each other, either directly or indirectly, for enabling transmission or interaction of data. For example, the components may be electrically coupled to each other via one or more communication buses 104 or signal lines. The memory 101 may be used to store software programs and modules that the processor 103 oversubscribes within the memory 101, thereby performing various functional applications and data processing.
The electronic device 100 may be, but is not limited to, a personal computer (personal computer, PC), a server, a distributed deployment computer, or the like. It is understood that the electronic device 100 is not limited to a physical server, and may be a virtual machine on a physical server, a virtual machine built on a cloud platform, or a computer that can provide the same function as the server or the virtual machine. The operating system of the electronic device 100 may be, but is not limited to, a Windows system, a Linux system, etc.
The Memory 101 may be, but is not limited to, random access Memory (Random Access Memory, RAM), read Only Memory (ROM), programmable Read Only Memory (Programmable Read-Only Memory, PROM), erasable Read Only Memory (Erasable Programmable Read-Only Memory, EPROM), electrically erasable Read Only Memory (Electric Erasable Programmable Read-Only Memory, EEPROM), etc.
The communication connection between the electronic device 100 and the external device is achieved through at least one communication interface 102 (which may be wired or wireless).
The processor 103 may be an integrated circuit chip with signal processing capabilities. In implementation, the steps of embodiments of the present invention may be performed by integrated logic circuitry in hardware in processor 103 or by instructions in software. The processor 103 may be a general-purpose processor including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but may also be a digital signal processor (Network Processor, DSP), application SPECIFIC INTEGRATED Circuit (ASIC), off-the-shelf Programmable gate array (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic device, discrete gate or transistor logic device, discrete hardware components.
It is to be understood that the configuration shown in fig. 1 is merely illustrative, and that electronic device 100 may also include more or fewer components than shown in fig. 1, or have a different configuration than shown in fig. 1. The components shown in fig. 1 may be implemented in hardware, software, or a combination thereof.
The unit test case generating method provided by the invention is exemplified below. Specifically, fig. 2 is a schematic flow chart of a method for generating a unit test case according to an embodiment of the present invention, referring to fig. 2, the method execution body may be the electronic device 100 shown in fig. 1, and the method includes the following steps as shown in fig. 2:
step 210: when the change of the module code is detected, judging whether the module code has a unit test case or not; wherein, the unit test case is generated by EvoSuite framework. If yes, go to step 220; if not, go to step 240.
Step 220: the execution unit tests the case, and the detection unit tests whether the case is successfully executed. If not, go to step 230; step 240 is performed.
Step 230: and detecting whether the module code is normal. If so, go to step 240; if not, modifying the module code by a developer.
Step 240: and generating the unit test cases by EvoSuite frames through Gradle plug-ins. The Gradle plug-in is used for triggering the EvoSuite framework to run.
The unit test case generation method is connected into a static scanning system of the project, and the static scanning system is a mode for carrying out potential error analysis under the condition that codes do not run. After the unit test case generating method is accessed into a static scanning system, when the code is submitted, whether the module code is changed or not is detected, so that the judgment of whether the unit test case needs to be generated or not is triggered.
Wherein the module code refers to the service code of each service module in one software project.
If the unit test case exists in the code, the unit test case is executed first, whether the updated code has an influence on the original code logic or not can be detected, if the unit test case fails to be executed, a developer is informed of the code detection, if the execution failure caused by logic change occurs, the unit test case is generated by the EvoSuite framework through the Gradle plugin, if the execution failure caused by the problem of the code itself occurs, the developer is informed of the code resubmitting after the code is modified, and the step 210 is executed.
Only EvoSuite frames are used for generating the unit test cases, related services are required to be started manually in a command line, and commands are input to generate the unit test cases. Through Gradle plug-in, the step of generating unit test cases can be automatically performed after code is submitted.
According to the method, through detecting the change of the module codes, when the change of the module codes is detected, whether the module codes have unit test cases or not is judged, if so, the unit test cases are executed, if the unit test cases fail to be executed, whether the module codes are normal or not is detected, if the module codes are normal, the unit test cases are generated by a EvoSuite framework through Gradle plug-in units, so that the unit test cases can be automatically generated, and the workload of manually writing and maintaining the test cases by developers is reduced.
Since EvoSuite frames are based on Java, but do not support Android related frames api, evoSuite frames cannot be directly used to generate Android related unit test cases. In order to generate the unit test cases in the Android environment, before generating the unit test cases, the method further comprises the following steps:
the Robolectric frames are incorporated into the EvoSuite frame.
The Robolectric framework simulates various behaviors of an Android system api on a common Java virtual machine, so that a framework capable of executing Android-related unit test logic on the common Java virtual machine is achieved.
Specifically, referring to fig. 3, fig. 3 is a schematic flow chart of a Robolectric-frame assembly and EvoSuite-frame assembly provided in an embodiment of the present invention, and the step of assembling Robolectric-frame assembly into EvoSuite-frame assembly may include:
step 410: the run tank of EvoSuite frames was set to RobolectricTestRunner.
Wherein RobolectricTestRunner is a key component in Robolectric framework, which is a customized test run container for executing Android unit test on Java virtual machine, and the run container is used for generating and executing unit test cases.
Step 420: and merging the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class.
Step 430: and configuring the transparent transmission attribute. Wherein the pass-through properties are used to pass the Robolectric frame required properties to the EvoSuite frame.
The original operation container of EvoSuite frames is replaced by an operation container RobolectricTestRunner capable of executing Android unit tests on a Java virtual machine.
Both EvoSuite and Robolectric frames have their own load classes that when combined, are needed to be combined, enabling the combined frame to automatically load the components required by the EvoSuite and Robolectric frames.
The Robolectric frames are combined and combined into a EvoSuite frame, only one input port of the frame is required for the EvoSuite frame to receive the parameters required for the Robolectric frame, and a pass-through attribute is configured for receiving the required attributes of the Robolectric frame and transmitting the required attributes of the Robolectric frame to the Robolectric frame.
In order to smoothly merge the first loading class and the second loading class without generating a conflict, so that the framework can load the components required by EvoSuite framework and Robolectric framework, referring to fig. 4, fig. 4 is another flow chart of a Robolectric framework merging EvoSuite framework provided in an embodiment of the present invention, and step 420 may include:
Step 421: a second load class is obtained. Wherein the second load class includes at least one second load item.
Step 422: and merging the second loading items into the first loading class in sequence to obtain a third loading class.
Wherein the first load class includes at least one first load item, the third load class includes at least two third load items, and the third load item includes all first load items and the second load items.
Step 423: and eliminating the third load item with conflict in the third load class.
The Robolectric framework is not native source code, but compiles the package and cannot directly view the source code inside it, so in order to obtain the second load Class of the Robolectric framework, the second load Class needs to be obtained by a getClassLoader by reflection, but the 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 approach is typically protected, so we need to allow access through setAccessible (true). Then, we call this getClassLoader method by invoke method and pass in the target class as parameter to get the second loaded class.
The loading classes of EvoSuite frames and Robolectric frames generally comprise a plurality of loading items, the loading items of Robolectric frames are added into the first loading class of EvoSuite frames in sequence, so that a third loading class is obtained, the third loading class comprises at least two third loading items, the third loading items comprise all loading items of EvoSuite frames and loading items of Robolectric frames, but loading conflict loading items possibly exist in the third loading items, and the conflicting loading items need to be removed to ensure that the problem caused by repeated loading is not caused.
In order to make the unit test cases generated by the integrated EvoSuite framework more reasonable and concise, referring to fig. 5, fig. 5 is a schematic flow diagram of a Robolectric framework integration and EvoSuite framework, where the step of integrating Robolectric frameworks into EvoSuite frameworks may further include:
Step 440: the pre-attributes are configured. The pre-attribute is used for adding the same execution method before a plurality of unit test cases when the unit test cases are generated.
Step 450: setting a character range of random data in the test case of the generating unit. So that the generated test data meets the actual requirements.
The original EvoSuite framework has no pre-attribute, and the generated test case has no setup method, if the same method is used in a plurality of test methods, each test method has the same code, and code redundancy is caused, so the pre-attribute can be added in the EvoSuite framework, and the setup method related to @ Before is allowed to be added in the generated unit test to execute initialization logic. Illustratively, the following are used for generation:
Setup is executed before both test1 and test2 in class ExampleTest execute. setup can be used to initialize test objects and set test environments, which can be called before each test method is executed, and objects and environments can be shared among multiple test methods, which can improve code reusability and maintainability and reduce generation of duplicate codes; the consistency of the test environment is ensured, and each test method can be ensured to run under the same environment condition by executing the same initialization code before each test method, so that the reliability of the test result is ensured; the test execution time is reduced.
For step 450, setting the character range can improve the robustness of the test, and avoid the problem of abnormal operation of the test case caused by that the generated test data includes some special symbols. Specifically, the character range may be set to a-Z and a-Z.
Further, the EvoSuite framework itself does not adapt Java9+, in order to enable it to run on Java9+, and the EvoSuite framework enables it to support a high-version Java server when starting Java services, so that it can be used in high-version Java.
In one possible implementation manner, in order to enable a smoother generation unit to be a use case, the Gradle plugin may include a use case generation task and a use case detection task, referring to fig. 6, fig. 6 is another flow chart of a unit test case generation method provided by an embodiment of the present invention, and step 240 may include:
Step 241: and calling a case generation task according to the module code to generate a unit to be detected test case.
Step 242: and calling a case detection task, executing the unit to be detected test case, and eliminating the unit to be detected test case with abnormal execution to obtain the unit test case.
For step 241, it may include:
And compiling the module codes to obtain compiled files.
And transmitting the compiled file to EvoSuite framework, and generating the unit test case to be detected by EvoSuite framework.
The case generation task firstly executes the corresponding compiling task of the service module to obtain a compiling file, then the compiling file is taken out and is used as an input parameter to be transmitted to the EvoSuite framework to generate the corresponding unit test case.
For step 242, the case detection task may repeatedly execute the unit test case generated by EvoSuite framework, and attempt to correct the test case with the execution abnormality removed, and finally output the unit test case that can be executed smoothly.
In practical application, through the method, the unit test case generation data of each service module in one project is as follows:
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
wherein module represents a business module, testCase represents a unit test case number, cov represents coverage, branchCov represents logical branch coverage, and time represents generation time.
An exemplary description of an apparatus capable of performing the above-described exemplary flow steps is provided below. Specifically, fig. 7 is a schematic diagram of a unit test case generating device provided by an embodiment of the present invention, referring to fig. 7, the unit test case generating device 300 includes a code change detecting unit 310, a case executing unit 320, a code logic detecting unit 330, and a case generating unit 340:
a code change detection unit 310, configured to determine whether a unit test case exists in the module code when a change of the module code is detected; the unit test cases are generated by EvoSuite framework.
And the case execution unit 320 is configured to, if yes, test the case by using the execution unit, and detect whether the execution of the case is successful.
The code logic detecting unit 330 is configured to detect whether the module code is normal if the execution fails.
The case generating unit 340 is configured to generate a unit test case from the EvoSuite framework through the Gradle plugin if the module code is normal; the Gradle plug-in is used to trigger EvoSuite the framework operations.
In summary, according to the method, the device, the electronic equipment and the storage medium for generating the unit test cases provided by the embodiment of the invention, whether the unit test cases exist or not can be automatically judged by detecting the change of the module codes, and the test cases are automatically executed, so that the degree of automation of the test is greatly improved, and the workload of manual operation is reduced; the unit test cases generated by the EvoSuite framework can cover various execution paths of the module codes, so that the accuracy and coverage rate of the test are improved, and if the test cases fail to be executed, whether the module codes are normal or not is also detected, and the reliability of the test results is further ensured; the Gradle plug-in triggers EvoSuite the frame to operate, so that the method can be conveniently integrated into the existing development flow, and has strong flexibility; by configuring the preposed attribute and the transparent attribute, the same execution method can be added when the unit test case is generated, and the required attribute of the Robolectric framework is transferred to the EvoSuite framework, so that the test efficiency is improved; the character range of the random data in the test case of the generating unit is set, so that the generated test data can better meet the actual requirements, and the test efficiency is further improved.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus and method may be implemented in other manners. The apparatus embodiments described above are merely illustrative, for example, of the flowcharts and block diagrams in the figures that illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
In addition, functional modules in the embodiments of the present invention may be integrated together to form a single part, or each module may exist alone, or two or more modules may be integrated to form a single part.
The functions, if implemented in the form of software functional modules and sold or used as a stand-alone product, may be stored on a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution in the form of a software product stored in a computer-readable storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods of the embodiments of the present invention. And the aforementioned storage medium includes: a usb disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The above is only a preferred embodiment of the present invention, and is not intended to limit the present invention, but various modifications and variations can be made to the present invention by those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
It will be evident to those skilled in the art that the invention is not limited to the details of the foregoing illustrative embodiments, and that the present invention may be embodied in other specific forms without departing from the spirit or essential characteristics thereof. The present embodiments are, therefore, to be considered in all respects as illustrative and not restrictive, the scope of the invention being indicated by the appended claims rather than by the foregoing description, and all changes which come within the meaning and range of equivalency of the claims are therefore intended to be embraced therein. Any reference sign in a claim should not be construed as limiting the claim concerned.

Claims (10)

1. A method for generating unit test cases, the method comprising:
When the change of the module code is detected, judging whether the module code has a unit test case or not; the unit test cases are generated by EvoSuite frames;
if yes, executing the unit test case, and detecting whether the unit test case is executed successfully or not;
if the execution fails, detecting whether the module code is normal;
If the module code is normal, generating a unit test case by the EvoSuite framework through a Gradle plug-in; the Gradle plug-in is used for triggering the EvoSuite framework to run.
2. The method of claim 1, wherein prior to generating the unit test cases, the method further comprises:
robolectric frames are incorporated into the EvoSuite frames.
3. The method of claim 2, wherein the step of incorporating Robolectric a frame into the EvoSuite frame comprises:
Setting the running container of the EvoSuite frame to RobolectricTestRunner; the operation container is used for generating and executing the unit test cases;
Merging the second loading class of the Robolectric framework into the first loading class of the EvoSuite framework to obtain a third loading class;
and configuring a transparent attribute, wherein the transparent attribute is used for transmitting the attribute required by the Robolectric framework to the EvoSuite framework.
4. A method according to claim 3, wherein the step of merging the second load class of Robolectric frames into the first load class of EvoSuite frames results in a third load class, comprises:
acquiring the second loading class; the second load class includes at least one second load item;
Sequentially merging the second loading items into the first loading class to obtain the third loading class; the first load class includes at least one first load item, the third load class includes at least two third load items, the third load items include all the first load items and all the second load items;
and rejecting the third load item with conflict in the third load class.
5. A method according to claim 3, characterized in that the method further comprises:
Configuring a pre-attribute, wherein the pre-attribute is used for adding the same execution method before a plurality of unit test cases when the unit test cases are generated;
Setting a character range of random data in the test case of the generating unit so as to enable the generated test data to meet the actual requirements.
6. The method according to claim 1, wherein the Gradle plugin includes a case generation task and a case detection task, and the step of generating unit test cases by the EvoSuite framework through the Gradle plugin includes:
According to the module code, calling the case generation task to generate a unit to be detected test case;
and calling the case detection task, executing the unit to be detected test case, and eliminating the unit to be detected test case with abnormal execution to obtain the unit test case.
7. The method of claim 6, wherein the step of calling the case generation task according to the module code to generate a unit to be detected test case comprises:
compiling the module codes to obtain compiled files;
and transmitting the compiled file to the EvoSuite framework, and generating a unit to be detected test case by the EvoSuite framework.
8. A unit test case generation apparatus, the apparatus comprising:
The code change detection unit is used for judging whether a unit test case exists in the module code or not when the change of the module code is detected; the unit test cases are generated by EvoSuite frames;
the case execution unit is used for executing the unit test case if yes, and detecting whether the unit test case is successfully executed;
The code logic detection unit is used for detecting whether the module code is normal or not if the execution fails;
The case generation unit is used for generating a unit test case from the EvoSuite framework through a Gradle plug-in if the module code is normal; the Gradle plug-in is used for triggering the EvoSuite framework to run.
9. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the method of any one of claims 1 to 7 when executing the program.
10. A storage medium having a computer program stored thereon, characterized by: the computer program, when executed by a processor, implements the method of any of claims 1 to 7.
CN202410184201.5A 2024-02-19 2024-02-19 Unit test case generation method and device, electronic equipment and storage medium Pending CN117971687A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410184201.5A CN117971687A (en) 2024-02-19 2024-02-19 Unit test case generation method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410184201.5A CN117971687A (en) 2024-02-19 2024-02-19 Unit test case generation method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN117971687A true CN117971687A (en) 2024-05-03

Family

ID=90864526

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410184201.5A Pending CN117971687A (en) 2024-02-19 2024-02-19 Unit test case generation method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN117971687A (en)

Similar Documents

Publication Publication Date Title
US9201757B2 (en) Offline type checking in programming languages
US9779014B2 (en) Resilient mock object creation for unit testing
CN108959508B (en) SQL data generation method and device
CN112306568A (en) Service instance configuration method and device, electronic equipment and storage medium
US11294804B2 (en) Test case failure with root cause isolation
CN112988578A (en) Automatic testing method and device
US9646252B2 (en) Template clauses based SAT techniques
CN116599881A (en) Cloud platform tenant modeling test method, device, equipment and storage medium
CN117971687A (en) Unit test case generation method and device, electronic equipment and storage medium
CN113179332B (en) Method, electronic device and storage medium for acquiring configuration information
KR102126960B1 (en) Reliability testing integrated-platform of weapon system software for next-generation naval ship
CN113986592A (en) Log recording method and device, terminal equipment and readable storage medium
CN114817047A (en) Compiler test method, case generation method and device and instruction storage structure
CN112256554B (en) Method and equipment for testing based on scene test cases
CN108845837B (en) Method for detecting operating mode of windows system and electronic equipment
CN113379501A (en) Message pushing method and device, electronic equipment and storage medium
CN113220586A (en) Automatic interface pressure test execution method, device and system
CN113238953A (en) UI automation test method and device, electronic equipment and storage medium
CN113032004A (en) Method, apparatus and program product for managing development jobs in a development environment
CN115640236B (en) Script quality detection method and computing device
CN117827632A (en) Test method, test device and storage medium
CN114942887A (en) Program safety testing method, device, equipment and medium
CN112650679B (en) Test verification method, device and computer system
CN113760405B (en) Signature verification method and device for gateway interface, storage medium and electronic equipment
CN117806943A (en) Test method, test device and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination