Unit test case generation method and device, equipment and storage medium

By evaluating and encapsulating the program code to improve code coupling, and generating unit test cases through semantic analysis, the problem of test instability caused by insufficient training data was solved, and the coverage and accuracy of unit tests were improved.

CN121501646APending Publication Date: 2026-02-10ANHUI SANQI JIYU NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511362359.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-23
Publication Date
2026-02-10

AI Technical Summary

Technical Problem

Existing unit test case generation methods suffer from insufficient or biased training data, resulting in a lack of guaranteed stability and reliability of test cases. Traditional manual writing methods consume a lot of time and manpower, affecting project development progress.

Method used

By evaluating the code coupling level of the program code, encapsulating and processing code segments with strong coupling, and combining semantic analysis to determine test boundary values ​​and scenarios, unit test cases are generated.

Benefits of technology

It improves the coverage and accuracy of unit test cases, reduces code coupling and redundant dependencies, and enhances the comprehensiveness and efficiency of test cases.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121501646A_ABST
    Figure CN121501646A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses a unit test case generation method and device, equipment and a storage medium, and the method comprises the steps: receiving a to-be-tested program code, carrying out the structure analysis of the program code, and determining the code dependency information of the program code and the business type corresponding to each code snippet in the program code; performing code coupling degree grade evaluation on the program code according to the code dependency relationship information to obtain a code coupling degree grade, and under the condition that the code coupling degree grade is greater than a preset risk grade, performing packaging processing on code snippets of the same service type in the program code to obtain a plurality of service code sets; and performing semantic analysis on each service code set to determine a test boundary value and a test scene, and generating a unit test case corresponding to each service code set based on the test boundary value and the test scene. The code coupling degree and redundant dependence between codes are reduced, and the comprehensiveness and accuracy of the unit test case are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device and storage medium for generating unit test cases. Background Technology

[0002] In the software development process, unit testing plays a crucial role as a fundamental step in ensuring software quality. Against the backdrop of the booming software industry, the continuous expansion of project scale, and the increasing complexity of business logic, unit testing faces many severe challenges. The traditional method of manually writing unit test cases requires developers to spend a lot of time and energy. Developers not only need to deeply understand the business logic, but also need to carefully design test cases and write corresponding test code for different input and output scenarios, boundary conditions, and abnormal situations, which greatly delays the project development progress and increases the investment of human resources.

[0003] In related technologies, in order to improve the efficiency of writing unit test code, models such as neural networks are usually used to learn from a large amount of code and test case data, in an attempt to establish a mapping relationship between code structure and test cases, thereby realizing the automatic generation of test cases. However, the quality and scale of training data are extremely important. If the training data is insufficient or biased, the coverage of test cases cannot be guaranteed, which in turn affects the stability and reliability of unit test cases. Summary of the Invention

[0004] This application provides a unit test case generation method, apparatus, device, and storage medium, which solves the problem that existing unit test case generation methods cannot guarantee the stability and reliability of test cases due to insufficient or biased training data. By evaluating the code coupling level and encapsulating highly coupled code, the code coupling and redundant dependencies between codes are reduced. By performing semantic analysis on each business code set to determine test boundary values ​​and test scenarios, corresponding implicit constraints can be accurately discovered, improving the coverage of unit test cases, and consequently, improving the comprehensiveness and accuracy of test cases.

[0005] In a first aspect, embodiments of this application provide a method for generating unit test cases, comprising: Receive the program code to be tested, perform structural analysis on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; The program code is evaluated for its code coupling level based on the code dependency information to obtain a code coupling level. If the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets. Semantic analysis is performed on each of the business code sets to determine test boundary values ​​and test scenarios, and unit test cases corresponding to each of the business code sets are generated based on the test boundary values ​​and test scenarios.

[0006] Optionally, the step of performing semantic analysis on each of the business code sets to determine test boundary values ​​and test scenarios includes: Semantic analysis is performed on the business code set to determine the target variable, the explicit constraints of the target variable, and the first test scenario corresponding to the explicit constraints; The explicit constraints are extended to obtain the implicit constraints corresponding to the explicit constraints. The implicit constraints are semantically matched with the explicit constraints, and the second test scenario corresponding to the implicit constraints is determined based on the matching results and the first test scenario. The test boundary value is determined based on the test range corresponding to the explicit or implicit constraints and the target variable.

[0007] Optionally, the step of expanding the explicit constraint to obtain the implicit constraint corresponding to the explicit constraint includes: The explicit constraints are subjected to semantic expansion, complement expansion, and data association expansion to obtain the implicit constraints corresponding to the explicit constraints.

[0008] Optionally, determining the test boundary value based on the test range corresponding to the explicit or implicit constraints and the target variable includes: Determine the effective boundary values ​​of the test range corresponding to the explicit or implicit constraints, as well as the unit variable parameters corresponding to the target variable; Calculate invalid boundary values ​​and valid intermediate values ​​based on the valid boundary values ​​and the unit variable parameters, and determine the valid boundary values, invalid boundary values, and intermediate values ​​as test boundary values.

