Unit test method and related device

By introducing Junit5, Mockito and Assert testing frameworks in unit testing, writing BaseTest classes and generating test classes, the problems of poor maintainability and reusability of existing unit test programs are solved, and the testing efficiency and quality are improved.

CN119883931APending Publication Date: 2025-04-25BEIJING HUANENG XINRUI CONTROL TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510077590.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-17
Publication Date
2025-04-25

AI Technical Summary

Technical Problem

The existing unit test programs have poor maintainability and reusability, which makes it difficult to guarantee the accuracy of unit test results and low testing efficiency.

Method used

The Junit5 test framework, Mockito test framework and Assert test framework are introduced, and public login and permission verification are implemented by writing BaseTest classes, and test classes are generated based on these frameworks, and test classes are executed to generate test reports.

Benefits of technology

It significantly improves the efficiency and quality of unit tests, enhances the maintainability and reusability of tests, simplifies the generation and management of test reports, and reduces test costs and risks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883931A_ABST
    Figure CN119883931A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of system development testing, and discloses a unit testing method and related device.The unit testing method comprises the steps that a testing assembly is introduced into a target project; wherein the test assembly comprises a Junit5 test framework, a Mokito test framework and an Assert test framework; based on the test component, compiling a BaseTest class; wherein the BaseTest class is used for realizing public login and authority verification; on the basis of the BaseTest class, generating a test class; specifically, a basic test function is realized through a Junit5 test framework, a dependency interface is simulated through a Mockito test framework, and unit test result judgment is realized through an Assert test framework; according to a unit test requirement of a target project, executing the test class, and generating a test report; according to the invention, the test efficiency and coverage rate are improved, the maintainability and reusability of the test are enhanced, and the accuracy and readability of the test result are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of system development and testing, and in particular relates to a unit testing method and related devices. Background Art

[0002] System software code testing is to test the code program of the entire system to verify whether the functions and performance of the software system meet the requirements; unit testing, as a crucial environment in system software code testing, mainly focuses on the basic components of the software, such as functions, classes or modules, to verify the correctness, with the aim of discovering various errors that may exist in each module and ensuring that each module works as expected.

[0003] In the existing unit testing process, the management and maintenance of test data are generally complex, and a large number of test tools and frameworks are needed to assist testing, such as TestNG and EasyMock. As a result, the existing unit testing programs have poor maintainability and reusability, and thus cannot ensure the accuracy of unit testing results, and there is a problem of low testing efficiency. Summary of the invention

[0004] In view of the technical problems existing in the prior art, the present invention provides a unit testing method and related devices to solve the technical problems that the existing unit testing programs have poor maintainability and reusability, and thus cannot ensure the accuracy of the unit testing results, and have low testing efficiency.

[0005] In order to achieve the above object, the technical solution adopted by the present invention is: The present invention provides a unit testing method, comprising: Introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; Based on the test component, write the BaseTest class; wherein the BaseTest class is used to implement public login and permission verification; Based on the BaseTest class, a test class is generated; specifically, the basic test function is implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework; According to the unit test requirements of the target project, the test class is executed and a test report is generated.

[0006] Furthermore, in the process of introducing the test component into the target project, the pom.xml file of the target project sets the Spring-boot-starter-test package; wherein the version of the Spring-boot-starter-test package is the same as or compatible with the development framework version of the target project.

[0007] Furthermore, the test class is annotated with @Test.

[0008] Furthermore, according to the unit test requirements of the target project, the test class is executed, and in the process of generating the test report, the test report is generated using the Idea development tool.

[0009] Furthermore, the file format of the test report is an XML file format or an HTML file format; wherein, if the file format of the test report is an HTML file format, the test includes a file named: XXX system unit test report.html file.

[0010] Furthermore, the test report includes statistical results of the number of errors, failures, and passes in the test classes.

[0011] The present invention also provides a unit testing system, comprising: A test component module, used to introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; The test base class module is used to write the BaseTest class based on the test component; wherein the BaseTest class is used to implement public login and permission verification; The test class module is used to generate a test class based on the BaseTest class; specifically, the basic test function is implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework; The test execution module is used to execute the test class and generate a test report according to the unit test requirements of the target project.

[0012] The present invention also provides a unit testing device, comprising: a processor suitable for executing a computer program; A computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by the processor, the unit testing method is executed.

[0013] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program implements the unit testing method when executed by a processor.

[0014] The present invention also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the unit testing method is implemented.

