Method and apparatus for generating unit test code

CN122507629APending Publication Date: 2026-08-04SAMSUNG SEMICON CHINA RES & DEV +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610611454.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-05-06
Publication Date
2026-08-04

AI Technical Summary

Technical Problem

但是由于单元测试代码需要开发人员编写维护,在人力资源有限的情况下,很多大型项目往往缺少足量的单元测试

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122507629A_ABST
    Figure CN122507629A_ABST
Patent Text Reader

Abstract

A method and apparatus for generating unit test code are provided. The method includes determining one or more assignment expressions and conditional expressions in a control flow of a function under test by traversing each expression of the control flow, wherein a variable in the conditional expression is associated with at least one of the one or more assignment expressions; eliminating the local variable in the conditional expression based on the at least one assignment expression; and generating the unit test code corresponding to the function under test based on the conditional expression in which the local variable is eliminated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of code testing (e.g., unit code testing), and more specifically to methods and apparatus for generating unit test code. Background Technology

[0002] Unit testing (UT) is the core of test-driven development. Unit tests verify code correctness, prevent regression errors, improve code quality, and / or reduce maintenance costs, making them an indispensable part of software projects. However, because unit test code needs to be written and maintained by developers, many large projects often lack a sufficient number of unit tests when human resources are limited. Furthermore, the quality of unit test code generated by current unit test code generation solutions is often low and fails to meet project requirements.

[0003] Therefore, a unit test code generation solution is needed that uses software to automatically generate unit test code for the source code under test. Summary of the Invention

[0004] According to one aspect of the present disclosure, a method for generating unit test code is provided, the method comprising: determining one or more assignment expressions and conditional expressions in the control flow by traversing each expression of the control flow of a function under test, wherein a variable in the conditional expression is associated with at least one of the one or more assignment expressions; eliminating local variables in the conditional expression based on the at least one assignment expression; and generating unit test code corresponding to the function under test based on the conditional expression in which the local variables are eliminated.

[0005] Optionally, the step of eliminating local variables in a conditional expression includes: storing the one or more assignment expressions and conditional expressions in an expression queue in sequence based on the order in which they appear in the control flow; and substituting the at least one assignment expression into the conditional expression in sequence by traversing the expression queue in reverse order of the at least one assignment expression preceding the conditional expression to eliminate local variables in the conditional expression.

[0006] Optionally, the steps of generating unit test code corresponding to the function under test include: generating a string conforming to z3 syntax based on the conditional expression in which local variables are eliminated; solving for the variable values ​​in the string conforming to z3 syntax using a z3 solver; and generating arrangement statements in the unit test code based on the solved variable values.

[0007] Optionally, the step of generating a string conforming to z3 syntax includes: generating a variable declaration string corresponding to the variable based on the variable in the conditional expression in which local variables are eliminated; and converting the operators, variables, and constants in the conditional expression in which local variables are eliminated into assertion strings conforming to z3 syntax, wherein the conversion of operators is based on a predetermined conversion table, wherein the predetermined conversion table includes information indicating the conversion relationship between operators with the same logic and z3 strings, wherein the string conforming to z3 syntax includes variable declaration strings and assertion strings.

[0008] Optionally, the transformation steps include: determining whether the control node in the control flow corresponding to the conditional expression is IfStmt; based on determining that the control node is IfStmt and based on the control node being a true branch, converting the operators, variables, and constants in the conditional expression where local variables are eliminated into assertion strings conforming to z3 syntax; and based on determining that the control node is IfStmt and based on the control node being a false branch, adding the prefix distinct and converting the operators, variables, and constants in the conditional expression where local variables are eliminated into assertion strings conforming to z3 syntax.

[0009] Optionally, the step of generating a string conforming to z3 syntax based on a conditional expression in which local variables are eliminated includes: determining whether the control node in the control flow corresponding to the conditional expression is a SwitchStmt; based on determining that the control node is a SwitchStmt, converting the operators, variables, and constants in the conditional expression in which local variables are eliminated into assertion strings conforming to z3 syntax, and determining whether the next node of the control node is a CaseStmt or a DefaultStmt; based on determining that the next node of the control node is a CaseStmt, supplementing the assertion string conforming to z3 syntax with the case value of the next node; and based on determining that the next node of the control node is a DefaultStmt and based on the switch condition of the control node being int or the case statement of the control node having exhausted enum values, supplementing the assertion string conforming to z3 syntax with a new value; and based on determining that the next node of the control node is a DefaultStmt and based on the switch condition of the control node not being int and the case statement not having exhausted enum values, supplementing the assertion string conforming to z3 syntax with the unused enum values ​​of the case statement.

[0010] Optionally, the step of generating unit test code corresponding to the function under test further includes: converting the original data type of the data in the string conforming to z3 syntax to z3::bitvector before solving the variable values ​​in the string conforming to z3 syntax, wherein the data includes input parameters, global variables and stub function return values ​​in the conditional statements in the control flow; and restoring the data type of the solved variable values ​​to the original data type corresponding to the solved variable values ​​before generating the arrangement statements in the unit test code.

[0011] Optionally, the step of generating unit test code corresponding to the function under test further includes: calculating the return value of the function under test based on the solved variable values ​​and the C++ mathematical expression library; and generating assertion statements in the unit test code based on the return value of the function under test.

[0012] Optionally, the steps for calculating the return value of the function under test include: generating a parameter variable mapping table and a global variable mapping table based on the traversal of the control flow, wherein the parameter variable mapping table includes parameters and their values ​​in the control flow, and the global variable mapping table includes global variables and their values, and wherein the parameter values ​​correspond to the solved variable values; traversing the statement nodes of the control flow and determining the statement type of the traversed statement nodes; based on the statement type of the traversed statement nodes being variable declaration statements, determining the values ​​of the declared variables in the variable declaration statements, and adding the declared variables and their values ​​to the local variable mapping table; and based on the statement type of the traversed statement nodes being assignment statements. The function calculates the lvalue of the assignment expression corresponding to the traversed statement node by using at least one of the parameter variable map, global variable map, and local variable map, and the C++ mathematical expression library, and updates the lvalue in the corresponding map of the parameter variable map, global variable map, and local variable map; and if the statement type of the traversed statement node is a return statement, it calculates the expression corresponding to the traversed statement node by using at least one of the parameter variable map, global variable map, and local variable map, and the C++ mathematical expression library, and outputs the calculation result of the expression corresponding to the traversed statement node as the return value of the function under test.