[0009] Optionally, the code dependency information includes code dependency type, code dependency quantity, and code dependency path. The step of evaluating the code coupling level of the program code based on the code dependency information includes: The code coupling degree is calculated based on the code dependency type, the number of code dependencies, the path length of the code dependency path, and the corresponding complexity weight coefficient, and the code coupling degree level to which the code coupling degree belongs is determined.

[0010] Optionally, the step of performing structural parsing on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code includes: The program code is subjected to structural parsing. Based on the structural parsing results, the data flow, control flow, and business types corresponding to code elements in each code segment are determined. Code dependency information is generated based on the data flow and control flow.

[0011] Optionally, after generating unit test cases corresponding to each of the business code sets based on the test boundary values ​​and the test scenario, the method further includes: Coverage is evaluated for each unit test case based on a preset evaluation dimension to obtain a coverage evaluation value. If the coverage evaluation value is less than the coverage threshold, the corresponding unit test case is optimized to obtain the first target test case. Redundancy and invalidity checks are performed on the first target test case. Based on the detection results, redundant and invalid test cases are deleted to obtain the second target test case.

[0012] In a second aspect, embodiments of this application provide a unit test case generation apparatus, comprising: The structure parsing module is used to receive the program code to be tested, perform structure parsing on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; The coupling level assessment module is used to assess the code coupling level of the program code based on the code dependency information, and obtain the code coupling level. The encapsulation processing module is used to encapsulate code segments of the same business type in the program code when the code coupling level is greater than the preset risk level, so as to obtain multiple business code sets. The semantic parsing module is used to perform semantic analysis on each of the aforementioned business code sets to determine test boundary values ​​and test scenarios; The unit test case generation module is used to generate unit test cases corresponding to each of the business code sets based on the test boundary values ​​and the test scenarios.

[0013] In a third aspect, embodiments of this application provide an electronic device, the device comprising: one or more processors; and a storage device configured to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the unit test case generation method described in the first aspect.

[0014] In a fourth aspect, embodiments of this application provide a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform the unit test case generation method as described in the first aspect.

[0015] This application embodiment receives the program code to be tested, performs structural analysis to determine the code dependency information and the business type corresponding to each code segment in the program code; evaluates the code coupling level based on the code dependency information to obtain the code coupling level; if the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets; semantic analysis is performed on each business code set to determine test boundary values ​​and test scenarios, and unit test cases are generated for each business code set based on the test boundary values ​​and test scenarios. In the above scheme, by evaluating the code coupling level of the program code and encapsulating the code with strong coupling, the code coupling and redundant dependencies between codes are reduced; by performing semantic analysis on each business code set to determine test boundary values ​​and test scenarios, the corresponding implicit constraints can be accurately discovered, improving the coverage of unit test cases, and consequently improving the comprehensiveness and accuracy of test cases. Attached Figure Description

[0016] Figure 1 This is a flowchart of a unit test case generation method provided in an embodiment of this application; Figure 2 This is a flowchart illustrating a method for determining test boundary values ​​and test scenarios provided in an embodiment of this application; Figure 3 This is a flowchart of a method for determining test boundary values ​​provided in an embodiment of this application; Figure 4 This is a schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application; Figure 5 This is a schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of this application clearer, specific embodiments of this application will be described in further detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are merely for explaining this application and not for limiting it. It should also be noted that, for ease of description, only the parts relevant to this application are shown in the drawings, not all of them. Before discussing exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe operations (or steps) as sequential processes, many of these operations can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the operations can be rearranged. The process can be terminated when its operation is completed, but may also have additional steps not included in the drawings. The process can correspond to a method, function, procedure, subroutine, subprogram, etc.

[0018] The technical solutions of the embodiments of this application will be clearly described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application are within the scope of protection of this application.

[0019] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.

[0020] The unit test case generation method, apparatus, device, and medium provided in this application will be described in detail below with reference to the accompanying drawings, through specific embodiments and application scenarios.

[0021] The unit test case generation method provided in this application is used in scenarios where the developed program is tested during the software development process. Based on the above application scenario, it can be understood that the execution subject of each step can be a computer device. This computer device refers to any electronic device with data computing, processing, and storage capabilities, such as mobile phones, PCs (Personal Computers), tablet computers, and other terminal devices, or it can be a server or other devices. This application does not limit the scope of the method.

[0022] Figure 1 This is a flowchart of a unit test case generation method provided in an embodiment of this application, such as... Figure 1 As shown, it includes: Step S101: Receive the program code to be tested, perform structural analysis on the program code to determine the code dependency information and the business type of each code segment in the program code.

