Test case generation method and device based on program execution path decomposition

Through the decomposition method based on the program execution path, splitting the object code and generating unit test cases, the problem that large models cannot cover all branches in logically complex programs is solved, and more efficient unit testing is achieved.

CN119226139APending Publication Date: 2024-12-31BEIJING GUIXIN TECHNOLOGY CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411121430.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Priority Date
2024-07-30
Filing Date
2024-08-15
Publication Date
2024-12-31

AI Technical Summary

Technical Problem

Existing large models have limitations when understanding logical complex programs, resulting in the generated test cases that cannot fully cover all logical branches and boundary conditions of the code, affecting the efficiency and effectiveness of unit testing.

Method used

Using a decomposition method based on the program execution path, by obtaining the object code, splitting it into multiple subcodes, and generating unit test cases for each subcode, and finally combining the test cases for generating the object code to ensure that all logical branches are covered.

Benefits of technology

Improves the efficiency and effectiveness of unit testing, ensuring that test cases can fully cover all logical branches and boundary conditions of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119226139A_ABST
    Figure CN119226139A_ABST
Patent Text Reader

Abstract

The invention provides a test case generation method and device based on program execution path decomposition, and the method comprises the steps: obtaining a target code, the target code comprises a code of a target function statement, and the target function statement refers to a function statement triggering at least two execution conditions; splitting the target code according to the code execution path to obtain a plurality of sub-codes, the plurality of sub-codes comprising sub-codes corresponding to at least two execution conditions respectively; aiming at any one of the plurality of sub-codes, generating a unit test case corresponding to the sub-code; and generating a test case of the target code according to the plurality of unit test cases in one-to-one correspondence with the plurality of sub-codes. According to the embodiment of the invention, the generated test case can fully cover all logic branches of the code, so that the purpose of improving the test efficiency and test effect of unit test is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the technical field of software development, and in particular to a method and device for generating test cases based on program execution path decomposition. Background Art

[0002] In the software development process, unit testing is a key link to ensure code quality and functional correctness. Unit testing refers to verifying the correctness of the smallest testable part of the software (such as a single function). The purpose of unit testing is to isolate a small part of the code and ensure that this part of the code works as expected under various input conditions.

[0003] In the prior art, large model technology is generally used to generate unit test cases for code. However, for programs with many structural levels and interrelated levels, for example, the execution of the second-level function requires the result of the first-level function, or it is a conditional branch of the first-level function, the large model has limitations in understanding, resulting in the generated test cases may not be able to fully cover all logical branches and boundary conditions of the code, thereby affecting the efficiency and effectiveness of the entire testing process. Summary of the invention

[0004] In view of this, the present disclosure proposes a test case generation method and device based on program execution path decomposition to solve the problem in the related art that due to the limitations of large models in understanding logically complex programs, the generated test cases may not cover all logical branches and boundary conditions of the code, affecting the test efficiency and effectiveness.

[0005] The first embodiment of the present disclosure proposes a test case generation method based on program execution path decomposition, the method comprising:

[0006] Obtaining a target code, wherein the target code includes a code of a target function statement, wherein the target function statement refers to a function statement that triggers at least two execution conditions;

[0007] Splitting the target code according to the code execution path to obtain a plurality of sub-codes, wherein the plurality of sub-codes include sub-codes corresponding to the at least two execution conditions respectively;

[0008] For any one of the multiple sub-codes, generating a unit test case corresponding to the sub-code;

[0009] Generate test cases for the target code based on a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

[0010] The disclosed embodiment splits the target code according to the code execution path to obtain multiple sub-codes; generates a test case corresponding to each target code, and generates a test case for the target code based on all the test cases, so that the generated test cases can fully cover all logical branches of the code, thereby achieving the purpose of improving the test efficiency and test effect of the unit test.

[0011] In the embodiment of the present disclosure, the target code is split according to the code execution path to obtain multiple sub-codes, including:

[0012] Convert the target code into a corresponding abstract syntax tree;

[0013] Splitting the abstract syntax tree according to the at least two execution conditions respectively to obtain a plurality of sub-abstract syntax trees, wherein an execution function corresponding to any one of the at least two execution conditions corresponds to a sub-abstract syntax tree in the plurality of sub-abstract syntax trees;

[0014] The multiple sub-abstract syntax trees are converted respectively to obtain the multiple sub-codes.

