Unit test generation methods, systems, and storage media for MC / DC coverage

By identifying atomic conditions through static analysis and instrumentation, a clear prompt word input large language model is constructed. Combined with a three-level feedback mechanism, test cases that meet MC/DC coverage are generated, which solves the problem that existing technologies cannot effectively construct independent influence pairs and improves test sufficiency and generation efficiency.

CN123068291APending Publication Date: 2026-11-10CHINA AEROSPACE TIMES ELECTRONICS CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611305701.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-26
Publication Date
2026-11-10

AI Technical Summary

Technical Problem

Existing unit test generation methods based on large language models cannot effectively construct test cases that meet MC/DC coverage, cannot guarantee the independent influence of each atomic condition on the decision result in complex Boolean expressions, and face path explosion and performance bottlenecks in complex expressions and large-scale systems.

Method used

By identifying atomic conditions in decision expressions through static analysis, instrumentation is used to collect coverage information, missing independent influence pairs are identified, a clear prompt word input large language model is constructed, candidate test cases are generated, and iterative optimization is performed through a three-level feedback mechanism. Combined with a branch coverage-oriented strategy, the MC/DC coverage rate is improved.

Benefits of technology

It significantly improves the sufficiency of testing for complex condition judgments, increases MC/DC coverage, reduces generation costs, improves generation efficiency and success rate, and enables the detection of potential defects in complex logic judgments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123068291A_ABST
    Figure CN123068291A_ABST
Patent Text Reader

Abstract

This invention belongs to the fields of software testing and artificial intelligence, and discloses a unit test generation method, system, and storage medium for MC / DC coverage. The method includes: performing static analysis on the objective function to identify decision expressions containing multi-atomic conditions; instrumenting existing test cases, collecting observations and decision results for each atomic condition; calculating MC / DC coverage based on the collected information and identifying target atomic conditions with missing independent influence pairs; constructing a large language model with prompt words containing missing information to generate candidate test cases; executing candidate test cases and classifying them: adding independent influence pairs is considered a core improvement, adding only line / branch coverage is considered semi-optimization, and no increment or failure is considered invalid; updating prompt words differentially based on the classification results, iterating until termination. This invention integrates the construction of independent influence pairs throughout the entire process, guiding the large language model to finely construct test cases through a three-level feedback mechanism, significantly improving MC / DC coverage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the interdisciplinary field of software engineering and artificial intelligence, and specifically relates to a unit test generation method, system and storage medium for MC / DC (Modified Condition / Decision Coverage, MC / DC) coverage. Background Technology

[0002] As software systems become increasingly complex, unit testing, as a fundamental means of ensuring code quality, is becoming increasingly important. Traditional methods of manually writing unit tests are time-consuming and labor-intensive, and struggle to cover complex boundary conditions. In recent years, large language models, such as GPT and CodeLlama, have demonstrated powerful capabilities in code understanding and generation tasks, making automated unit test generation technology based on large language models a research hotspot.

[0003] Existing unit test generation methods based on large language models have the following main shortcomings: First, existing methods generally use line coverage or branch coverage as the main optimization objective, and the generation strategy focuses on making the program execute to uncovered statements or branches. However, they cannot guarantee that the independent influence of each atomic condition on the decision result in complex Boolean expressions can be verified. Line coverage only requires each statement to be executed at least once, and branch coverage only requires each true / false branch of a decision to be executed at least once. Neither of these criteria can detect defects caused by errors in the logical relationships between conditions. For example, if the decision expression in the program is mistakenly written as A || B when the correct expression should be A && B, line coverage and branch coverage may not be able to detect this error, but MC / DC can effectively detect such logical defects by verifying the independent influence of each atomic condition.

[0004] Secondly, existing technologies are mostly geared towards high-level languages ​​such as Java, making them difficult to directly apply to C language security-critical software widely used in fields such as aerospace, rail transportation, and automotive electronics.

[0005] Modified Condition / Decision Coverage (MC / DC) is a mandatory coverage criterion in safety-critical software standards such as DO-178C. MC / DC requires that each atomic condition independently affects the decision result; that is, for each atomic condition in a decision expression, there must exist a pair of test cases such that only the value of that atomic condition changes, while the other atomic conditions remain unchanged (or are masked due to short-circuit semantics), and the decision result changes accordingly. Compared to line coverage and branch coverage, MC / DC can more effectively detect logical errors in complex condition decisions. However, constructing test case pairs that meet the MC / DC requirements (i.e., independent influence pairs) requires a deep understanding of the causal relationship between atomic conditions and decision results, which is significantly challenging for large language models. Existing symbolic execution-based MC / DC test generation methods are prone to path explosion problems and low generation efficiency in scenarios such as complex expressions, pointer dereferencing, and array access. Search-based methods face performance limitations in large-scale systems.