[0023] The program code to be tested can refer to the source code or executable code of a program that needs to be verified for its correctness, reliability, performance, and other indicators through various testing methods. Code dependency information can be a description of the dependency relationships between different code modules, components, libraries, or programs in software development, such as the relationship between modules or functions. A code snippet can refer to a section of code within a complete program that has a relatively independent function or logic; it is a portion of code extracted for a specific purpose. The business type corresponding to the code snippet can refer to the specific business function implemented by the code snippet, the business domain it belongs to, or the business scenario of the service it provides, such as e-commerce transactions and user management.

[0024] In one embodiment, the program code uploaded by the developer that needs to be tested can be parsed by an AST (Abstract Syntax Tree Parser) to determine the dependency relationships between different code modules, components, libraries or programs, and to determine the corresponding business type based on the method names in each code segment.

[0025] In another embodiment, structural parsing of the program code is performed to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code. This includes: performing structural parsing on the program code respectively, determining the data flow, control flow and the business type corresponding to the code elements in each code segment based on the structural parsing results, and generating code dependency information based on the data flow and control flow.

[0026] Data flow refers to the transmission, processing, and transformation of data within a program, describing the flow path and state changes of data between variables, expressions, and functions / methods. Control flow refers to the order of statement execution and branching logic, determining which path the code follows, how many times it is repeated, and whether a certain section of code is executed. Code elements refer to the basic building blocks of code. In one embodiment, the program code is structurally parsed using an Abstract Syntax Tree (AST), a Control Flow Graph (CFG), and a Data Flow Graph (DFG) to identify data flow identifiers and control flow identifiers in the program code. Data flow identifiers may include data origin identifiers (SOURCE_INPUT (user input data, such as the input() function)), data destination identifiers (SINK_OUTPUT (output to the console, such as print())), data transformation identifiers (TRANSFORM_CALC calculation transformation, such as a + b), and data state identifiers (STATE_RAW (raw, unprocessed data)). Control flow identifiers may include control structure identifiers (FLOW_BRANCH_IF (if branch, such as if condition:)), condition identifiers (CONDITION_TRUE (execution path when the condition is true)), and call relationship identifiers (CALL_ENTER (entering the called function, such as entering B when A() calls B())). The target input data, the calling relationship between data, and the output result are determined based on the data flow identifier. The code branch is determined based on the control flow identifier. Thus, code dependency information is generated based on the input data, the calling relationship between data, the output result, and the code branch. The corresponding business type is determined by naming or commenting each code element.

[0027] This application embodiment performs structural analysis on the program code, determines the data flow, control flow, and business types corresponding to code elements in each code segment based on the analysis results, and generates code dependency information based on the data flow and control flow. In the above scheme, identifying data flow, control flow, and code elements improves the accuracy of determining the program code's hierarchical structure, facilitates the determination of multi-dimensional business collaboration relationships, and also reduces the efficiency and accuracy of parsing complex program code.

[0028] Step S102: Evaluate the code coupling level of the program code based on the code dependency information to obtain the code coupling level. If the code coupling level is greater than the preset risk level, encapsulate the code segments of the same business type in the program code to obtain multiple business code sets.

[0029] Code coupling level can be used as a grading standard to measure the strength of dependencies between different modules in a software system. It reflects the tightness of the interrelationships and mutual influences between modules or code segments. A higher level means stronger dependencies between modules, weaker independence, and greater difficulty in code modification, maintenance, and extension. Encapsulation refers to binding data and the methods that operate on that data into an independent unit, hiding internal implementation details through access control mechanisms, and exposing only necessary interfaces for external interaction. This effectively isolates code segments and reduces coupling between them.

[0030] In one embodiment, the code coupling level is determined based on the dependency hierarchy in the code dependency information. For example, if the dependency relationship between modules or code snippets is A→B→C→D, the dependency hierarchy is level 4. The code coupling level corresponding to the dependency relationship is determined based on a pre-set mapping relationship between dependency hierarchy and code coupling level. For instance, if the dependency hierarchy is no more than 2 levels, the corresponding dependency hierarchy is level 1; if the dependency hierarchy is 3-4 levels, the corresponding dependency hierarchy is level 2; and if the dependency hierarchy exceeds 4 levels, the corresponding dependency hierarchy is level 3. Therefore, the code coupling level corresponding to the dependency relationship A→B→C→D is level 2. After determining the code coupling level, it is checked whether the code coupling is greater than a preset risk level. If it is greater, the coupling is high, which will affect the effectiveness of unit tests. In this case, the code snippets are encapsulated according to their business type to reduce the coupling of the program code, resulting in multiple encapsulated business code sets. For example, code snippets related to e-commerce order business are encapsulated to obtain the code set corresponding to e-commerce order business.

[0031] In one embodiment, code dependency information includes code dependency type, code dependency quantity, and code dependency path. The program code is evaluated for code coupling level based on the code dependency information, including: calculating code coupling degree based on code dependency type, code dependency quantity, path length of code dependency path and corresponding complexity weight coefficient, and determining the code coupling degree to which the code coupling degree belongs.

