MC / DC test case generation method related to multi-conditional expression and related product
By determining the first and second conditions in a multi-condition expression and setting initial values and preconditions, specific MC/DC test cases are generated, solving the problem of excessive test cases and improving testing efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-02-28
- Publication Date
- 2026-04-14
Smart Images

Figure CN121858460A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of code testing technology, and in particular to a method for generating MC / DC test cases for multi-condition expressions and related products. Background Technology
[0002] In the software code testing and verification phase, coverage testing of decision logic is the core step to ensure the correctness of code functionality and avoid logical vulnerabilities. Decision scenarios involving multiple conditions are high-frequency and complex scenarios in code testing.
[0003] In related technologies, for decision logic containing n conditions, if a full combination result test is adopted, 2^n test cases need to be designed to cover the decision results of all condition combinations. Although this full-scale testing mode can achieve complete coverage of the decision logic, the number of test cases will increase exponentially with the increase in the number of decision conditions, significantly increasing the workload and time cost of test execution, significantly increasing the testing burden, and reducing the overall testing efficiency. Summary of the Invention
[0004] To address the aforementioned issues, this application provides a method and related products for generating MC / DC test cases based on multi-condition expressions, thereby reducing the workload and time cost of test execution and improving overall test efficiency.
[0005] The embodiments of this application disclose the following technical solutions: In a first aspect, embodiments of this application provide a method for generating MC / DC test cases based on multi-condition expressions, the method comprising: From the n conditions in a multi-condition expression, determine the first condition and n-1 second conditions other than the first condition; where n is an integer greater than or equal to 2. For any second condition, determine the initial value and the precondition for the second condition; wherein, the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the precondition is other second conditions or first conditions that affect the overall result of the multi-condition expression. For the first condition, two MC / DC test cases are determined based on the value of the first condition and the operators corresponding to each second condition. For any second condition, one MC / DC test case is determined based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions.
[0006] In one possible implementation, based on the value of the first condition and the operators corresponding to each of the second conditions, two MC / DC test cases are determined regarding the first condition, including: Determine the operator corresponding to each second condition; For any second condition, the value of the second condition is determined according to the corresponding operator; wherein, the operator includes the AND operator, and the value of the second condition is TRUE; the operator includes the OR operator, and the value of the second condition is FALSE. If the values of the second conditions are determined, and the first condition is TRUE, then one MC / DC test case is determined for the first condition; if the first condition is FALSE, then another MC / DC test case is determined for the first condition.
[0007] In one possible implementation, for any second condition, based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions, a Monte Carlo (MC) / DC test case is determined for the second condition, including: For any second condition, let the value of the second condition be the inverted value of the initial value of the second condition, the value of the precondition be the initial value of the second condition, and the values of the other second conditions be the corresponding initial values, thus determining an MC / DC test case for the second condition.
[0008] In one possible implementation, from the n conditions in a multi-condition expression, a first condition and n-1 second conditions other than the first condition are determined, including: For multi-condition expressions, a syntax tree is created; the syntax tree includes n condition leaf nodes and n-1 operator branch nodes. The left-side condition leaf node of the left subtree is used as the first condition, and the right-side condition leaf node of the left subtree and the condition leaf node of the right subtree are used as the second condition.
[0009] Secondly, embodiments of this application provide an MC / DC test case generation device for multi-condition expressions, the device comprising: a first determining module, a second determining module, and a third determining module; The first determining module is configured to determine a first condition and n-1 second conditions other than the first condition from n conditions in a multi-condition expression; where n is an integer greater than or equal to 2. The second determining module is configured to determine an initial value and a precondition for any second condition; wherein the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the precondition is another second condition or a first condition that affects the overall result of the multi-condition expression. The third determining module is configured to, for the first condition, determine two MC / DC test cases for the first condition based on the value of the first condition and the operators corresponding to each second condition; and for any second condition, determine one MC / DC test case for the second condition based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions.
[0010] In one possible implementation, the third determining module is configured to determine the operator corresponding to each second condition; for any second condition, determine the value of the second condition according to the corresponding operator; wherein the operator includes the AND operator, and the value of the second condition is TRUE, the operator includes the OR operator, and the value of the second condition is FALSE; when the values of each second condition are determined, if the first condition is TRUE, determine one MC / DC test case for that condition; if the first condition is FALSE, determine another MC / DC test case for the first condition.
[0011] In one possible implementation, the third determining module is configured to, for any second condition, set the value of the second condition to the inverted value of the initial value of the second condition, set the value of the precondition to the initial value of the second condition, and set the values of the other second conditions to their respective initial values, thereby determining an MC / DC test case for the second condition.
[0012] In one possible implementation, the first determining module is configured to create a syntax tree for a multi-condition expression; wherein the syntax tree includes n condition leaf nodes and n-1 operator branch nodes; the left condition leaf node of the left subtree is taken as the first condition, and the right condition leaf node of the left subtree and the condition leaf node of the right subtree are taken as the second condition.
[0013] Thirdly, embodiments of this application provide a computer device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the MC / DC test case generation method for multi-condition expressions as described in any embodiment of the first aspect.
[0014] Fourthly, embodiments of this application provide a computer-readable storage medium storing instructions that, when executed on a terminal device, cause the terminal device to perform the MC / DC test case generation method for multi-condition expressions as described in any embodiment of the first aspect.
[0015] To reduce the workload and time cost of test execution and improve overall testing efficiency, this application provides a method for generating MC / DC test cases based on multi-condition expressions. The method includes: determining a first condition and n-1 second conditions (excluding the first condition) from n conditions in a multi-condition expression; where n is an integer greater than or equal to 2; determining an initial value and a precondition for any second condition; where the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the preconditions are other second conditions or the first condition that do affect the overall result of the multi-condition expression; determining two MC / DC test cases for the first condition based on its value and the operators corresponding to each second condition; and determining one MC / DC test case for any second condition based on its initial value, the preconditions, and the initial values of other second conditions. In this application embodiment, two test cases are generated for the first condition, and n-1 test cases are generated for the n-1 second conditions, for a total of n+1 test cases, reducing the workload and time cost of test execution and improving overall testing efficiency. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 A flowchart illustrating a method for generating MC / DC test cases using multi-condition expressions, provided in an embodiment of this application; Figure 2 A schematic diagram of a syntax tree corresponding to a multi-condition expression provided in an embodiment of this application; Figure 3 This is a schematic diagram of an MC / DC test case generation device for multi-condition expressions provided in an embodiment of this application. Detailed Implementation
[0018] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present application.
[0019] The terms "first" and "second," etc., used in the specification and claims of this application are used to distinguish different objects, not to describe a specific order of objects. For example, "first operation instruction" and "second operation instruction," etc., are used to distinguish different operation instructions, not to describe a specific order of operation instructions.
[0020] In the embodiments of this application, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design that is described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design. Specifically, the use of the terms "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.
[0021] In the description of the embodiments of this application, unless otherwise stated, "multiple" means two or more, for example, multiple processing units means two or more processing units, multiple elements means two or more elements, etc.
[0022] The technical solution of this application will be described below with reference to the accompanying drawings.
[0023] See Figure 1 The figure is a flowchart of a method for generating MC / DC test cases using multi-condition expressions provided in an embodiment of this application.
[0024] like Figure 1 As shown, the method includes the following steps: S1000: From n conditions in a multi-condition expression, determine the first condition and n-1 second conditions other than the first condition; where n is an integer greater than or equal to 2.
[0025] It should be understood that the specific content of the multi-condition expression is not specifically limited in the embodiments of this application. However, in order to facilitate the understanding of the technical solution of this application, the multi-condition expression A||B&&C will be used as an example for explanation and introduction in the subsequent embodiments.
[0026] In one possible implementation, a syntax tree is created for a multi-condition expression; wherein the syntax tree includes n condition leaf nodes and n-1 operator branch nodes; the left condition leaf node of the left subtree is taken as the first condition, and the right condition leaf node of the left subtree and the condition leaf node of the right subtree are taken as the second condition.
[0027] For example, for a multi-condition expression A||B&&C, create as follows Figure 2 The syntax tree shown is as follows. The root node is "&&", the left subtree is A||B, and condition A is the first condition, while conditions B and C are the second conditions.
[0028] S2000: For any second condition, determine the initial value and the precondition for the second condition; wherein the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the precondition is another second condition or first condition that affects the overall result of the multi-condition expression.
[0029] The second condition (condition B) corresponds to the operator "||". To ensure that the second condition (condition B) does not affect the overall result of the multi-condition expression, the initial value of the second condition (condition B) is FALSE. The second condition (condition C) corresponds to the operator "&&". To ensure that the second condition (condition C) does not affect the overall result of the multi-condition expression, the initial value of the second condition (condition C) is TRUE.
[0030] The following will introduce two possible implementation methods for determining the initial value and preconditions for the second condition: In one possible implementation, if a first condition or another second condition exists that corresponds to the same operator as the second condition, the first condition or the other second condition is used as a precondition for the second condition. For example, in the case of condition B, condition A is used as a precondition for condition B.
[0031] In another possible implementation, if there is no first or second condition with the same operator as the second condition, other second or first conditions that affect the overall result of the multi-condition expression are used as preconditions. For example, in condition C, condition A is used as a precondition of condition C.
[0032] S3000: For the first condition, determine two MC / DC test cases related to the first condition based on the value of the first condition and the operators corresponding to each second condition; for any second condition, determine one MC / DC test case related to the second condition based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions.
[0033] For the first condition (condition A), the method for determining the MC / DC test cases that can independently affect the overall result of the multi-condition expression is as follows: Determine the operator corresponding to each second condition; for any second condition, determine the value of the second condition based on the corresponding operator; wherein, the operator includes the AND operator and the value of the second condition is TRUE, the operator includes the OR operator and the value of the second condition is FALSE; when the values of each second condition are determined, if the first condition is TRUE, determine one MC / DC test case for the first condition; if the first condition is FALSE, determine another MC / DC test case for the first condition.
[0034] For the left subtree to independently affect the result of a multi-condition expression, the value of the second condition (condition C) must be TRUE. Similarly, for A||B in the left subtree, if the first condition (condition AA) is to independently affect the result, the value of the second condition (condition B) must be FALSE. Thus, the MC / DC test cases where the two first conditions (condition A) can independently affect the overall result of the multi-condition expression are shown in Table 1 below: Table 1
[0035] For any second condition, let the value of the second condition be the inverted value of the initial value of the second condition, the value of the precondition be the initial value of the second condition, and the values of the other second conditions be the corresponding initial values, thus determining an MC / DC test case for the second condition.
[0036] For example, the corresponding MC / DC test cases for the second condition (condition B) are shown in Table 2 below: Table 2
[0037] For example, the corresponding MC / DC test cases for the second condition (condition C) are shown in Table 3 below: Table 3
[0038] In this embodiment of the application, two test cases are generated for the first condition, and n-1 test cases are generated for n-1 second conditions, for a total of n+1 test cases are generated, which reduces the workload and time cost of test execution and improves the overall test efficiency.
[0039] Based on the MC / DC test case generation method with multi-condition expressions described in the foregoing embodiments, this application also provides an MC / DC test case generation device with multi-condition expressions.
[0040] See Figure 3 The figure shows a multi-condition expression MC / DC test case generation device provided in an embodiment of this application.
[0041] like Figure 3 As shown, the MC / DC test case generation device with multi-condition expressions includes: a first determining module 1000, a second determining module 2000, and a third determining module 3000.
[0042] The first determining module 1000 is configured to determine a first condition and n-1 second conditions other than the first condition from n conditions in a multi-condition expression; where n is an integer greater than or equal to 2. The second determining module 2000 is configured to determine an initial value and a precondition for any second condition; wherein the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the precondition is another second condition or a first condition that affects the overall result of the multi-condition expression. The third determining module 3000 is configured to, for the first condition, determine two MC / DC test cases for the first condition based on the value of the first condition and the operators corresponding to each second condition; and for any second condition, determine one MC / DC test case for the second condition based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions.
[0043] In this embodiment of the application, two test cases are generated for the first condition, and n-1 test cases are generated for n-1 second conditions, for a total of n+1 test cases are generated, which reduces the workload and time cost of test execution and improves the overall test efficiency.
[0044] In one possible implementation, the third determining module 3000 is configured to determine the operator corresponding to each second condition; for any second condition, determine the value of the second condition according to the corresponding operator; wherein the operator includes the AND operator, and the value of the second condition is TRUE, the operator includes the OR operator, and the value of the second condition is FALSE; when the values of each second condition are determined, if the first condition is TRUE, determine one MC / DC test case for the first condition; if the first condition is FALSE, determine another MC / DC test case for the first condition.
[0045] In one possible implementation, the third determining module 3000 is configured to, for any second condition, set the value of the second condition to the inverted value of the initial value of the second condition, set the value of the precondition to the initial value of the second condition, and set the values of the other second conditions to their respective initial values, thereby determining an MC / DC test case for the second condition.
[0046] In one possible implementation, the first determining module 1000 is configured to create a syntax tree for a multi-condition expression; wherein the syntax tree includes n condition leaf nodes and n-1 operator branch nodes; the left condition leaf node of the left subtree is used as the first condition, and the right condition leaf node of the left subtree and the condition leaf node of the right subtree are used as the second condition.
[0047] In addition, embodiments of this application provide a computer device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the MC / DC test case generation method for multi-condition expressions as described in any of the foregoing embodiments.
[0048] In addition, embodiments of this application provide a computer-readable storage medium storing instructions that, when executed on a terminal device, cause the terminal device to perform the MC / DC test case generation method for multi-condition expressions as described in any of the foregoing embodiments.
[0049] It should be noted that the various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, for the device and system embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the description of the method embodiments. The device and system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components indicated as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of the solution in this embodiment according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0050] The above description is merely one specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for generating MC / DC test cases based on multi-condition expressions, characterized in that, The method includes: From n conditions in a multi-condition expression, determine the first condition and n-1 second conditions other than the first condition; where n is an integer greater than or equal to 2; For any of the second conditions, determine an initial value and a prerequisite condition for the second condition; wherein the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the prerequisite condition is another second condition or the first condition that affects the overall result of the multi-condition expression. For the first condition, two MC / DC test cases are determined based on the value of the first condition and the operators corresponding to each of the second conditions; for any second condition, one MC / DC test case is determined based on the initial value of the second condition, the precondition of the second condition, and the initial values of other second conditions.
2. The method according to claim 1, characterized in that, The step of determining two MC / DC test cases related to the first condition based on the value of the first condition and the operators corresponding to each of the second conditions includes: Determine the operator corresponding to each of the second conditions; For any of the second conditions, the value of the second condition is determined according to the corresponding operator; wherein the operator includes the AND operator, and the value of the second condition is TRUE; the operator includes the OR operator, and the value of the second condition is FALSE. If the values of the second conditions are determined, and the first condition is TRUE, an MC / DC test case is determined for the first condition; if the first condition is FALSE, another MC / DC test case is determined for the first condition.
3. The method according to claim 1, characterized in that, For any of the second conditions, a test case for MC / DC is determined based on the initial value of the second condition, the preconditions of the second condition, and the initial values of other second conditions, including: For any one of the second conditions, let the value of the second condition be the inverted value of the initial value of the second condition, the value of the precondition be the initial value of the second condition, and the values of the other second conditions be the corresponding initial values, and determine an MC / DC test case for the second condition.
4. The method according to any one of claims 1-3, characterized in that, The step of determining the first condition and n-1 second conditions (excluding the first condition) from n conditions in a multi-condition expression includes: For the multi-condition expression, a syntax tree is created; wherein the syntax tree includes n condition leaf nodes and n-1 operator branch nodes; The left condition leaf node of the left subtree is used as the first condition, and the right condition leaf node of the left subtree and the condition leaf node of the right subtree are used as the second condition.
5. A test case generation device for MC / DC with multi-condition expressions, characterized in that, The device includes: a first determining module, a second determining module, and a third determining module; The first determining module is configured to determine a first condition and n-1 second conditions other than the first condition from n conditions in a multi-condition expression; where n is an integer greater than or equal to 2. The second determining module is configured to determine an initial value and a prerequisite for any second condition; wherein the initial value is the value of the second condition that does not affect the overall result of the multi-condition expression, and the prerequisite is another second condition or the first condition that affects the overall result of the multi-condition expression. The third determining module is configured to, for the first condition, determine two MC / DC test cases related to the first condition based on the value of the first condition and the operators corresponding to each of the second conditions; and for any second condition, determine one MC / DC test case related to the second condition based on the initial value of the second condition, the precondition of the second condition, and the initial values of other second conditions.
6. The apparatus according to claim 5, characterized in that, The third determining module is configured to determine the operator corresponding to each of the second conditions; for any second condition, determine the value of the second condition according to the corresponding operator; wherein the operator includes an AND operator, and the value of the second condition is TRUE; the operator includes an OR operator, and the value of the second condition is FALSE; when the values of each of the second conditions are determined, if the first condition is TRUE, determine one MC / DC test case for the first condition; if the first condition is FALSE, determine another MC / DC test case for the first condition.
7. The apparatus according to claim 5, characterized in that, The third determining module is configured to, for any one of the second conditions, set the value of the second condition to the inverted value of the initial value of the second condition, set the value of the precondition to the initial value of the second condition, and set the values of the other second conditions to the corresponding initial values, thereby determining an MC / DC test case for the second condition.
8. The apparatus according to any one of claims 5-7, characterized in that, The first determining module is configured to create a syntax tree for the multi-condition expression; wherein the syntax tree includes n condition leaf nodes and n-1 operator branch nodes; the left condition leaf node of the left subtree is used as the first condition, and the right condition leaf node of the left subtree and the condition leaf node of the right subtree are used as the second condition.
9. A computer device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the MC / DC test case generation method for multi-condition expressions as described in any one of claims 1-4.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores instructions that, when executed on a terminal device, cause the terminal device to perform the MC / DC test case generation method for multi-condition expressions as described in any one of claims 1-4.