Unit test case screening method and device, equipment, medium and program product

Through the unit test case screening mechanism based on code coverage, combined with monitoring business function test data and preset classification algorithm, the problem of redundancy and insufficient code coverage of the unit test case automation generation tool is solved, and the unit test case is realized Efficient filtering and code quality improvement.

CN118916281APending Publication Date: 2024-11-08CCB FINTECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410973544.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-11-08

AI Technical Summary

Technical Problem

The use cases generated by the existing unit test case automation generation tool are redundant, and the filtering method based on set rules and features cannot achieve the most complete code coverage, and it is impossible to effectively filter out a complete set of unit test cases.

Method used

Introduce a unit test case filtering mechanism based on code coverage, generate unit test cases to be filtered by testing data collected when monitoring business function tests, compare the code coverage of candidate unit test cases and unit test cases to be filtered, based on presets The classification algorithm determines the filtered unit test cases.

Benefits of technology

Filter unit test cases more accurately and effectively, achieve maximum coverage of the tested code, reduce the number of unit test cases, and improve the overall code quality of unit tests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916281A_ABST
    Figure CN118916281A_ABST
Patent Text Reader

Abstract

The invention provides a unit test case screening method and device, equipment, a medium and a program product, and relates to the technical field of computers. The method comprises the steps of obtaining a target case group corresponding to a target method, wherein the target case group comprises a to-be-screened unit test case of the target method generated based on a unit test case generation rule and test data collected during monitoring of a service function test; determining candidate unit test cases containing extreme values and / or enumeration values according to the unit test cases to be screened; executing a unit test on the candidate unit test case, and obtaining a first code coverage rate; unit testing is executed on the unit testing cases to be screened, and a second code coverage rate is obtained; and based on the first code coverage rate and the second code coverage rate, determining a screened unit test case corresponding to the target method from the to-be-screened unit test cases. According to the method and the device, the unit test cases can be screened more accurately and effectively.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a unit test case screening method, device, equipment, medium and program product. Background Art

[0002] Unit testing refers to the inspection and verification of the smallest testable unit in the software, which is used to find hidden problems in the code as early as possible before integration testing. The effectiveness and coverage of unit test cases directly affect the comprehensive testing of the system to be tested.

[0003] At present, a unit test case automatic generation tool is usually used to automatically generate unit test cases. However, the unit test cases automatically generated in the above manner are redundant. Therefore, an effective solution for screening unit test cases is urgently needed. Summary of the invention

[0004] The present application provides a unit test case screening method, device, equipment, medium and program product to accurately and effectively screen unit test cases.

[0005] In a first aspect, the present application provides a unit test case screening method, comprising:

[0006] Obtain a target use case group corresponding to a target method, the target use case group includes unit test cases to be screened of the target method, the unit test cases to be screened are generated based on a unit test case generation rule and test data collected during monitoring business function testing;

[0007] Determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values;

[0008] Execute unit testing on the candidate unit test case, and obtain a first code coverage rate corresponding to the candidate unit test case;

[0009] Execute unit tests on the unit test cases to be screened, and obtain second code coverage corresponding to the unit test cases to be screened;

[0010] Based on the first code coverage and the second code coverage, a filtered unit test case corresponding to the target method is determined from the unit test cases to be filtered.

[0011] In one possible design, based on the first code coverage and the second code coverage, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened, including: if the first code coverage and the second code coverage are the same, then the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened as candidate unit test cases; if the first code coverage and the second code coverage are different, then based on a preset classification algorithm, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened.

[0012] In one possible design, based on a preset classification algorithm, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened, including: grouping the non-candidate unit test cases in the unit test cases to be screened to obtain multiple first test case groups; for each first test case group in the multiple first test case groups, performing unit tests on the candidate unit test cases and the non-candidate unit test cases in the first test case group, and obtaining the third code coverage corresponding to the first test case group; if the third code coverage is different from the first code coverage, the non-candidate unit test cases in the first test case group are added to the candidate case group containing the candidate unit test cases, and the screened unit test cases are determined to be the unit test cases contained in the candidate case group; if the third code coverage is the same as the first code coverage, the first test case group is deleted.

[0013] In one possible design, the unit test case screening method also includes: if the number of non-candidate unit test cases in the first test case group is greater than a quantity threshold, grouping the non-candidate unit test cases in the first test case group to obtain multiple second test case groups; based on the second test case group and the candidate unit test cases, determining whether to add the non-candidate unit test cases in the second test case group to the candidate case group.

[0014] In a possible design, the unit test cases to be screened include candidate unit test cases, unit test cases to be executed, and other unit test cases; the candidate unit test cases are stored in the candidate workspace; the unit test cases to be executed are stored in the execution workspace; and other unit test cases are stored in the pending workspace.

[0015] In one possible design, before obtaining the target use case group corresponding to the target method, the unit test case screening method also includes: preprocessing the target test data corresponding to the target method based on preset condition rules to obtain preprocessed test data, the preprocessing including sorting and / or screening the target test data; generating unit test cases to be screened for the target method based on the preprocessed test data and unit test case generation rules.

[0016] In one possible design, the test data collected when monitoring business function testing includes the target method, the target type to which the target method belongs, the parameter value, return value and exception return information of the target method, and the internal type that the target method depends on and the method corresponding to the internal type.

[0017] In a possible design, the target use case grouping is obtained by grouping the test data based on the target type to which the target method belongs and the declaration form of the target method.

[0018] In a second aspect, the present application provides a unit test case screening device, comprising:

[0019] An acquisition module is used to acquire a target use case group corresponding to a target method, wherein the target use case group includes unit test cases to be screened of the target method, and the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing;

[0020] A first determination module is used to determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values;

[0021] A first processing module, configured to execute unit testing on candidate unit test cases and obtain a first code coverage rate corresponding to the candidate unit test cases;

[0022] A second processing module is used to perform unit testing on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened;

[0023] The second determining module is used to determine the screened unit test cases corresponding to the target method from the unit test cases to be screened based on the first code coverage and the second code coverage.

[0024] In one possible design, the second determination module is specifically used to: if the first code coverage and the second code coverage are the same, then determine the screened unit test cases corresponding to the target method from the unit test cases to be screened as candidate unit test cases; if the first code coverage and the second code coverage are different, then based on a preset classification algorithm, determine the screened unit test cases corresponding to the target method from the unit test cases to be screened.

[0025] In one possible design, when the second determination module is used to determine the screened unit test cases corresponding to the target method from the unit test cases to be screened based on a preset classification algorithm, it is specifically used to: group the non-candidate unit test cases in the unit test cases to be screened to obtain multiple first test case groups; for each first test case group in the multiple first test case groups, perform unit tests on the candidate unit test cases and the non-candidate unit test cases in the first test case group, and obtain the third code coverage corresponding to the first test case group; if the third code coverage is different from the first code coverage, add the non-candidate unit test cases in the first test case group to the candidate case group containing the candidate unit test cases, and determine that the screened unit test cases are unit test cases included in the candidate case group; if the third code coverage is the same as the first code coverage, delete the first test case group.

[0026] In one possible design, the second determination module is also used for: if the number of non-candidate unit test cases in the first group of test cases is greater than a quantity threshold, grouping the non-candidate unit test cases in the first group of test cases to obtain multiple second groups of test cases; based on the second group of test cases and the candidate unit test cases, determining whether to add the non-candidate unit test cases in the second group of test cases to the candidate case group.

[0027] In a possible design, the unit test cases to be screened include candidate unit test cases, unit test cases to be executed, and other unit test cases; the candidate unit test cases are stored in the candidate workspace; the unit test cases to be executed are stored in the execution workspace; and other unit test cases are stored in the pending workspace.

[0028] In one possible design, the acquisition module is also used to: before obtaining the target use case group corresponding to the target method, preprocess the target test data corresponding to the target method based on preset conditional rules to obtain preprocessed test data, and the preprocessing includes sorting and / or screening the target test data; based on the preprocessed test data and unit test case generation rules, generate unit test cases to be screened for the target method.

[0029] In one possible design, the test data collected when monitoring business function testing includes the target method, the target type to which the target method belongs, the parameter value, return value and exception return information of the target method, and the internal type that the target method depends on and the method corresponding to the internal type.

[0030] In a possible design, the target use case grouping is obtained by grouping the test data based on the target type to which the target method belongs and the declaration form of the target method.

[0031] In a third aspect, the present application provides an electronic device comprising: at least one processor and a memory; the memory stores computer execution instructions; the at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the unit test case screening method described in the first aspect and various possible designs of the first aspect.

[0032] In a fourth aspect, the present application provides a computer-readable storage medium, which stores computer execution instructions. When the computer execution instructions are executed by a processor, they are used to implement the unit test case screening method described in the first aspect and various possible designs of the first aspect.

[0033] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the unit test case screening method described in the first aspect and various possible designs of the first aspect.

[0034] The unit test case screening method, apparatus, equipment, medium and program product provided in the present application obtain a target case group corresponding to a target method, wherein the target case group includes unit test cases to be screened of the target method, and the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing; according to the unit test cases to be screened, candidate unit test cases are determined, and parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values; unit tests are performed on the candidate unit test cases, and a first code coverage rate corresponding to the candidate unit test cases is obtained; unit tests are performed on the unit test cases to be screened, and a second code coverage rate corresponding to the unit test cases to be screened is obtained; based on the first code coverage rate and the second code coverage rate, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened. In the present application, a unit test case screening mechanism based on code coverage is introduced. The unit test cases to be screened are screened based on the first code coverage corresponding to the candidate unit test cases and the second code coverage corresponding to the unit test cases to be screened, so as to determine the unit test cases after screening. Compared with the unit test case screening mechanism based on set rules and features, the present application can screen unit test cases more accurately and effectively. On the premise of achieving maximum coverage of the code under test, the number of unit test cases can be effectively reduced, thereby improving the overall code quality of the unit test. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0036] Figure 1 A schematic diagram of an application scenario provided for an embodiment of the present application;

[0037] Figure 2 A flowchart of a unit test case screening method provided in an embodiment of the present application;

[0038] Figure 3 A flowchart of a unit test case screening method provided by another embodiment of the present application;

[0039] Figure 4 A flowchart of a unit test case screening method provided in yet another embodiment of the present application;

[0040] Figure 5 A schematic diagram of the structure of a unit test case screening device provided in one embodiment of the present application;

[0041] Figure 6 A schematic diagram of the structure of an electronic device provided in one embodiment of the present application.

[0042] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0043] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0044] It should be noted that the collection, storage, use, processing, transmission, provision and disclosure of financial data or user data and other information involved in the technical solution of this application are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals. The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards, and provide corresponding operation portals for users to choose to authorize or refuse.

[0045] First, some technical terms involved in this application are explained:

[0046] An open source framework (Spring) was created to address the complexity of enterprise application development; Spring is a lightweight container framework for Inversion of Control (IoC) and Aspect-Oriented Programming (AOP); for applications based on the Spring framework, a series of bean objects (i.e. objects managed by the Spring container) need to be generated during the implementation of program functions, and specific functions are implemented by operating these bean objects.

[0047] Unit testing technology and unit test code generation auxiliary technology are commonly used technologies in the testing field. For example, unit test code generation technology based on database tables and standard templates, unit test code generation methods based on generative artificial intelligence (AI) dialogue technology, etc.

[0048] At present, a unit test case automatic generation tool is usually used to automatically generate unit test cases. However, the unit test cases automatically generated in the above manner are redundant. Therefore, an effective solution for screening unit test cases is urgently needed.

[0049] In addition, in the related art, unit test cases can be screened based on set rules and features, but because the rules lack judgments on features based on business and other criteria, the unit test case set screened by the above screening method cannot achieve the purpose of the most complete code coverage, and thus cannot effectively screen out a complete set of unit test cases.

[0050] To solve the above problems, an embodiment of the present application provides a unit test case screening method, introduces a unit test case screening mechanism based on code coverage, generates unit test cases to be screened for the target method based on the test data collected when monitoring business function testing; determines candidate unit test cases whose parameter values ​​contain extreme values ​​and / or enumeration values ​​from the unit test cases to be screened; compares the first code coverage corresponding to the candidate unit test cases with the second code coverage corresponding to the unit test cases to be screened, and determines the screened unit test cases corresponding to the target method from the unit test cases to be screened, which can screen unit test cases more accurately and effectively, and can effectively reduce the number of unit test cases while achieving maximum coverage of the code under test, thereby improving the overall code quality of the unit test.

[0051] In the following, the application scenarios of the solutions provided in this application are first illustrated.

[0052] Figure 1This is a schematic diagram of an application scenario provided by an embodiment of the present application. Figure 1 As shown, in this application scenario, the developer performs functional testing on the application through the client 101, the server 102 monitors the business function test and collects test data, and obtains the target use case grouping corresponding to the target method of the target type of the application based on the collected test data, and the target use case grouping includes the unit test cases to be screened of the target method. The server 102 determines the candidate unit test cases whose parameter values ​​contain extreme values ​​and / or enumeration values ​​from the unit test cases to be screened; compares the first code coverage corresponding to the candidate unit test cases with the second code coverage corresponding to the unit test cases to be screened, and determines the screened unit test cases corresponding to the target method from the unit test cases to be screened. Among them, the client 101 and the server 102 can communicate through a wired network or a wireless network.

[0053] It should be noted that Figure 1 This is only a schematic diagram of an application scenario provided by the embodiment of the present application. Figure 1 does not limit the equipment included in Figure 1 The positional relationship between the devices is limited.

[0054] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0055] Figure 2 The flowchart of the unit test case screening method provided by an embodiment of the present application. The method of the embodiment of the present application can be applied to an electronic device, which can be a server or a server cluster. Figure 2 As shown, the method of the embodiment of the present application includes:

[0056] S201. Obtain a target use case group corresponding to a target method, wherein the target use case group includes unit test cases to be screened of the target method, and the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing.

[0057] In the embodiments of the present application, illustratively, when performing business function testing, the business function testing can be monitored, such as using AOP technology to intercept the input and output of Spring beans to collect test data.

[0058] Optionally, the test data collected when monitoring business function testing may include the target method, the target type to which the target method belongs, the parameter value, return value and exception return information of the target method, and the internal type on which the target method depends and the method corresponding to the internal type.

[0059] Exemplarily, the collected test data is parsed and processed to obtain relevant information of the target type and corresponding test conditions. Specifically, the entry interface type information is read, including type and method, to form the target type and target method to be tested, wherein the type is, for example, a Java (an object-oriented programming language) class, and the method is, for example, a method contained in a Java class. The parameter values ​​of the input parameters of the entry request and the return value or abnormal return information are read as the expected conditions for the direct input parameters and return value determination of the unit test request. The internal type and the set of methods corresponding to the internal type on which the target method depends obtained according to the call stack are read, and the internal type should be an instance object managed by SpringBeanFactory (an interface of a Spring framework, used to manage and configure Beans in applications). Based on the above set, the target type set to be simulated (Mock) and the target method set to be defined by Motckito (a test framework) can be obtained, and the input parameters and return values ​​or abnormal return results of each target method call need to be read.

[0060] Optionally, the target use case grouping is obtained by grouping the test data based on the target type to which the target method belongs and the declaration form of the target method.

[0061] Exemplarily, after the functional test is completed, the parsed and processed test data is classified. The test data can be grouped according to the target type to which the target method belongs and the declaration form of the target method. Test data with the same target type are grouped together, and in the above grouping, further grouping is performed according to the declaration form of the target method to form a secondary grouping data set.

[0062] Optionally, the target test data corresponding to the target method can be preprocessed based on preset conditional rules to obtain preprocessed test data, and the preprocessing includes sorting and / or screening the target test data; based on the preprocessed test data and unit test case generation rules, unit test cases to be screened for the target method are generated.

[0063] Exemplarily, the preset conditional rules are conditional rules for preprocessing, which can be configured as needed, for example, sorting by the first letter of the name, filtering out test data that does not meet the requirements, etc. Based on the preset conditional rules, the test data in the secondary grouping data set (i.e., the target test data corresponding to the target method) is preprocessed to obtain preprocessed test data. Preprocessing, for example, is to sort and filter the target test data. Based on the preprocessed test data and the unit test case generation rules, the unit test cases to be screened for the target method are generated, thereby obtaining the target case grouping. Among them, the unit test case generation rules can be configured as needed to generate unit test cases.