[0032] In this context, code dependency type refers to different types of dependency relationships in a software project. It is mainly used to describe the classification of code dependencies during project operation or development, and can include circular dependencies and multi-level indirect dependencies. The number of code dependencies can refer to the total number of other modules that a module (such as a class, function, file, component, etc.) directly depends on, or it can be extended to include the total number of indirect dependencies. A code dependency path can refer to one or more chain-like paths formed from a module (or component, function, etc.) to its dependent modules, and then to other modules that these dependent modules depend on. It can be used to describe the dependency transitive relationship between different modules in the code. For example, the dependency path from module A to module D is: Module A → Module B → Module C → Module D. In one embodiment, a pre-defined mapping relationship between code dependency types and complexity values ​​determines the first complexity corresponding to a code dependency type, and a first coupling value is calculated based on the first complexity and its corresponding complexity weight coefficient. A pre-defined mapping relationship between the number of code dependencies and complexity values ​​determines the second complexity corresponding to the number of code dependencies, and a second coupling value is calculated based on the second complexity and its corresponding complexity weight coefficient. A pre-defined mapping relationship between the length of a code dependency path and complexity values ​​determines the third complexity corresponding to a code dependency path, and a third coupling value is calculated based on the third complexity and its corresponding complexity coefficient. The first, second, and third coupling values ​​are then summed to obtain the code coupling degree of the program code. In another embodiment, different code coupling degree ranges are pre-defined to correspond to different code coupling degree levels. After calculating the code coupling degree of the program code, the range to which the code coupling degree belongs can be determined first, and the code coupling degree level corresponding to the program code can be determined based on the pre-defined mapping relationship between the code coupling degree range and the code coupling degree level.

[0033] This application's embodiments calculate code coupling degree based on code dependency type, code dependency quantity, code dependency path length, and corresponding complexity weight coefficients, and determine the code coupling degree level to which the code coupling degree belongs. The above scheme comprehensively considers the impact of code dependency type, code dependency quantity, and code dependency path on code coupling degree from multiple dimensions, improving the accuracy of determining the code coupling degree level.

[0034] Step S103: Perform semantic analysis on each business code set to determine test boundary values ​​and test scenarios, and generate unit test cases corresponding to each business code set based on the test boundary values ​​and test scenarios.

[0035] In software testing, test boundary values ​​refer to critical or extreme values ​​for input conditions or output results, as well as special values ​​that just exceed or fall below these critical values. Test scenarios can refer to a collection of specific scenarios covered by test cases, including software functions, business processes, input / output conditions, abnormal situations, and operating environments, such as "null value verification," "user does not exist," "incorrect password," and "verification successful." Unit test cases refer to a set of specific test scenarios designed to verify the logical correctness of the smallest testable unit in software, typically including elements such as input data, execution steps, and expected results.

[0036] In one embodiment, semantic analysis of the program code in each business code set is performed using Natural Language Processing (NLP) technology to determine the business logic of the code, and based on the business logic, corresponding verification conditions, abnormal situations or operating environments are determined. The boundary values ​​of the verification conditions are extracted, and the determined test boundary values ​​and test scenarios are output to a pre-set unit test case generation model to obtain unit test cases corresponding to each business code set.

[0037] This application embodiment receives the program code to be tested, performs structural analysis to determine the code dependency information and the business type corresponding to each code segment in the program code; evaluates the code coupling level based on the code dependency information to obtain the code coupling level; if the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets; semantic analysis is performed on each business code set to determine test boundary values ​​and test scenarios, and unit test cases are generated for each business code set based on the test boundary values ​​and test scenarios. In the above scheme, by evaluating the code coupling level of the program code and encapsulating the code with strong coupling, the code coupling and redundant dependencies between codes are reduced; by performing semantic analysis on each business code set to determine test boundary values ​​and test scenarios, the corresponding implicit constraints can be accurately discovered, improving the coverage of unit test cases, and consequently improving the comprehensiveness and accuracy of test cases.

[0038] In one embodiment, after generating unit test cases corresponding to each business code set based on test boundary values ​​and test scenarios, the method further includes: performing coverage evaluation on each unit test case based on a preset evaluation dimension to obtain a coverage evaluation value; performing coverage optimization processing on the corresponding unit test case when the coverage evaluation value is less than the coverage threshold to obtain a first target test case; performing redundancy detection and invalidity detection on the first target test case; and deleting redundant and invalid test cases based on the detection results to obtain a second target test case.