[0013] According to one aspect of the present disclosure, an apparatus for generating unit test code is provided, the apparatus comprising: at least one processor, and a memory storing one or more instructions, wherein the one or more instructions, when executed by the at least one processor, cause the at least one processor to: determine one or more assignment expressions and conditional expressions in the control flow by traversing each expression of the control flow of a function under test, wherein a variable in the conditional expression is associated with at least one of the one or more assignment expressions; eliminate local variables in the conditional expressions based on the at least one assignment expression; and generate unit test code corresponding to the function under test based on the conditional expressions in which the local variables are eliminated.

[0014] Optionally, the one or more instructions further cause the at least one processor to: store the one or more assignment expressions and conditional expressions in an expression queue in order of their appearance in the control flow; and to substitute the at least one assignment expression into the conditional expression in reverse order by traversing the expression queue in reverse order of the at least one assignment expression preceding the conditional expression, thereby eliminating local variables in the conditional expression.

[0015] Optionally, the one or more instructions further cause the at least one processor to: generate a string conforming to z3 syntax based on a conditional expression in which local variables are eliminated; solve for the variable values ​​in the string conforming to z3 syntax using a z3 solver; and generate arrangement statements in unit test code based on the solved variable values.

[0016] Optionally, the one or more instructions further cause the at least one processor to: generate a variable declaration string corresponding to the variable based on the variable in the conditional expression in which the local variable is eliminated; and convert the operators, variables, and constants in the conditional expression in which the local variable is eliminated into assertion strings conforming to z3 syntax, wherein the conversion of operators is based on a predetermined conversion table, wherein the predetermined conversion table includes information indicating the conversion relationship between operators having the same logic and z3 strings, wherein the strings conforming to z3 syntax include variable declaration strings and assertion strings.

[0017] Optionally, the one or more instructions further cause the at least one processor to: determine whether the control node in the control flow corresponding to the conditional expression is IfStmt; based on determining that the control node is IfStmt and based on the control node being a true branch, convert the operators, variables, and constants in the conditional expression in which local variables are eliminated into assertion strings conforming to z3 syntax; and based on determining that the control node is IfStmt and based on the control node being a false branch, add the prefix distinct and convert the operators, variables, and constants in the conditional expression in which local variables are eliminated into assertion strings conforming to z3 syntax.

[0018] Optionally, the one or more instructions further cause the at least one processor to: determine whether the control node in the control flow corresponding to the conditional expression is a SwitchStmt; based on determining that the control node is a SwitchStmt, convert the operators, variables, and constants in the conditional expression where local variables are eliminated into assertion strings conforming to z3 syntax, and determine whether the next node of the control node is a CaseStmt or a DefaultStmt; based on determining that the next node of the control node is a CaseStmt, supplement the assertion string conforming to z3 syntax with the case value of the next node; based on determining that the next node of the control node is a DefaultStmt and based on the switch condition of the control node being int or the case statement of the control node having exhausted enum values, supplement the assertion string conforming to z3 syntax with a new value; and based on determining that the next node of the control node is a DefaultStmt and based on the switch condition of the control node not being int and the case statement not having exhausted enum values, supplement the assertion string conforming to z3 syntax with the unused enum values ​​of the case statement.

[0019] Optionally, the one or more instructions further cause the at least one processor to: convert the original data type of the data in the string conforming to z3 syntax to z3::bitvector before solving for the variable values ​​in the string conforming to z3 syntax, wherein the data includes input parameters, global variables, and stub function return values ​​in the conditional statements in the control flow; and restore the data type of the solved variable values ​​to the original data type corresponding to the solved variable values ​​before generating the arrangement statements in the unit test code.

[0020] Optionally, the one or more instructions further cause the at least one processor to: calculate the return value of the function under test based on the solved variable values ​​and the C++ mathematical expression library; and generate assertion statements in the unit test code based on the return value of the function under test.

[0021] Optionally, the one or more instructions further cause the at least one processor to: generate a parameter variable mapping table and a global variable mapping table based on control flow traversal, wherein the parameter variable mapping table includes parameters and their values ​​in the control flow, wherein the global variable mapping table includes global variables and their values, and wherein the parameter values ​​correspond to the solved variable values; traverse the statement nodes of the control flow and determine the statement type of the traversed statement nodes; based on the statement type of the traversed statement nodes being variable declaration statements, determine the values ​​of the declared variables in the variable declaration statements, and add the declared variables and their values ​​to the local variable mapping table; and based on the statement type of the traversed statement nodes... The type is an assignment statement, which uses at least one of the parameter variable map, global variable map, and local variable map, along with the C++ mathematical expression library, to calculate the lvalue of the assignment expression corresponding to the traversed statement node, and updates the lvalue in the corresponding map of the parameter variable map, global variable map, and local variable map; and the statement type based on the traversed statement node is a return statement, which uses at least one of the parameter variable map, global variable map, and local variable map, along with the C++ mathematical expression library, to calculate the expression corresponding to the traversed statement node, and outputs the calculated result of the expression as the return value of the function under test.

[0022] According to one aspect of the present disclosure, a non-transitory computer-readable storage medium storing instructions is provided, wherein the instructions, when executed by at least one processor, implement a method for generating unit test code, the method comprising: determining one or more assignment expressions and conditional expressions in the control flow by traversing each expression of the control flow of a function under test, wherein a variable in the conditional expression is associated with at least one of the one or more assignment expressions; eliminating local variables in the conditional expression based on the at least one assignment expression; and generating unit test code corresponding to the function under test based on the conditional expression in which the local variables are eliminated.

[0023] Optionally, the step of eliminating local variables in a conditional expression includes: storing the one or more assignment expressions and conditional expressions in an expression queue in the order in which they appear in the control flow; and substituting the at least one assignment expression into the conditional expression by traversing the expression queue in reverse order of the at least one assignment expression preceding the conditional expression to eliminate local variables in the conditional expression. Attached Figure Description

[0024] The above and other aspects and features will become clearer from the following description of embodiments in conjunction with the accompanying drawings.

[0025] Figure 1This is a flowchart illustrating a unit test code generation method according to at least one example embodiment of the present disclosure.