[0006] Through research, the inventors have discovered a previously unrecognized technical challenge in existing technologies: large language models suffer from a systematic difficulty in understanding the causal relationships between atomic conditions and decision results in complex Boolean expressions. While their generated test cases often reach the target decision expression (satisfying line coverage or branch coverage), they fail to construct independent influence pairs that satisfy the condition of "flipping only a single atomic condition while keeping the rest unchanged," resulting in MC / DC coverage being far below expectations. The root cause of this difficulty lies in the fact that existing methods input "improving MC / DC coverage" as a holistic objective into the model, lacking granular guidance on "how to construct independent influence pairs." This leads to the model's inability to accurately understand the causal relationships between atomic conditions and decision results, resulting in a predicament of repeatedly exploring reachable paths without focusing on the fine-grained construction of atomic condition levels. This problem has not been identified or resolved in existing literature and patents, constituting the core of the technical challenge that this invention aims to solve.

[0007] Therefore, there is an urgent need for a large model unit test generation method oriented towards MC / DC coverage criteria, which takes the construction of independent influence pairs as the core guide of the generation process, and guides the large language model to iteratively generate test cases with high MC / DC coverage through a refined feedback mechanism. Summary of the Invention

[0008] The technical problem this invention aims to solve is that existing unit test generation methods based on large language models, which optimize for line coverage or branch coverage, cannot guarantee the verification of the independent influence relationships between atomic conditions and decision results in complex Boolean expressions, leading to insufficient test sufficiency. Furthermore, large language models struggle to understand the causal relationships between atomic conditions and decision results required by the MC / DC criterion, resulting in directly generated test cases that cannot effectively construct independent influence pairs, leading to low MC / DC coverage. In addition, existing symbolic execution and search methods face path explosion and performance bottlenecks in complex expressions and large-scale system scenarios, making it difficult to efficiently generate test cases that satisfy MC / DC.

[0009] In view of the shortcomings of the prior art, the present invention provides a method, system and storage medium for generating large model unit tests for MC / DC coverage, aiming to integrate the construction of MC / DC independent influence pairs into the entire process of static analysis, dynamic execution and feedback iteration, thereby improving the sufficiency of tests under complex conditions.

[0010] To achieve the above objectives, the present invention adopts the following technical solution: On one hand, the present invention provides a unit test generation method for MC / DC coverage, comprising the following steps: S1: Obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expression that contains two or more atomic conditions, and extract each atomic condition and its logical structure relationship.

[0011] Specifically, the source code of the objective function is traversed using an abstract syntax tree to locate the decision statements in the control structure. The control structure includes if statements, while statements, do-while statements, and for statements. The Boolean expressions in the decision statements are used as decision expressions. These decision expressions are recursively decomposed to extract atomic conditions, and the source code line number, number of atomic conditions, condition text, and expression tree structure of each decision expression are recorded. An atomic condition is a Boolean expression that does not contain logical operators and consists only of relational operators (such as <, >, ==, etc.). A decision expression consists of atomic conditions and zero or more logical operators (such as &&, ||, !, etc.).

[0012] S2: Based on the decision expression and the atomic conditions, instrument the objective function; execute the test cases in the existing test case set to run the instrumented objective function, and collect the observation values ​​of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics.

[0013] Specifically, a decision number is created before each decision is executed. The value (true / false) of each atomic condition is recorded during evaluation. The final decision result (true / false) is recorded after the decision expression is evaluated. For decision expressions containing short-circuit logic (e.g., B is not evaluated when A is false in A && B), pointer dereferencing, array access, or function calls, the original evaluation order of the objective function is maintained to avoid altering program behavior due to instrumentation. Short-circuit semantics refers to the language feature where the remaining atomic conditions are no longer evaluated when the final result of a decision expression can be determined by the evaluated parts; for example, for A && B, if A is false, B is not evaluated; for A || B, if A is true, B is not evaluated.

[0014] S3: Based on the information collected in S2, calculate the MC / DC coverage of the current test case set and identify the target atomic conditions of missing independent influence pairs; the independent influence pair refers to two test executions for the same atomic condition, where the atomic condition has different values ​​and different judgment results, and the other atomic conditions have the same values ​​or are short-circuited and masked.

[0015] Specifically, each decision execution is represented as an event, which includes the decision location, the set of atomic condition observations, the final decision result, and information on unevaluated conditions. For multiple execution events of the same decision expression, for each atomic condition, a search is conducted from the existing test case set to determine if there exists a pair of test cases that satisfy MC / DC coverage—that is, there are two executions where the atomic condition takes the values ​​true and false, the final decision results are true and false, and the remaining atomic conditions take the same value in both executions (or are considered equivalent because they are not evaluated due to short-circuiting). If no such pair exists, the atomic condition is determined as the target atomic condition for the missing independent influence pair.

[0016] For example, for the decision expression `if (A && B)`, if the current test case set contains: execution 1: `(A=true, B=true) → result true`; execution 2: `(A=false, B=true) → result false`, then the independent effects of atomic condition A are covered. If for atomic condition B, only the above two sets of executions exist, but there is no corresponding execution for `(A=true, B=false)`, then the independent effects of B are missing.

[0017] S4: Construct prompt words containing the missing independent impact pair information, input them into the large language model, and generate candidate test cases.

[0018] Specifically, the prompt explicitly injects summary information of the coverage gap that independently affects the missing data. This summary information includes: the line number of the decision expression, the text of the decision expression, the sequence number and text of the missing atomic condition, whether the atomic condition has been observed to be true, whether it has been observed to be false, whether there is a short-circuit failure, and the current reason for the missing data. The prompt explicitly requires the large language model to construct a pair of test cases, so that both test cases execute the same decision expression and only change the value of the target atomic condition, while keeping the other atomic conditions unchanged or short-circuited.