[0064] Optionally, you can also set preset exclusion rules for known unexpected test data before grouping the test data based on specific conditional rules. For example, in a repeatedly executed order creation process, you can set exclusion rules to exclude most invalid order test data.

[0065] S202. Determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values.

[0066] Exemplarily, the basic range covered by the unit test case can be preconfigured, and the basic range includes, for example, the boundary value of the unit test case. For the unit test case to be screened, it is preferred to sort according to the sortable parameter data and the enumerable data, and the option whose parameter value is the extreme value (i.e., maximum value or minimum value) or the independent enumeration value in the ordered result set is preferentially selected. Optionally, other predefined unit test case data screening methods can be added according to the needs of the test target (i.e., the target method) to determine the candidate unit test case. The above-mentioned test data containing extreme values ​​or independent enumeration values ​​are selected as the initial candidate data generated by the unit test case to be screened, and the unit test case to be screened corresponding to the initial candidate data is used as the candidate unit test case.

[0067] S203: Execute unit testing on the candidate unit test case, and obtain a first code coverage rate corresponding to the candidate unit test case.

[0068] Exemplarily, after determining the candidate unit test cases, the unit test execution component and the coverage analysis component can be deployed to build a unit test execution analysis framework. Among them, the unit test execution component is, for example, a test plug-in for Maven (a project management and build automation tool), the coverage analysis component is, for example, JaCoCo (a code coverage analysis tool), and the code coverage is, for example, code line coverage or code branch coverage. Unit tests are performed on the candidate unit test cases, and a unit test code coverage report is obtained based on JaCoCo, and the content related to the target type and target method of the target case grouping in the unit test code coverage report is saved, so as to obtain the first code coverage corresponding to the candidate unit test case.

[0069] S204: Execute unit test on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened.

[0070] In this step, unit testing is performed on non-candidate unit test cases in the unit test cases to be screened together with candidate unit test cases, that is, unit testing is performed on the unit test cases to be screened, and the second code coverage corresponding to the unit test cases to be screened can be obtained.

[0071] It should be noted that the embodiment of the present application does not limit the order in which steps S203 and S204 are executed.

[0072] S205. Determine, based on the first code coverage and the second code coverage, a screened unit test case corresponding to the target method from the unit test cases to be screened.

[0073] It can be understood that the first code coverage corresponding to the candidate unit test case is used as a benchmark, and the second code coverage obtained after adding the non-candidate unit test case is compared. The difference between the second code coverage and the first code coverage is the actual code coverage comparison. In this step, after obtaining the first code coverage and the second code coverage, the filtered unit test case corresponding to the target method can be determined from the unit test cases to be screened based on the first code coverage and the second code coverage. Exemplarily, if the first code coverage and the second code coverage are the same, it means that the candidate unit test case has covered the code coverage range of all unit test cases (i.e., the unit test cases to be screened), and no additional unit test cases are needed. The unit test cases to be screened of the target method are screened. If the first code coverage and the second code coverage are not the same, it is necessary to further screen and retain the non-candidate unit test cases in the unit test cases to be screened, so as to further obtain unit test cases that can improve the code coverage. For how to determine the filtered unit test case corresponding to the target method from the unit test cases to be screened based on the first code coverage and the second code coverage, refer to the subsequent embodiments, which will not be repeated here.

[0074] By executing steps S201 to S205, the screening of the unit test cases to be screened for the target method can be completed, thereby completing the screening of the unit test cases to be screened for all types and methods corresponding to the test data, and then obtaining a complete and effective unit test set after screening. Optionally, after obtaining the screened unit test cases, a predefined unit test case template can be obtained according to the target type to which the target method belongs, and according to preset rules, the screened unit test cases can be filled into the unit test case template to generate a unit test case file for unit testing.

[0075] The unit test case screening method provided in the embodiment of the present application obtains a target case group corresponding to a target method, the target case group includes unit test cases to be screened of the target method, the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing; according to the unit test cases to be screened, candidate unit test cases are determined, and parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values; unit tests are performed on the candidate unit test cases, and a first code coverage rate corresponding to the candidate unit test cases is obtained; unit tests are performed on the unit test cases to be screened, and a second code coverage rate corresponding to the unit test cases to be screened is obtained; based on the first code coverage rate and the second code coverage rate, a screened unit test case corresponding to the target method is determined from the unit test cases to be screened. In an embodiment of the present application, a unit test case screening mechanism based on code coverage is introduced. The unit test cases to be screened are screened based on the first code coverage corresponding to the candidate unit test cases and the second code coverage corresponding to the unit test cases to be screened, so as to determine the filtered unit test cases. Compared with the unit test case screening mechanism based on set rules and features, the embodiment of the present application can screen unit test cases more accurately and effectively. On the premise of achieving maximum coverage of the code under test, the number of unit test cases can be effectively reduced, thereby improving the overall code quality of the unit test.

[0076] Based on the above embodiment, optionally, the unit test cases to be screened include candidate unit test cases, unit test cases for unit tests to be executed, and other unit test cases; the candidate unit test cases are stored in the candidate workspace; the unit test cases for unit tests to be executed are stored in the execution workspace; and other unit test cases are stored in the pending workspace.