[0039] The preset evaluation dimensions may include statement coverage, branch coverage, condition coverage, path coverage, function coverage, and basic block coverage. In one embodiment, statement coverage, branch coverage, condition coverage, path coverage, function coverage, and basic block coverage are calculated based on the preset evaluation dimensions. Taking statement coverage as an example: statement coverage = (number of executed statements / total number of statements) * 100%, branch coverage = (number of executed branches / total number of branches) * 100%, and so on. Each coverage and its cumulative value are calculated sequentially. The cumulative value of each coverage is determined as the coverage evaluation value, and the coverage evaluation value is compared with a preset coverage threshold. If the coverage evaluation value is less than the coverage threshold, the corresponding unit test cases are optimized to obtain the first target test cases with high coverage, such as improving the coverage of test cases by refining condition coverage. For example, for compound conditions (such as a && b, c || d), supplementary test cases cover all true / false combinations of sub-conditions. For instance, the condition (x>5) && (y<10) needs to cover four cases: (true, true), (true, false), (false, true), and (false, false), ensuring the logical correctness of each sub-condition. After obtaining the first target test cases, redundancy and invalidity checks are performed on them. Based on the check results, redundant and invalid test cases are deleted to obtain the second target test cases.

[0040] This application embodiment evaluates the coverage of each unit test case based on a preset evaluation dimension to obtain a coverage evaluation value. If the coverage evaluation value is less than the coverage threshold, the corresponding unit test case is optimized to obtain a first target test case. Redundancy and invalidity detection are performed on the first target test case. Based on the detection results, redundant and invalid test cases are deleted to obtain a second target test case. This approach can ensure the coverage of the generated unit test cases while significantly improving testing efficiency and quality.

[0041] Figure 2 This is a flowchart illustrating a method for determining test boundary values ​​and test scenarios provided in an embodiment of this application. Figure 2 As shown, it includes: Step S1031: Perform semantic analysis on the business code set to determine the target variable, the explicit constraints of the target variable, and the first test scenario corresponding to the explicit constraints.

[0042] Step S1032: Extend the explicit constraints to obtain the implicit constraints corresponding to the explicit constraints.

[0043] Step S1033: Perform semantic matching between implicit constraints and explicit constraints, and determine the second test scenario corresponding to the implicit constraints based on the matching results and the first test scenario.

[0044] Step S1034: Determine the test boundary values ​​based on the test range and target variable corresponding to the explicit or implicit constraints.

[0045] In this context, the target variable can refer to a core variable, such as `age`, whose value or state needs to be verified through testing during the logical execution of the code snippet to ensure it meets expectations. Explicit constraints refer to limitations, value ranges, state rules, or relationships with other variables of the variable being tested, directly specified in the code snippet through explicit logic, comments, type definitions, or rule descriptions. Implicit constraints refer to limitations, value rules, or state requirements on variables that are not explicitly declared through code logic, comments, or documentation, but are implicit in the business context, contextual logic, historical implementation, or interactions with other modules, requiring deduction to identify. It is understood that each explicit constraint can correspond to one or more implicit constraints.

[0046] In one embodiment, semantic analysis of the business code set is performed using natural language processing technology to understand the functional intent, variable roles, and logical relationships of the code. From the semantic logic of the code, target variables that directly reflect functional correctness and corresponding test scenarios are located. For example, semantic analysis of the business code set determines its execution logic as "users aged 18 or older can open a credit card account." Therefore, the target variable is determined to be "user age," with an explicit constraint of "user age ≥ 18 years old." Correspondingly, the test range for this explicit constraint is ≥ 18, and the test boundary value is 18. Based on business common sense, its implicit constraint could be "age cannot exceed 150 years old," with a test range ≤ 150 and a test boundary value of 150. Alternatively, based on pre-set system restrictions, its implicit constraint could be "age must be verified through the real-name authentication interface, i.e., consistent with the age on the ID card," with the corresponding test boundary value being the ID card age, such as 17, and the corresponding test range being equal to 17. Semantic matching of explicit and implicit constraints is performed. Based on the explicit and implicit constraints in the example above, the explicit constraint restricts the age required to open a credit card, corresponding to the first test scenario of "verification successful." The implicit constraint, "age cannot exceed 150 years old," restricts the age for not opening a credit card. Since the implicit constraint does not semantically match the explicit constraint, the second test scenario corresponding to the implicit constraint is the opposite of the first test scenario, and the second test scenario is "verification failed." If the implicit constraint, "age must be verified through the real-name authentication interface," is a condition for opening a credit card, and semantically matches the explicit constraint, then the second test scenario corresponding to the implicit constraint is the same as the first test scenario corresponding to the explicit constraint, both being "verification successful."

[0047] This application embodiment determines the target variable, its explicit constraints, and the corresponding first test scenario by performing semantic analysis on the business code set; it then expands the explicit constraints to obtain the corresponding implicit constraints; it performs semantic matching between the implicit and explicit constraints, and determines the second test scenario based on the matching result and the first test scenario; finally, it determines the test boundary values ​​based on the test range corresponding to the explicit or implicit constraints and the target variable. In this scheme, the explicit constraints of the target variable can be determined through semantic analysis of the business code set, and the corresponding implicit constraints can be expanded to obtain the corresponding implicit constraints, thereby improving the coverage and accuracy of test cases.

[0048] In one possible embodiment, the implicit constraints corresponding to the explicit constraints are obtained by performing condition expansion on the explicit constraints, including: performing semantic expansion, complement expansion and data association expansion on the explicit constraints respectively to obtain the implicit constraints corresponding to the explicit constraints.