[0019] For example, the prompt could be designed as follows: "The current test suite lacks an independent effect pair for the atomic condition y<10 in the decision expression if (x>0 && y<10). An existing test case (x=5, y=5) executes to this decision, where the condition x>0 is true and y<10 is true, resulting in a true decision. Please generate a new, similar test case that keeps x>0 true, changes y<10 to false, and adjusts the decision accordingly. Ensure that the new test case actually executes the decision expression." S5: Execute the candidate test cases and collect coverage information. Based on the coverage increment, make a graded judgment: if at least one independent impact pair is added, it is marked as a core improvement and included in the test suite; if only row coverage or branch coverage is added but no independent impact pair is added, it is marked as semi-optimization; if there is no effective coverage increment or the execution fails, it is marked as invalid.

[0020] S6: Update the prompt words based on the classification judgment result of S5, and return to S4 to iterate until the termination condition is met; among them, the candidate use cases in the semi-optimized state feedback that the objective function is reachable and indicate to maintain reachability to flip the target atomic condition; invalid candidate use cases reduce their reference weight and indicate to change the input direction.

[0021] Specifically, if a candidate test case generated in the previous round is marked as semi-optimized, the prompt in subsequent iterations will indicate that the objective function is reachable, and that reachability should be maintained by constructing a pair of similar inputs to flip the missing atomic conditions, thus avoiding resolving the reachability problem. If a candidate test case generated in the previous round is marked as invalid, the prompt in subsequent iterations will reduce the reference weight of that candidate test case and change the input design direction. The termination condition is any of the following: MC / DC coverage reaches a preset threshold (e.g., 100%), the maximum number of iterations is reached, or no core improvements are generated for a consecutive preset number of rounds.

[0022] Preferably, the present invention further includes a dual-strategy fusion step: fusing the branch coverage-oriented strategy and the MC / DC coverage-oriented strategy to generate two sets of test cases respectively, and taking the union of the two sets as the final test suite. The branch coverage-oriented strategy focuses on exploring different execution paths of the program, while the MC / DC coverage-oriented strategy focuses on constructing independent influence pairs. The two strategies are complementary, and a more stable improvement in MC / DC coverage can be obtained through the union of the test suites.

[0023] On the other hand, the present invention also provides a unit test generation system for MC / DC coverage, comprising: The static analysis module is used to obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expression containing two or more atomic conditions, and extract each atomic condition and its logical structure relationship. The instrumentation acquisition module is used to instrument the objective function based on the decision expression and the atomic conditions, execute test cases in the existing test case set to run the instrumented objective function, and collect the observation values ​​of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics. The coverage calculation module is used to calculate the MC / DC coverage of the current test case set based on the information collected by the instrumentation acquisition module, and to identify the target atomic conditions of missing independent influence pairs. The prompt word construction module is used to construct prompt words containing the missing independent influence pair information, input them into the large language model, and generate candidate test cases; The grading and determination module is used to execute the candidate test cases and collect coverage information, and to make grading and determination based on the coverage increment: if a new independent impact pair is added, it is marked as a core improvement and included in the test suite; if only line coverage or branch coverage is added, it is marked as semi-optimization; if there is no effective increment or the execution fails, it is marked as invalid. The iterative feedback module is used to update the prompt words according to the judgment result of the hierarchical judgment module, and trigger the prompt word construction module to iteratively generate candidate test cases until the termination condition is met; wherein, the candidate test cases in the semi-optimized state provide feedback that the objective function is reachable and indicate that reachability should be maintained to flip the target atomic condition; invalid candidate test cases reduce their reference weight and indicate that the input direction should be changed.

[0024] On the other hand, the present invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described method.

[0025] Compared with the prior art, the beneficial effects of the present invention are as follows: (1) The construction of MC / DC independent influence pairs is taken as the core guideline of the generation process, rather than just taking MC / DC coverage as the final statistical indicator. This refines the generation goal of the large language model from "covering branches" to "constructing conditionally independent influence pairs", which significantly improves the sufficiency of testing under complex condition judgments. Existing LLM-based test generation methods only focus on line coverage or branch coverage, which cannot guarantee that the atomic conditionally independent influence relationship is verified. This invention can effectively detect potential defects in complex logic judgments by constructing independent influence pairs throughout the entire process.

[0026] (2) A three-level feedback mechanism of "core improvement - semi-optimization - invalidation" was designed to clearly distinguish between "only improving row / branch coverage" and "truly adding independent impact pairs", avoiding repeated path exploration by the model when reachability has been solved, and guiding it to focus on the fine construction of atomic condition level. General LLM test generation methods usually adopt "success / failure" binary feedback or simple retry strategy, which cannot distinguish the type of coverage increment, resulting in low generation efficiency; the three-level feedback mechanism of this invention significantly improves the iterative generation efficiency by accurately distinguishing the type of coverage increment.