[0077] Exemplarily, when unit test case screening is performed, in order to improve execution efficiency, the execution work area, candidate work area and pending work area can be set under the unit test execution analysis framework. Among them, the unit test cases of the unit test to be executed are retained in the execution work area, all candidate unit test cases are retained in the candidate work area, and other unit test cases are retained in the pending work area. For each target case grouping, before each unit test is executed, the existing unit test cases in the execution work area are cleaned up, and according to the execution target of this unit test, only the unit test cases of the unit test executed this time are copied to the execution work area for execution. And after the code coverage analysis, according to the reservation rules (such as whether the code coverage is improved), it is determined whether the unit test cases in the execution work area are transferred to the candidate work area, so as to improve the execution efficiency of the single unit test and the code coverage analysis. It should be noted that the unit test cases of the above-mentioned execution unit test this time include the unit test cases whose target methods have been screened in the target case grouping and the unit test cases to be screened in the target case grouping.

[0078] Figure 3 This is a flowchart of a unit test case screening method provided by another embodiment of the present application. Based on the above embodiment, the present application embodiment further illustrates the unit test case screening method. Figure 3 As shown, the method of the embodiment of the present application may include:

[0079] S301. Obtain a target use case group corresponding to a target method, wherein the target use case group includes unit test cases to be screened of the target method, and the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing.

[0080] The detailed description of this step can be found in Figure 2 The relevant description of S201 in the illustrated embodiment will not be repeated here.

[0081] S302: Determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values.

[0082] The detailed description of this step can be found in Figure 2 The relevant description of S202 in the illustrated embodiment will not be repeated here.

[0083] S303: Execute unit testing on the candidate unit test case, and obtain a first code coverage rate corresponding to the candidate unit test case.

[0084] The detailed description of this step can be found in Figure 2 The relevant description of S203 in the illustrated embodiment will not be repeated here.

[0085] S304: Execute unit testing on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened.

[0086] The detailed description of this step can be found in Figure 2 The relevant description of S204 in the illustrated embodiment will not be repeated here.

[0087] It should be noted that the embodiment of the present application does not limit the order in which steps S303 and S304 are executed.

[0088] In the embodiment of the present application, Figure 2 The step S205 may further include the following two steps S305 and S306:

[0089] S305: If the first code coverage and the second code coverage are the same, determine the filtered unit test case corresponding to the target method from the unit test cases to be filtered as the candidate unit test case.

[0090] In this step, after obtaining the first code coverage rate corresponding to the candidate unit test case and the second code coverage rate corresponding to the unit test case to be screened, the first code coverage rate and the second code coverage rate can be compared, and the difference between the second code coverage rate and the first code coverage rate is the actual code coverage comparison. If the first code coverage rate and the second code coverage rate are the same, it means that the candidate unit test case has covered the code coverage range of all unit test cases (i.e., the unit test cases to be screened), and no additional unit test cases are needed. Therefore, the filtered unit test case corresponding to the target method is determined from the unit test cases to be screened as the candidate unit test case, and accordingly, the screening of the unit test cases to be screened of the target method is completed.

[0091] S306: If the first code coverage rate and the second code coverage rate are different, based on a preset classification algorithm, determine the filtered unit test cases corresponding to the target method from the unit test cases to be filtered.

[0092] Exemplarily, the first code coverage and the second code coverage are compared. If the first code coverage and the second code coverage are different, it means that the non-candidate unit test cases in the unit test cases to be screened need to be further screened and retained to further obtain unit test cases that can improve the code coverage. The screened unit test cases corresponding to the target method can be determined from the unit test cases to be screened based on a preset classification algorithm. The preset classification algorithm can be defined as needed.

[0093] Further, optionally, based on a preset classification algorithm, determining the screened unit test cases corresponding to the target method from the unit test cases to be screened can include: grouping the non-candidate unit test cases in the unit test cases to be screened to obtain multiple first test case groups; for each first test case group in the multiple first test case groups, performing unit tests on the candidate unit test cases and the non-candidate unit test cases in the first test case group, and obtaining a third code coverage rate corresponding to the first test case group; if the third code coverage rate is different from the first code coverage rate, adding the non-candidate unit test cases in the first test case group to the candidate case group containing the candidate unit test cases, and determining that the screened unit test cases are unit test cases contained in the candidate case group; if the third code coverage rate is the same as the first code coverage rate, deleting the first test case group.

[0094] Exemplarily, the non-candidate unit test cases in the unit test cases to be screened are divided into a plurality of first test case groups. For each of the plurality of first test case groups, the obtained candidate unit test cases are sequentially combined with the non-candidate unit test cases in each first test case group, and the unit test is performed to obtain the third code coverage rate corresponding to the first test case group. The third code coverage rate is compared with the first code coverage rate. If the third code coverage rate is different from the first code coverage rate, it means that the code coverage rate has been improved. Therefore, the non-candidate unit test cases in the first test case group can be added to the candidate case group containing the candidate unit test cases, and the unit test cases after screening are determined to be the unit test cases contained in the candidate case group. If the third code coverage rate is the same as the first code coverage rate, it means that there is no need to add additional unit test cases. Therefore, the non-candidate unit test cases in the first test case group are deleted.

[0095] Optionally, the unit test case screening method provided in the embodiment of the present application may also include: if the number of non-candidate unit test cases in the first test case group is greater than a quantity threshold, grouping the non-candidate unit test cases in the first test case group to obtain multiple second test case groups; based on the second test case group and the candidate unit test cases, determining whether to add the non-candidate unit test cases in the second test case group to the candidate case group.