[0049] For example, if the explicit constraint is "age≥18return True", the target variable can be determined as "user age" based on the semantics of this explicit constraint. Therefore, "user age" should be a valid numerical value, non-negative, and not a string. Thus, by semantically expanding this explicit constraint, the implicit constraints obtained are "if (age<0)returnFalse" and "if isinstance(age, str)return False". The implicit constraint obtained by complementing this explicit constraint is "if age < 18 return False". Data association expansion of this explicit constraint can be performed by determining the corresponding implicit constraints based on the pre-set associated data of the target variable. For example, if the data associated with user age is ID card information, then the implicit constraint "if notisinstance(input_age, (int, float)) or isinstance(input_age, bool) or input_age<0return False" can be obtained by association expansion of this explicit constraint.

[0050] This application's embodiments obtain implicit constraints corresponding to explicit constraints by performing semantic expansion, complement expansion, and data association expansion on explicit constraints respectively. This allows for sufficient expansion of explicit constraints based on multiple dimensions, improving the comprehensiveness of test conditions and consequently increasing the test coverage and accuracy of test cases.

[0051] Figure 3 This is a flowchart of a method for determining test boundary values ​​provided in an embodiment of this application, as shown below. Figure 3 As shown, it includes: Step S10341: Determine the effective boundary values ​​of the test range corresponding to the explicit or implicit constraints, as well as the unit variable parameters corresponding to the target variable.

[0052] Step S10342: Calculate the invalid boundary value and the valid intermediate value based on the valid boundary value and the unit variable parameter, and determine the valid boundary value, invalid boundary value and intermediate value as the test boundary value.

[0053] Among them, valid boundary values ​​refer to specific numerical values ​​or conditions at critical positions within the system's defined valid input range. These values ​​are the boundary points of the valid input range and represent critical situations that the system is expected to handle correctly. Invalid boundary values ​​refer to critical values ​​that exceed the system's defined valid input range but are very close to the valid boundary. These values ​​themselves do not conform to the input conditions defined in the requirements specification, but because they are very close to the valid boundary, they are key objects for testing the system's logic for handling invalid inputs outside the boundary. Intermediate values ​​refer to numerical values ​​or conditions far from the boundary within the system's defined valid input range, representing "normal / typical input situations." These values ​​are not critical boundaries but rather regular values ​​within the valid range, reflecting the input scenarios most likely to be used by the user. Unit variable parameters refer to the smallest granularity of change used when adjusting the target variable. For example, if the test range of an explicit or implicit constraint is ≥18, the corresponding valid boundary value is 18, and since the target variable is the user's age, and the unit variable parameter for age is 1, the invalid boundary value corresponding to this explicit constraint can be determined to be 17, and the intermediate value to be 9.

[0054] This application's embodiments determine the effective boundary values ​​of the test range corresponding to explicit or implicit constraints, as well as the unit variable parameters corresponding to the target variable; calculate invalid boundary values ​​and effective intermediate values ​​based on the effective boundary values ​​and unit variable parameters, and determine the effective boundary values, invalid boundary values, and intermediate values ​​as test boundary values. This can eliminate the situation where only the boundary values ​​are correct but the intermediate values ​​are abnormal, and can also avoid logical omissions and redundancies, thus fully ensuring the accuracy of the test.

[0055] Figure 4 This is a schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application, as shown below. Figure 4 As shown, it includes: The structure parsing module 21 is used to receive the program code to be tested, perform structure parsing on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; Coupling degree level assessment module 22 is used to assess the code coupling degree of the program code based on the code dependency information, and obtain the code coupling degree level. Encapsulation processing module 23 is used to encapsulate code segments of the same business type in the program code when the code coupling level is greater than the preset risk level, so as to obtain multiple business code sets. Semantic parsing module 24 is used to perform semantic analysis on each of the business code sets to determine test boundary values ​​and test scenarios; The unit test case generation module 25 is used to generate unit test cases corresponding to each of the business code sets based on the test boundary values ​​and the test scenarios.

[0056] This application embodiment receives the program code to be tested, performs structural analysis to determine the code dependency information and the business type corresponding to each code segment in the program code; evaluates the code coupling level based on the code dependency information to obtain the code coupling level; if the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets; semantic analysis is performed on each business code set to determine test boundary values ​​and test scenarios, and unit test cases are generated for each business code set based on the test boundary values ​​and test scenarios. In the above scheme, by evaluating the code coupling level of the program code and encapsulating the code with strong coupling, the code coupling and redundant dependencies between codes are reduced; by performing semantic analysis on each business code set to determine test boundary values ​​and test scenarios, the corresponding implicit constraints can be accurately discovered, improving the coverage of unit test cases, and consequently improving the comprehensiveness and accuracy of test cases.