[0027] (3) By using context-enhanced prompts for missing independent influence pairs, the abstract coverage gap is transformed into a specific input construction task ("flipping specific atomic conditions while keeping the rest unchanged"), which reduces the understanding cost of large language models and improves the generation success rate.

[0028] (4) A dual-strategy fusion mechanism of branch-oriented and MC / DC-oriented approaches is proposed. By utilizing the complementarity of the two strategies, a more stable MC / DC coverage improvement can be obtained through the union of test suites.

[0029] Furthermore, there are two main technical approaches in the existing technology: one is the constraint-solving scheme represented by symbolic execution, which generates test cases that satisfy MC / DC through formal logical reasoning. This approach relies on precise path conditions and constraint solvers and does not require an iterative feedback mechanism, but it faces the path explosion problem in scenarios such as complex expressions, pointer dereferencing, and array access. The other approach is the scheme represented by directly generating test cases from a large language model, which takes MC / DC coverage as a one-time generation target, relies on the statistical generation capability of the model, and lacks dynamic verification and fine-grained feedback on the generated results. The above two approaches are fundamentally different in terms of technical principles and solutions: the former adopts a symbolic reasoning path, while the latter adopts a statistical generation path. Even if the two are simply combined, the technical solution of this invention cannot be obtained naturally. The reason is that the core of this invention lies in "dynamically identifying missing pairs based on instrumentation-level coverage information of atomic conditions → combining differential chemical card feedback → iteratively focusing on the construction of independent influence pairs," which requires a deep understanding of the behavioral characteristics of LLM (i.e., its systematic defects in understanding the causal relationship between atomic conditions and decision results). The discovery of this technical problem and the design of a targeted solution are not obvious in this field. Attached Figure Description

[0030] Figure 1 This is a flowchart illustrating a unit test generation method for MC / DC coverage according to an embodiment of the present invention. Detailed Implementation

[0031] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to specific embodiments and the accompanying drawings. It should be understood that these descriptions are merely exemplary and not intended to limit the scope of the invention. Furthermore, descriptions of well-known structures and techniques are omitted in the following description to avoid unnecessarily obscuring the concept of the invention.

[0032] The accompanying drawings illustrate a layer structure according to an embodiment of the present invention. These drawings are not to scale, and some details have been enlarged for clarity, and some details may have been omitted. The shapes of the various regions and layers shown in the drawings, as well as their relative sizes and positional relationships, are merely exemplary and may deviate from reality due to manufacturing tolerances or technical limitations. Furthermore, those skilled in the art can design regions / layers with different shapes, sizes, and relative positions as needed.

[0033] Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without inventive effort are within the scope of protection of the present invention.

[0034] In the description of this invention, it should be noted that the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.

[0035] Furthermore, the technical features involved in the different embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.

[0036] Example 1

[0037] like Figure 1 As shown, the present invention provides a unit test generation method for MC / DC coverage, which specifically includes the following steps: Step S1: Static Analysis Obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expressions that contain two or more atomic conditions, and extract each atomic condition and its logical structure relationship.

[0038] Specifically, the source code of the target function is traversed using a syntax tree to locate the decision statements in the control structure; the control structure includes if statements, while statements, do-while statements, and for statements; the Boolean expressions in the decision statements are used as decision expressions, the decision expressions are recursively decomposed to extract the atomic conditions, and the source code line number, decision sequence number in the same line, number of atomic conditions, condition text, and expression tree structure of each decision expression are recorded.

[0039] For example, consider the following C language functions: int process(int x, int y, int z) { int result = 0; if (x > 0 && y < 10) { result = 1; } if (x > 5 || z == 0) { result = result + 2; } return result; } Static analysis identified two decision expressions: Decision 1 (line 3): x > 0 && y < 10, containing the atomic conditions x > 0 and y < 10, with the logical relation being AND. Decision 2 (line 6): x > 5 || z == 0, containing the atomic conditions x > 5 and z == 0, the logical relation is OR. For decision 1, both the conditions x > 0 and y < 10 are atomic conditions. The position of each atomic condition in the expression tree is also recorded: atomic condition x > 0 is the left child node, y < 10 is the right child node, and the root node is &&.

[0040] Step S2: Stake installation and coverage information collection Based on the decision expression and the atomic conditions, the objective function is instrumented to generate an instrumented objective function; when the instrumented objective function is executed based on test cases, the observed values ​​of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics are collected.

[0041] Specifically, a decision number is created before each decision is executed, the value (true / false) of the atomic condition is recorded when the atomic condition is evaluated, and the final decision result (true / false) is recorded after the decision expression is evaluated. For decision expressions containing short-circuit logic, pointer dereferencing, array access, or function calls, the original evaluation order of the target function is maintained to avoid changing program behavior due to instrumentation.