[0096] Exemplarily, when the number of non-candidate unit test cases in the first group of test cases is large, and there is a situation where the non-candidate unit test cases in the first group of test cases can improve the code coverage, the non-candidate unit test cases in the first group of test cases can be recursively subdivided into an appropriate scale, and unit test execution and code coverage comparison are performed with reference to the above embodiments. In the extreme case, a final group contains only one unit test case.

[0097] The unit test case screening method provided in the embodiment of the present application introduces a unit test case screening mechanism based on code coverage, determines candidate unit test cases from the unit test cases to be screened of the target method contained in the target case group, and screens the unit test cases to be screened based on the first code coverage corresponding to the candidate unit test cases and the second code coverage corresponding to the unit test cases to be screened. If the first code coverage and the second code coverage are the same, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened as candidate unit test cases; if the first code coverage and the second code coverage are not the same, the screened unit test cases corresponding to the target method are determined from the unit test cases to be screened based on a preset classification algorithm. Compared with the unit test case screening mechanism based on set rules and features, the embodiment of the present application can screen unit test cases more accurately and effectively, and can effectively reduce the number of unit test cases on the premise of achieving maximum coverage of the tested code, thereby improving the overall code quality of the unit test.

[0098] Based on the above embodiments, Figure 4 A flowchart of a unit test case screening method provided by another embodiment of the present application. Figure 4 As shown, the method of the embodiment of the present application may include:

[0099] S401. Acquire test data collected during monitoring business function testing.

[0100] Exemplarily, the test data may include, for example, the target method, the target type to which the target method belongs, parameter values, return values ​​and exception return information of the target method, as well as the internal types that the target method depends on and the methods corresponding to the internal types.

[0101] S402: Group the test data based on the target type to which the target method belongs and the declaration form of the target method to obtain target test data corresponding to the target method.

[0102] S403. Based on preset conditional rules, preprocess the target test data corresponding to the target method to obtain preprocessed test data, wherein the preprocessing includes sorting and / or screening the target test data; based on the preprocessed test data and unit test case generation rules, generate unit test cases to be screened for the target method, and obtain a target case grouping containing unit test cases to be screened for the target method.

[0103] S404: Determine candidate unit test cases according to the unit test cases to be screened, wherein the parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values.

[0104] S405: Execute unit testing on the candidate unit test case, and obtain a first code coverage rate corresponding to the candidate unit test case.

[0105] S406: Execute unit testing on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened.

[0106] It should be noted that the embodiment of the present application does not limit the order in which steps S405 and S406 are executed.

[0107] S407: If the first code coverage and the second code coverage are the same, determine the filtered unit test case corresponding to the target method from the unit test cases to be filtered as the candidate unit test case.

[0108] S408. If the first code coverage rate and the second code coverage rate are different, the non-candidate unit test cases in the unit test cases to be screened are grouped based on a preset classification algorithm to obtain a plurality of first groups of test cases to be tested.

[0109] S409: For each first group of test cases in the plurality of first groups of test cases, perform unit testing on candidate unit test cases and non-candidate unit test cases in the first group of test cases, and obtain a third code coverage rate corresponding to the first group of test cases.

[0110] S410. If the third code coverage is different from the first code coverage, the non-candidate unit test cases in the first test case group are added to the candidate case group containing the candidate unit test cases, and the filtered unit test cases are determined to be the unit test cases contained in the candidate case group.

[0111] S411. If the third code coverage is the same as the first code coverage, delete the first group of test cases.

[0112] It can be understood that, through steps S409 to S411, non-candidate unit test cases in each first test case group can be screened. Optionally, when the number of non-candidate unit test cases in the first test case group is large, and there are non-candidate unit test cases in the first test case group that can improve code coverage, the non-candidate unit test cases in the first test case group can be recursively subdivided into a suitable scale, and the unit test execution and code coverage comparison are performed with reference to the above embodiment to determine the screened unit test cases corresponding to the final target method.

[0113] The unit test case screening method provided in the embodiment of the present application introduces a unit test case screening mechanism based on code coverage, obtains test data collected during monitoring business function testing, groups the test data based on the target type to which the target method belongs and the declaration form of the target method, and obtains target test data corresponding to the target method; preprocesses the target test data corresponding to the target method based on preset condition rules to obtain preprocessed test data, and the preprocessing includes sorting and / or screening the target test data; generates unit test cases to be screened for the target method based on the preprocessed test data and unit test case generation rules, and obtains a target case grouping containing unit test cases to be screened for the target method; and Determine candidate unit test cases in unit test cases, screen the unit test cases to be screened based on the first code coverage corresponding to the candidate unit test cases and the second code coverage corresponding to the unit test cases to be screened, if the first code coverage and the second code coverage are the same, determine the screened unit test cases corresponding to the target method from the unit test cases to be screened as candidate unit test cases; if the first code coverage and the second code coverage are not the same, then based on a preset classification algorithm, divide the non-candidate unit test cases in the unit test cases to be screened into multiple first test case groups, sequentially combine the obtained candidate unit test cases with the non-candidate unit test cases in each first test case group, and perform unit testing to obtain the third code coverage corresponding to the first test case group. Compare the third code coverage and the first code coverage to determine the final unit test cases corresponding to the target method. The embodiment of the present application can screen unit test cases more accurately and effectively, and can effectively reduce the number of unit test cases on the premise of achieving maximum coverage of the tested code, thereby improving the overall code quality of the unit test.