[0057] In one possible embodiment, the semantic parsing module 24 is used for: Semantic analysis is performed on the business code set to determine the target variable, the explicit constraints of the target variable, and the first test scenario corresponding to the explicit constraints; The explicit constraints are extended to obtain the implicit constraints corresponding to the explicit constraints. The implicit constraints are semantically matched with the explicit constraints, and the second test scenario corresponding to the implicit constraints is determined based on the matching results and the first test scenario. The test boundary value is determined based on the test range corresponding to the explicit or implicit constraints and the target variable.

[0058] In one possible embodiment, the semantic parsing module 24 is specifically used for: The explicit constraints are subjected to semantic expansion, complement expansion, and data association expansion to obtain the implicit constraints corresponding to the explicit constraints.

[0059] In one possible embodiment, the semantic parsing module 24 is specifically used for: Determine the effective boundary values ​​of the test range corresponding to the explicit or implicit constraints, as well as the unit variable parameters corresponding to the target variable; Calculate invalid boundary values ​​and valid intermediate values ​​based on the valid boundary values ​​and the unit variable parameters, and determine the valid boundary values, invalid boundary values, and intermediate values ​​as test boundary values.

[0060] In one possible embodiment, the code dependency information includes code dependency type, code dependency quantity, and code dependency path, and the coupling degree level evaluation module 22 is specifically used for: The code coupling degree is calculated based on the code dependency type, the number of code dependencies, the path length of the code dependency path, and the corresponding complexity weight coefficient, and the code coupling degree level to which the code coupling degree belongs is determined.

[0061] In one possible embodiment, the structure parsing module 21 is specifically used for: The program code is subjected to structural parsing. Based on the structural parsing results, the data flow, control flow, and business types corresponding to code elements in each code segment are determined. Code dependency information is generated based on the data flow and control flow.

[0062] In one possible embodiment, the optimization processing module is used for: Coverage is evaluated for each unit test case based on a preset evaluation dimension to obtain a coverage evaluation value. If the coverage evaluation value is less than the coverage threshold, the corresponding unit test case is optimized to obtain the first target test case. Redundancy and invalidity checks are performed on the first target test case. Based on the detection results, redundant and invalid test cases are deleted to obtain the second target test case.

[0063] This application also provides an electronic device that can integrate a unit test case generation apparatus provided in this application. Figure 5 This is a schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application, with reference to... Figure 5 The unit test case generation device includes: an input device 33, an output device 34, a memory 32, and one or more processors 31; the memory 32 is used to store one or more programs; when one or more programs are executed by one or more processors 31, the one or more processors 31 implement the unit test case generation method provided in the above embodiments. The input device 33, output device 34, memory 32, and processors 31 can be connected via a bus or other means. Figure 5 Taking the example of a connection between China and Israel via a bus.

[0064] The memory 32, as a computing device readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as the program instructions / modules corresponding to the unit test case generation method provided in any embodiment of this application. The memory 32 may primarily include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a function; the data storage area may store data created based on the use of the device. Furthermore, the memory 32 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 32 may further include memory remotely located relative to the processor 31, and these remote memories can be connected to the device via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0065] Input device 33 can be used to receive input digital or character information, and to generate key signal inputs related to user settings and function control of the device. Output device 34 may include display devices such as a display screen.

[0066] The processor 31 executes various functional applications and data processing of the device by running software programs, instructions and modules stored in the memory 32, thereby realizing the unit test case generation method described above.

[0067] The unit test case generation apparatus, device, and computer provided above can be used to execute the unit test case generation method provided in any of the above embodiments, and have corresponding functions and beneficial effects.

[0068] This application also provides a storage medium for storing computer-executable instructions, which, when executed by a computer processor, are used to perform the unit test case generation method provided in the above embodiments. The unit test case generation method includes: Receive the program code to be tested, perform structural analysis on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; The program code is evaluated for its code coupling level based on the code dependency information to obtain a code coupling level. If the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets. Semantic analysis is performed on each of the business code sets to determine test boundary values ​​and test scenarios, and unit test cases corresponding to each of the business code sets are generated based on the test boundary values ​​and test scenarios.

[0069] Storage medium – any type of memory device or storage device. The term “storage medium” is intended to include: mounting media, such as CD-ROMs, floppy disks, or magnetic tape devices; computer system memory or random access memory, such as DRAM, DDR RAM, SRAM, EDO RAM, Rambus RAM, etc.; non-volatile memory, such as flash memory, magnetic media (e.g., hard disks or optical storage); registers or other similar types of memory elements, etc. Storage media may also include other types of memory or combinations thereof. Furthermore, storage media may reside in a first computer system in which a program is executed, or may reside in a different second computer system connected to the first computer system via a network (such as the Internet). The second computer system can provide program instructions to the first computer for execution. The term “storage medium” can include two or more storage media that may reside in different locations (e.g., in different computer systems connected via a network). Storage media may store program instructions (e.g., specifically implemented as a computer program) executable by one or more processors.