[0042] Pseudocode example of instrumentation code: / / Original code if (x > 0 && y < 10) { result = 1; } / / Code after instrumentation int cond_id = ++cond_counter; int cond_val_1, cond_val_2, final_result; if (x > 0) { cond_val_1 = 1; record(cond_id, 1, 1);} else { cond_val_1 = 0; record(cond_id, 1, 0);} if (y < 10) { cond_val_2 = 1; record(cond_id, 2, 1);} else { cond_val_2 = 0; record(cond_id, 2, 0);} final_result = (cond_val_1 && cond_val_2); record_final(cond_id, final_result); if (final_result) { result = 1; } Step S3: MC / DC Coverage Calculation and Independent Impact of Missing Data on Identification Based on the collected observations, the final decision results, and the information on the unevaluated atomic conditions, calculate the MC / DC coverage of the current test case set and identify the target atomic conditions for missing independent influence pairs; wherein, an independent influence pair refers to two test executions for a certain atomic condition in the same decision expression, where the value of the atomic condition is different and the final decision results are different, while the values ​​of the other atomic conditions are the same or are masked due to short-circuit semantics.

[0043] Specifically, each decision execution is represented as an event, including the decision location, atomic condition observation set, final decision result, and unevaluated condition information; for multiple execution events of the same decision expression, for each atomic condition, a search is conducted from the test case set to see if there exists a pair of test cases that satisfy MC / DC coverage; otherwise, the atomic condition is determined as the target atomic condition for missing independent influence pairs.

[0044] For example, for the decision expression if (A && B), suppose that after executing a set of test cases, the following events are collected: Event 1 (Test Case tc1): A=true, B=true, Result=true Event 2 (Test Case tc2): A=false, B=true, Result = false For atomic condition A: there exist events 1 (A=true, B=true, result=true) and 2 (A=false, B=true, result=false), where A takes different values ​​and the results are different, while the other conditions B are the same. Therefore, the independent effects of A have been covered.

[0045] For atomic condition B: Currently, there is only the case where B=true (both events 1 and 2 have B=true), and the execution record when B=false is missing. Therefore, the independent influence of B is missing.

[0046] For short-circuit semantics: If the expression is if (A && B && C) and the existing events are A=false (short-circuit, B and C are not evaluated) and A=true, B=true, C=false, for atomic condition A, the values ​​of B and C are both "not evaluated" in both executions, which can be regarded as equivalent. Therefore, it can be used to determine whether the independent influence pair of A is covered, provided that the evaluation state of all atoms except A is consistent in both executions (both are evaluated and have the same value, or neither is evaluated).

[0047] Step S4: Confirmation word construction and large language model generation Construct prompt words containing the information of the missing independent influence pairs, input the prompt words into a large language model, and generate candidate test cases.

[0048] Specifically, the prompt explicitly injects summary information of the coverage gap that the independent impact of missing values ​​is independent. The summary information of the coverage gap includes: the line number of the decision expression, the text of the decision expression, the sequence number and text of the missing atomic condition, whether the atomic condition has been observed to be true, whether it has been observed to be false, whether there is a short-circuit unevaluated case, and the current reason for the missing value. The prompt explicitly requires the large language model to construct a pair of similar test cases, so that both test cases execute the same decision expression and only change the value of the target atomic condition, while keeping the other atomic conditions unchanged or short-circuited.

[0049] Following the example above, the prompt words can be designed as follows: The current test suite lacks MC / DC independent influence pairs for specific atomic conditions in the following decision expressions.

[0050] The conditional expression is located on line 3. The conditional expression text is: x > 0 && y < 10 Missing atom condition: y < 10 (number 2) The current observation state of this atomic condition is: a true value has been observed, and a false value has not been observed. Current reason for missing: A valid execution is missing where the condition for this atom is false while all other atomic conditions remain unchanged. Task Requirements: Please generate a test case for the function to be tested, so that it, together with the existing test case (input: x=5, y=5, z=0), forms an independent influence pair with the atomic condition "y < 10". The new test case should satisfy the following: reaching the decision expression "x > 0 && y < 10" on line 3, keeps x > 0 as true, y < 10 becomes false, and the decision result changes from true to false. Please output the test case code directly.

[0051] The prompt word requires the large language model to construct a pair of test cases, so that both test cases execute the same decision expression, and only change the value of the target atomic condition, while keeping the other atomic conditions unchanged or short-circuited.

[0052] Step S5: Grading Determination Execute candidate test cases and collect coverage information, then classify them according to the increment of the coverage information relative to the current test case set: If a candidate test case adds at least one independent impact pair, it is marked as a core improvement and included in the MC / DC incremental test suite; If a candidate test case only adds line coverage or branch coverage but does not add independent influence pairs, it is marked as a semi-optimized state and retained for subsequent iteration hints; If a candidate test case does not effectively cover the increment or fails to execute, it is marked as invalid.

[0053] For example, if the test case (x=5, y=15, z=0) generated by LLM reaches the decision x>0 && y<10, where x>0 is true and y<10 is false, and the decision result is false, and this test case (x=5, y=5, z=0) (x>0 is true, y<10 is true, and the result is true) forms an independent influence pair with the atomic condition y<10, then it is marked as a core improvement.

[0054] If the test case generated by LLM only executes a branch that was not previously covered (such as the z==0 branch), but does not add any new independent influence pairs, it is marked as semi-optimal -- this test case indicates that the model understands how to reach the objective function, but has not yet constructed effective independent influence pairs.

[0055] If the test cases generated by LLM fail to execute (e.g., compilation errors, runtime exceptions) or do not add any new coverage after execution, they are marked as invalid.