[0026] Figure 2 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0027] Figure 3 This is a flowchart illustrating a method for generating a string conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0028] Figure 4 This is a flowchart illustrating a method for generating assertion strings conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0029] Figure 5 This is a flowchart illustrating a method for generating a string conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0030] Figure 6 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0031] Figure 7 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0032] Figure 8 This is a flowchart illustrating a local variable elimination algorithm according to at least one example embodiment of the present disclosure.

[0033] Figure 9 This is a flowchart illustrating a method for transforming control flow into a z3 solver according to at least one example embodiment of the present disclosure.

[0034] Figure 10 This is a flowchart illustrating a method for calculating the function return value after the execution of control flow according to at least one example embodiment of the present disclosure.

[0035] Figure 11 A block diagram of a unit test code generation apparatus according to at least one example embodiment of the present disclosure is shown. Detailed Implementation

[0036] In the following description, embodiments are illustrated with reference to the accompanying drawings. The same reference numerals may be used for the same components in the drawings, and redundant descriptions may be omitted. However, embodiments may be implemented in various forms and are not limited to the specific examples described herein.

[0037] Figure 1This is a flowchart illustrating a unit test code generation method (i.e., a method for generating unit test code) according to at least one example embodiment of the present disclosure.

[0038] Reference Figure 1 In operation S110, one or more assignment expressions and condition expressions in the control flow can be determined by traversing each expression of the control flow of the function under test. Variables in the condition expressions are associated with at least one of the one or more assignment expressions.

[0039] The function under test can refer to a specific function that is targeted in code testing to verify its functional correctness, performance, stability, and other characteristics. The control flow of the function under test can refer to the execution order and path of the statements within the function under test, and describe how the program jumps from one instruction to another based on conditional statements, loop structures, and other control statements during runtime.

[0040] For example, as an example only, Table 1 shows an example function under test, func(int k), and its two control flows, 1 and 2, which can be represented by path 1 and path 2, respectively. For example, the statements in path 1 can correspond to "g = (k>>1) + k + 2", "g>20 : true", and "return g". For example, the statements in path 2 can correspond to "g = (k>>1) + k + 2", "g>20 : false", and "return 0".

[0041] Table 1

[0042] For example, by traversing the control flow 1 of the example function under test func(int k), the expression of the control flow 1 of the example function under test func(int k) may include the assignment expression "g = (k>>1) + k + 2" and the conditional expression "g>20". Since the variable "g" in the conditional expression "g>20" is related to the variable "g" in the assignment expression "g = (k>>1) + k + 2", the variable "g" in the conditional expression "g>20" can be associated with the assignment expression "g = (k>>1) + k + 2".

[0043] For example, by traversing the control flow 2 of the example tested function func(int k), the expression of control flow 2 can include the assignment expression "g = (k>>1) + k + 2" and the conditional expression "!(g>20)". Since the variable "g" in the conditional expression "!(g>20)" is related to the variable "g" in the assignment expression "g = (k>>1) + k + 2", the variable "g" in the conditional expression "!(g>20)" can be associated with the assignment expression "g = (k>>1) + k + 2".

[0044] In operation S120, local variables in a conditional expression can be eliminated based on at least one assignment expression.

[0045] In one example embodiment, one or more assignment expressions and conditional expressions can be stored sequentially in an expression queue according to the order in which they appear in the control flow. Then, at least one assignment expression can be substituted into the conditional expression by iterating backwards through the expression queue at least one assignment expression preceding the conditional expression, thereby eliminating local variables in the conditional expression.

[0046] In the control flow 1 of the function under test, func(int k), the assignment expression “g = (k>>1) + k + 2” and the conditional expression “g>20” are stored sequentially in the example expression queue shown in Table 2 below, according to the order in which they appear in control flow 1. At least one assignment expression “g = (k>>1) + k + 2” is substituted into the conditional expression “g>20” by traversing the expression queue in reverse order of at least one assignment expression “g = (k>>1) + k + 2” preceding the conditional expression “g>20”, thus eliminating the local variable “g” in the conditional expression “g>20”. After the local variable “g” is eliminated, the conditional expression “g>20” becomes the conditional expression “(k>>1) + k + 2>20”.

[0047] Table 2

[0048] In the control flow 2 of the function under test, func(int k), the assignment expression “g = (k>>1) + k + 2” and the conditional expression “!(g>20)” are stored sequentially in the example expression queue shown in Table 3 below, according to the order in which they appear in control flow 2. At least one assignment expression “g = (k>>1) + k + 2” is substituted into the conditional expression “!(g>20)” by traversing the expression queue in reverse order of at least one assignment expression “g = (k>>1) + k + 2” preceding the conditional expression “!(g>20)”, thus eliminating the local variable “g” in the conditional expression “!(g>20)”. After the local variable “g” is eliminated, the conditional expression “!(g>20)” becomes “!((k>>1) + k + 2>20)”.

[0049] Table 3

[0050] In operation S130, unit test code corresponding to the function under test can be generated based on the conditional expression in which local variables are eliminated.

[0051] In a non-restricted example, existing control flow analysis-based generation methods can be used to generate unit test code corresponding to the function under test.

[0052] In related technologies, unit test code corresponding to the function under test is generated based on the original control flow (e.g., the original conditional expression). Therefore, the generated unit test code often contains various intermediate variables, which makes the quality of the unit test code relatively low (e.g., relatively low execution efficiency or more prone to errors).

[0053] In contrast, the unit test code generation method according to at least one example embodiment of this disclosure can generate high-quality unit test code by eliminating local variables in conditional expressions and generating unit test code corresponding to the function under test based on conditional expressions in which local variables are eliminated.

[0054] Figure 2 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0055] Reference Figure 2 In operation S210, a string conforming to the z3 syntax can be generated based on the conditional expression in which local variables are eliminated.

[0056] The z3 syntax is the syntax included in the z3 solver. The z3 solver can be an SMT solver developed by Microsoft Research, used to check the satisfiability of logical expressions and find a feasible solution from a set of constraints.

[0057] For example, in the control flow 1 of the function under test func(int k), a string conforming to z3 syntax (e.g., the z3 expression corresponding to the conditional expression "(k>>1)+ k+ 2>20") can be generated based on the conditional expression "(k>>1)+ k+ 2>20" in which local variables are eliminated.

[0058] For example, in the control flow 2 of the function under test func(int k), a string conforming to z3 syntax (e.g., the z3 expression corresponding to the conditional expression "!((k>>1) + k + 2>20)" where local variables are eliminated) can be generated based on the conditional expression "!((k>>1) + k + 2>20)".