[0015] Compared with the prior art, the present invention has the following beneficial effects: The unit testing method provided by the present invention significantly improves the efficiency and quality of unit testing, enhances the maintainability and reusability of testing, facilitates the generation and management of test reports, and reduces testing costs and risks by integrating a test framework, writing a BaseTest class, and automatically generating a test report. Specifically, the test components including the Junit5 test framework, the Mockito test framework, and the Assert test framework are introduced into the target project. Among them, the Junit5 test framework provides rich assertion methods, parameterized tests, test suites and other functions, making the test simpler, more intuitive and flexible. The Mockito test framework simulates dependency relationships. In this way, unit testing can be performed without relying on external systems, simplifying the test environment construction process and improving test efficiency. The Assert test framework provides an assertion method to verify the correctness of the test results. Through the assertion, you can clearly understand whether the test passes and the reasons for failure. Secondly, by writing the BaseTest class, the common login and permission verification logic can be implemented to avoid repeated code writing and improve the maintainability and reusability of the test code. Among them, when generating specific test classes based on the BaseTest class, testers only need to focus on the specific test logic without having to rewrite common logic such as login and permission verification, thereby simplifying the test class writing process.

[0016] The unit testing system, unit testing equipment, computer-readable storage medium and computer program product provided by the present invention have all the advantages of the above-mentioned unit testing method. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0018] Figure 1 A flow chart of the unit testing method provided in Example 1; Figure 2 A structural block diagram of the unit test system provided in Example 2; Figure 3 This is a structural block diagram of the unit testing equipment provided in Example 3. DETAILED DESCRIPTION

[0019] In order to make the technical problems, technical solutions and beneficial effects solved by this application clearer, the technical solutions in the embodiments of this application will be described clearly and completely in combination with the drawings in the embodiments of this application; obviously, the described embodiments are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0020] Example 1 As attached Figure 1 As shown, this embodiment 1 provides a unit testing method, comprising the following steps: Step 1: Build a test environment. Specifically, the test environment includes development language, development framework and development tools. The development language is Java, and the version is Java8. The development framework uses SpringBoot2.4.4, and the development tool is Idea2023.3.2.

[0021] Step 2: Introduce test components into the target project, wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework. Specifically, set the Spring-boot-starter-test package in the pom.xml file of the target project, wherein the version of the Spring-boot-starter-test package is the same as or compatible with the development framework version of the target project.

[0022] It should be noted that the Junit5 test framework, Mockito test framework and Assert test framework are all integrated in the development framework SpringBoot2.4.4. By relying on the Spring-boot-starter-test package in the pom.xml file of the target project of the system to be tested, the dependency of the test component can be realized; specifically, by adding preset code in the pom.xml file to introduce the test component: wherein the preset code is specifically: <dependency> <groupid> org.springframework.boot< / groupid> <artifactid> spring-boot-starter-test< / artifactid> <scope> test< / scope> < / dependency> It should also be noted that the version of the test component is based on the version of Springboot; if other versions of test components need to be introduced, they can be implemented by separately introducing them and specifying the version number; the separately introduced version is compatible with the current Springboot version.

[0023] Step 3: Write the BaseTest class based on the test component, which is used to implement public login and permission verification. By writing the BaseTest class, public dependencies are introduced to implement login and permission verification. When other test classes inherit the BaseTest class, they can have the functions without having to implement them separately, which effectively improves the reusability of the test code and simplifies other functional test class codes, so that you only need to focus on specific functional tests.

[0024] Preferably, the code implementation of the BaseTest class is as follows: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment. RAND OM_PORT ) @AutoConfigureMockMvc @ActiveProfiles("test") public abstract class BaseTest { @Autowired protected MockMvc mockMvc; @Before public void setup() { } public ResultActions buildRequest(Supplier <mockhttpservletrequestbuilder>method) throws Exception{ Stringheader="BeareryJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjUxZjc0MGRkLTFiZDMtNDc0Ny04MDlhLWE2OD FkY2NiY2NkMSJ9.UYiOgkFVj_IeEaxmj0HHUfUjB3fvR5UEvOytpnN9AmMGjRnQcxRPX5cawkVuOanRIj_5JK5yBAH7Q3gnhIFbAA"; Return this.mockMvc.perform(method.get().characterEncoding(StandardCharsets. UTF_8 .name()). contentType(MediaType. APPLICATION_JSON ) .header(HttpHeaders. AUTHORIZATION , header)); } } It should be noted that by adding preset annotations in the BaseTest class, the common dependencies of the test are introduced; wherein the preset annotations are as follows: @RunWith(SpringRunner.class), @SpringBootTest(webEnvironment=pringBootTest.WebEnvironment. RANDOM_PORT ), @AutoConfigureMockMvc, @ActiveProfiles("test"); handle the permission verification of interface calls in the buildRequest method.

[0025] Step 4: Generate a test class based on the BaseTest class. Specifically, the basic test function is implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework.