[0056] Step S6: Differentiated Feedback and Iteration Based on the result of the classification determination, the prompt word is updated, and steps S4 to S5 are repeated until the preset termination condition is met.

[0057] Specifically, if the candidate test cases generated in the previous round were marked as semi-optimized, the prompt in subsequent iterations will indicate: the objective function is reachable; reachability should be maintained, and a pair of similar inputs should be constructed to flip the missing atomic conditions, avoiding resolving the reachability problem. Example feedback: The test cases generated in the previous round successfully reached the target decision expression, but the value of the target atomic condition "y < 10" remained unchanged (it is still true). While maintaining reachability (ensuring x > 0 is true), please adjust the value of y only to make "y < 10" false, leaving the other input parameters unchanged.

[0058] If a candidate test case generated in the previous round is marked as invalid, its reference weight will be reduced in the prompts of subsequent iterations, and the input design direction will be changed. Feedback example: The test cases generated in the previous round failed to execute the target decision expression (x=100 makes x>0 true but no branch is entered). Please redesign the input to ensure that the decision expression on line 3 is executed, and focus on how to make the atomic condition "y < 10" false.

[0059] The termination condition is any of the following: the MC / DC coverage reaches a preset threshold (e.g., 100%), the maximum number of iterations is reached (e.g., 20 rounds), or no core improvements are generated for a consecutive preset number of rounds (e.g., 5 rounds).

[0060] It should be noted that steps S1 to S6 are not isolated operations, but rather an organic whole that supports each other functionally and interacts with each other, forming a closed-loop collaborative mechanism. The static analysis of S1 provides precise judgment expressions and atomic condition locations for the instrumentation of S2, avoiding omissions or misjudgments of the coverage information that needs to be collected during the instrumentation process. S2 and S3 together constitute the "measurement-diagnosis" stage. S2 collects fine-grained coverage data at the atomic condition level through instrumentation, and S3 accurately locates missing independent influence pairs based on this data, providing clear targets for subsequent generation. S4 is the "precise treatment" based on the diagnostic results of S3, transforming abstract coverage gaps into specific flip instructions, giving the model generation behavior clear atomic condition-level guidance. S5 and S6 together constitute the "evaluation-adjustment" stage. S5 accurately distinguishes the type of coverage increment through three-level hierarchical judgment, and S6 outputs differentiated generation instructions based on the judgment results, forming an iterative closed loop of "generation → evaluation → feedback → regeneration".

[0061] The synergistic effect among the above steps is manifested in the following ways: the accurate identification of missing independent influence pairs depends on the fine-grained judgment structure information and atomic condition observation data provided by S1 and S2; the effective implementation of differentiated iterative feedback is predicated on the accurate classification of coverage increment types by S5; and the injection of missing information in the S4 prompt depends on the output of S3. These technical features are interconnected and interdependent, collectively achieving the overall technical effect of "constructing MC / DC independent influence pairs throughout the entire process of static analysis, dynamic execution, and feedback iteration." No single step should be viewed in isolation. Example 2: Dual-strategy fusion In a preferred embodiment of the present invention, a dual-strategy fusion step is further included: fusing the branch coverage-oriented strategy and the MC / DC coverage-oriented strategy to generate two sets of test cases respectively, and taking the union of the two sets as the final test suite.

[0062] The branch coverage-oriented strategy focuses on directing program execution to uncovered branches. It can employ methods similar to steps S1-S6, but uses the missing branch coverage gap rather than the missing MC / DC independent influence pairs as the core message. The MC / DC coverage-oriented strategy is the method described in Example 1. The test cases generated by the two strategies cover different types of test requirements: the branch coverage-oriented strategy helps explore different execution paths of the program, while the MC / DC coverage-oriented strategy focuses on constructing independent influence pairs. Taking the union of the two sets yields a more stable improvement in MC / DC coverage.

[0063] Example 3: System Example This invention provides a unit test generation system for MC / DC coverage, comprising: The static analysis module is used to obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expression containing two or more atomic conditions, and extract each atomic condition and its logical structure relationship. The instrumentation acquisition module is used to instrument the objective function based on the decision expression and the atomic conditions, execute test cases in the existing test case set to run the instrumented objective function, and collect the observation values ​​of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics. The coverage calculation module is used to calculate the MC / DC coverage of the current test case set based on the information collected by the instrumentation acquisition module, and to identify the target atomic conditions of missing independent influence pairs. The prompt word construction module is used to construct prompt words containing the missing independent influence pair information, input them into the large language model, and generate candidate test cases; The grading and determination module is used to execute the candidate test cases and collect coverage information, and to make grading and determination based on the coverage increment: if a new independent impact pair is added, it is marked as a core improvement and included in the test suite; if only line coverage or branch coverage is added, it is marked as semi-optimization; if there is no effective increment or the execution fails, it is marked as invalid. The iterative feedback module is used to update the prompt words according to the judgment result of the hierarchical judgment module, and trigger the prompt word construction module to iteratively generate candidate test cases until the termination condition is met; wherein, the candidate test cases in the semi-optimized state provide feedback that the objective function is reachable and indicate that reachability should be maintained to flip the target atomic condition; invalid candidate test cases reduce their reference weight and indicate that the input direction should be changed.