[0015] In the embodiment of the present disclosure, the target function statement is any one of the following: a judgment function statement, a selection function statement, and a loop function statement.

[0016] In the embodiment of the present disclosure, the target function statement is the judgment function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, including:

[0017] When a judgment node of the judgment function statement exists in the plurality of child nodes of the abstract syntax tree, determining a positive branch structure and a negative branch structure corresponding to the judgment node;

[0018] Based on the affirmative branch structure, generating a first sub-abstract syntax tree corresponding to the affirmative branch structure;

[0019] Based on the negation branch structure, a second sub-abstract syntax tree corresponding to the negation branch structure is generated.

[0020] The disclosed embodiment determines the affirmative branch structure and the negative branch structure corresponding to the judgment node in the abstract syntax tree, and generates a corresponding sub-abstract syntax tree, which helps to generate test cases for different branches of the program, thereby achieving the purpose of improving statement coverage and branch coverage, and can also more accurately test the behavior of the program under different conditions.

[0021] In the embodiment of the present disclosure, the target function statement is the selection function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, including:

[0022] When a selection node of the selection function statement exists among the multiple child nodes of the abstract syntax tree, determining multiple types of branch structures corresponding to the selection node;

[0023] For any one type branch structure among the multiple type branch structures, a third sub-abstract syntax tree corresponding to the type branch structure is generated based on the type branch structure.

[0024] The disclosed embodiment determines multiple types of branch structures corresponding to selection nodes in an abstract syntax tree, and generates a sub-abstract syntax tree corresponding to each type of branch structure. The generation of multiple sub-abstract syntax trees can ensure that the test path covers all possible execution paths, including those complex paths formed by combinations of different conditional branches, thereby achieving the purpose of improving conditional coverage and further improving the test efficiency and test effect of unit testing.

[0025] In an embodiment of the present disclosure, the target function statement is the loop function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, including:

[0026] When there is a target loop node of the loop function statement among the multiple child nodes of the abstract syntax tree, determining a target branch structure corresponding to the target loop node; the target loop node refers to a loop node with an original variable as a cutoff judgment condition, and the target branch structure includes the target loop node and all child nodes of the target loop node;

[0027] Based on the target branch structure, a fourth sub-abstract syntax tree corresponding to the target branch structure is generated.

[0028] In the embodiment of the present disclosure, generating a test case of the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes includes:

[0029] The multiple unit test cases are merged to obtain the test cases of the target code.

[0030] In the embodiment of the present disclosure, before generating the unit test case corresponding to the sub-code, the method further includes:

[0031] Acquire a training sample; the training sample includes a plurality of sample codes and a plurality of sample unit test cases corresponding to the plurality of sample codes one by one;

[0032] The training samples are used to train an initial model to obtain the unit test case generation model, and the unit test case generation model is used to generate unit test cases according to the sub-codes.

[0033] In the embodiment of the present disclosure, the obtaining of training samples includes:

[0034] Acquire a plurality of sample codes and a plurality of sample unit test cases corresponding one-to-one to the plurality of sample codes;

[0035] For any one of the plurality of sample codes, extract a corresponding feature vector from the sample code; the feature vector includes at least one of the following: a structural feature, a grammatical feature, and a logical complexity feature;

[0036] The feature vector of each sample code and the corresponding sample unit test case are used as the training samples.

[0037] The embodiment of the second aspect of the present disclosure provides a test case generation device based on program execution path decomposition, including:

[0038] A code acquisition module, used to acquire a target code, wherein the target code includes a code of a target function statement, wherein the target function statement refers to a function statement that triggers at least two execution conditions;

[0039] A code splitting module, used for splitting the target code according to the code execution path to obtain a plurality of sub-codes, wherein the plurality of sub-codes include the sub-codes corresponding to the at least two execution conditions respectively;

[0040] A unit test case generation module, used for generating a unit test case corresponding to any one of the multiple sub-codes;

[0041] The test case generation module is used to generate test cases for the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

[0042] An embodiment of the third aspect of the present disclosure provides a computer device, which includes a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the test case generation method based on program execution path decomposition described in the first aspect by executing the computer instructions.

[0043] An embodiment of the fourth aspect of the present disclosure provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute the test case generation method based on program execution path decomposition described in the first aspect above.

