A program mutation testing method and system based on test case optimization
By constructing type dependency graphs and transitive closures, the target test cases are determined, the mutation testing process is optimized, the problem of low mutation testing efficiency is solved, and mutation testing resource consumption is reduced and efficiency is improved.
Patent Information
- Application Number
- CN202310862152.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-12
- Publication Date
- 2024-09-03
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Mutation testing consumes a lot of computing resources, resulting in low efficiency. There is an urgent need to improve the efficiency of mutation testing.
By obtaining the program bytecode, generating variants, constructing type dependency graphs, calculating transitive closures, determining target test cases, and determining the calling order of test cases based on the type dependency graph, the mutation testing process is optimized.
It reduces the number of test case executions, improves the efficiency of mutation testing, and optimizes the resource consumption of mutation testing.
Smart Images

Figure CN116841899B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of mutation testing optimization, and in particular to a program mutation testing method and system based on test case optimization. Background Art
[0002] Mutation testing is a software testing method based on program source code defects. It can be applied in the fields of test case adequacy assessment, error location, defect simulation, and automatic test case generation. The main process is as follows Figure 1 As shown in the figure, the program under test P is the source code of the project. The mutation operator is a transformation rule that defines how to generate a program (variant) with minimal difference from the original program under the premise of complying with the grammatical rules. For example, the MATH mutation operator can transform mathematical operations, such as converting "+" into "-", such as Figure 2 (a) is the source program under test. After converting a+b into ab, we get Figure 2 (b) is a variant of P`. Figure 1 In the above, the mutation operator is applied to the tested program P to obtain the mutant. Mutation testing is a very effective testing method, which can not only trigger each branch of the tested code, but also verify the status and results of the program. Although mutation testing can be effectively used as an effective evaluation tool for the adequacy of test cases, mutation testing consumes a lot of computing resources. Therefore, there is an urgent need for a mutation testing technology that can improve the efficiency of mutation testing. Summary of the invention
[0003] The purpose of the present invention is to provide a program mutation testing method and system based on test case optimization, which can improve the efficiency of mutation testing.
[0004] To achieve the above object, the present invention provides the following solutions:
[0005] A program mutation testing method based on test case optimization, the method comprising:
[0006] Get the bytecode of the program to be tested;
[0007] generating a variant of the program bytecode;
[0008] Obtain a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships;
[0009] Calculating the transitive closure of the type vertex to which the variant belongs in the type dependency graph;
[0010] Determining a target test case having a dependency relationship with the variant according to the transitive closure;
[0011] Each of the target test cases is called to perform mutation testing on the variant.
[0012] Optionally, after determining the target test case having a dependency relationship with the variant, the method further includes:
[0013] According to the type dependency graph, determine the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case, and obtain the weight corresponding to each target test case;
[0014] The calling order of each target test case is determined according to the size of the corresponding weight.
[0015] Optionally, the calling order of each test case is determined according to the corresponding weights, including:
[0016] The calling order of the corresponding target test cases is determined by sorting the weights from small to large.
[0017] Optionally, determining a target test case having a dependency relationship with the variant according to the transitive closure specifically includes:
[0018] Determine, according to the transitive closure, the vertices traversed by the transitive closure;
[0019] The intersection of the vertices traversed by the transitive closure and the test case vertex set is taken to obtain the target test case having a dependency relationship with the variant.
[0020] Optionally, before obtaining the type dependency graph, the method further includes: constructing the type dependency graph, specifically including:
[0021] Get several types of program bytecodes;
[0022] Determine the constant pool for each type of program bytecode;
[0023] The type dependency graph is constructed according to the type dependency relationship in the constant pool.
[0024] Optionally, determine the constant pool for each type of program bytecode, including:
[0025] Static code analysis tools are used to determine the constant pool for each type of program bytecode.
[0026] The present invention also provides a program variation testing system based on test case optimization, the system comprising:
[0027] A bytecode acquisition module is used to acquire the bytecode of the program to be tested;
[0028] A variant generation module, used for generating a variant of the program bytecode;
[0029] A type dependency graph acquisition module, used to acquire a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships;
[0030] A transitive closure calculation module, used for calculating the transitive closure of the type vertex to which the variant belongs in the type dependency graph;
[0031] A target test case determination module, used to determine a target test case having a dependency relationship with the variant according to the transitive closure;
[0032] The calling module is used to call each of the target test cases to perform mutation testing on the variant.
[0033] Optionally, the system further includes a calling sequence determining module, wherein the calling sequence determining module is used to:
[0034] According to the type dependency graph, determine the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case, and obtain the weight corresponding to each target test case;
[0035] The calling order of each target test case is determined according to the size of the corresponding weight.
[0036] Optionally, the target test case determination module includes:
[0037] A vertex determination unit, used for determining the vertices passed by the transitive closure according to the transitive closure;
[0038] The target test case determination unit is used to take the intersection of the vertices traversed by the transitive closure and the test case vertex set to obtain the target test case having a dependency relationship with the variant.
[0039] Optionally, the system further includes a type dependency graph construction module, wherein the type dependency graph construction module is used to:
[0040] Get several types of program bytecodes;
[0041] Determine the constant pool for each type of program bytecode;
[0042] The type dependency graph is constructed according to the type dependency relationship in the constant pool.
[0043] According to the specific embodiments provided by the present invention, the present invention discloses the following technical effects: the present invention provides a program mutation testing method and system based on test case optimization, the method comprising: obtaining the program bytecode to be tested; generating a variant of the program bytecode; obtaining a type dependency graph; the type dependency graph comprises type vertices and test case vertices, and directed edges between vertices; directed edges are used to connect vertices with direct dependencies; calculating the transitive closure of the type vertex to which the variant belongs in the type dependency graph; determining the target test case that has a dependency relationship with the variant according to the transitive closure; calling each target test case to perform mutation testing on the variant. The present invention determines the target test case that the variant has a dependency relationship through the type dependency graph, so that each time the variant is verified, only the associated test case needs to be executed, thereby improving the efficiency of mutation testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0045] Figure 1 A schematic diagram of a flow chart of an existing variation testing method provided in Example 1 of the present invention;
[0046] Figure 2 A schematic diagram of a mutation operator and a mutant provided in Example 1 of the present invention; Figure 2 (a) is the schematic diagram of the source program under test. Figure 2 (b) Schematic diagram of variant P';
[0047] Figure 3 A schematic diagram of a program variation testing method based on test case optimization provided in Example 1 of the present invention;
[0048] Figure 4 A constant pool of type A program bytecode provided in Embodiment 1 of the present invention;
[0049] Figure 5 A schematic diagram of a type dependency graph provided in Example 1 of the present invention; Figure 5 (a) is a schematic diagram of the type dependency graph; Figure 5 (b) is the transitive closure result based on point A; Figure 5 (c) is a schematic diagram of the reverse direction of the directed edge based on the transitive closure result of point A;
[0050] Figure 6 A program variation test block diagram based on test case optimization provided in Example 2 of the present invention. DETAILED DESCRIPTION
[0051] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0052] The purpose of the present invention is to provide a program mutation testing method and system based on test case optimization, which determines the target test cases with which the mutant has dependencies through a type dependency graph, so that each time the mutant is verified, only the associated test cases need to be executed, thereby reducing the number of execution times of the test cases and improving the mutation testing efficiency; and determines the calling order of the test cases through the weights of directed edges, further improving the mutation testing efficiency.
[0053] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0054] like Figure 3 As shown, the present invention provides a program variation testing method based on test case optimization, the method comprising:
[0055] S1: Get the bytecode of the program to be tested.
[0056] S2: Generate a variant of the program bytecode.
[0057] S3: Obtain a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships.
[0058] S4: Calculate the transitive closure of the type vertex to which the variant belongs in the type dependency graph.
[0059] S5: According to the transitive closure, determine the target test case that has a dependency relationship with the variant.
[0060] S6: Call each of the target test cases to perform mutation testing on the variant.
[0061] First, before obtaining the type dependency graph, this embodiment further includes: constructing the type dependency graph, specifically including:
[0062] Get several types of program bytecode.
[0063] Determine the constant pool for each type of program bytecode.
[0064] The type dependency graph is constructed according to the type dependency relationship in the constant pool.
[0065] Among them, the constant pool of each type of program bytecode is determined, specifically including:
[0066] Static code analysis tools are used to determine the constant pool for each type of program bytecode.
[0067] Specifically: the type dependency graph is obtained by a static code analysis tool. In this embodiment, the Jdep tool is used to parse the constant pool of the Java bytecode file, such as Figure 4 The figure below shows the constant pool of the bytecode of type A program. The gray box indicates the types used by this type. For example, there are five types: A, B, C, D, and E. Therefore, class A points to B, C, D, and E respectively. To obtain the type dependency graph: 1. Use Jdep to analyze the constant pools in all bytecode files. 2. The constant pool stores the type dependency. 3. The dependency is the directed edge of the type dependency graph. Using a mapping set to save all the edges can store the type dependency graph. Figure 5 (a) is the type dependency graph obtained by statically analyzing the project source code. Figure 5 In (a), the vertices are types, including type vertices and test case vertices. The edge weight is 1, indicating a direct call relationship between classes. In the figure, T1 to T5 are test classes (i.e., test cases, T1 to T5 constitute a test case set), and AF represents the tested class (i.e., target class), where mutants can be generated on the target class).
[0068] Then, the transitive closure is calculated based on the type dependency graph. Since indirect test cases may also kill mutants, the indirect test classes need to be found as well, so the transitive closure needs to be calculated. The process of calculating the transitive closure is to continuously add edges to the graph.
[0069] The process of calculating the transitive closure is as follows:
[0070] (1) Initialize an edge set to store newly added edges, which are then used to add edges to the type dependency graph.
[0071] (2) Traverse all vertices in the type dependency graph. For each vertex, start depth-first traversal with each vertex as the source point. Each time a vertex is traversed, add an edge from the source point to the vertex to the edge set, and the edge weight is the depth.
[0072] (3) Finally, add the newly added edges to the type dependency graph.
[0073] Specific as Figure 5As shown in (b), the figure only shows the transitive closure result based on point A, because there is a path from T3 to A. Therefore, an edge is established from T3 to A with a weight of 2. The same is true for T4 to A, and an edge with a weight of 3 is established. There is also a path from F to A with a depth of 2, so an edge from F to A is added with a weight of 2.
[0074] After calculating the transitive closure, reverse the direction of all directed edges in the type dependency graph. The pointing relationship in the type dependency graph is that the caller points to the callee. However, what is actually needed is which test cases the variants point to, that is, which callers the callees point to. Therefore, in order to be logical and convenient for operation, this embodiment reverses the direction of all directed edges in the graph, such as Figure 5 (c) as shown.
[0075] After reversing the direction, use case size reduction and sorting are performed, including:
[0076] According to the transitive closure, a target test case having a dependency relationship with the variant is determined, specifically including:
[0077] The vertices traversed by the transitive closure are determined according to the transitive closure.
[0078] The intersection of the vertices traversed by the transitive closure and the test case vertex set is taken to obtain the target test case having a dependency relationship with the variant.
[0079] Specifically:
[0080] (1) Find the vertex of the type to which the variant belongs. The variant is determined by the variant identifier, which consists of: mutation operator + target class + mutation point. The type of the variant is determined by the variant identifier.
[0081] (2) Traverse all outgoing edges of this type. Outgoing edges can point to either the target class or the test class, such as Figure 5 In (c), A points to C, and A also points to T1.
[0082] (3) Intersect the vertex pointed to by the outgoing edge with the test case vertex set. The resulting intersection is the target test case associated with the variant. T1-T5 refers to test classes. Each test class contains test cases. Test cases are actually the data of the test code, which can simulate scenarios one by one.
[0083] Assume that the variant is generated in class A. In the transitive closure, class A directly points to T1, T3, and T4. Therefore, when verifying the variant later, only the three target test cases T1, T3, and T4 need to be executed, and T2 and T5 do not need to be executed, thus reducing the scale of test cases and achieving test case optimization.
[0084] In this embodiment, after determining the target test case having a dependency relationship with the variant, the method further includes:
[0085] According to the type dependency graph, the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case is determined, and the weight corresponding to each target test case is obtained.
[0086] The calling order of each target test case is determined according to the size of the corresponding weight.
[0087] For example, Figure 5 In (c), the path from A to T4 includes three directed edges, namely A to C, C to F, and F to T4, so the weight of A’s target test case T4 is 3.
[0088] After obtaining each edge, the calling order of each test case is determined according to the corresponding weight, including:
[0089] The calling order of the corresponding target test cases is determined by sorting the weights from small to large.
[0090] Based on the above method, T1 has the smallest edge weight, so T1 is called first, followed by T3 and T4.
[0091] The problem of high resource consumption in mutation testing mainly focuses on two aspects: variant selection optimization and variant execution optimization. Reducing the number of variants includes variant sampling, mutation operator selection, variant clustering, meta-mutants, etc. Reducing the execution cost of variants includes architecture optimization, weak mutation testing, regression test selection mutation testing, machine learning-based mutation testing, runtime optimization, etc.
[0092] The present invention establishes a type dependency graph, in which the vertices are types and the edges are the dependencies between types, and calculates the transitive closure based on the type dependency graph. The transitive closure reflects the direct and indirect dependencies between types, and is used to analyze the association between variants and test cases. Each time a variant is verified, only the associated test case needs to be executed. Due to the existence of the first failure mode, the execution order of the test case will affect the execution time of the variant. Using the path length between the variant and the test case as a metric, a suitable test case calling order is arranged for each variant, thereby improving the execution efficiency of the mutation test. In-depth research in the field of mutation testing can improve the execution performance of mutation testing, which has important theoretical and application value.
[0093] Example 2
[0094] like Figure 6 As shown, this embodiment provides a program variation testing system based on test case optimization, the system comprising:
[0095] The bytecode acquisition module T1 is used to acquire the bytecode of the program to be tested.
[0096] The variant generation module T2 is used to generate variants of the program bytecode.
[0097] The type dependency graph acquisition module T3 is used to acquire a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships.
[0098] The transitive closure calculation module T4 is used to calculate the transitive closure of the type vertex to which the variant belongs in the type dependency graph.
[0099] The target test case determination module T5 is used to determine the target test case having a dependency relationship with the variant according to the transitive closure.
[0100] The calling module T6 is used to call each of the target test cases to perform mutation testing on the variant.
[0101] In this embodiment, the system further includes a calling sequence determining module, and the calling sequence determining module is used to:
[0102] According to the type dependency graph, the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case is determined, and the weight corresponding to each target test case is obtained.
[0103] The calling order of each target test case is determined according to the size of the corresponding weight.
[0104] In this embodiment, the target test case determination module includes:
[0105] The vertex determination unit is used to determine the vertices that the transitive closure passes through according to the transitive closure. The target test case determination unit is used to take the intersection of the vertices that the transitive closure passes through and the test case vertex set to obtain the target test case that has a dependency relationship with the variant.
[0106] In this embodiment, the system further includes a type dependency graph construction module, and the type dependency graph construction module is used to:
[0107] Get several types of program bytecode.
[0108] Determine the constant pool for each type of program bytecode.
[0109] The type dependency graph is constructed according to the type dependency relationship in the constant pool.
[0110] Each embodiment in this specification focuses on the differences from other embodiments, and the same or similar parts between the embodiments can be referred to each other. For the system disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0111] The principles and implementation methods of the present invention are described in this article using specific examples. The description of the above embodiments is only used to help understand the method and core idea of the present invention. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A program mutation testing method based on test case optimization, characterized in that: The method comprises: Get the bytecode of the program to be tested; generating a variant of the program bytecode; Obtain a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships; Before obtaining the type dependency graph, the process also includes: constructing the type dependency graph, including: Get several types of program bytecodes; Determine the constant pool for each type of program bytecode; Constructing the type dependency graph according to the type dependency relationship in the constant pool; The transitive closure of the type vertex to which the variant belongs in the type dependency graph is calculated; the transitive closure reflects the direct and indirect dependency relationship between types and is used to analyze the association between the variant and the test case; the process of calculating the transitive closure is as follows: Initialize an edge set to store newly added edges, which will be used to add edges to the type dependency graph later. Traverse all vertices in the type dependency graph, and for each vertex, start depth-first traversal with each vertex as the source point. Each time a vertex is traversed, add an edge from the source point to the vertex to the edge set, and the edge weight is the depth. Finally, add the newly added edges to the type dependency graph; According to the transitive closure, a target test case having a dependency relationship with the variant is determined, specifically including: Determine, according to the transitive closure, the vertices traversed by the transitive closure; Taking the intersection of the vertices traversed by the transitive closure and the test case vertex set to obtain the target test case having a dependency relationship with the variant; Each of the target test cases is called to perform mutation testing on the variant.
2. The program mutation testing method based on test case optimization according to claim 1 is characterized in that: After determining the target test cases that have dependencies on the variant, it also includes: According to the type dependency graph, determine the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case, and obtain the weight corresponding to each target test case; The calling order of each of the target test cases is determined according to the corresponding weights.
3. The program variation testing method based on test case optimization according to claim 2 is characterized in that: The calling order of each test case is determined according to the corresponding weight, including: The calling order of the corresponding target test cases is determined by sorting the weights from small to large.
4. The program variation testing method based on test case optimization according to claim 1, characterized in that: Determining the constant pool of each type of program bytecode specifically includes: A static code analysis tool is used to determine the constant pool of each type of program bytecode.
5. A program mutation testing system based on test case optimization, characterized in that: The system comprises: A bytecode acquisition module is used to acquire the bytecode of the program to be tested; A variant generation module, used for generating a variant of the program bytecode; The type dependency graph building block is used to: Get several types of program bytecodes; Determine the constant pool for each type of program bytecode; Constructing the type dependency graph according to the type dependency relationship in the constant pool; A type dependency graph acquisition module, used to acquire a type dependency graph; the type dependency graph includes type vertices and test case vertices, and directed edges between vertices; the directed edges are used to connect vertices with direct dependency relationships; The transitive closure calculation module is used to calculate the transitive closure of the type vertex to which the variant belongs in the type dependency graph; the transitive closure reflects the direct and indirect dependency relationship between types and is used to analyze the association between the variant and the test case; the process of calculating the transitive closure is as follows: Initialize an edge set to store newly added edges, which will be used to add edges to the type dependency graph later. Traverse all vertices in the type dependency graph, and for each vertex, start depth-first traversal with each vertex as the source point. Each time a vertex is traversed, add an edge from the source point to the vertex to the edge set, and the edge weight is the depth. Finally, add the newly added edges to the type dependency graph; A target test case determination module is used to determine a target test case having a dependency relationship with the variant according to the transitive closure; the target test case determination module includes: A vertex determination unit, used for determining the vertices passed by the transitive closure according to the transitive closure; A target test case determination unit, used to take the intersection of the vertices traversed by the transitive closure and the test case vertex set to obtain a target test case having a dependency relationship with the variant; The calling module is used to call each of the target test cases to perform mutation testing on the variant.
6. The program variation testing system based on test case optimization according to claim 5, characterized in that: The system further comprises a calling sequence determining module, wherein the calling sequence determining module is used to: According to the type dependency graph, determine the number of directed edges on the path from the type vertex to which the variant belongs to the vertex of each target test case, and obtain the weight corresponding to each target test case; The calling order of each of the target test cases is determined according to the corresponding weights.