[0114] The following is an embodiment of the device of the present application, which can be used to execute the embodiment of the method of the present application. For details not disclosed in the embodiment of the device of the present application, please refer to the embodiment of the method of the present application.

[0115] Figure 5 A schematic diagram of the structure of a unit test case screening device provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, the unit test case screening device 500 of the embodiment of the present application includes: an acquisition module 501, a first determination module 502, a first processing module 503, a second processing module 504 and a second determination module 505. Among them:

[0116] Acquisition module 501 is used to acquire a target use case group corresponding to a target method. The target use case group includes unit test cases to be screened for the target method. The unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing.

[0117] The first determination module 502 is used to determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values.

[0118] The first processing module 503 is used to execute unit testing on the candidate unit test cases and obtain a first code coverage rate corresponding to the candidate unit test cases.

[0119] The second processing module 504 is used to execute unit test on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened.

[0120] The second determining module 505 is used to determine the filtered unit test cases corresponding to the target method from the unit test cases to be filtered based on the first code coverage and the second code coverage.

[0121] In one possible design, the second determination module 505 can be specifically used for: if the first code coverage and the second code coverage are the same, then determining the screened unit test cases corresponding to the target method from the unit test cases to be screened as candidate unit test cases; if the first code coverage and the second code coverage are different, then determining the screened unit test cases corresponding to the target method from the unit test cases to be screened based on a preset classification algorithm.

[0122] In one possible design, when the second determination module 505 is used to determine the screened unit test cases corresponding to the target method from the unit test cases to be screened based on a preset classification algorithm, it can be specifically used to: group the non-candidate unit test cases in the unit test cases to be screened to obtain multiple first test case groups; for each first test case group in the multiple first test case groups, perform unit tests on the candidate unit test cases and the non-candidate unit test cases in the first test case group, and obtain the third code coverage corresponding to the first test case group; if the third code coverage is different from the first code coverage, add the non-candidate unit test cases in the first test case group to the candidate case group containing the candidate unit test cases, and determine that the screened unit test cases are unit test cases included in the candidate case group; if the third code coverage is the same as the first code coverage, delete the first test case group.

[0123] In one possible design, the second determination module 505 can also be used for: if the number of non-candidate unit test cases in the first group of test cases is greater than a quantity threshold, grouping the non-candidate unit test cases in the first group of test cases to obtain multiple second groups of test cases; based on the second group of test cases and the candidate unit test cases, determining whether to add the non-candidate unit test cases in the second group of test cases to the candidate case group.

[0124] In a possible design, the unit test cases to be screened include candidate unit test cases, unit test cases to be executed, and other unit test cases; the candidate unit test cases are stored in the candidate workspace; the unit test cases to be executed are stored in the execution workspace; and other unit test cases are stored in the pending workspace.

[0125] In one possible design, the acquisition module 501 can also be used for: before obtaining the target use case group corresponding to the target method, based on preset conditional rules, preprocessing the target test data corresponding to the target method to obtain preprocessed test data, the preprocessing including sorting and / or screening the target test data; based on the preprocessed test data and unit test case generation rules, generating unit test cases to be screened for the target method.

[0126] In one possible design, the test data collected when monitoring business function testing includes the target method, the target type to which the target method belongs, the parameter value, return value and exception return information of the target method, and the internal type that the target method depends on and the method corresponding to the internal type.

[0127] In a possible design, the target use case grouping is obtained by grouping the test data based on the target type to which the target method belongs and the declaration form of the target method.

[0128] It is worth noting that the unit test case screening device provided in the above embodiments can be used to execute the steps in the unit test case screening method provided in any of the above embodiments. The specific implementation method and technical effect are similar and will not be repeated here.

[0129] The above-mentioned device embodiments provided in the present application are merely illustrative, and the module division therein is merely a logical function division, and there may be other division methods in actual implementation. For example, multiple modules can be combined or integrated into another system. The coupling between the modules can be achieved through some interfaces, which are usually electrical communication interfaces, but it is not ruled out that they may be mechanical interfaces or other forms of interfaces. Therefore, the modules described as separate components may or may not be separated in terms of logistics, and may be located in one place or distributed to different locations of the same or different devices.

[0130] Figure 6 This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. Figure 6 As shown, the electronic device 600 may include: at least one processor 601 and a memory 602 .

[0131] The memory 602 is used to store programs. Specifically, the programs may include program codes, and the program codes include computer-executable instructions.

[0132] The memory 602 may include a high-speed random access memory (Random Access Memory, RAM), and may also include a non-volatile memory (Non-Volatile Memory), such as at least one disk memory.

[0133] The processor 601 is used to execute the computer execution instructions stored in the memory 602 to implement the unit test case screening method described in the aforementioned method embodiment. Among them, the processor 601 may be a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiment of the present application. Specifically, when implementing the unit test case screening method described in the aforementioned method embodiment, the electronic device can be, for example, an electronic device with processing function such as a server.

[0134] Optionally, the electronic device 600 may further include a communication interface 603. In a specific implementation, if the communication interface 603, the memory 602 and the processor 601 are implemented independently, the communication interface 603, the memory 602 and the processor 601 may be interconnected through a bus and communicate with each other. The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc., but it does not mean that there is only one bus or one type of bus.

[0135] Optionally, in a specific implementation, if the communication interface 603, the memory 602 and the processor 601 are integrated on a chip, the communication interface 603, the memory 602 and the processor 601 can communicate through an internal interface.