[0064] For the specific implementation of each of the above modules, please refer to the description of steps S1-S6 in Example 1.

[0065] Comparative experiment To verify the impact of the three-level feedback mechanism of this invention on the test generation effect, a comparative experiment was conducted on the LeetCode C language objective function dataset. This dataset consists of C language implementations of multiple algorithm problems and their objective functions, covering complex decision expressions composed of logical and relational operators, as well as program structures such as pointer dereferencing, character pointer and structure pointer operations, and one-dimensional and multi-dimensional array access. It can be used to examine the unit test generation capability under complex input constraints. Both large language model methods use Qwen3-Coder-Next and employ the same objective function and coverage measurement process; the specific test inputs generated by KLEE are evaluated using the same instrumentation and coverage calculation process.

[0066] Three comparison methods are set up: (1) directly calling the large language model without providing hierarchical feedback based on execution results; (2) using KLEE for symbolic execution and generating specific test inputs; (3) using the three-level feedback method described in Example 1, classifying and iteratively generating candidate test cases based on compilation results, line and branch coverage increments, and newly added MC / DC independent influence pairs. This setting is used to verify whether coverage improvement is solely generated by the MC / DC target description in the prompt words.

[0067] Table 1. Comparison of coverage rates (%) of different test generation methods

[0068] As shown in Table 1, when directly calling the large language model, the line coverage, branch coverage, and MC / DC coverage are 19.86%, 19.17%, and 13.10%, respectively; the corresponding results of the three-level feedback method reach 92.89%, 88.93%, and 72.05%, respectively, representing absolute improvements of 73.03, 69.76, and 58.95 percentage points. Furthermore, the compilation success rate of test cases generated by directly calling the large language model is only 20.61%. This indicates that simply declaring the MC / DC target in the prompt words is insufficient to stably construct inputs that meet the independent influence requirement, while continuously feeding the actual coverage gaps back to the large language model can obtain a more complete test suite.

[0069] Compared to KLEE, the three-level feedback method improves row coverage, branch coverage, and MC / DC coverage by 7.47, 11.27, and 8.40 percentage points, respectively. Under the objective function and experimental configuration of this embodiment, these results demonstrate that feedback iteration based on missing independent influence pairs not only drives path exploration but also more directly guides the construction of atomic conditionally independent influence relationships.

[0070] The above comparison results support the following technical effects: the coverage improvement of the present invention does not only come from adding MC / DC requirements to the prompt words, but is related to the closed-loop collaborative mechanism formed by static analysis, stake measurement, identification of missing independent influence pairs, three-level hierarchical judgment and feedback iteration.

[0071] Example 4: Storage Medium Example This invention provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method described in Embodiment 1 or Embodiment 2. The storage medium includes, but is not limited to, non-volatile storage media such as ROM, RAM, magnetic disk, optical disk, and flash memory.

[0072] The large language model described in this invention includes, but is not limited to, pre-trained language models capable of code understanding and generation, such as the GPT series models, CodeLlama, and CodeT5.

[0073] The above description of the embodiments is only for the purpose of helping to understand the method and core idea of ​​this application; at the same time, for those skilled in the art, there will be changes in the specific implementation and application scope based on the idea of ​​this application. Therefore, the content of this specification should not be construed as a limitation of this application.

[0074] Certain terms are used in the specification and claims to refer to specific components. Those skilled in the art will understand that hardware manufacturers may use different names to refer to the same component. This specification and claims do not distinguish components based on differences in name, but rather on differences in function. The terms "comprising" and "including" used throughout the specification and claims are open-ended and should be interpreted as "comprising / including but not limited to". "Approximately" means that within an acceptable margin of error, those skilled in the art can solve the technical problem and substantially achieve the technical effect within a certain margin of error. The following descriptions in the specification are preferred embodiments for carrying out this application; however, these descriptions are for the purpose of illustrating the general principles of this application and are not intended to limit the scope of this application. The scope of protection of this application shall be determined by the appended claims.

[0075] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a product or system comprising a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a product or system. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the product or system that includes said element.

[0076] It should be understood that the term "and / or" used in this article is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this article generally indicates that the preceding and following related objects have an "or" relationship.

[0077] The foregoing description illustrates and describes several preferred embodiments of this application. However, as previously stated, it should be understood that this application is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. It can be used in various other combinations, modifications, and environments, and can be altered within the scope of the application concept described herein through the foregoing teachings or techniques or knowledge in related fields. Any modifications and variations made by those skilled in the art that do not depart from the spirit and scope of this application should be within the protection scope of the appended claims.

Claims