[0044] Additional aspects and advantages of the present disclosure will be given in part in the following description and in part will become apparent from the following description or be learned through practice of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] By reading the detailed description of the preferred embodiment below, various other advantages and benefits will become clear to those of ordinary skill in the art. The accompanying drawings are only used for the purpose of illustrating the preferred embodiment and are not considered to be limitations of the present disclosure. In addition, the same reference symbols are used to represent the same components throughout the accompanying drawings.

[0046] In the attached picture:

[0047] Figure 1 A flow chart of a method for generating test cases based on program execution path decomposition provided by an embodiment of the present disclosure is shown;

[0048] Figure 2 A schematic diagram of the structure of a test case generation device based on program execution path decomposition provided by an embodiment of the present disclosure is shown;

[0049] Figure 3 A schematic diagram of the structure of a computer device provided by an embodiment of the present disclosure is shown;

[0050] Figure 4 A schematic diagram of a storage medium provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0051] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0052] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in the present disclosure should have the common meanings understood by those skilled in the art to which the present disclosure belongs.

[0053] The following describes the technical scenarios involved in the embodiments of the present disclosure.

[0054] After implementing a new function or fixing a bug, software developers need to write the corresponding unit test code. Unit testing is usually run automatically during the software build process. It can help software developers verify the correctness of software functions, especially for regression testing scenarios, that is, to ensure that each function implemented in the past still works properly in the current software version. However, writing unit tests takes a lot of time for software developers. Therefore, there is an urgent need for a technical solution that can automatically generate unit test cases.

[0055] The current unit testing method based on large models mainly generates unit tests for the target code (generally in functions) by inputting prompts into the large model. Although this method can generate unit tests, the large model is essentially a language model, and its effect on generating unit tests for complex logic code is not good.

[0056] To solve the above technical problems, the present invention provides a test case generation method based on program execution path decomposition, which can make the generated test cases fully cover all logical branches of the code, thereby achieving the purpose of improving the test efficiency and test effect of unit testing.

[0057] According to an embodiment of the present disclosure, an embodiment of a test case generation method based on program execution path decomposition is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0058] In this embodiment, a test case generation method based on program execution path decomposition is provided. Figure 1 is a flow chart of a test case generation method based on program execution path decomposition according to an embodiment of the present disclosure, such as Figure 1 As shown, the process includes the following steps:

[0059] Step S101, obtaining the target code.

[0060] Specifically, the target code refers to the function-level code, that is, the code containing the target function statement. The target function statement refers to the function statement that triggers at least two execution conditions, including but not limited to: judgment function statement (such as if statement), selection function statement (such as switch case statement) and loop function statement (such as for statement or while statement using the original variable as the cutoff judgment condition).

[0061] You can understand "the target function statement is a function statement that triggers at least two execution conditions" through the following example:

[0062] The if function statement includes a true branch structure and a false branch structure. When the conditional expression of the if statement is evaluated to true, the code block under the true branch structure will be executed; if the conditional expression of the if statement is evaluated to false, the code block under the true branch structure (that is, the code block under the else statement) can be selected to be executed.

[0063] The switch function statement includes multiple case labels; if the expression of the switch function statement matches the value of any case label, the code block corresponding to the case label is executed.

[0064] Step S102, splitting the target code according to the code execution path to obtain multiple sub-codes.

[0065] Specifically, the code execution path can be determined according to the function statements contained in the target code. According to the determined multiple code execution paths, the target code is split into multiple sub-codes corresponding to the multiple code execution paths, for example: code execution path 1-sub-code 1, code execution path 2-sub-code 2.

[0066] In some specific embodiments, the above step S102 includes steps S1021 to S1023:

[0067] Step S1021, converting the target code into a corresponding abstract syntax tree.

[0068] Specifically, the target code can be parsed into a corresponding Abstract Syntax Tree (AST) through lexical analysis. For example, the source code is decomposed into multiple characters (such as keywords, identifiers, operators, etc.), and then a character sequence is generated; the character sequence is then converted into a structured syntax tree, namely AST. This process involves using a set of grammar rules to determine how characters are combined into a legal grammatical structure.

[0069] Step S1022: split the abstract syntax tree according to the at least two execution conditions to obtain multiple sub-abstract syntax trees.

[0070] Specifically, the execution function corresponding to any one of the at least two execution conditions corresponds to a sub-abstract syntax tree among the multiple sub-abstract syntax trees.

[0071] In some specific embodiments, the target function statement is a judgment function statement; the above step S1022 includes steps a1 to a3:

[0072] Step a1: when there is a judgment node of the judgment function statement among the multiple child nodes of the abstract syntax tree, determine the affirmative branch structure and the negative branch structure corresponding to the judgment node.

[0073] The above step a1 is explained by taking an example: an if node is determined from the abstract syntax tree AST of the target code; one of the child nodes of the if node is a conditional expression, such as age>18; another child node of the if node is a positive branch structure, that is, when the condition satisfies age>18, the code corresponding to the positive branch structure is executed; another child node of the if node is a negative branch structure, that is, when the condition does not satisfy age>18, the code corresponding to the negative branch structure is executed.

[0074] Step a2: Based on the affirmative branch structure, generate a first sub-abstract syntax tree corresponding to the affirmative branch structure.

[0075] Step a3: Based on the negation branch structure, generate a second sub-abstract syntax tree corresponding to the negation branch structure.

[0076] In the above steps a2 and a3, the positive branch structure can be understood as the true branch structure in the above if statement, and the negative branch structure can be understood as the false branch structure in the above if statement. After determining the positive branch structure and the negative branch structure from the abstract syntax tree, a first sub-abstract syntax tree corresponding to the positive branch structure and a second sub-abstract syntax tree corresponding to the negative branch structure can be generated.

[0077] The relationship between the abstract syntax tree, the first sub-abstract syntax tree, and the second sub-abstract syntax tree is explained through the following example:

[0078] When the abstract syntax tree is used to represent: determine whether a is greater than 1; when a is greater than 1, execute code block 1 corresponding to the affirmative branch structure; when a is less than or equal to 1, execute code block 2 corresponding to the negative branch structure.

[0079] The first sub-abstract syntax tree can be used to represent: when a is greater than 1, execute code block 1.

[0080] The first sub-abstract syntax tree can be used to indicate that when a is less than or equal to 1, code block 2 is executed.

[0081] In some specific embodiments, the target function statement is a selection function statement; the above step S1022 includes steps b1 and b2:

[0082] Step b1: when a selection node of the selection function statement exists among the multiple child nodes of the abstract syntax tree, determine multiple types of branch structures corresponding to the selection node.

[0083] The above step b1 is explained by taking an example: when a switch node for selecting a function statement is determined from multiple child nodes of the abstract syntax tree AST, each case branch structure corresponding to the switch node is taken as a type branch structure.

[0084] Step b2: for any one type branch structure among the multiple type branch structures, based on the type branch structure, generate a third sub-abstract syntax tree corresponding to the type branch structure.

[0085] The abstract syntax tree and the third sub-abstract syntax tree are explained through the following example:

[0086] The abstract syntax tree can be used to represent: matching the value of the switch statement expression with multiple case labels; when the expression value is 1, the code block of the case1 branch structure can be executed; when the expression value is 2, the code block of the case2 branch structure can be executed; if no case branch structure matches the value of the switch statement expression, the code block of the default branch structure is executed.

[0087] Assume that the abstract syntax tree is split into a third sub-abstract syntax tree 1, a third sub-abstract syntax tree 2, and a third sub-abstract syntax tree 3; wherein the third sub-abstract syntax tree 1 can be used to indicate that when the value of the expression is 1, the code block of the case1 branch structure can be executed; the third sub-abstract syntax tree 2 can be used to indicate that when the value of the expression is 2, the code block of the case2 branch structure can be executed; the third sub-abstract syntax tree 3 can be used to indicate that when the value of the expression is 3, the code block of the case3 branch structure can be executed.

[0088] In some specific embodiments, the target function statement is a loop function statement; the above step S1022 includes steps c1 and c2:

[0089] Step c1: when there is a target loop node of the loop function statement among the multiple child nodes of the abstract syntax tree, determine a target branch structure corresponding to the target loop node.

[0090] Specifically, the target loop node refers to a loop node that uses the original variable as the cutoff judgment condition, wherein the original variable can be understood as an existing variable in the code, not a newly created variable; the target branch structure includes the target loop node and all child nodes of the target loop node.

[0091] Step c2: Based on the target branch structure, generate a fourth sub-abstract syntax tree corresponding to the target branch structure.

[0092] Step S1023: Convert the multiple sub-abstract syntax trees respectively to obtain the multiple sub-codes.