[0026] It is worth noting that when generating a test class, the test class needs to inherit the BaseTest class, and then write specific functional test code; the @Test annotation is added to the test class; wherein, @Test indicates that the method is a test method; for example: the annotation adding process of the getInfo method is specifically: @Test void getInfo() throwsException {}.

[0027] Step 5: Execute the test class and generate a test report according to the unit test requirements of the target project. Specifically, when executing a single test class, right-click on the class to pop up the menu and select Run; if executing all test classes under the package, right-click on the package to pop up the menu and select Run; execute all as above. After the test class is executed, the test report is automatically generated through the development tool Idea2023.3.2.

[0028] Step 6: Export the test report according to a preset file format. Specifically, the file format of the test report is an XML file format or an HTML file format; wherein, if the file format of the test report is an HTML file format, the test includes a file named: XXX system unit test report.html file.

[0029] It should be noted that the test report counts the number of test cases with errors, failures, and passes; the test report can be used to understand the overall unit testing status of the current system; clicking on a specific test case will display specific report information, and the code bug can be modified according to the information prompts; after the modification is completed, the unit test can be executed again; and this process is repeated until all bugs are fixed.

[0030] Example 2 As attached Figure 2 As shown, this embodiment 2 provides a unit testing system, including an environment building module, a test component module, a test base class module, a test class module, a test execution module and a report export module.

[0031] An environment building module is used to build a test environment; a test component module is used to introduce test components into a target project; wherein the test components include a Junit5 test framework, a Mockito test framework and an Assert test framework; a test base class module is used to write a BaseTest class based on the test component; wherein the BaseTest class is used to implement public login and permission verification; a test class module is used to generate a test class based on the BaseTest class; specifically, the basic test functions are implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework; a test execution module is used to execute the test class and generate a test report according to the unit test requirements of the target project; a report export module is used to export the test report in a preset file format.

[0032] Example 3 As attached Figure 3 As shown, this embodiment 3 provides a unit testing device, including: a memory for storing a computer program; and a processor for implementing the steps of the unit testing method when executing the computer program.

[0033] When the processor executes the computer program, the steps of the above-mentioned unit testing method are implemented, for example: Build a test environment; introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; based on the test components, write BaseTest class; wherein the BaseTest class is used to implement public login and permission verification; based on the BaseTest class, generate a test class; specifically, implement basic test functions through Junit5 test framework, simulate dependency interfaces through Mockito test framework, and implement unit test result determination through Assert test framework; execute the test class and generate a test report according to the unit test requirements of the target project; export the test report in a preset file format.

[0034] Alternatively, when the processor executes the computer program, the functions of each module in the above-mentioned unit test system are realized, for example: An environment building module is used to build a test environment; a test component module is used to introduce test components into a target project; wherein the test components include a Junit5 test framework, a Mockito test framework and an Assert test framework; a test base class module is used to write a BaseTest class based on the test component; wherein the BaseTest class is used to implement public login and permission verification; a test class module is used to generate a test class based on the BaseTest class; specifically, the basic test functions are implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework; a test execution module is used to execute the test class and generate a test report according to the unit test requirements of the target project; a report export module is used to export the test report in a preset file format.

[0035] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing preset functions, and the instruction segments are used to describe the execution process of the computer program in the unit test device.

[0036] For example, the computer program can be divided into an environment building module, a test component module, a test base class module, a test class module, a test execution module and a report export module, and the specific functions of each module are as follows: an environment building module, which is used to build a test environment; a test component module, which is used to introduce test components into a target project; wherein the test components include a Junit5 test framework, a Mockito test framework and an Assert test framework; a test base class module, which is used to write a BaseTest class based on the test component; wherein the BaseTest class is used to implement public login and permission verification; a test class module, which is used to generate a test class based on the BaseTest class; specifically, the basic test function is implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result judgment is implemented through the Assert test framework; a test execution module, which is used to execute the test class and generate a test report according to the unit test requirements of the target project; a report export module, which is used to export the test report in a preset file format.

[0037] The unit test device can be a computing device such as a desktop computer, a notebook, a PDA, and a cloud server. The unit test device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the above are examples of unit test devices and do not constitute a limitation on the unit test device. More components than the above may be included, or some components may be combined, or different components may be included. For example, the unit test device may also include input and output devices, network access devices, buses, etc.

[0038] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the unit test equipment, and uses various interfaces and lines to connect various parts of the entire unit test equipment.

[0039] The memory may be used to store the computer program and / or module, and the processor implements various functions of the unit testing device by running or executing the computer program and / or module stored in the memory and calling the data stored in the memory.