[0136] The present application also provides a computer-readable storage medium, which may include: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory, a disk or an optical disk, and other media that can store program codes. Specifically, a computer program is stored in the computer-readable storage medium. When at least one processor of an electronic device executes the computer program, the electronic device executes each step of the unit test case screening method provided in the above-mentioned various implementation methods.

[0137] The embodiment of the present application also provides a computer program product, which includes a computer program, and the computer program is stored in a readable storage medium. At least one processor of the electronic device can read the computer program from the readable storage medium, and at least one processor executes the computer program so that the electronic device implements each step of the unit test case screening method provided by the various embodiments described above.

[0138] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any variations, uses or adaptations of the present application, which follow the general principles of the present application and include common knowledge or customary techniques in the art that are not disclosed in the present application. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.

[0139] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

Claims

1. A unit test case screening method, characterized in that: include: Obtain a target use case group corresponding to a target method, wherein the target use case group includes unit test cases to be screened for the target method, and the unit test cases to be screened are generated based on a unit test case generation rule and test data collected during monitoring business function testing; Determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values; Execute unit testing on the candidate unit test case, and obtain a first code coverage rate corresponding to the candidate unit test case; Execute unit test on the unit test case to be screened, and obtain a second code coverage rate corresponding to the unit test case to be screened; Based on the first code coverage and the second code coverage, a filtered unit test case corresponding to the target method is determined from the unit test cases to be filtered.

2. The unit test case screening method according to claim 1, characterized in that: The determining, based on the first code coverage and the second code coverage, the screened unit test case corresponding to the target method from the unit test cases to be screened includes: If the first code coverage rate is the same as the second code coverage rate, determining the screened unit test case corresponding to the target method from the unit test cases to be screened as the candidate unit test case; If the first code coverage rate and the second code coverage rate are different, based on a preset classification algorithm, a screened unit test case corresponding to the target method is determined from the unit test cases to be screened.

3. The unit test case screening method according to claim 2, characterized in that: The step of determining the screened unit test case corresponding to the target method from the unit test cases to be screened based on a preset classification algorithm includes: Grouping the non-candidate unit test cases in the unit test cases to be screened to obtain a plurality of first test case groups; For each first group of test cases in the multiple first groups of test cases, unit testing is performed on the candidate unit test cases and non-candidate unit test cases in the first group of test cases, and a third code coverage rate corresponding to the first group of test cases is obtained; if the third code coverage rate is different from the first code coverage rate, the non-candidate unit test cases in the first group of test cases are added to the candidate case group containing the candidate unit test cases, and the filtered unit test cases are determined to be unit test cases contained in the candidate case group; if the third code coverage rate is the same as the first code coverage rate, the first group of test cases is deleted.

4. The unit test case screening method according to claim 3, characterized in that: Also includes: If the number of non-candidate unit test cases in the first group of test cases is greater than the number threshold, grouping the non-candidate unit test cases in the first group of test cases to obtain a plurality of second groups of test cases; Based on the second group of test cases and the candidate unit test cases, it is determined whether to add non-candidate unit test cases in the second group of test cases to the candidate test case group.

5. The unit test case screening method according to any one of claims 1 to 4, characterized in that: The unit test cases to be screened include the candidate unit test cases, unit test cases for unit tests to be executed, and other unit test cases; the candidate unit test cases are stored in the candidate workspace; the unit test cases for unit tests to be executed are stored in the execution workspace; and the other unit test cases are stored in the pending workspace.

6. The unit test case screening method according to any one of claims 1 to 4, characterized in that: Before obtaining the target use case group corresponding to the target method, the method further includes: Based on preset condition rules, preprocessing the target test data corresponding to the target method to obtain preprocessed test data, wherein the preprocessing includes sorting and / or screening the target test data; Based on the preprocessed test data and the unit test case generation rule, a unit test case to be screened of the target method is generated.

7. The unit test case screening method according to any one of claims 1 to 4, characterized in that: The test data collected during the monitoring of business function testing includes the target method, the target type to which the target method belongs, the parameter value, return value and exception return information of the target method, as well as the internal type that the target method depends on and the method corresponding to the internal type.

8. The unit test case screening method according to any one of claims 1 to 4, characterized in that: The target use case grouping is obtained by grouping the test data based on the target type to which the target method belongs and the declaration form of the target method.

9. A unit test case screening device, characterized in that: include: An acquisition module is used to acquire a target use case group corresponding to a target method, wherein the target use case group includes unit test cases to be screened of the target method, and the unit test cases to be screened are generated based on unit test case generation rules and test data collected during monitoring business function testing; A first determination module is used to determine candidate unit test cases according to the unit test cases to be screened, wherein parameter values ​​of the candidate unit test cases include extreme values ​​and / or enumeration values; A first processing module, configured to execute a unit test on the candidate unit test case and obtain a first code coverage rate corresponding to the candidate unit test case; A second processing module is used to perform unit testing on the unit test cases to be screened, and obtain a second code coverage rate corresponding to the unit test cases to be screened; A second determination module is used to determine the filtered unit test cases corresponding to the target method from the unit test cases to be filtered based on the first code coverage and the second code coverage.

10. An electronic device comprising: at least one processor and memory; The memory stores computer-executable instructions; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the unit test case screening method according to any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the unit test case screening method according to any one of claims 1 to 8 when executed by a processor.

12. A computer program product, characterized in that It comprises a computer program, which implements the unit test case screening method described in any one of claims 1 to 8 when executed by a processor.

Citation Information

Cited By

  • Method and device for generating test case, electronic equipment and storage medium

    CN120144379A