[0093] Specifically, the sub-abstract syntax tree can be converted into the corresponding sub-code in the following manner: for each node in the sub-AST, the corresponding code generation logic is executed according to its type; for each node, the corresponding source code fragment is generated; finally, all the generated code fragments are combined in the correct order to form the final sub-code. Other methods can also be used to convert the sub-abstract syntax tree into the corresponding sub-code, which is not specifically limited here, as long as the purpose of converting the sub-abstract syntax tree into the corresponding sub-code is achieved.

[0094] Step S103: for any one of the multiple sub-codes, generate a unit test case corresponding to the sub-code.

[0095] Specifically, a unit test case refers to the test code written for the smallest testable unit in the software (usually a single function) to verify that the behavior of the unit is as expected. The purpose of unit testing is to isolate a part of the code and ensure that it works properly as designed.

[0096] In some specific embodiments, the word code can be input into a trained unit test case generation model to output the corresponding unit test case.

[0097] In some specific embodiments, before the above step S103, the method further includes:

[0098] Step d1, obtaining training samples.

[0099] Specifically, the training sample includes a plurality of sample codes and a plurality of sample unit test cases corresponding one-to-one to the plurality of sample codes.

[0100] In some specific embodiments, the above step d1 further includes steps e1 to e3:

[0101] Step e1, obtaining multiple sample codes and multiple sample unit test cases corresponding to the multiple sample codes.

[0102] Step e2: for any one sample code among the multiple sample codes, extract a corresponding feature vector from the sample code.

[0103] The feature vector includes at least one of the following: a structural feature, a grammatical feature, and a logical complexity feature.

[0104] Step e3: taking the feature vector of each sample code and the corresponding sample unit test case as the training sample.

[0105] Step d2, using the training samples to train an initial model to obtain the unit test case generation model, wherein the unit test case generation model is used to generate unit test cases according to the sub-codes.

[0106] Specifically, the initial model can select a large language model suitable for sequence-to-sequence tasks, such as a Transformer-based model, such as GPT (Generative Pre-trained Transformer), BERT (Bidirectional Encoder Representations from Transformers), etc.

[0107] In some specific embodiments, during the model training process, a validation set can be used to evaluate the performance of the model using appropriate evaluation indicators, such as BLEU score, ROUGE score, or a custom evaluation indicator. The model can also be fine-tuned based on the evaluation results, which may include adjusting the network structure, optimizing the training process, or using more data for training.

[0108] Step S104: generating test cases for the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

[0109] Specifically, multiple unit test cases can be merged by taking the union method to obtain the test cases of the target code.

[0110] Corresponding to the implementation of the above test case generation method based on program execution path decomposition, the embodiment of the present disclosure also provides a test case generation device based on program execution path decomposition, which is used to perform the above Figure 1 The test case generation method based on program execution path decomposition described in any of the illustrated embodiments. Figure 2 As shown, the test case generation device based on program execution path decomposition includes:

[0111] A code acquisition module, used to acquire a target code, wherein the target code includes a code of a target function statement, wherein the target function statement refers to a function statement that triggers at least two execution conditions;

[0112] A code splitting module, used for splitting the target code according to the code execution path to obtain a plurality of sub-codes, wherein the plurality of sub-codes include the sub-codes corresponding to the at least two execution conditions respectively;

[0113] A unit test case generation module, used for generating a unit test case corresponding to any one of the multiple sub-codes;

[0114] The test case generation module is used to generate test cases for the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

[0115] Optionally, the code splitting module is also used to: convert the target code into a corresponding abstract syntax tree; split the abstract syntax tree according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, and the execution function corresponding to any one of the at least two execution conditions corresponds to a sub-abstract syntax tree in the multiple sub-abstract syntax trees; convert the multiple sub-abstract syntax trees to obtain the multiple sub-codes.

[0116] Optionally, the target function statement is any one of the following: a judgment function statement, a selection function statement, and a loop function statement.

[0117] Optionally, the target function statement is the judgment function statement; the code splitting module is also used to: when a judgment node of the judgment function statement exists among multiple child nodes of the abstract syntax tree, determine the affirmative branch structure and the negative branch structure corresponding to the judgment node; based on the affirmative branch structure, generate a first sub-abstract syntax tree corresponding to the affirmative branch structure; based on the negative branch structure, generate a second sub-abstract syntax tree corresponding to the negative branch structure.

[0118] Optionally, the target function statement is the selection function statement; the code splitting module is also used to: when a selection node of the selection function statement exists among multiple child nodes of the abstract syntax tree, determine multiple type branch structures corresponding to the selection node; for any one type branch structure among the multiple type branch structures, based on the type branch structure, generate a third sub-abstract syntax tree corresponding to the type branch structure.