[0059] In operation S220, the z3 solver can be used to solve for variable values ​​in strings that conform to the z3 syntax.

[0060] For example, in the control flow 1 of the function under test, func(int k), a z3 solver is used to solve for the variable values ​​in a string that conforms to z3 syntax (e.g., the z3 expression corresponding to the conditional expression "(k>>1) + k + 2>20"). For example, the result of the z3 solver could be k = 19.

[0061] For example, in the control flow 2 of the function under test, func(int k), a z3 solver is used to solve for the variable values ​​in a string that conforms to z3 syntax (e.g., the z3 expression corresponding to the conditional expression "!((k>>1) + k + 2>20)"). For example, the result of the z3 solver could be k = 0.

[0062] In operation S230, the arrangement statements in the unit test code are generated based on the solved variable values.

[0063] For example, in the control flow 1 of the function under test func(int k), the arrangement statement in the unit test code generated based on the solved variable value "k = 19" may include the statement "int k = 19".

[0064] For example, in the control flow 2 of the function under test func(int k), the arrangement statement in the unit test code generated based on the solved variable value "k = 0" may include the statement "int k = 0".

[0065] Table 4 below shows an example of generating arrangement statements based on control flow paths. Referring to Table 4, local variable elimination can be performed during the generation of z3 expressions from the control flow paths. Before performing z3 evaluation, the z3 expressions can be converted to z3 strings. By using easily processed z3 strings instead of the more complex z3 expressions for evaluation, the processing difficulty is reduced.

[0066] Table 4

[0067] In the method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure, high-quality test code can be constructed because the logical relationship contained in the conditional statement can be converted into a z3 prover expression and calculated to obtain various parameters (e.g., input parameter values ​​and stub function return values) for constructing test cases.

[0068] Figure 3 This is a flowchart illustrating a method for generating a string conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0069] Reference Figure 3 In operation S310, a variable declaration string corresponding to the variable can be generated based on the variable in the conditional expression where the local variable is eliminated.

[0070] For example, in a non-restrictive example, when the variable in the conditional expression where local variables are eliminated includes i, the variable declaration string corresponding to variable i can be generated as "(declare-fun i () (_ BitVec32))".

[0071] In operation S320, operators, variables, and constants in conditional expressions where local variables are eliminated can be converted into assertion strings that conform to z3 syntax.

[0072] Operator conversions can be based on a predefined conversion table. This table includes information indicating the conversion relationships between operators with the same logic and z3 strings. Furthermore, conversions of variables and constants can be based on z3 syntax.

[0073] In a non-restrictive example, the predefined conversion table could be Table 5 as shown below.

[0074] Table 5

[0075] Figure 4 This is a flowchart illustrating a method for generating assertion strings conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0076] Reference Figure 4 In operation S410, it can be determined whether the control node in the control flow corresponding to the conditional expression is IfStmt.

[0077] IfStmt can be a branching structure based on Boolean conditional judgments, and it indicates that the control node is a conditional statement.

[0078] In operation S420, based on the determination that the control node is IfStmt, when the control node is a true branch, the operators, variables, and constants in the conditional expressions where local variables are eliminated are converted into assertion strings conforming to z3 syntax. For example, "based on A, when B" can be used interchangeably with "based on A and based on B".

[0079] In operation S430, when the control node is a dummy branch, the prefix "distinct" is added and the operators, variables and constants in the conditional expression where local variables are eliminated are converted into assertion strings that conform to z3 syntax.

[0080] Figure 5 This is a flowchart illustrating a method for generating a string conforming to z3 syntax according to at least one example embodiment of the present disclosure.

[0081] Reference Figure 5 In operation S510, it can be determined whether the control node in the control flow corresponding to the conditional expression is SwitchStmt.

[0082] SwitchStmt can be a value-matching branching structure and indicates that the control node is a branching statement.

[0083] In operation S520, based on the determination that the control node is SwitchStmt, the operators, variables and constants in the conditional expressions where local variables are eliminated are converted into assertion strings that conform to z3 syntax, and it is determined whether the next node of the control node is CaseStmt or DefaultStmt.

[0084] CaseStmt can be a flow control structure. CaseStmt can select the matching branch from multiple branches based on the value of an expression and execute it. DefaultStmt can be an optional branch in a multi-branch conditional structure and is used to handle all cases that are not matched by other case branches.

[0085] In operation S530, based on the determination that the next node of the control node is CaseStmt, the assertion string conforming to the z3 syntax is supplemented with the case value of the next node.

[0086] In operation S540, based on the determination that the next node of the control node is DefaultStmt, when the switch condition of the control node is int or the case statement of the control node has exhausted the enum values, the assertion string conforming to the z3 syntax is supplemented with a new value. Furthermore, in operation S540, based on the determination that the next node of the control node is DefaultStmt, when the switch condition of the control node is not int and the case statement has not exhausted the enum values, the assertion string conforming to the z3 syntax is supplemented with the unused enum values ​​from the case statement.

[0087] For example, referring to the code example below, after `SwitchStmt` on path 0 is a `CaseStmt` node. Using the enum value "TWO" of the `case` statement, the assertion string conforming to z3 syntax can be supplemented with the value 2. An assertion string conforming to z3 syntax (e.g., a z3 expression) could be "num = 2". Furthermore, after `SwitchStmt` on path 1 is a `DefaultStmt` node, and since none of the `case` statement values ​​have exhausted the values ​​defined by the `enum ENUM_t`, the unused enum value "THREE" (i.e., the value 3) is used to supplement the assertion string conforming to z3 syntax. An assertion string conforming to z3 syntax (e.g., a z3 expression) could be "num = 3". An exemplary logic is presented below.

[0088]

[0089] Figure 6 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0090] Reference Figure 6 In operation S610, before evaluating the variable values ​​in a string conforming to z3 syntax, the original data type of the data in the string conforming to z3 syntax is converted to z3::bitvector. The data includes input parameters from conditional statements in the control flow, global variables, and stub function return values.