[0070] Of course, the computer-executable instructions provided in the embodiments of this application are not limited to the unit test case generation method described above, but can also execute related operations in the unit test case generation method provided in any embodiment of this application.

[0071] The unit test case generation apparatus, device, and storage medium provided in the above embodiments can execute the unit test case generation method provided in any embodiment of this application. For technical details not described in detail in the above embodiments, please refer to the unit test case generation method provided in any embodiment of this application.

[0072] The above description is merely a preferred embodiment and the technical principles employed in this application. This application is not limited to the specific embodiments described herein, and various obvious changes, readjustments, and substitutions that can be made by those skilled in the art will not depart from the scope of protection of this application. Therefore, although this application has been described in detail through the above embodiments, this application is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of this application, the scope of which is determined by the scope of the claims.

Claims

1. A method for generating unit test cases, characterized in that, include: Receive the program code to be tested, perform structural analysis on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; The program code is evaluated for its code coupling level based on the code dependency information to obtain a code coupling level. If the code coupling level is greater than a preset risk level, code segments of the same business type in the program code are encapsulated to obtain multiple business code sets. Semantic analysis is performed on each of the business code sets to determine test boundary values ​​and test scenarios, and unit test cases corresponding to each of the business code sets are generated based on the test boundary values ​​and test scenarios.

2. The unit test case generation method according to claim 1, characterized in that, The step of performing semantic analysis on each of the aforementioned business code sets to determine test boundary values ​​and test scenarios includes: Semantic analysis is performed on the business code set to determine the target variable, the explicit constraints of the target variable, and the first test scenario corresponding to the explicit constraints; The explicit constraints are extended to obtain the implicit constraints corresponding to the explicit constraints. The implicit constraints are semantically matched with the explicit constraints, and the second test scenario corresponding to the implicit constraints is determined based on the matching results and the first test scenario. The test boundary value is determined based on the test range corresponding to the explicit or implicit constraints and the target variable.

3. The unit test case generation method according to claim 2, characterized in that, The step of expanding the explicit constraint to obtain the implicit constraint corresponding to the explicit constraint includes: The explicit constraints are subjected to semantic expansion, complement expansion, and data association expansion to obtain the implicit constraints corresponding to the explicit constraints.

4. The unit test case generation method according to claim 2, characterized in that, The step of determining the test boundary value based on the test range corresponding to the explicit or implicit constraints and the target variable includes: Determine the effective boundary values ​​of the test range corresponding to the explicit or implicit constraints, as well as the unit variable parameters corresponding to the target variable; Calculate invalid boundary values ​​and valid intermediate values ​​based on the valid boundary values ​​and the unit variable parameters, and determine the valid boundary values, invalid boundary values, and intermediate values ​​as test boundary values.

5. The unit test case generation method according to claim 1, characterized in that, The code dependency information includes code dependency type, code dependency quantity, and code dependency path. The step of evaluating the code coupling level of the program code based on the code dependency information includes: The code coupling degree is calculated based on the code dependency type, the number of code dependencies, the path length of the code dependency path, and the corresponding complexity weight coefficient, and the code coupling degree level to which the code coupling degree belongs is determined.

6. The unit test case generation method according to claim 1, characterized in that, The step of performing structural analysis on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code includes: The program code is subjected to structural parsing. Based on the structural parsing results, the data flow, control flow, and business types corresponding to code elements in each code segment are determined. Code dependency information is generated based on the data flow and control flow.

7. The unit test case generation method according to claim 1, characterized in that, After generating unit test cases corresponding to each of the business code sets based on the test boundary values ​​and the test scenario, the method further includes: Coverage is evaluated for each unit test case based on a preset evaluation dimension to obtain a coverage evaluation value. If the coverage evaluation value is less than the coverage threshold, the corresponding unit test case is optimized to obtain the first target test case. Redundancy and invalidity checks are performed on the first target test case. Based on the detection results, redundant and invalid test cases are deleted to obtain the second target test case.

8. A unit test case generation device, characterized in that, include: The structure parsing module is used to receive the program code to be tested, perform structure parsing on the program code to determine the code dependency information of the program code and the business type corresponding to each code segment in the program code; The coupling level assessment module is used to assess the code coupling level of the program code based on the code dependency information, and obtain the code coupling level. The encapsulation processing module is used to encapsulate code segments of the same business type in the program code when the code coupling level is greater than the preset risk level, so as to obtain multiple business code sets. The semantic parsing module is used to perform semantic analysis on each of the aforementioned business code sets to determine test boundary values ​​and test scenarios; The unit test case generation module is used to generate unit test cases corresponding to each of the business code sets based on the test boundary values ​​and the test scenarios.

9. An electronic device, characterized in that, The device includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement the unit test case generation method as described in any one of claims 1-7.

10. A storage medium for storing computer-executable instructions, characterized in that, The computer-executable instructions, when executed by a computer processor, are used to perform the unit test case generation method as described in any one of claims 1-7.