[0119] Optionally, the target function statement is the loop function statement; the code splitting module is also used to: when there is a target loop node of the loop function statement among multiple child nodes of the abstract syntax tree, determine the target branch structure corresponding to the target loop node; the target loop node refers to a loop node with the original variable as the cutoff judgment condition, and the target branch structure includes the target loop node and all child nodes of the target loop node; based on the target branch structure, generate a fourth sub-abstract syntax tree corresponding to the target branch structure.

[0120] Optionally, the test case generation module is further used to merge the multiple unit test cases to obtain the test cases of the target code.

[0121] Optionally, the device further comprises:

[0122] A sample acquisition module, used to acquire training samples; the training samples include a plurality of sample codes and a plurality of sample unit test cases corresponding to the plurality of sample codes one by one;

[0123] A model training module is used to train an initial model using the training samples to obtain the unit test case generation model, and the unit test case generation model is used to generate unit test cases according to the sub-codes.

[0124] Optionally, the sample acquisition module is also used to: acquire multiple sample codes, and multiple sample unit test cases corresponding one-to-one to the multiple sample codes; for any one sample code among the multiple sample codes, extract a corresponding feature vector from the sample code; the feature vector includes at least one of the following: structural features, grammatical features, and logical complexity features; and use the feature vector of each sample code and the corresponding sample unit test case as the training samples.

[0125] The test case generation device based on program execution path decomposition provided by the above-mentioned embodiments of the present disclosure and the test case generation method based on program execution path decomposition provided by the embodiments of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.

[0126] The present disclosure also provides a computer device to execute the above-mentioned test case generation method based on program execution path decomposition. Figure 3 , which shows a schematic diagram of a computer device provided by some embodiments of the present disclosure. Figure 3 As shown, the computer device 3 includes: a processor 300, a memory 301, a bus 302 and a communication interface 303, wherein the processor 300, the communication interface 303 and the memory 301 are connected via the bus 302; the memory 301 stores a computer program that can be run on the processor 300, and the processor 300 executes the aforementioned Figure 1 Any of the illustrated embodiments provides a test case generation method based on program execution path decomposition.

[0127] The memory 301 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 303 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.

[0128] The bus 302 may be an ISA bus, a PCI bus, or an EISA bus. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 301 is used to store programs. After receiving the execution instruction, the processor 300 executes the program. Figure 1 The test case generation method based on program execution path decomposition disclosed in any of the illustrated embodiments may be applied to the processor 300 or implemented by the processor 300 .

[0129] The processor 300 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 300. The above processor 300 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in the embodiments of the present disclosure can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 301, and the processor 300 reads the information in the memory 301 and completes the steps of the above method in combination with its hardware.

[0130] The computer device provided by the embodiment of the present disclosure and the test case generation method based on program execution path decomposition provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by them.

[0131] The present disclosure also provides a computer-readable storage medium corresponding to the test case generation method based on program execution path decomposition provided in the above embodiment. Figure 4 The computer-readable storage medium shown is a CD 30 on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the test case generation method based on program execution path decomposition provided by any of the aforementioned embodiments.

[0132] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.

[0133] The computer-readable storage medium provided by the above-mentioned embodiments of the present disclosure and the test case generation method based on program execution path decomposition provided by the embodiments of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.

[0134] It should be noted that:

[0135] In the description provided herein, a large number of specific details are described. However, it is understood that the embodiments of the present disclosure can be practiced without these specific details. In some instances, well-known structures and technologies are not shown in detail so as not to obscure the understanding of this description.

[0136] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present disclosure, the various features of the present disclosure are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting that the claimed disclosure requires more features than those explicitly recited in each claim. More specifically, as reflected in the claims below, the inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present disclosure.

[0137] In addition, those skilled in the art will appreciate that, although some embodiments described herein include certain features included in other embodiments but not other features, the combination of features of different embodiments is meant to be within the scope of this disclosure and form different embodiments. For example, in the claims below, any one of the claimed embodiments may be used in any combination.

[0138] The above is only a preferred specific implementation of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any changes or substitutions that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed in the present disclosure should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.

Claims