[0091] The input parameters, global variables, and stub function return values ​​in conditional statements may have different data types. In at least one example embodiment of this disclosure, to avoid compatibility issues when solving z3, all data types are converted to z3::bitvector. For example, each variable a can be declared in the form ((declare-fun a () (_ BitVec 32)).

[0092] In operation S620, before generating the arrangement statement in the unit test code, the data type of the solved variable value is restored to the original data type corresponding to the solved variable value.

[0093] For example, after the calculation is complete, the result should be converted to a primitive data type. For data types shorter than 32 bits, their maximum value needs to be limited in the z3 string to avoid numerical overflow issues.

[0094] Figure 7 This is a flowchart illustrating a method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure.

[0095] Reference Figure 7 In operating the S710, the return value of the function under test can be calculated based on the solved variable values ​​and the C++ mathematical expression library.

[0096] The solved variable values ​​can be obtained by using the z3 solver to solve for the variable values ​​in a string that conforms to the z3 syntax.

[0097] The C++ Mathematical Expression Library (ExprTk) is a simple, easy-to-integrate, and extremely efficient mathematical expression parser and evaluation engine.

[0098] In one example embodiment, a parameter variable map and a global variable map are generated by traversing the control flow. The parameter variable map includes parameters in the control flow and their values. The global variable map includes global variables and their values, where the parameter values ​​correspond to the solved variable values. Then, statement nodes in the control flow are traversed, and the statement type of the traversed statement nodes is determined. If the statement type of the traversed statement node is a variable declaration statement, the values ​​of the declared variables in the variable declaration statement are determined, and the declared variables and their values ​​are added to the local variable map. If the statement type of the traversed statement node is an assignment statement, the lvalue of the assignment expression corresponding to the traversed statement node is calculated using at least one of the parameter variable map, the global variable map, and the local variable map, along with a C++ mathematical expression library, and the lvalue is updated in the corresponding mappings in the parameter variable map, the global variable map, and the local variable map. The statement type of the traversed statement node is a return statement. The expression corresponding to the traversed statement node is calculated by using at least one of the parameter variable map, global variable map, and local variable map, as well as the C++ mathematical expression library, and the result of the expression is output as the return value of the function under test.

[0099] Because expressions can be transformed into object expressions that the tool can process, and the variable mapping table is updated in real time, the validity of the calculation is guaranteed, thus providing high-quality test code quickly.

[0100] In the S720 operation, assertion statements in the unit test code are generated based on the return value of the function under test.

[0101] For example, when the return value of the function under test is 6, the assertion statement in the unit test code can be generated as ASSERT_EQ(26, r), where r = func(k).

[0102] For example, when the return value of the function under test is 0, the assertion statement in the unit test code can be generated as ASSERT_EQ(0, r), where r = func(k).

[0103] Table 6 below provides an example of generating assertion statements based on the z3 solution result. Referring to Table 6, the z3 solution result for control flow 1 is k=19, the calculated function return value is 26, and the generated assertion statement is ASSERT_EQ(26, r). The z3 solution result for control flow 2 is k=0, the calculated function return value is 0, and the generated assertion statement is ASSERT_EQ(0, r).

[0104] Table 6

[0105] Figure 8 This is a flowchart illustrating a local variable elimination algorithm according to at least one example embodiment of the present disclosure.

[0106] Reference Figure 8 The process starts and the "expression queue" is cleared.

[0107] In operation S810, all expressions 'n' in the specified path are traversed.

[0108] In operation S820, it can be determined whether the expression 'n' is a conditional expression.

[0109] In operation S830, based on the determination that expression 'n' is a conditional expression, the assignment expressions in the expression queue are traversed from newest to oldest. Assume the variable being assigned in the assignment expression is 'x'. Optionally, expression 'n' can be added to the expression queue.

[0110] In operation S840, based on finding the assignment expression, it is determined whether 'x' exists in the expression 'n'.

[0111] In operation S850, based on the determination that 'x' exists in expression 'n', 'x' in expression 'n' is replaced with the rvalue in the assignment expression.

[0112] In operation S860, based on the fact that the expression 'n' is not a conditional expression, it is determined whether the expression 'n' is an assignment expression.

[0113] In operation S870, based on the determination that expression 'n' is an assignment expression, the assignment expression is added to the expression queue.

[0114] Based on the local variable elimination algorithm in the example above, the process starts and the expression queue is cleared. Each expression in the specified control flow is traversed. If the expression is not a conditional expression, it is appended to the end of the "expression queue". If the expression is a conditional expression, then "local variable elimination" is performed. This is done by iterating backwards through each expression (e.g., an assignment expression) `expr` in the expression queue. If the left-hand side of `expr` appears in a conditional expression, then that left-hand side in the conditional expression is replaced with the right-hand side of `expr`. This process of substituting `expr` into the "expression queue" sequentially achieves "local variable elimination".

[0115] For example, the local variable elimination algorithm can be integrated into the Z3 expression transformation process or can be performed before the Z3 expression transformation process. The expression substitution effect is achieved by selectively reselecting the transformed expression.

[0116] Local variable elimination algorithms can involve variable registration and renaming in software. The variable registration and renaming described below are merely examples, and this disclosure is not limited to them; variable registration and renaming can be of any kind. For access to structure members in expressions, clang::CallExpr, numerical access, etc., the Item is encapsulated as a small node for computation and assigned an internal_name. A DataFlowItem structure is defined to record the "Item type" and "Item attributes". The "Item type" includes clang::DeclRefExpr, clang::CallExpr, clang::MemberExpr, clang::ArraySubscripExpr, etc. The "Item attributes" can include "dereference", "l-value", "r-value", and "condition", etc. The "dereference" attribute can be passed to the "core algorithm that transforms the control flow (e.g., clangAST control flow) into the z3 solver". The "l-value", "r-value", and "condition" attributes can be used as blocking criteria when passing results to the TestCase module. A std::map is established.<std::string, DataFlowItem *> string2DataFlowItem supports the function of reverse lookup of DataFlowItem using internal_name.

[0117] Register the evaluated expressions (or simply expressions) into a std::vector in the order they appear. <clang::expr>In `assignInfoPtrs`, the evaluated expression is substituted into the conditional expression in reverse chronological order. This distinguishes between different versions of the same variable and converts the conditional expression into an expression containing only global variables and function parameters. Modifications to the clang AST in the program code may introduce problems. The "registration," "substitution," and "translation into Z3string" are mainly implemented by two mutually calling functions (i.e., `eliminateLocalVariable()` and `clangStmtToStr()`), which are algorithmically equivalent to the process described above.

[0118] The `eliminateLocalVariable()` function checks for local variables in a given expression. If local variables are present, it searches the `std::vector` in the order of newest to oldest variables. <clang::expr>If a matching element is found, the `clangStmtToStr()` function is called to translate that element; otherwise, `clangStmtToStr()` is called to translate the parameter expression. The `clangStmtToStr()` function can distinguish between different types of parameter expressions and iterate through subexpressions. If the current expression is a variable, the `eliminateLocalVariable()` function is called to attempt elimination. If not, it is broken down into subexpressions, and `clangStmtToStr()` is called to process each subexpression. Initialization assignment statements in local variable declarations are also registered to `std::vector`. <clang::expr>In `assignInfoPtrs`, if an uninitialized local variable is used in a substitution, it will default to 0 and print an error message.

[0119] Figure 9 This is a flowchart illustrating a method for transforming control flow into a z3 solver according to at least one example embodiment of the present disclosure.

[0120] exist Figure 9 In this process, by analyzing the control flow (e.g., clang-AST control flow), a string conforming to z3 syntax (i.e., z3 string) is generated. Then, the z3::solver is constructed using the z3::solver::from_string() method to perform calculations and obtain the input parameter values ​​(including global variables and stub function return values) that satisfy a specific control flow. These values ​​are then used to generate the initialization statements for unit tests.

[0121] The conditional branch nodes in the clang-AST control flow include if-else statements (clang::IfStmt) and switch-case statements (clang::SwitchStmt). Conditional statements are obtained through the getCond() function and form the basis for constructing z3 expressions. Function calls appearing in conditional statements need to be stubbed, converting them into variables representing the return values ​​of the stub functions. Local variables appearing in conditional statements need to be processed using the aforementioned "local variable elimination" algorithm, converting them into expressions composed of input parameters, global variables, and stub function return values. The constructed z3 string can be converted into a z3 solver using the z3::solver::from_string() method, and the variables within it can be solved. The solved variable values ​​can be directly used to generate unit test scheduling statements.

[0122] The input parameters, global variables, and stub function return values ​​in conditional statements may have different data types. To avoid compatibility issues when solving z3 expressions, all data types are converted to z3::bitvector, and each variable 'a' is declared in the form of ((declare-fun a () (_ BitVec 32)). After calculation, the results are converted back to the original data types. For data types shorter than 32 bits, their maximum value needs to be limited in the z3 string to avoid numerical overflow. The z3 string is divided into two parts: the first part is the variable declared with `declare-fun`, and the second part is the prefix expression conforming to z3 syntax obtained from the conditional statement conversion.

[0123] Further reference Figure 9 In operation S910, all control nodes in the control flow are traversed.

[0124] In operating S920, when the control node is IfStmt, it is determined whether IfStmt is a true branch or a false branch.

[0125] In operation S930, when IfStmt is determined to be a true branch, the variables and constants on the path are converted to bitvector data type, and the conditional statement is recursively processed starting from the operator to generate the assertion string.

[0126] When the if conditional statement is transformed, the operator is first obtained from clang-AST, converted into the string corresponding to the z3bitvector operator according to Table 4 above, and then the sub-statements of the operator are recursively transformed.

[0127] If a pointer to the operator -> exists in a conditional statement (e.g., p->a), it is evaluated as a separate variable.

[0128] If a pointer dereference operator * exists in a conditional statement, such as *p, it is treated as a separate variable and p != 0 is added to ensure that the pointer is correctly initialized in the arrangement statement.

[0129] If a structure pointer operator exists in a conditional statement, such as st.a, it is evaluated as a separate variable.

[0130] If the conditional statement contains the increment operator ++ or the decrement operator --, it will be treated as +1 or -1.

[0131] If the conditional statement contains the numeric subscript operator [], such as array[1], it will be treated as a separate variable for evaluation.

[0132] If there is an implicit boolean type conversion in the conditional statement, such as if(a), it will be treated as if(a != 0).

[0133] In operation S940, the prefix "distinct" can be added, and then operation S930 can be performed.

[0134] In the S950 operation, variables on the path can be resolved and declarations can be generated using declare-fun().

[0135] In operating S960, when the control node is SwitchStmt, the switch condition is obtained, and the variable is converted into a bitvector data type to generate an assertion string.

[0136] In S970 operation, when the next node of the control node is CaseStmt, the case value is converted to a bitvector data type to complete the assertion string.

[0137] In operating S980, when the next node of the control node is DefaultStmt, it is determined whether "the switch condition of the control node is int or the case statement of the control node has exhausted the enum value" is true.

[0138] In S990 operation, when "the switch condition of the control node is int or the case statement of the control node has exhausted the enum value" is true, a new value is selected to complete the assertion string.

[0139] In operation S991, when "the switch condition of the control node is int or the case statement of the control node has exhausted the enum value" is false, an enum value that is not used in the new case statement is selected to complete the assertion string.

[0140] In operation S992, when all control nodes have been traversed, the z3 string is converted into z3::solver and solved using the z3::solver::from_string() method.

[0141] After completing the z3 string (z3String), initialize z3::context cc; z3::solver ss(cc); call the function ss.from_string(z3String.c_str()); to convert z3String into a z3 solver. Then call ss.check(), if the return value is z3::sat, it means the solution was successful; otherwise, it means there is no solution for that path.

[0142] Call z3::model m = ss.get_model(); to get the z3 model, and iterate through the z3 model to get the solution results for each variable.

[0143] Based on the original data types of each variable, the result in z3::bitvector format is converted to the corresponding data type to complete the solution of the control flow path.

[0144] Figure 10 This is a flowchart illustrating a method for calculating the function return value after the execution of control flow according to at least one example embodiment of the present disclosure.

[0145] In the method for calculating the function return value after the execution of control flow in at least one example embodiment of this disclosure, different variables used in the function can be collected into different variable mapping tables, and the values ​​of the variables in the mapping tables can be initialized according to the given parameter values. The AST statements in the given execution path are converted into objects that can be recognized by the EXPRTK tool and calculated, and the calculation results are updated in the mapping table. Finally, the characteristics of the AST return statement are judged, and the values ​​that need to be asserted are matched and extracted. Search the AST clang::ReturnStmt to determine whether to initiate constant return value processing or variable return value processing. If the return value is a variable, then initiate the example processing below.

[0146] The `assignInputMap` is processed to add function parameters to the parameter mapping table. The AST statements in the specified path are traversed. The `collectGlobalVar` and `addLocalSymbolToMap` functions are applied to extract global variables into the global variable mapping table. Each AST statement in the path is parsed and transformed into an object schema that the EXPRTK tool can process, and the `processUsingExprtk` function is applied to obtain the settlement result. Each AST statement in the path is parsed, and the `addLocalVarByDecodeRExpr` and `updateLocalOrGlobMap` functions are applied to collect and update local variables into the local variable mapping table and update global variable values ​​into the global variable mapping table. For statements containing function calls as expressions, substitution processing is performed, converting them into symbols that the tool can recognize. After traversing all nodes on the selected path, the final function assert value update is completed.

[0147] Further reference Figure 10 In operation S1010, determine whether the return statement of the selected path is a constant expression.

[0148] In operation S1011, when the return statement of the selected path is determined to be a constant expression, the EXPRTK tool can be used to calculate the constant expression.

[0149] The result can be output in operation S1012.

[0150] In operation S1013, when it is determined that the return statement of the selected path is not a constant expression, the function parameter table is parsed. Furthermore, the parsed parameter variables can be added to the parameter variable table.

[0151] In operation S1014, the selected path statements are traversed. Additionally, the traversed global variables can be added to the global variable mapping table.

[0152] In operation S1015, the selected path statements are re-traversed.

[0153] In operation S1016, the next statement node s in the path is determined.

[0154] In operation S1017, determine whether statement node s is a variable declaration statement.

[0155] In operation S1018, when it is determined that statement node s is a variable declaration statement, it is determined whether the variable in statement node s has been initialized.

[0156] In operation S1019, when a variable in statement node s is initialized, the variable and its value in statement node s are retrieved. The retrieved variable and its value can then be added to the local variable mapping table.

[0157] In operation S1020, when a variable in statement node s is not initialized, a default value can be assigned to the variable. The variable in statement node s and the assigned default value can be added to the local variable mapping table.

[0158] In operation S1021, when it is determined that statement node s is not a variable declaration statement, it is determined whether statement node s is an assignment statement.

[0159] In operation S1022, when it is determined that statement node s is an assignment statement, substitution processing is performed based on statement node s. In one example, variable symbols that the EXPRTK tool cannot handle can be replaced (e.g., function calls used in statements or member variables in structures).

[0160] In operation S1023, the EXPRTK tool can be used to calculate the assignment statement corresponding to the statement node s after the substitution process has been performed. This calculation can use one or more of the local variable map, global variable map, and parameter variable map. Afterwards, the corresponding one of the local variable map, global variable map, and parameter variable map can be updated based on the calculation result.

[0161] In operation S1024, when it is determined that statement node s is not an assignment statement, it is determined whether statement node s is a return statement.

[0162] In operation S1025, when it is determined that statement node s is a return statement, the replacement process is performed based on statement node s.

[0163] In operation S1026, the EXPRTK tool can be used to calculate the assignment statement corresponding to the statement node s after the substitution process has been performed. This calculation can use one or more of the local variable map, global variable map, and parameter variable map.

[0164] In operation S1027, the calculation results can be output.

[0165] If it is determined in operation S1024 that statement node s is not a return statement, the process can continue to operation S1017.

[0166] Figure 11 A block diagram is shown of a unit test code generation apparatus (i.e., an apparatus for generating unit test code) according to at least one example embodiment of the present disclosure.

[0167] Reference Figure 11 The unit test code generation apparatus 1100 may include at least one memory 1110 and at least one processor 1120.

[0168] At least one memory 1110 may store one or more instructions. When executed by at least one processor, these one or more instructions are configured to cause the at least one processor to: determine one or more assignment expressions and conditional expressions in the control flow by traversing each expression of the control flow of the function under test, wherein variables in the conditional expressions are associated with at least one of the one or more assignment expressions; eliminate local variables in the conditional expressions based on at least one assignment expression; and generate unit test code corresponding to the function under test based on the conditional expressions in which local variables are eliminated. In the same or different embodiments, at least one memory 1110 may also store unit test code corresponding to the function under test.

[0169] Reference Figures 1 to 10 One or more of the described unit test code generation methods can be executed by the unit test code generation apparatus 1100 (e.g., at least one processor 1120 in the unit test code generation apparatus 1100). Therefore, for the sake of brevity, repeated descriptions will be omitted.

[0170] In one embodiment, the unit test code generation apparatus 1100 may be electrically or communicatively connected to a monitor or display device to display and / or edit the generated unit test code. It will be understood that those skilled in the art have knowledge of implementing the unit test code generation apparatus 1100 according to the embodiments disclosed herein.

[0171] The unit test code generation method according to at least one example embodiment of the present disclosure can generate high-quality unit test code by eliminating local variables in conditional expressions and generating unit test code corresponding to the function under test based on the conditional expressions in which local variables are eliminated.

[0172] According to at least one example embodiment of this disclosure, the z3 expression can be converted into a z3 string before performing the z3 solving process. Since the easier-to-process z3 string is used instead of the more difficult-to-process z3 expression for solving, the processing difficulty can be reduced.

[0173] In the method for generating unit test code corresponding to the function under test according to at least one example embodiment of the present disclosure, high-quality test code can be constructed because the logical relationship contained in the conditional statement can be converted into a z3 prover expression and calculated to obtain various parameters (e.g., input parameter values ​​and stub function return values) for constructing test cases.

[0174] In at least one example embodiment of this disclosure, all data types are converted to z3::bitvector to avoid compatibility issues when solving z3.

[0175] In at least one example embodiment of this disclosure, the result is converted to a primitive data type after the calculation is completed. For data types shorter than 32 bits, the maximum value needs to be limited in the z3 string to avoid numerical overflow issues.

[0176] In at least one example embodiment of this disclosure, the validity of the calculation is guaranteed because the expression can be transformed into an object expression that the tool can process and the variable mapping table is updated in real time, thereby providing high-quality test code quickly.

[0177] Embodiments of this disclosure provide a computer-readable storage medium storing a computer program or instructions that, when executed by at least one processor, can perform or implement the steps and corresponding content of the foregoing method embodiments.

[0178] Embodiments of this disclosure also provide a computer program product, including a computer program that, when executed by a processor, can implement the steps and corresponding content of the foregoing method embodiments.

[0179] The terms "first," "second," "third," "fourth," "1," "2," etc. (if present) in the specification, claims, and accompanying drawings of this disclosure are used to distinguish similar objects and are not necessarily used to describe a particular order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this disclosure described herein can be implemented in a sequence other than that shown in the figures or text.

[0180] It should be understood that although arrows indicate various operation steps in the flowcharts of the embodiments of this disclosure, the order in which these steps are implemented is not limited to the order indicated by the arrows. Unless explicitly stated herein, in some implementation scenarios of the embodiments of this disclosure, the implementation steps in each flowchart can be executed in other orders as required. Furthermore, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on the actual implementation scenario. Some or all of these sub-steps or stages can be executed at the same time, and each sub-step or stage can also be executed at different times. In scenarios where execution times differ, the execution order of these sub-steps or stages can be flexibly configured as required, and the embodiments of this disclosure do not limit this.< / clang::expr> < / clang::expr> < / clang::expr>

Claims

1. A method for generating unit test code, the method being executed by one or more processors, the method comprising: One or more assignment expressions and condition expressions in the control flow are determined by traversing each expression of the control flow of the function under test, wherein the variable in the condition expression is associated with at least one of the one or more assignment expressions; Eliminate local variables in conditional expressions based on at least one assignment expression; and Unit test code corresponding to the function under test is generated based on the conditional expression in which local variables are eliminated.

2. The method according to claim 1, wherein, The steps to eliminate local variables in a conditional expression include: The one or more assignment expressions and conditional expressions are stored sequentially in an expression queue based on their order of appearance in the control flow; and Local variables in the conditional expression are eliminated by sequentially substituting the at least one assignment expression preceding the conditional expression into the conditional expression through a reverse traversal of the expression queue.

3. The method according to claim 1, wherein, The steps for generating unit test code corresponding to the function under test include: Generate strings that conform to z3 syntax based on conditional expressions where local variables are eliminated; The z3 solver is used to solve for variable values ​​in strings that conform to z3 syntax; and The arrangement statements in the unit test code are generated based on the solved variable values.

4. The method according to claim 3, wherein, The steps to generate a string that conforms to z3 syntax include: Generate variable declaration strings corresponding to the variables in the conditional expressions where local variables are eliminated; and Operators, variables, and constants in conditional expressions where local variables are eliminated are converted into assertion strings conforming to z3 syntax. The operator conversion is based on a predetermined conversion table, which includes information indicating the conversion relationships between operators with the same logic and z3 strings. Among them, strings that conform to z3 syntax include variable declaration strings and assertion strings.

5. The method according to claim 4, wherein, The conversion steps include: Determine whether the control node in the control flow corresponding to the conditional expression is IfStmt; Based on the determination that the control node is IfStmt and that the control node is a true branch, operators, variables, and constants in the conditional expressions where local variables are eliminated are converted into assertion strings conforming to z3 syntax. Based on the determination that the control node is IfStmt and that the control node is a false branch, add the prefix distinct and convert the operators, variables, and constants in the conditional expressions where local variables are eliminated into assertion strings that conform to z3 syntax.

6. The method according to claim 3, wherein, The steps to generate a string conforming to z3 syntax based on a conditional expression where local variables are eliminated include: Determine whether the control node in the control flow corresponding to the conditional expression is a SwitchStmt; Based on the determination that the control node is SwitchStmt, the operators, variables and constants in the conditional expressions where local variables are eliminated are converted into assertion strings that conform to z3 syntax, and it is determined whether the next node of the control node is CaseStmt or DefaultStmt. Based on the determination that the next node of the control node is CaseStmt, the assertion string conforming to z3 syntax is supplemented with the case value of the next node; and Based on the determination that the next node of the control node is DefaultStmt and based on the control node's switch condition being int or the control node's case statement exhausting enum values, the assertion string conforming to z3 syntax is supplemented with a new value; and Based on the fact that the next node of the control node is DefaultStmt and the fact that the switch condition of the control node is not int and the case statement has not exhausted the enum value, the assertion string that conforms to the z3 syntax is supplemented with the unused enum value of the case statement.

7. The method according to claim 3, wherein, The steps for generating unit test code corresponding to the function under test also include: Before evaluating the variable values ​​in strings conforming to z3 syntax, the original data type of the data in the strings is converted to z3::bitvector, where the data includes input parameters from conditional statements in the control flow, global variables, and stub function return values; and Before generating the arrangement statements in the unit test code, restore the data type of the solved variable values ​​to the original data type corresponding to the solved variable values.

8. The method according to claim 3, wherein, The steps for generating unit test code corresponding to the function under test also include: The return value of the function under test is calculated based on the solved variable values ​​and the C++ mathematical expression library; and Assertions in unit test code are generated based on the return value of the function under test.

9. The method according to claim 8, wherein, The steps for calculating the return value of the function being tested include: The parameter variable mapping table and the global variable mapping table are generated based on the traversal of the control flow. The parameter variable mapping table includes the parameters and their values ​​in the control flow, and the global variable mapping table includes the global variables and their values. The parameter values ​​correspond to the solved variable values. Traverse the statement nodes of the control flow and determine the statement type of the traversed statement nodes. The statement type of the statement node based on traversal is the variable declaration statement. The value of the declared variable in the variable declaration statement is determined, and the declared variable and its value are added to the local variable mapping table. The statement type for traversed statement nodes is an assignment statement. The left-hand side of the assignment expression corresponding to the traversed statement node is calculated using at least one of the parameter variable map, global variable map, and local variable map, along with the C++ mathematical expression library. The left-hand side is then updated in the corresponding map within the parameter variable map, global variable map, and local variable map. The statement type based on the traversal of statement nodes is a return statement. The expression corresponding to the traversed statement node is calculated by using at least one of the parameter variable map, global variable map, and local variable map, as well as the C++ mathematical expression library, and the calculation result of the expression corresponding to the traversed statement node is output as the return value of the function under test.

10. An apparatus for generating unit test code, the apparatus comprising: At least one processor, and Memory, which stores one or more instructions. Wherein, when the one or more instructions are executed by the at least one processor, the at least one processor: One or more assignment expressions and condition expressions in the control flow are determined by traversing each expression of the control flow of the function under test, wherein the variable in the condition expression is associated with at least one of the one or more assignment expressions; Local variables in conditional expressions are eliminated based on at least one assignment expression; and Unit test code corresponding to the function under test is generated based on the conditional expression in which local variables are eliminated.