Test case generation method and device and storage medium
By building an abstract syntax tree and reducing execution paths, the problem of missing execution paths in existing test methods in complex software structures is solved, high-quality test case generation is achieved, and test coverage and efficiency are ensured.
Patent Information
- Application Number
- CN202411360683.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-09-27
AI Technical Summary
When existing search-based software testing methods deal with complex software structures, there are missing execution paths, resulting in low quality of test cases and inability to effectively cover all branches and boundaries of the software.
By constructing an abstract syntax tree, collect the execution results of the method under constraints, reduce the original execution path according to the optimization scale, obtain the target execution path covering the constraints, and extract the context information of the method to build the target prompt word and enter the language The model generates high-quality test cases.
Improve the quality of test cases, ensure the accuracy of test objects, reduce the amount of operations, and generate high-quality test cases covering all possible execution paths of the software.
Smart Images

Figure CN119396679A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a test case generation method, device and storage medium. Background Art
[0002] During the software development process, the software is tested to find and fix errors and defects in the software to ensure that the final product meets user needs and can function properly.
[0003] At present, search-based software testing (SBST) is mainly used to generate test cases, and the test cases are called to test the software.
[0004] However, SBST relies on a search algorithm to traverse the possible input space, but for complex software structures, such as deeply nested conditional statements and multi-layer loops, there are omissions in the execution paths that are effectively covered by the search algorithm, resulting in low quality test cases and omissions in verifying various branches and boundary conditions of the software. Summary of the invention
[0005] In view of this, the present invention provides a method, device and storage medium for generating a test case, so as to improve the quality of the test case.
[0006] A first aspect of the present invention provides a method for generating a test case, comprising:
[0007] Constructing an abstract syntax tree for a source code to be tested; the source code has a plurality of methods to be tested;
[0008] Collecting the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain an original execution path;
[0009] Simplifying the original execution path according to the set optimization scale to obtain a target execution path covering the constraint conditions;
[0010] extracting context information of the method from the target execution path;
[0011] constructing a target prompt word according to the context information;
[0012] The target prompt word is input into a preset language model to generate a test case.
[0013] A second aspect of the present invention provides a test case generation device, comprising:
[0014] An abstract syntax tree construction module is used to construct an abstract syntax tree for a source code to be tested; the source code has a plurality of methods to be tested;
[0015] An original execution path collection module, used to collect the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain the original execution path;
[0016] A target execution path simplification module, used to simplify the original execution path according to a set optimization scale to obtain a target execution path covering the constraint conditions;
[0017] A context information extraction module, used to extract context information of the method from the target execution path;
[0018] A target prompt word construction module, used to construct a target prompt word according to the context information;
[0019] The test case generation module is used to input the target prompt word into a preset language model to generate a test case.
[0020] A third aspect of the present invention provides an electronic device, the electronic device comprising:
[0021] at least one processor; and
[0022] a memory communicatively connected to the at least one processor; wherein,
[0023] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the test case generation method as described in the first aspect above.
[0024] A fourth aspect of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for generating a test case as described in the first aspect above is implemented.
[0025] A fifth aspect of the present invention provides a computer program product, which includes a computer program. When the computer program is executed by a processor, it implements the test case generation method as described in the first aspect above.
[0026] In this embodiment, an abstract syntax tree is constructed for the source code to be tested; there are multiple methods to be tested in the source code; the execution results of the methods under constraints are collected in each branch of the abstract syntax tree to obtain the original execution path; the original execution path is simplified according to the set optimization scale to obtain the target execution path covering the constraints; the context information of the method is extracted from the target execution path; the target prompt word is constructed according to the context information; the target prompt word is input into the preset language model to generate a test case. This embodiment searches for the target execution path covering the constraints, which not only ensures the accuracy of the test object, but also effectively reduces the amount of calculation, uses the context information of the method in the target execution path to construct the target prompt word, and guides the generation of test cases in the language model, so that the language model can fully understand the information of the target execution path and construct high-quality test cases.
[0027] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0029] Figure 1 It is a flowchart of a method for generating a test case provided in Embodiment 1 of the present invention.
[0030] Figure 2 This is a flowchart of a method for generating a test case provided in Embodiment 2 of the present invention.
[0031] Figure 3 It is a structural schematic diagram of a test case generation device provided in Embodiment 3 of the present invention.
[0032] Figure 4 It is a structural schematic diagram of an electronic device provided in Embodiment 4 of the present invention. DETAILED DESCRIPTION
[0033] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0034] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present invention described herein can cover sequential implementations other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0035] Embodiment 1
[0036] See also Figure 1 , showing a flow chart of a test case generation method provided by the first embodiment of the present invention. This embodiment is applicable to simplifying the execution path in the source code and using the language model (LM) to construct the test case according to the context information of the execution path. The method can be executed by a test case generation device, which can be implemented in the form of hardware and / or software. The test case generation device can be configured in an electronic device, and a software testing platform is installed in the electronic device, for example, PingCode, TestLink, Kiwi TCMS, Squash TM, FitNesse, Tuleap, Robot Framework, SpecFlow, TestMaster, Nitrate, etc. Figure 1 As shown, the method includes:
[0037] Step 101: construct an abstract syntax tree for the source code to be tested.
[0038] In this embodiment, the source code of the software to be tested may be input into the software testing platform, wherein the source code contains a plurality of methods to be tested.
[0039] In the software testing platform, the corresponding parsers such as Tree-sitter, JavaScriptParser, Babylon, etc. can be used to parse the grammatical structure of the source code according to the programming language of the source code, so as to construct an abstract syntax tree (AST). The abstract syntax tree represents the grammatical structure of the programming language in a tree form, and each node on the abstract syntax tree represents a structure in the source code.
[0040] Taking Tree-sitter as an example, the grammar definition file of a specific programming language (such as CFG (Context-Free Grammar)) can be used to parse the source code. Each programming language has its corresponding grammar file, which defines the grammatical rules of the language (such as keywords, grammatical structure, etc.) and can be parsed by Tree-sitter into internal data structures for generating AST.
[0041] The Tree-sitter parsing function is a variant of the LR(1) parsing algorithm, which can handle complex language structures such as left recursion and ambiguity on a single-pass basis.
[0042] Pass the source code string to the parsing function of Tree-sitter, which will parse the source code according to the defined grammar rules and build an abstract syntax tree.
[0043] Step 102: Collect the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain the original execution path.
[0044] In the initial stage of generating test cases, paths can be collected in the abstract syntax tree. By traversing the structure of the source code in the abstract syntax tree, all possible execution paths are extracted from the source code and recorded as original execution paths. That is, each original execution path represents the execution flow of the source code under certain constraints.
[0045] In a specific implementation, a depth-first traversal (DFFS) may be performed on the abstract syntax tree to identify branch structures in the source code, such as if-else statements, switch-case statements, and the like.
[0046] During the depth-first traversal, the constraints of each node on each branch structure (such as cond1, cond2, etc.) and their execution results (True or False) are recorded in order to generate a possible execution path, which is recorded as the original execution path.
[0047] Each branch structure is traversed in this way until the entire source code is traversed. In this way, all possible original execution paths are generated and collected into the path set P.
[0048] For each original execution path p i , can be expressed as a conditional sequence (for example, p i =(cond1=Ture,cond2=False,cond3=Ture)). At this point, the path set P contains all possible original execution paths, the number of which is 2 n where n is the number of constraints.
[0049] For example, assuming there is a branch structure check_file_status(path:PATH)→str, the process of traversing the original execution path is as follows:
[0050] S1. Access the first method statement resolve_path(path). Since it does not involve constraints, no information is recorded.
[0051] S2. Enter the first if statement and record the constraint of path.exists() to ensure that the current path is executable on the abstract syntax tree. Traverse to the return'missing' statement and record the path that returns 'missing' when path.exists() is false.
[0052] S3. Under the condition that path.exists() is true, the traversal continues to access the second if statement, records the constraint that path.is_readable() is true, and records the path that returns 'readable'.
[0053] S4. Continue traversing until the final return'unreadable' statement is reached. At this time, the path when path.exists() is true but path.is_readable() is false is recorded. The collected paths and return values (i.e., execution results) are used to generate the original execution path, which is used to generate test prompts, specifying constraints and execution results.
[0054] Step 103: Simplify the original execution path according to the set optimization scale to obtain a target execution path that covers the constraint conditions.
[0055] In this embodiment, an optimization scale can be set, and all original execution paths in the path set can be simplified according to the optimization scale. Simplification is an optimization step, and its purpose is to reduce the number of original execution paths to be tested. The remaining original execution paths are recorded as target execution paths. In this way, the amount of test calculations can be reduced, providing a data basis for optimizing test cases, and at the same time, ensuring that each target execution path can cover unique constraints.
[0056] Among them, the optimization scale is an adjustable parameter in the simplification. For a given number of original execution paths, the larger the optimization scale, the more target execution paths after simplification. Conversely, the smaller the optimization scale, the smaller the target execution paths after simplification. In this way, the number of target execution paths after simplification can be adjusted.
[0057] In a specific implementation, the minimization constraint set minconstraints and the minimization path set minpaths may be initialized, and both the minimization constraint set minconstraints and the minimization path set minpaths are empty sets.
[0058] In the process of traversing each original execution path, an original execution path p is extracted from the path set P using random methods, each constraint condition path_constraints is extracted from the current original execution path p, and each constraint condition path_constraints of the current original execution path p is compared with the constraint condition path_constraints in the minimized constraint set minconstraints to determine whether the minimized constraint set minconstraints contains each constraint condition path_constraints of the current original execution path p.
[0059] If at least one constraint path_constraints in the current original execution path p is different from all the constraints path_constraints in the minimized constraint set minconstraints, that is, there is at least one new constraint path_constraints in the current original execution path p, then at least one new constraint path_constraints in the current original execution path p can be added to the minimized constraint set minconstraints, and the current original execution path p can be added to the minimized path set minpaths.
[0060] If all constraints path_constraints in the current original execution path p are the same as the constraints path_constraints in the minimized constraint set minconstraints, that is, there are no new constraints path_constraints in the current original execution path p, then the number of other original execution paths that contain the constraints path_constraints in the current original execution path p can be counted in the minimized path set minpaths.
[0061] The number is compared with the set optimization scale, where the optimization scale is an integer greater than or equal to 0.
[0062] If the number is greater than the set optimization scale, the current original execution path p is ignored.
[0063] If the number is less than or equal to the set optimization scale, the current original execution path p is added to the minimized path set minpaths.
[0064] If all original execution paths in the path set P are traversed, the original execution path of the minimized constraint set minpaths is determined as the target execution path. At this time, each original execution path in the minimized constraint set minpaths covers at least one unique constraint condition.
[0065] Reduction describes the process of minimizing the original execution path, with the goal of finding the minimum set of paths that satisfy the minimum constraints in a given set of paths.
[0066] Then, the reduction can be expressed using the following mathematical expression:
[0067]
[0068] Among them, minpaths is the final minimization constraint set, paths is the path set, p is the original execution path in the path set, splitConstraints is the minimization constraint set, minconstraints is the already included minimization constraint set, and c is the existing condition.
[0069] For n constraints, the number of simplified target execution paths |M| is at most n+1, that is, |M|≤n+1, which can effectively reduce the number of target execution paths.
[0070] Step 104: Extract method context information from the target execution path.
[0071] In this embodiment, the context information of the method to be tested is deeply analyzed in the target execution path. The context information is the basis for constructing effective prompt words, so that the language model can fully understand the method to be tested and ensure that the prompt words can fully guide the language model to generate test cases that meet expectations.
[0072] In the specific implementation, a depth-first traversal is performed on the abstract syntax tree to locate the node where the method to be tested is located in the target execution path, and the signature information of the method is extracted from the node of the target execution path as context information; wherein the signature information includes information such as name, parameter list, parameter type and return type.
[0073] Methods with different parameter types have different meanings, so the context information of the corresponding method can be expanded from the node of the target execution path according to the parameter type.
[0074] Exemplarily, parameter types include basic data types, user-defined types, and complex data types.
[0075] Basic data types include int, float, etc., which can usually be used directly and recognized by the language model.
[0076] If the parameter type is a user-defined type, such as a class or structure, the information about the attributes (member variables) and definitions of the user-defined type is read from the nodes of the target execution path as context information so that the language model can correctly generate test cases related to the user-defined type.
[0077] If the parameter type is a complex data type, such as a pointer, array, template class, etc., the basic data type pointed to by the complex data type is parsed as context information, and the usage pattern of the complex data type is read from the node of the target execution path by using type inference, recursion, etc. as context information.
[0078] For example, for a template class in C++, its instantiation information can be parsed and the usage pattern can be obtained to determine the specific template parameter types and consider the specific manifestations of these template parameter types in different instantiation scenarios.
[0079] In addition, this embodiment constructs test cases through a multi-round iteration mode. If the current iteration is not the first round, the test results recorded when the source code is tested using the test cases in the previous iteration can be queried, and the test results can be expanded into the context information of the method to be tested in the current iteration.
[0080] This embodiment implements a context expansion and feedback mechanism based on the test results of the test cases, which not only enriches the background information of the language model, but also guides the language model to refer to the successful cases and failed cases in the test results of the previous test cases when generating new test cases, so as to follow the successful experience, avoid repeated errors, and avoid falling into an endless loop, thereby generating more effective and more comprehensive test cases, ensuring that the final test cases of the iteration can continuously approach the expected test results.
[0081] Step 105: construct a target prompt word according to the context information.
[0082] In this embodiment, a prompt word Prompt may be constructed based on the context information of the method to be tested, ensuring that all collected context information is reasonably and fully expressed in the prompt word, thereby helping the language model to generate accurate test cases.
[0083] In a specific implementation, a prompt template set for the source code and matching the information type of the context information may be queried; wherein the information type includes parameter types in the signature information, test results, and the like.
[0084] The context information is written into the prompt template according to the information type to obtain structured original prompt words, ensuring that the context information of each information type is properly expressed. For example, for a method containing a template class, the type of the template parameter is clearly indicated in the original prompt word; for complex data types, the object pointed to by the pointer, the size and content of the array, etc. are clearly indicated in the original prompt word to help the language model correctly understand and generate reasonable test cases.
[0085] If the context information of each information type has completed the construction of the original prompt words, all the original prompt words can be combined into the target prompt words.
[0086] The prompt template contains information about specific test scenarios. Writing context information into the prompt template can combine the context information with the specific test scenario to indicate the behavior of the language model when generating test cases.
[0087] For example, for signature information of some complex data types, the target prompts clearly state how to initialize them in the test cases and how to call the methods they provide.
[0088] For signature information of certain user-defined types, the target prompt clearly indicates which properties to initialize, which methods to call, and so on.
[0089] Step 106: Input the target prompt word into a preset language model to generate a test case.
[0090] In this embodiment, the target prompt word can be input into a preset language model, especially LLM (Large Language Model), and the language model generates test cases according to the guidance of the target prompt word.
[0091] In this embodiment, an abstract syntax tree is constructed for the source code to be tested; there are multiple methods to be tested in the source code; the execution results of the methods under constraints are collected in each branch of the abstract syntax tree to obtain the original execution path; the original execution path is simplified according to the set optimization scale to obtain the target execution path covering the constraints; the context information of the method is extracted from the target execution path; the target prompt word is constructed according to the context information; the target prompt word is input into the preset language model to generate a test case. This embodiment searches for the target execution path covering the constraints, which not only ensures the accuracy of the test object, but also effectively reduces the amount of calculation, uses the context information of the method in the target execution path to construct the target prompt word, and guides the generation of test cases in the language model, so that the language model can fully understand the information of the target execution path and construct high-quality test cases.
[0092] Embodiment 2
[0093] See also Figure 2 , shows a flow chart of a method for generating a test case provided by the second embodiment of the present invention. This embodiment adds a process of optimizing the test case on the basis of the previous embodiment. Figure 2 As shown, the method includes:
[0094] Step 201: construct an abstract syntax tree for the source code to be tested.
[0095] There are multiple methods to be tested in the source code.
[0096] Step 202: Collect the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain the original execution path.
[0097] Step 203: simplify the original execution path according to the set optimization scale to obtain a target execution path that covers the constraint conditions.
[0098] Step 204: Extract method context information from the target execution path.
[0099] Step 205: construct a target prompt word according to the context information.
[0100] Step 206: Input the target prompt word into a preset language model to generate a test case.
[0101] Step 207: Use the test case to test the source code and obtain the test result.
[0102] Execute each generated test case, test the source code, and record the test results in real time. During the execution of the test case, capture all possible exceptions, errors, and method return values to ensure the reliability and accuracy of the test case.
[0103] Furthermore, during the execution of the test case, a comprehensive exception handling mechanism is set up. If the test case calls a non-existent method or uses the wrong parameter type, the test case is recorded as failed, and the exception type and corresponding stack information are recorded in detail.
[0104] Step 208: Calculate multiple original evaluation indicators for the test case based on the test results.
[0105] In this embodiment, an automated testing tool provided by a software testing platform may be called to analyze the test results, thereby calculating a plurality of original evaluation indicators for the test case.
[0106] Exemplarily, the original evaluation indicators include successful execution ratio, successful call ratio, line coverage and branch coverage.
[0107] Among them, the successful execution ratio is the ratio of the number of successfully executed test cases to the total number of test cases during the first execution. The successful execution ratio reflects the effectiveness of the generated test cases during the first run.
[0108] The successful execution ratio can be expressed as:
[0109]
[0110] Among them, Pass@1 is the successful execution ratio, T i is the i-th test case, N is the number of test cases, Exec(T i ) is the test case T i The execution result of the test case T i If the execution is successful and there are no errors, Exec(T i ) returns 1, otherwise Exec(T i ) returns 0.
[0111] The successful call ratio is the ratio of test cases that are successfully called and executed correctly to the total number of test cases, reflecting the accuracy of the generated test cases.
[0112] The successful call ratio can be expressed as:
[0113]
[0114] Among them, FM Call@1 is the successful call ratio, T i is the i-th test case, N is the number of test cases, Exec(T i ) is the test case T i The execution result of the test case T i If the execution is successful and there are no errors, Exec(T i ) returns 1, otherwise Exec(T i) returns 0, Call(Target,T i ) is the test case T i Whether the function of the method to be tested is called correctly. If the call is correct, Call(Target, T i ) returns 1, otherwise Call(Target,T i ) returns 0.
[0115] Line coverage is defined as the ratio of the number of code lines executed during the execution of the test case to the total number of code lines. It can be used to measure the coverage depth of the test case on the source code.
[0116] The line coverage definition can be expressed as:
[0117]
[0118] Among them, Line Coverage is the line coverage, L i is the i-th line of code, M is the total number of lines in the source code, Cover(L i ) is whether the test case covers the code line L i If a function is covered by at least one test case, Cover(L i ) returns 1, otherwise Cover(L i ) returns 0.
[0119] Branch coverage is defined as the ratio of branch structures covered by test cases to the total branch structures, which can be used to evaluate whether the test cases fully cover all possible execution paths in the source code.
[0120] Branch coverage can be expressed as:
[0121]
[0122] Among them, Branch Coverage is the branch coverage, B j is the jth branch structure, B is the total number of branch structures, Cover(B j ) is the test case to see if it covers the branch structure B j If a function is covered by at least one test case, Cover(B j ) returns 1, otherwise Cover(B j ) returns 0.
[0123] Of course, the above original evaluation indicators are only used as examples. When implementing this embodiment, other original evaluation indicators can be set according to actual conditions, such as defect density, defect closing speed, mean repair time, performance indicators (such as response time, throughput, etc.), etc., and this embodiment does not limit this. In addition, in addition to the above original evaluation indicators, those skilled in the art can also use other original evaluation indicators according to actual needs, and this embodiment does not limit this.
[0124] Step 209: Merge multiple original evaluation indicators into a target evaluation indicator.
[0125] In this embodiment, a plurality of original evaluation indicators may be fused into a target evaluation indicator in a linear or nonlinear manner.
[0126] Exemplarily, the product of the successful execution ratio and the first weight, the product of the successful call ratio and the second weight, the product of the line coverage and the third weight, and the product of the branch coverage and the fourth weight are added together to obtain the target evaluation index, which can comprehensively measure the correctness and coverage of the test cases.
[0127] Then, the target evaluation index can be expressed as:
[0128] Overall Quality
[0129] =α·Pass@1+β·FM Call@1+γ·Line Coverage
[0130] +δ Branch Coverage
[0131] Among them, Overall Quality is the target evaluation indicator, Pass@1 is the successful execution ratio, FM Call@1 is the successful call ratio, Line Coverage is the line coverage, Branch Coverage is the branch coverage, α is the first weight, β is the second weight, γ is the third weight, δ is the fourth weight, α+β+γ+δ=1.
[0132] Step 210: If the target evaluation index is less than or equal to the first threshold, and the number of test cases generated in the current iteration is less than or equal to the second threshold, the optimization scale is increased according to part of the original evaluation index, and the process returns to step 203.
[0133] In this embodiment, the target evaluation index can be compared with a preset first threshold (such as 0.8), and the number of test cases generated in the current iteration can be counted and compared with a preset second threshold (such as 3).
[0134] In addition to helping to improve the accuracy of test cases, the second threshold can also avoid unobjectionable cycles in the process of generating test cases.
[0135] If the target evaluation index is less than or equal to the first threshold, and the number of test cases generated in the current iteration is less than or equal to the second threshold, it can be decided whether to increase the optimization scale according to the situation of some original evaluation indicators, and steps 203 to 209 are executed to enter the next round of iteration. At this time, more original execution paths can be included as target execution paths, and more detailed context information or clearer constraints can be added to guide the language model to generate more accurate test cases.
[0136] Exemplarily, if the row coverage is less than or equal to the third threshold and the branch coverage is less than or equal to the fourth threshold, it means that the coverage of the test case is low, and the optimization scale can be increased, such as setting the sum of the optimization scale and the preset step size as the new optimization scale.
[0137] At this point, you can identify code paths that are not fully covered, and for these paths, construct more precise target prompts or generate more diverse test cases through iterative optimization to improve coverage.
[0138] Step 211: If the target evaluation index is greater than the first threshold, or the number of times the test case is generated in the current iteration is greater than the second threshold, it is determined that the test case is completed.
[0139] If the target evaluation index is greater than the first threshold, or the number of test cases generated in the current iteration is greater than the second threshold, it can be determined that the test case has been built and the test case is output to the tester for review and correction. When the review and correction are completed, the test case is used to test the source code.
[0140] Embodiment 3
[0141] See also Figure 3 , shows a schematic diagram of the structure of a test case generation device provided by Embodiment 3 of the present invention. Figure 3 As shown, the device comprises:
[0142] An abstract syntax tree construction module 301 is used to construct an abstract syntax tree for a source code to be tested; the source code has a plurality of methods to be tested;
[0143] The original execution path collection module 302 is used to collect the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain the original execution path;
[0144] The target execution path simplification module 303 is used to simplify the original execution path according to the set optimization scale to obtain a target execution path covering the constraint conditions;
[0145] A context information extraction module 304, used to extract context information of the method from the target execution path;
[0146] A target prompt word construction module 305 is used to construct a target prompt word according to the context information;
[0147] The test case generation module 306 is used to input the target prompt word into a preset language model to generate a test case.
[0148] In one embodiment of the present invention, the target execution path reduction module 303 includes:
[0149] The set initialization module is used to initialize the minimization constraint set and the minimization path set;
[0150] A constraint comparison module, used for comparing each constraint of the current original execution path with the constraint in the minimized constraint set during traversing each original execution path;
[0151] a difference processing module, configured to add at least one constraint in the current original execution path to the minimization constraint set and add the current original execution path to the minimization path set if at least one constraint in the current original execution path is different from all the constraints in the minimization constraint set;
[0152] A quantity counting module, configured to count the number of other original execution paths in the minimized path set that contain the constraints in the current original execution path if all the constraints in the current original execution path are the same as the constraints in the minimized constraint set;
[0153] A path adding module, configured to add the current original execution path to the minimized path set if the number is less than or equal to a set optimization scale;
[0154] The target execution path determination module is used to determine the original execution path of the minimized constraint set as the target execution path if all the original execution paths are traversed.
[0155] In one embodiment of the present invention, the context information extraction module 304 includes:
[0156] A signature information extraction module, used to extract the signature information of the method from the node of the target execution path as context information; the signature information includes parameter types;
[0157] A parameter type expansion module is used to expand the context information of the method from the nodes of the target execution path according to the parameter type.
[0158] In one embodiment of the present invention, the parameter types include basic data types, user-defined types and complex data types;
[0159] The parameter type expansion module includes:
[0160] A custom processing module, configured to read the attributes and definitions of the user-defined type from the nodes of the target execution path as context information if the parameter type is the user-defined type;
[0161] A complex processing module is used to parse the basic data type pointed to by the complex data type as context information if the parameter type is the complex data type, and to read the usage mode of the complex data type from the node of the target execution path as context information.
[0162] In one embodiment of the present invention, the context information extraction module 304 further includes:
[0163] A test result query module, used to query the test results recorded when the source code is tested using the previous test case;
[0164] The test result expansion module is used to expand the test result into context information of the method.
[0165] In one embodiment of the present invention, the target prompt word construction module 305 includes:
[0166] a prompt template query module, adapted to query a prompt template set for the source code and matching the information type of the context information;
[0167] An original prompt word construction module, used for writing the context information into the prompt template according to the information type to obtain an original prompt word;
[0168] The original prompt word composition module is used to combine all the original prompt words into target prompt words.
[0169] In one embodiment of the present invention, it also includes:
[0170] A source code testing module, used to test the source code using the test case to obtain a test result;
[0171] An original evaluation index calculation module, used to calculate a plurality of original evaluation indexes for the test case according to the test result;
[0172] A target evaluation index fusion module, used for fusing the plurality of original evaluation indexes into a target evaluation index;
[0173] An optimization scale upward adjustment module, configured to increase the optimization scale according to part of the original evaluation index if the target evaluation index is less than or equal to the first threshold, and the number of times the test case is generated in the current iteration is less than or equal to the second threshold, and return to execute the target execution path simplification module 303;
[0174] The test case completion construction module is used to determine that the test case has been completed if the target evaluation index is greater than a first threshold, or the number of times the test case is generated in the current iteration is greater than a second threshold.
[0175] In one embodiment of the present invention, the original evaluation indicators include successful execution ratio, successful call ratio, line coverage and branch coverage;
[0176] The target evaluation index fusion module includes:
[0177] A linear fusion module, used for adding the product between the successful execution ratio and the first weight, the product between the successful call ratio and the second weight, the product between the line coverage and the third weight, and the product between the branch coverage and the fourth weight to obtain a target evaluation index;
[0178] The optimization scale-up module comprises:
[0179] The coverage increasing module is used to increase the optimization scale if the row coverage is less than or equal to a third threshold and the branch coverage is less than or equal to a fourth threshold.
[0180] The test case generation device provided in the embodiment of the present invention can execute the test case generation method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of executing the test case generation method.
[0181] Embodiment 4
[0182] See also Figure 4, shows a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, blade servers, mainframe computers, and other suitable computers. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or claimed herein.
[0183] like Figure 4 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0184] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0185] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the test case generation method.
[0186] In some embodiments, the test case generation method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the test case generation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to execute the test case generation method in any other appropriate manner (e.g., by means of firmware).
[0187] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0188] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0189] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0190] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0191] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0192] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0193] Embodiment 5
[0194] An embodiment of the present invention further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the method for generating a test case provided by any embodiment of the present invention is implemented.
[0195] In the process of implementation, the computer program product can be written in one or more programming languages or a combination thereof to perform the computer program code of the present invention, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (for example, using an Internet service provider to connect through the Internet).
[0196] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0197] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for generating a test case, characterized in that: include: Build an abstract syntax tree for the source code to be tested; The source code has multiple methods to be tested; Collecting the execution results of the method under the constraint conditions in each branch of the abstract syntax tree to obtain an original execution path; Simplifying the original execution path according to the set optimization scale to obtain a target execution path covering the constraint conditions; extracting context information of the method from the target execution path; constructing a target prompt word according to the context information; The target prompt word is input into a preset language model to generate a test case.
2. The method for generating a test case according to claim 1, characterized in that: The simplifying of the original execution path according to the set optimization scale to obtain a target execution path covering the constraint conditions includes: Initialize the minimization constraint set and the minimization path set; In the process of traversing each of the original execution paths, comparing each of the constraint conditions of the current original execution path with the constraint conditions in the minimization constraint set; If at least one of the constraints in the current original execution path is different from all the constraints in the minimization constraint set, then at least one of the constraints in the current original execution path is added to the minimization constraint set, and the current original execution path is added to the minimization path set; If all the constraints in the current original execution path are the same as the constraints in the minimized constraint set, then counting the number of other original execution paths in the minimized path set that contain the constraints in the current original execution path; If the number is less than or equal to the set optimization scale, adding the current original execution path to the minimized path set; If all the original execution paths are traversed, the original execution path that minimizes the constraint set is determined as the target execution path.
3. The method for generating a test case according to claim 1, characterized in that: The extracting the context information of the method from the target execution path includes: Extracting signature information of the method from a node of the target execution path as context information; the signature information includes parameter types; The context information of the method is expanded from the nodes of the target execution path according to the parameter type.
4. The method according to claim 3, characterized in that The parameter types include basic data types, user-defined types and complex data types; The expanding the context information of the method from the nodes of the target execution path according to the parameter type includes: If the parameter type is the user-defined type, then reading the attributes and definitions of the user-defined type from the node of the target execution path as context information; If the parameter type is the complex data type, the basic data type pointed to by the complex data type is parsed as context information, and the usage mode of the complex data type is read from the node of the target execution path as context information.
5. The method for generating a test case according to claim 3, characterized in that: The extracting the context information of the method from the target execution path further includes: Query the test results recorded when the source code is tested using the previous test case; The test result is expanded into context information of the method.
6. The method for generating a test case according to claim 1, characterized in that: The step of constructing a target prompt word according to the context information includes: querying a prompt template set for the source code and matching the information type of the context information; Writing the context information into the prompt template according to the information type to obtain an original prompt word; All the original prompt words are combined into a target prompt word.
7. The method for generating a test case according to any one of claims 1 to 6, characterized in that: Also includes: Using the test case to test the source code, and obtaining a test result; Calculating a plurality of original evaluation indicators for the test case according to the test result; Merging the plurality of original evaluation indicators into a target evaluation indicator; If the target evaluation index is less than or equal to the first threshold, and the number of times the test case is generated in the current iteration is less than or equal to the second threshold, the optimization scale is increased according to part of the original evaluation index, and the optimization scale set according to the execution is returned to simplify the original execution path to obtain a target execution path covering the constraint condition; If the target evaluation index is greater than a first threshold, or the number of times the test case is generated in the current iteration is greater than a second threshold, it is determined that the test case has been constructed.
8. The method for generating a test case according to claim 7, characterized in that: The original evaluation indicators include successful execution ratio, successful call ratio, line coverage and branch coverage; The step of fusing the plurality of original evaluation indicators into a target evaluation indicator comprises: Add the product of the successful execution ratio and the first weight, the product of the successful call ratio and the second weight, the product of the line coverage and the third weight, and the product of the branch coverage and the fourth weight to obtain a target evaluation index; The step of increasing the optimization scale according to part of the original evaluation indicators includes: If the row coverage is less than or equal to a third threshold, and the branch coverage is less than or equal to a fourth threshold, the optimization scale is increased.
9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the test case generation method according to any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for generating a test case according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Test case generation system based on dynamic symbolic execution
CN110515856A
Unit test case generation method and device, electronic equipment and storage medium
CN117421235A
Code analysis optimization method and device, computer equipment and storage medium
CN118132424A