[0040] The memory may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (SmartMediaCard, SMC), a secure digital (SecureDigital, SD) card, a flash card (FlashCard), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0041] Example 4 This embodiment 4 further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the unit testing method are implemented, for example: Build a test environment; introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; based on the test components, write BaseTest class; wherein the BaseTest class is used to implement public login and permission verification; based on the BaseTest class, generate a test class; specifically, implement basic test functions through Junit5 test framework, simulate dependency interfaces through Mockito test framework, and implement unit test result determination through Assert test framework; execute the test class and generate a test report according to the unit test requirements of the target project; export the test report in a preset file format.

[0042] If the modules / units integrated in the unit testing system are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium.

[0043] Based on such understanding, the present invention implements all or part of the process in the above-mentioned unit testing method, and can also be completed by instructing related hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and when the computer program is executed by a processor, the steps of the above-mentioned unit testing method can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or preset intermediate form, etc.

[0044] The computer-readable storage medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal and software distribution medium, etc.

[0045] It should be noted that the content contained in the computer-readable storage medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable storage media do not include electrical carrier signals and telecommunication signals.

[0046] Example 5 This embodiment 5 provides a computer product, which computer program product includes a computer program, which is stored in a computer-readable storage medium; the processor of the unit testing device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the unit testing device can execute the unit testing method described in embodiment 1, which will not be repeated here.

[0047] It should be noted that a person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing related hardware through a computer program. The program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods.

[0048] In the present invention, by introducing a mature test framework, realizing common logic, generating efficient test classes and generating detailed test reports, the efficiency and quality of the test are significantly improved; wherein, by writing the BaseTest class, the logic of common login and permission verification is realized, so that in subsequent test classes, there is no need to repeatedly write the common logic, thereby reducing code redundancy and improving the readability and maintainability of the test code; based on the BaseTest class, a test class for a specific function can be quickly generated, and the test class combines the basic test function of Junit5, the simulation function of Mockito and the assertion function of Assert, which can fully cover the unit test requirements of the target project, so that the test code is highly consistent with the business code, ensuring the comprehensiveness and accuracy of the test.

[0049] The unit testing method described in the present invention is easy to write and easy to use. It can not only meet the unit testing requirements of the target project, but also improve the efficiency and quality of the test, provide strong guarantees for the stability and reliability of the project, and play an important role in improving code quality and development efficiency and reducing maintenance costs.

[0050] The above embodiment is only one of the implementation methods that can realize the technical solution of the present invention. The scope of protection claimed by the present invention is not limited only to this embodiment, but also includes changes, replacements and other implementation methods that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed by the present invention.< / mockhttpservletrequestbuilder>

Claims

1. A unit testing method, characterized in that: include: Introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; Based on the test component, write the BaseTest class; wherein the BaseTest class is used to implement public login and permission verification; Generate a test class based on the BaseTest class; specifically, implement basic test functions through the Junit5 test framework, simulate dependency interfaces through the Mockito test framework, and implement unit test result determination through the Assert test framework; According to the unit test requirements of the target project, the test class is executed and a test report is generated.

2. A unit testing method according to claim 1, characterized in that: In the process of introducing the test component into the target project, the pom.xml file of the target project sets the Spring-boot-starter-test package; wherein the version of the Spring-boot-starter-test package is the same as or compatible with the development framework version of the target project.

3. A unit testing method according to claim 1, characterized in that: The @Test annotation is added to the test class.

4. A unit testing method according to claim 1, characterized in that: According to the unit test requirements of the target project, the test class is executed, and in the process of generating the test report, the Idea development tool is used to generate the test report.

5. A unit testing method according to claim 1, characterized in that: The file format of the test report is XML file format or html file format; wherein, if the file format of the test report is html file format, the test includes a file named: XXX system unit test report.html file.

6. A unit testing method according to claim 1, characterized in that: The test report includes statistical results of the number of errors, failures, and passed test classes.

7. A unit testing system, characterized in that: include: A test component module, used to introduce test components into the target project; wherein the test components include Junit5 test framework, Mockito test framework and Assert test framework; The test base class module is used to write the BaseTest class based on the test component; wherein the BaseTest class is used to implement public login and permission verification; The test class module is used to generate a test class based on the BaseTest class; specifically, the basic test function is implemented through the Junit5 test framework, the dependency interface is simulated through the Mockito test framework, and the unit test result determination is implemented through the Assert test framework; The test execution module is used to execute the test class and generate a test report according to the unit test requirements of the target project.

8. A unit testing device, characterized in that: include: a processor suitable for executing a computer program; A computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by the processor, the unit testing method according to any one of claims 1 to 6 is executed.

9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the unit testing method according to any one of claims 1 to 6 is implemented.

10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the unit testing method according to any one of claims 1 to 6 is implemented.