1. A test case generation method based on program execution path decomposition, characterized in that: The method comprises: Obtaining a target code, wherein the target code includes a code of a target function statement, wherein the target function statement refers to a function statement that triggers at least two execution conditions; Splitting the target code according to the code execution path to obtain a plurality of sub-codes, wherein the plurality of sub-codes include sub-codes corresponding to the at least two execution conditions respectively; For any one of the multiple sub-codes, generating a unit test case corresponding to the sub-code; Generate test cases for the target code based on a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

2. The method according to claim 1, characterized in that The target code is split according to the code execution path to obtain multiple sub-codes, including: Convert the target code into a corresponding abstract syntax tree; Splitting the abstract syntax tree according to the at least two execution conditions respectively to obtain a plurality of sub-abstract syntax trees, wherein an execution function corresponding to any one of the at least two execution conditions corresponds to a sub-abstract syntax tree in the plurality of sub-abstract syntax trees; The multiple sub-abstract syntax trees are converted respectively to obtain the multiple sub-codes.

3. The method according to claim 2, characterized in that The target function statement is any one of the following: a judgment function statement, a selection function statement and a loop function statement.

4. The method according to claim 3, characterized in that The target function statement is the judgment function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, including: When a judgment node of the judgment function statement exists in the plurality of child nodes of the abstract syntax tree, determining a positive branch structure and a negative branch structure corresponding to the judgment node; Based on the affirmative branch structure, generating a first sub-abstract syntax tree corresponding to the affirmative branch structure; Based on the negation branch structure, a second sub-abstract syntax tree corresponding to the negation branch structure is generated.

5. The method according to claim 3, characterized in that: The target function statement is the selection function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain multiple sub-abstract syntax trees, including: When a selection node of the selection function statement exists among the multiple child nodes of the abstract syntax tree, determining multiple types of branch structures corresponding to the selection node; For any one type branch structure among the multiple type branch structures, a third sub-abstract syntax tree corresponding to the type branch structure is generated based on the type branch structure.

6. The method according to claim 3, characterized in that The target function statement is the loop function statement; the abstract syntax tree is split according to the at least two execution conditions to obtain a plurality of sub-abstract syntax trees, including: When there is a target loop node of the loop function statement among the multiple child nodes of the abstract syntax tree, determining a target branch structure corresponding to the target loop node; the target loop node refers to a loop node with an original variable as a cutoff judgment condition, and the target branch structure includes the target loop node and all child nodes of the target loop node; Based on the target branch structure, a fourth sub-abstract syntax tree corresponding to the target branch structure is generated.

7. The method according to claim 1 or 2, characterized in that: The step of generating a test case for the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes comprises: The multiple unit test cases are merged to obtain the test cases of the target code.

8. The method according to claim 1, characterized in that Before generating the unit test case corresponding to the sub-code, the method further includes: Acquire a training sample; the training sample includes a plurality of sample codes and a plurality of sample unit test cases corresponding to the plurality of sample codes one by one; The training samples are used to train an initial model to obtain a unit test case generation model, where the unit test case generation model is used to generate unit test cases according to the sub-codes.

9. The method according to claim 8, characterized in that The obtaining of training samples comprises: Acquire a plurality of sample codes and a plurality of sample unit test cases corresponding one-to-one to the plurality of sample codes; For any one of the plurality of sample codes, extract a corresponding feature vector from the sample code; the feature vector includes at least one of the following: a structural feature, a grammatical feature, and a logical complexity feature; The feature vector of each sample code and the corresponding sample unit test case are used as the training samples.

10. A test case generation device based on program execution path decomposition, characterized in that: The device comprises: A code acquisition module, used to acquire a target code, wherein the target code includes a code of a target function statement, wherein the target function statement refers to a function statement that triggers at least two execution conditions; A code splitting module, used for splitting the target code according to the code execution path to obtain a plurality of sub-codes, wherein the plurality of sub-codes include the sub-codes corresponding to the at least two execution conditions respectively; A unit test case generation module, used for generating a unit test case corresponding to any one of the multiple sub-codes; The test case generation module is used to generate test cases for the target code according to a plurality of unit test cases corresponding one-to-one to the plurality of sub-codes.

Citation Information

Patent Citations

  • Unit test automatic execution method based on test case automatic generation algorithm

    CN110543421A

  • Software test case generation method and device, equipment and medium

    CN116089308A

  • Code analysis optimization method and device, computer equipment and storage medium

    CN118132424A

  • Unit test case processing method and device, storage medium and electronic equipment

    CN118193370A

  • Method and apparatus for the performing unit testing of software modules in software systems

    US20110258601A1