1. A method for generating unit tests for MC / DC coverage, characterized in that, Includes the following steps: S1: Obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expression that contains two or more atomic conditions, and extract each atomic condition and its logical structure relationship; S2: Based on the decision expression and the atomic conditions, instrument the objective function; Execute test cases from the existing test case set to run the instrumented target function, collect the observations of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics; S3: Based on the information collected in S2, calculate the MC / DC coverage of the current test case set and identify the target atomic conditions of missing independent influence pairs; the independent influence pair refers to two test executions for the same atomic condition, where the atomic condition has different values ​​and different judgment results, and the other atomic conditions have the same values ​​or are short-circuited and masked. S4: Construct prompt words containing the missing independent impact pair information, input them into the large language model, and generate candidate test cases; S5: Execute the candidate test cases and collect coverage information. Make a graded judgment based on the coverage increment: if a new independent impact pair is added, mark it as a core improvement and include it in the test suite; If only new row coverage or branch coverage is added, it is marked as semi-optimized; if there is no effective increment or execution fails, it is marked as invalid. S6: Update the prompt words based on the classification judgment result of S5, and return to S4 to iterate until the termination condition is met; among them, the candidate use cases in the semi-optimized state feedback that the objective function is reachable and indicate to maintain reachability to flip the target atomic condition; invalid candidate use cases reduce their reference weight and indicate to change the input direction.

2. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, The static analysis in step S1 includes: traversing the abstract syntax tree of the objective function and locating the decision statements in the control structure; the control structure includes if statements, while statements, do-while statements and for statements; taking the Boolean expressions in the decision statements as decision expressions, recursively decomposing the decision expressions, extracting each atomic condition, and recording the source code line number, number of atomic conditions, condition text and expression tree structure of each decision expression.

3. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, The step S2, which involves collecting the observed values ​​of each atomic condition, the final decision result of the decision expression, and the information of atomic conditions that were not evaluated due to short-circuit semantics, specifically includes: creating a decision number before each decision is executed, recording the value of each atomic condition during the evaluation of each atomic condition, and recording the final decision result after the evaluation of the decision expression is completed; for decision expressions containing short-circuit logic, pointer dereferencing, array access, or function calls, maintaining the original evaluation order of the objective function.

4. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, The identification of the target atomic condition for missing independent influence pairs in step S3 specifically includes: representing each decision execution as an event, the event including the decision position, the set of atomic condition observations, the final decision result, and unevaluated condition information; for multiple execution events of the same decision expression, for each atomic condition, searching from the existing test case set for whether there is a pair of test cases that satisfy MC / DC coverage; if not, then the atomic condition is determined as the target atomic condition for missing independent influence pairs.

5. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, The prompt word in step S4 contains a summary of the coverage gap information of the missing independent impact pair. The summary of the coverage gap information includes: the line number of the decision expression, the text of the decision expression, the sequence number and text of the missing atomic condition, whether the atomic condition has been observed to be true, whether it has been observed to be false, whether there is a short-circuit unevaluated case, and the current reason for the missing condition. The prompt word requires the large language model to construct a pair of test cases, so that both test cases execute the same decision expression and only change the value of the target atomic condition, while keeping the other atomic conditions unchanged or short-circuited.

6. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, The termination condition in step S6 is any of the following: the MC / DC coverage reaches a preset threshold, the maximum number of iterations is reached, or no core improvement is generated for a consecutive preset number of rounds.

7. The unit test generation method for MC / DC coverage as described in claim 1, characterized in that, After step S6, the process also includes: merging the branch coverage-oriented strategy and the MC / DC coverage-oriented strategy to generate two sets of test cases respectively, and taking the union of the two sets as the final test suite.

8. A unit test generation system for MC / DC coverage, characterized in that, The system is used to implement the method as described in any one of claims 1-7, the system comprising: The static analysis module is used to obtain the target function to be tested, perform static analysis on the source code of the target function, identify the decision expression containing two or more atomic conditions, and extract each atomic condition and its logical structure relationship. The instrumentation acquisition module is used to instrument the objective function based on the decision expression and the atomic conditions, execute test cases in the existing test case set to run the instrumented objective function, and collect the observation values ​​of each atomic condition, the final decision result of the decision expression, and the atomic condition information that was not evaluated due to short-circuit semantics. The coverage calculation module is used to calculate the MC / DC coverage of the current test case set based on the information collected by the instrumentation acquisition module, and to identify the target atomic conditions of missing independent influence pairs. The prompt word construction module is used to construct prompt words containing the missing independent influence pair information, input them into the large language model, and generate candidate test cases; The grading and determination module is used to execute the candidate test cases and collect coverage information, and to make grading and determination based on the coverage increment: if a new independent impact pair is added, it is marked as a core improvement and included in the test suite; if only line coverage or branch coverage is added, it is marked as semi-optimization; if there is no effective increment or the execution fails, it is marked as invalid. The iterative feedback module is used to update the prompt words according to the judgment result of the hierarchical judgment module, and trigger the prompt word construction module to iteratively generate candidate test cases until the termination condition is met; wherein, the candidate test cases in the semi-optimized state provide feedback that the objective function is reachable and indicate that reachability should be maintained to flip the target atomic condition; invalid candidate test cases reduce their reference weight and indicate that the input direction should be changed.

9. The unit test generation system for MC / DC coverage as described in claim 8, characterized in that, The instrumentation acquisition module collects the observed values ​​of each atomic condition, the final judgment result of the decision expression, and the information of atomic conditions that were not evaluated due to short-circuit semantics. Specifically, it includes: creating a decision number before each decision is executed, recording the value of each atomic condition when it is evaluated, and recording the final judgment result after the decision expression is evaluated; for decision expressions containing short-circuit logic, pointer dereferencing, array access, or function calls, the original evaluation order of the target function is maintained.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 7.