Test case generation method and device, electronic equipment and storage medium

CN115904926BActive Publication Date: 2026-09-22FACE CUTE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111162300.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-09-30
Publication Date
2026-09-22
Estimated Expiration
2041-09-30

AI Technical Summary

Technical Problem

[0004]本公开实施例提供一种测试用例生成方法、装置、电子设备及存储介质,以克服基于SBST对微服务架构中的待测函数生成测试用例的过程中,存在的生成速度降低,甚至无法正常生成测试用例的问题

Benefits of technology

[0014]本实施例提供的测试用例生成方法、装置、电子设备及存储介质,通过解析待测函数,获得函数结构信息,所述函数结构信息用于表征所述待测函数对其他函数的调用关系;根据所述函数结构信息,确定候选函数,所述候选函数为所述待测函数内部所调用的调用函数;确定所述候选函数对应的声明候选集;基于所述声明候选集,生成备选测试用例;基于启发式搜索算法,对所述备选测试用例进行迭代演化,生成目标测试用例,目标测试用例用于对待测函数进行测试。由于在对待测函数进行基于启发式搜索算法生成测试用例之前,先通过待测函数的函数结构信息,确定了待测函数内用于直接调用的候选函数,之后再对候选函数基于启发式搜索算法进行计算,能够避免由于相关的调用函数过多导致的数量爆炸,从而使启发式搜索算法能够快速收敛,提高测试用例的生成速度和测试用例的质量。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115904926B_ABST
    Figure CN115904926B_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure provide a test case generation method and device, electronic equipment and storage medium. The function structure information is obtained by analyzing the to-be-tested function, and the function structure information is used to represent the calling relationship of the to-be-tested function to other functions. According to the function structure information, the candidate function is determined, and the candidate function is a calling function called inside the to-be-tested function. The declaration candidate set corresponding to the candidate function is determined and the corresponding alternative test case is generated. Based on the heuristic search algorithm, the alternative test case is iteratively evolved to generate a target test case, which is used to test the to-be-tested function. The candidate function for direct calling in the to-be-tested function is determined, and the candidate function is calculated based on the heuristic search algorithm, which can avoid the number explosion caused by too many related calling functions, thereby realizing fast convergence of the algorithm, improving the generation speed of the test case and the quality of the test case.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of computer technology, and in particular to a test case generation method, apparatus, electronic device, and storage medium. Background Technology

[0002] Software testing is a crucial step in ensuring software quality during software development. To achieve testing objectives, a large number of test cases need to be written, which in turn enable automated testing. To address the challenge of test case writing, existing technologies utilize heuristic search algorithms to implement Search-Based Software Testing (SBST) for automatic test case generation. This technology transforms the problem of automatic test case generation into a function optimization problem, which is then solved using heuristic search algorithms, thereby achieving automatic test case generation.

[0003] However, in the process of generating test cases for functions under test in a microservice architecture, the functions under test may involve a large number of calling functions. Directly processing the functions under test based on heuristic search algorithms will lead to an explosion in the number of related calling functions, which will reduce the speed of test case generation or even prevent the normal generation of test cases. Summary of the Invention

[0004] This disclosure provides a test case generation method, apparatus, electronic device, and storage medium to overcome the problems of reduced generation speed or even failure to generate test cases normally during the process of generating test cases for functions under test in a microservice architecture based on SBST.

[0005] In a first aspect, embodiments of this disclosure provide a test case generation method, including:

[0006] The function to be tested is parsed to obtain its structure information, which represents the call relationship between the function to be tested and other functions. Based on the structure information, candidate functions are determined, which are the calling functions called internally by the function to be tested. A set of candidate declarations corresponding to the candidate functions is determined. Based on the candidate declaration set, alternative test cases are generated. Using a heuristic search algorithm, the alternative test cases are iteratively evolved to generate target test cases, which are used to test the function to be tested.

[0007] Secondly, embodiments of this disclosure provide a test case generation apparatus, comprising:

[0008] The parsing module is used to parse the function under test and obtain function structure information, which is used to characterize the call relationship between the function under test and other functions;

[0009] The determination module is used to determine candidate functions based on the function structure information, wherein the candidate functions are the calling functions called inside the function to be tested;

[0010] The generation module is used to determine the declaration candidate set corresponding to the candidate function, generate alternative test cases based on the declaration candidate set, and iteratively evolve the alternative test cases based on a heuristic search algorithm to generate target test cases, which are used to test the function to be tested.

[0011] Thirdly, embodiments of this disclosure provide an electronic device, including: at least one processor and a memory; the memory stores computer execution instructions; the at least one processor executes the computer execution instructions stored in the memory, causing the at least one processor to perform the test case generation method as described in the first aspect and various possible designs of the first aspect.

[0012] Fourthly, embodiments of this disclosure provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the test case generation method described in the first aspect and various possible designs of the first aspect.

[0013] Fifthly, embodiments of this disclosure provide a computer program product, including a computer program that, when executed by a processor, implements the test case generation method described in the first aspect and various possible designs of the first aspect.

[0014] The test case generation method, apparatus, electronic device, and storage medium provided in this embodiment obtain function structure information by parsing the function under test. This function structure information characterizes the call relationships between the function under test and other functions. Based on the function structure information, candidate functions are determined, which are the calling functions called internally by the function under test. A set of declaration candidates corresponding to the candidate functions is determined. Based on the declaration candidate set, alternative test cases are generated. Using a heuristic search algorithm, the alternative test cases are iteratively evolved to generate target test cases, which are used to test the function under test. Because candidate functions for direct calls within the function under test are determined first using the function structure information of the function under test before generating test cases based on the heuristic search algorithm, the number of related calling functions can be avoided from exploding. This allows the heuristic search algorithm to converge quickly, improving the generation speed and quality of test cases. Attached Figure Description

[0015] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 A schematic diagram of an application with a microservice architecture provided in an embodiment of this disclosure;

[0017] Figure 2 A flowchart illustrating the test case generation method provided in this embodiment of the disclosure. Figure 1 ;

[0018] Figure 3 A flowchart illustrating the test case generation method provided in this embodiment of the disclosure. Figure 2 ;

[0019] Figure 4 for Figure 3 A schematic diagram illustrating the implementation steps of step S204 in the illustrated embodiment;

[0020] Figure 5 for Figure 3 A schematic diagram illustrating the implementation steps of step S206 in the illustrated embodiment;

[0021] Figure 6 A schematic diagram illustrating a method for determining a target test case suite based on a genetic algorithm, as provided in this embodiment of the disclosure.

[0022] Figure 7A structural block diagram of the test case generation apparatus provided in the embodiments of this disclosure;

[0023] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure;

[0024] Figure 9 This is a schematic diagram of the hardware structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation

[0025] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, and not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.

[0026] The application scenarios of the embodiments of this disclosure are explained below:

[0027] The test case generation method provided in this embodiment can be applied to unit testing scenarios, more specifically, to unit testing of applications in a microservice architecture. Microservices (or microservice architecture) are a variation of Service-Oriented Architecture (SOA), constructing an application as a set of loosely coupled services. In a microservice architecture, services are fine-grained, and protocols are lightweight. Microservices build applications as independent components, with each application process running as a service. These services communicate using lightweight Application Programming Interfaces (APIs) through well-defined interfaces. Typically, these services are built around business functions, with each service performing one function. Because they (services) run independently, they can be updated, deployed, and extended to meet the needs of specific program functions. In a microservice architecture, method calls are accomplished through communication protocols. These protocols are often extensive, with a single transmitted structure often containing hundreds of fields. Furthermore, a single running program does not possess complete information about the system. In a microservice architecture, method calls require invoking other programs to be implemented.

[0028] Figure 1 A schematic diagram of an application with a microservice architecture provided in this disclosure embodiment, such as... Figure 1As shown, multiple services (services A to E) of an application are constructed and communicate with each other through a lightweight protocol to achieve different functions of the application. Each service is started by executing its corresponding program, which includes one or more functions. The test case generation method provided in this embodiment can generate test cases for the functions mentioned above using a search-based automatic test case generation technique. For example, the automatic test case generation technique is based on a heuristic search algorithm. By defining a fitness function, the problem of automatically generating test cases is transformed into a function optimization problem, which is then solved using a heuristic search algorithm.

[0029] In the prior art, reference Figure 1 The diagram illustrates a microservice architecture application. In a microservice architecture, for a function to be tested that requires test cases, it may contain one or more calling functions. In the heuristic search algorithm calculation process for this function, existing technologies use all functions in the current package as a candidate set for calculation. This includes not only the calling functions within the function to be tested but also many functions unrelated to the function to be tested. In a microservice framework, a function to be tested may reference dozens of packages. According to existing solutions, the candidate set may contain hundreds of functions, most of which are unrelated to the function to be tested. Using all functions in the current package as candidates for evolutionary calculation leads to an explosion in the number of functions, resulting in a decrease in test case generation speed or even the inability to generate test cases correctly. This disclosure provides a test case generation method to solve the above problems.

[0030] Figure 2 A flowchart illustrating the test case generation method provided in this embodiment of the disclosure. Figure 1 The method in this embodiment can be applied to a server. For example, the function under test is written in Go, and the test case generation method includes:

[0031] Step S101: parse the function to be tested and obtain the function structure information. The function structure information is used to characterize the calling relationship between the function to be tested and other functions.

[0032] For example, a function is a functional unit used to implement the services of an application in a microservice architecture. By calling a function, the corresponding functionality can be achieved. The function under test is the function tested as the object of test during unit testing. The test case generation method provided in this embodiment can generate target test cases corresponding to the function under test, thereby testing the function under test based on the target test cases. The function under test also includes other code, including methods and functions. In this embodiment, the other methods and functions in the function under test are collectively referred to as calling functions. Further, based on the static function structure of the function under test, by parsing the function under test, function structure information representing the calling relationship between the calling functions of the function under test can be obtained. For example, by obtaining and parsing the Abstract Syntax Tree (AST) of the function under test, function structure information can be generated. The AST is an abstract representation of the source code syntax structure. It represents the syntax structure of a programming language in a tree-like form, where each node in the tree represents a structure in the source code. The method of obtaining calling functions in a function by parsing the AST is prior art known to those skilled in the art and will not be elaborated here.

[0033] In one possible implementation, the function structure information includes a call graph. The method for parsing the function under test and obtaining its structure information includes: acquiring a pre-defined call graph component; parsing the function under test based on the call graph component to generate its call graph, where the call graph is an abstract expression of the function under test. For example, the call graph component is a package adapted from the Go language that defines a call graph and various algorithms and utilities operating on it. More specifically, this call graph component is `callgraph`. The call graph of the function under test can be obtained through `callgraph` and stored in the format `*ssa.function` to achieve an abstract expression of the function under test. Here, `callgraph` is existing technology; its specific definition and usage are not elaborated here.

[0034] Step S102: Based on the function structure information, determine the candidate functions. The candidate functions are the calling functions called inside the function to be tested.

[0035] For example, after obtaining the function structure information, the functions and methods directly called inside the function under test can be detected based on the abstract function structure represented by the function structure information, i.e., the calling functions. The following is a specific example illustrating the calling functions in the function under test:

[0036]

[0037]

[0038] Here, `targetFunc()` is a function whose output value is of type `int`, i.e., the function under test. This function includes two inner functions: `A.RPC()` and `if()`. `A.RPC()` is a method for remote procedure call, and `if()` is an `if` statement. Based on the abstract function structure of the function under test as represented by the function structure information, we can identify `A.RPC()` as the calling function from the two inner functions and determine its location; this process will not be elaborated further. Furthermore, the calling function `A.RPC()` is a call to the package of the function under test, meaning it needs to call an external program to execute. Before calling the external program, it needs to be referenced. Therefore, this directly called function is identified as a candidate function.

[0039] Step S103: Determine the candidate set of declarations corresponding to the candidate functions, generate alternative test cases based on the candidate set of declarations, and iteratively evolve the alternative test cases based on a heuristic search algorithm to generate target test cases. The target test cases are used to test the function under test.

[0040] For example, before calculating a function using a heuristic search algorithm, a statement needs to be set to determine a candidate set. The heuristic search algorithm then calculates the functions in the candidate set to generate target test cases. Specifically, in this embodiment, after determining the candidate functions, a candidate set of statements corresponding to the candidate functions is determined, and alternative test cases are generated. The process of generating test cases is not detailed here. Then, the heuristic search algorithm evolves the alternative test cases to generate the target test cases. In this process, functions unrelated to the function under test are not included in the statement candidate set, greatly reducing the number of functions in the statement candidate set. This prevents the heuristic search algorithm from experiencing an explosion in the number of functions and parameters when evolving the alternative test cases generated from the functions in the statement candidate set, thus avoiding the problem of slowing down test case generation or even failing to generate test cases. Only candidate functions are calculated, not other functions unrelated to the function under test.

[0041] In this embodiment, the function structure information is obtained by parsing the function under test. This information represents the calling relationship between the functions called by the function under test. Based on the function structure information, candidate functions are determined. These candidate functions are the calling functions called within the function under test. A set of declarations corresponding to the candidate functions is determined. Based on this set, alternative test cases are generated. A heuristic search algorithm is then used to iteratively evolve the alternative test cases to generate target test cases, which are then used to test the function under test. Because the candidate functions called within the function under test are determined first using the function structure information, and then the alternative test cases corresponding to these candidate functions are calculated using the heuristic search algorithm to generate target test cases, the explosion in the number of related calling functions can be avoided. This allows the heuristic search algorithm to converge quickly, improving the generation speed and quality of target test cases.

[0042] refer to Figure 3 , Figure 3 A flowchart illustrating the test case generation method provided in this embodiment of the disclosure. Figure 2 This embodiment further refines steps S102-S103, describing in detail the steps of evolving the test function based on a heuristic search algorithm. The test case generation method includes:

[0043] Step S201: parse the function to be tested to obtain function structure information. The function structure information is used to characterize the calling relationship of the function to be tested.

[0044] Step S202: Based on the function structure information, determine the candidate functions. The candidate functions are the calling functions called inside the function to be tested.

[0045] Optionally, after determining the candidate functions, the process also includes:

[0046] Obtain the simulation data of the candidate function, which is used to characterize the simulated output value of the candidate function; based on the simulation data of the candidate function, generate the virtual function corresponding to the candidate function, which is executed when the candidate function is called and outputs the simulated output value to replace the call to the candidate function.

[0047] The following is a specific example to illustrate this:

[0048]

[0049] Referring to the function body shown above, in the `targetFunc()` function, `bytedan.RPC()` is a remote procedure call method in the program `bytedan`. Since `bytedan` is another program in the microservice architecture, it may be stored in other code repositories. During testing, directly calling the `bytedan.RPC()` method in this function value will cause an error. Therefore, this problem can be solved by simulating this method and truncating its call to the method in the external program. Specifically, for example, by obtaining the preset simulation data of the `bytedan.RPC(){}` function, such as 2, a simulation function for simulating the `bytedan.RPC(){}` function is generated. After the `bytedan.RPC(){}` function is called, the program executes the corresponding simulation function and returns a simulation data (2), thereby replacing the process of calling the external program. For example, the virtual function is generated by the mock component, which is a tool for simulating functions, such as the Monkey program. In the subsequent steps, when it comes to calling the candidate function, the corresponding virtual function can be used to respond to it, which will not be explained further.

[0050] Step S203: Determine the candidate set of declarations corresponding to the candidate functions.

[0051] Step S204: Generate alternative test cases based on the declared candidate set.

[0052] For example, the candidate set is information used to determine the functions to be computed and evolved during the generation of test cases based on the heuristic search algorithm. The candidate set needs to be set before the heuristic search algorithm is executed, and then alternative test cases are generated based on the candidate set.

[0053] Optionally, the function structure information includes an abstract syntax tree, such as Figure 4 As shown, step S204 includes three specific implementation steps: S2041, S2042, and S2043.

[0054] Step S2041: Based on reflection mechanism and abstract syntax tree, determine the predicate information of candidate functions corresponding to the declaration candidate set, wherein the predicate information is used to characterize the triggering conditions in the kernel predicate of the candidate function.

[0055] For example, after determining the candidate set of declarations, the predicate information of the candidate functions corresponding to the candidate set of declarations is determined based on reflection and abstract syntax trees. This includes: instrumenting the kernel predicates of the candidate functions corresponding to the candidate set of declarations to generate stub functions; and obtaining the predicate information of the candidate functions corresponding to the candidate set of declarations based on the stub functions. For example, the kernel predicate is a conditional statement declaration, such as "if" in a conditional statement, and the predicate information represents the triggering condition within the kernel predicate, i.e., the conditional statement in the if statement.

[0056] Step S2042: Generate the target field and its corresponding field value based on the predicate information; wherein, the target field is the field referenced by the triggered condition in the kernel predicate of the candidate function.

[0057] Step S2043: Generate alternative test cases based on the target field and its corresponding field value.

[0058] Heuristic search cannot handle complex structures with many fields. Evolutionary algorithms primarily involve adding or subtracting a Gaussian-distributed digit from numeric values, or adding or subtracting a character from string values. The main reason complex structures are difficult to compute is the sheer number of fields; evolutionary algorithms only work on one field at a time, making it impossible to handle so many. This method first instrumentes the predicate of the function under test. The predicate typically refers to an `If` statement. Using Go's reflection mechanism, the method in this embodiment obtains the type and name of the word on the left side of the predicate, and the value and type of the word on the right side. Through combination, permutation, and search, fields with the same numeric type or name as the core predicate of the complex structure can be found and assigned values. This method avoids constructing complex structures, instead constructing only a few fields to generate alternative test cases. It also prepares conditions for hitting branches (predicates), improving code coverage.

[0059]

[0060] Taking the function body above as an example (the numbers before each line of code are the line numbers):

[0061] In line 3 of the code, the content of Field1 in `Response` is validated. If Field1 is 100, the function returns 5. Using Go's Abstract Syntax Tree (AST) and reflection, the content of line 3 can be obtained. Therefore, during the evolution of the heuristic search algorithm, Field1 is assigned the value 100. During the algorithm's evolution, by assigning 100 to Field1 in `Response`, we can successfully enter the branch in line 4, thus improving code coverage on one side. This method can significantly reduce the computation time of the genetic algorithm.

[0062] Step S205: Generate a suite of alternative test cases based on the alternative test cases.

[0063] A test case suite is a collection of multiple alternative test cases. By using preset grouping rules or random grouping, the multiple alternative test cases generated in the above steps are combined into a set of alternative test case suites.

[0064] Step S206: Based on a heuristic search algorithm, the candidate test case suite is iteratively evolved to generate a target test case suite, which contains the target test cases.

[0065] The candidate test case suite includes multiple test cases, each composed of numerous declarations. This embodiment uses a heuristic search algorithm to calculate a score for each candidate test case in the suite, which can be determined based on factors such as code coverage. Then, based on each candidate test case, the score of the corresponding candidate test case suite is determined. Thus, an optimal candidate test case suite is selected from the multiple candidate test case suites. This suite is then evolved to generate multiple new candidate test case suites, and the evaluation and evolution continue until a convergence condition is met. The candidate test case suite that meets the convergence condition is selected as the target test case suite, and the candidate test cases in the target test case suite are selected as the target test cases.

[0066] Optionally, heuristic search algorithms include genetic algorithms, such as... Figure 5 As shown, step S206 includes four specific implementation steps: S2061, S2062, S2063, and S2064.

[0067] Step S2061: Calculate the fitness value of the candidate test case suite based on the fitness function of the genetic algorithm;

[0068] Step S2062: Determine the iterative test case suite based on the fitness value of the candidate test case suites, wherein the iterative test case suite is the candidate test case suite with the highest fitness value;

[0069] Step S2063: According to the genetic algorithm, the iterative test case suite is mutated into a preset number of secondary alternative test case suites;

[0070] Step S2064: Based on the fitness function of the genetic algorithm, iteratively calculate the fitness value of the secondary candidate test case suite, and determine the iterative test cases corresponding to the secondary candidate test case suite, until the iterative test cases meet the stopping condition of the genetic algorithm.

[0071] Figure 6This is a schematic diagram illustrating a method for determining a target test case suite based on a genetic algorithm, as provided in an embodiment of this disclosure. Figure 6 As shown, exemplarily, based on a preset fitness function of the genetic algorithm, the fitness values ​​of candidate test case suites A, B, C, and D are calculated respectively. The specific definition of the fitness value is determined by the fitness function, for example, by code coverage; no specific restrictions are imposed here. Then, the candidate test case suite with the highest fitness value among the four, for example, candidate test case suite C, is used as an iterative test case suite for mutation, generating a preset number (e.g., four) of secondary candidate test case suites, namely secondary candidate test case suite C1, secondary candidate test case suite C2, secondary candidate test case suite C3, and secondary candidate test case suite C4. Next, using the same method as in the steps above for processing candidate test case suites A, B, C, and D, the fitness values ​​of secondary candidate test case suites C1, C2, C3, and C4 are calculated. The secondary candidate test case suite with the highest fitness value is selected as the iterative test case suite, mutated, and a preset number of corresponding secondary candidate test case suites are generated again. This iterative calculation continues until the stopping condition of the genetic algorithm is met, such as the fitness value of a test case or test case suite being greater than a preset value. This yields one or a set of optimal test cases, i.e., the target test cases.

[0072] Optionally, after obtaining the target test case suite, it can be trimmed and modified, for example, by trimming it according to the length of the target test case suite or the test cases within it, to generate a target test case suite or test cases of appropriate length. Additionally, this embodiment also includes adding assertions to the declarations to ensure the accuracy of the test cases.

[0073] Step S207: Compile the target test cases to generate executable test cases. The executable test cases are used to test the function under test after execution.

[0074] For example, in Go, the target test cases need to be compiled before execution. Therefore, after obtaining the target test cases, they are compiled to obtain the corresponding compiled executable file. Running this executable file allows testing of the function under test based on the target test cases.

[0075] In this embodiment, the implementation methods of steps S201 and S202 are the same as those of this disclosure. Figure 2 The implementation methods of steps S101 and S102 in the illustrated embodiment are the same, and will not be described in detail here.

[0076] Corresponding to the test case generation method in the above embodiment, Figure 7 This is a structural block diagram of a test case generation apparatus provided in an embodiment of this disclosure. For ease of explanation, only the parts relevant to the embodiments of this disclosure are shown. (Refer to...) Figure 7 The test case generation device 3 includes:

[0077] The parsing module 31 is used to parse the function under test and obtain the function structure information. The function structure information is used to characterize the calling relationship of the function under test.

[0078] The determination module 32 is used to determine candidate functions based on function structure information. The candidate functions are the calling functions called inside the function under test.

[0079] The generation module 33 is used to determine the candidate set of declarations corresponding to the candidate functions, generate alternative test cases based on the candidate set of declarations, and iteratively evolve the alternative test cases based on a heuristic search algorithm to generate the target test cases.

[0080] In one embodiment of this disclosure, the function structure information includes an abstract syntax tree; when generating candidate test cases based on the declaration candidate set, the generation module 33 is specifically used to: determine the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection mechanism and abstract syntax tree, wherein the predicate information is used to characterize the triggering conditions within the kernel predicate of the candidate function; generate target fields and corresponding field values ​​according to the predicate information; wherein the target field is the field referenced by the triggering condition within the kernel predicate of the candidate function; and generate candidate test cases according to the target fields and corresponding field values.

[0081] In one embodiment of this disclosure, when the generation module 33 determines the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection mechanism and abstract syntax tree, it is specifically used to: instrument the kernel predicates of the candidate functions corresponding to the declaration candidate set to generate stub functions; and obtain the predicate information of the candidate functions corresponding to the declaration candidate set based on the stub functions.

[0082] In one embodiment of this disclosure, the kernel predicate is a conditional statement declaration.

[0083] In one embodiment of this disclosure, the heuristic search algorithm includes a genetic algorithm. When the generation module 33 iteratively evolves candidate test cases based on the heuristic search algorithm to generate target test cases, it specifically performs the following: calculating the fitness value of candidate test cases based on the fitness function of the genetic algorithm; determining iterative test cases based on the fitness values ​​of the candidate test cases, wherein the iterative test cases are the candidate test cases with the highest fitness values; mutating the iterative test cases into a preset number of secondary candidate test cases according to the genetic algorithm; iteratively calculating the fitness values ​​of the secondary candidate test cases based on the fitness function of the genetic algorithm, and determining the iterative test cases corresponding to the secondary candidate test cases, until the iterative test cases meet the stopping condition of the genetic algorithm.

[0084] In one embodiment of this disclosure, the determining module 32 is further configured to: generate a suite of candidate test cases based on the candidate test cases; the heuristic search algorithm includes a genetic algorithm, and when the generating module 33 iteratively evolves the candidate test cases based on the heuristic search algorithm to generate target test cases, it is specifically configured to: iteratively evolve the suite of candidate test cases based on the genetic algorithm to generate a target test case suite, wherein the target test case suite contains target test cases.

[0085] In one embodiment of this disclosure, the function structure information includes a call graph; the parsing module 31 is specifically used to: obtain a preset call graph component; parse the function to be tested according to the call graph component, and generate a call graph of the function to be tested, wherein the call graph is an abstract expression of the function to be tested.

[0086] In one embodiment of this disclosure, the determining module 32 is further configured to: acquire simulation data of the candidate function, the simulation data being used to characterize the simulation output value of the candidate function; generate a virtual function corresponding to the candidate function based on the simulation data of the candidate function, the virtual function being executed when the candidate function is called, and outputting a simulation output value to replace the call to the candidate function.

[0087] In one embodiment of this disclosure, the virtual function is generated by a mock component.

[0088] In one embodiment of this disclosure, the function under test is based on the Go language; the generation module 33 is further configured to: compile the target test case to generate an executable test case, which is used to test the function under test after being executed.

[0089] The test case generation device 3 provided in this embodiment can be used to execute the technical solution of the above method embodiment. Its implementation principle and technical effect are similar, and will not be described again in this embodiment.

[0090] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure, such as... Figure 8 As shown, the electronic device 4 includes at least one processor 41 and a memory 42;

[0091] Memory 42 stores instructions executed by the computer;

[0092] At least one processor 41 executes computer execution instructions stored in memory 42, causing at least one processor 41 to perform, for example, Figures 2-6 The test case generation method in the illustrated embodiment.

[0093] The processor 41 and the memory 42 are connected via a bus 43.

[0094] For relevant instructions, please refer to the corresponding text. Figures 2-6 The relevant descriptions and effects of the steps in the corresponding embodiments are understood, and will not be elaborated on here.

[0095] refer to Figure 9 The diagram illustrates a structural schematic of an electronic device 900 suitable for implementing embodiments of the present disclosure. The electronic device 900 can be a terminal device or a server. The terminal device can include, but is not limited to, mobile terminals such as mobile phones, laptops, digital radio receivers, personal digital assistants (PDAs), portable Android devices (PADs), portable media players (PMPs), and in-vehicle terminals (e.g., in-vehicle navigation terminals), as well as fixed terminals such as digital TVs and desktop computers. Figure 9 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.

[0096] like Figure 9 As shown, the electronic device 900 may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage device 908 into a random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the electronic device 900. The processing unit 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0097] Typically, the following devices can be connected to I / O interface 905: input devices 906 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 907 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 908 including, for example, magnetic tapes, hard disks, etc.; and communication devices 909. Communication device 909 allows electronic device 900 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 9 An electronic device 900 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.

[0098] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 909, or installed from a storage device 908, or installed from a ROM 902. When the computer program is executed by a processing device 901, it performs the functions defined in the methods of embodiments of this disclosure.

[0099] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in connection with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.

[0100] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.

[0101] The aforementioned computer-readable medium carries one or more programs, which, when executed by the electronic device, cause the electronic device to perform the methods shown in the above embodiments.

[0102] Computer program code for performing the operations of this disclosure can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, and C++, and conventional procedural programming languages ​​such as the "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 standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0103] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0104] The units described in the embodiments of this disclosure can be implemented in software or in hardware. The name of a unit does not necessarily limit the unit itself; for example, the first acquisition unit can also be described as "a unit that acquires at least two Internet Protocol addresses".

[0105] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: Field Programmable Gate Arrays (FPGAs), Application-Specific Integrated Circuits (ASICs), Application Standard Products (ASSPs), System-on-Chip (SoCs), Complex Programmable Logic Devices (CPLDs), and so on.

[0106] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0107] In a first aspect, according to one or more embodiments of this disclosure, a test case generation method is provided, comprising:

[0108] The function to be tested is parsed to obtain its structure information, which represents the calling relationship between the function to be tested and other functions. Based on the structure information, candidate functions are determined, which are the calling functions called internally by the function to be tested. The candidate functions are calculated using a heuristic search algorithm to generate target test cases, which are used to test the function to be tested.

[0109] According to one or more embodiments of this disclosure, the function structure information includes an abstract syntax tree; generating candidate test cases based on the declaration candidate set includes: determining the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection and the abstract syntax tree, wherein the predicate information is used to characterize the triggering condition within the kernel predicate of the candidate function; generating a target field and its corresponding field value based on the predicate information; wherein the target field is a field within the kernel predicate of the candidate function that is referenced by the triggering condition; and generating candidate test cases based on the target field and its corresponding field value.

[0110] According to one or more embodiments of this disclosure, determining the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection mechanism and abstract syntax tree includes: instrumenting the kernel predicates of the candidate functions corresponding to the declaration candidate set to generate stub functions; and obtaining the predicate information of the candidate functions corresponding to the declaration candidate set by calling the stub functions.

[0111] According to one or more embodiments of this disclosure, the kernel predicate is a conditional statement declaration.

[0112] According to one or more embodiments of this disclosure, the heuristic search algorithm includes a genetic algorithm. Based on the heuristic search algorithm, the candidate test cases are iteratively evolved to generate target test cases, including: calculating the fitness value of the candidate test cases based on the fitness function of the genetic algorithm; determining iterative test cases based on the fitness values ​​of the candidate test cases, wherein the iterative test cases are the candidate test cases with the highest fitness values; mutating the iterative test cases into a preset number of secondary candidate test cases according to the genetic algorithm; iteratively calculating the fitness values ​​of the secondary candidate test cases based on the fitness function of the genetic algorithm, and determining the iterative test cases corresponding to the secondary candidate test cases, until the iterative test cases meet the stopping condition of the genetic algorithm.

[0113] According to one or more embodiments of this disclosure, the method further includes: generating a suite of candidate test cases based on the candidate test cases; the heuristic search algorithm includes a genetic algorithm, and iteratively evolving the candidate test cases based on the heuristic search algorithm to generate target test cases includes: iteratively evolving the suite of candidate test cases based on the genetic algorithm to generate target test case suite, wherein the target test case suite contains the target test cases.

[0114] According to one or more embodiments of this disclosure, the function structure information includes a call graph; parsing the function to be tested to obtain function structure information includes: obtaining a preset call graph component; parsing the function to be tested according to the call graph component to generate a call graph of the function to be tested, wherein the call graph is an abstract expression of the function to be tested.

[0115] According to one or more embodiments of this disclosure, the method further includes: obtaining simulated data of the candidate function, the simulated data being used to characterize the simulated output value of the candidate function; generating a virtual function corresponding to the candidate function based on the simulated data of the candidate function, the virtual function being executed when the candidate function is called, and outputting the simulated output value to replace the call to the candidate function.

[0116] According to one or more embodiments of this disclosure, the virtual function is generated by a mock component.

[0117] According to one or more embodiments of this disclosure, the function under test is based on the Go language; the method further includes: compiling the target test case to generate an executable test case, wherein the executable test case is used to test the function under test after being executed.

[0118] Secondly, according to one or more embodiments of this disclosure, a test case generation apparatus is provided, comprising:

[0119] The parsing module is used to parse the function under test and obtain function structure information, which is used to characterize the call relationship between the function under test and other functions;

[0120] The determination module is used to determine candidate functions based on the function structure information, wherein the candidate functions are the calling functions called inside the function to be tested;

[0121] The generation module is used to determine the declaration candidate set corresponding to the candidate function, generate alternative test cases based on the declaration candidate set, and iteratively evolve the alternative test cases based on a heuristic search algorithm to generate target test cases, which are used to test the function to be tested.

[0122] According to one or more embodiments of this disclosure, the function structure information includes an abstract syntax tree; when generating candidate test cases based on the declaration candidate set, the generation module is specifically configured to: determine the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection mechanism and abstract syntax tree, wherein the predicate information is used to characterize the triggering condition within the kernel predicate of the candidate function; generate a target field and its corresponding field value based on the predicate information; wherein the target field is a field within the kernel predicate of the candidate function referenced by the triggering condition; and generate candidate test cases based on the target field and its corresponding field value.

[0123] According to one or more embodiments of this disclosure, when the generation module determines the predicate information of the candidate functions corresponding to the declaration candidate set based on reflection mechanism and abstract syntax tree, it is specifically used to: instrument the kernel predicates of the candidate functions corresponding to the declaration candidate set to generate stub functions; and obtain the predicate information of the candidate functions corresponding to the declaration candidate set by calling the stub functions.

[0124] According to one or more embodiments of this disclosure, the kernel predicate is a conditional statement declaration.

[0125] According to one or more embodiments of this disclosure, the heuristic search algorithm includes a genetic algorithm. When the generation module iteratively evolves the candidate test cases based on the heuristic search algorithm to generate target test cases, it is specifically configured to: calculate the fitness value of the candidate test cases based on the fitness function of the genetic algorithm; determine iterative test cases based on the fitness values ​​of the candidate test cases, wherein the iterative test cases are the candidate test cases with the highest fitness values; mutate the iterative test cases into a preset number of secondary candidate test cases according to the genetic algorithm; iteratively calculate the fitness values ​​of the secondary candidate test cases based on the fitness function of the genetic algorithm, and determine the iterative test cases corresponding to the secondary candidate test cases, until the iterative test cases meet the stopping condition of the genetic algorithm.

[0126] According to one or more embodiments of this disclosure, the determining module is further configured to: generate a suite of candidate test cases based on the candidate test cases; the heuristic search algorithm includes a genetic algorithm, and when the generating module iteratively evolves the candidate test cases based on the heuristic search algorithm to generate target test cases, it is specifically configured to: iteratively evolve the suite of candidate test cases based on the genetic algorithm to generate a target test case suite, wherein the target test case suite contains the target test cases.

[0127] According to one or more embodiments of this disclosure, the function structure information includes a call graph; the parsing module is specifically used to: obtain a preset call graph component; parse the function under test according to the call graph component, and generate a call graph of the function under test, wherein the call graph is an abstract expression of the function under test.

[0128] According to one or more embodiments of this disclosure, the determining module is further configured to: acquire simulation data of the candidate function, the simulation data being used to characterize the simulation output value of the candidate function; generate a virtual function corresponding to the candidate function based on the simulation data of the candidate function, the virtual function being executed when the candidate function is called, and outputting the simulation output value to replace the call to the candidate function.

[0129] According to one or more embodiments of this disclosure, the virtual function is generated by a mock component.

[0130] According to one or more embodiments of this disclosure, the function under test is based on the Go language; the generation module is further configured to: compile the target test case to generate an executable test case, the executable test case being used to test the function under test after being executed.

[0131] Thirdly, according to one or more embodiments of this disclosure, an electronic device is provided, comprising: at least one processor and a memory; the memory storing computer-executable instructions; the at least one processor executing the computer-executable instructions stored in the memory, such that the at least one processor performs the test case generation method as described in the first aspect above and various possible designs of the first aspect.

[0132] Fourthly, according to one or more embodiments of the present disclosure, a computer-readable storage medium is provided, wherein computer-executable instructions are stored therein, and when a processor executes the computer-executable instructions, the test case generation method described in the first aspect and various possible designs of the first aspect is implemented.

[0133] Fifthly, embodiments of this disclosure provide a computer program product, including a computer program that, when executed by a processor, implements the test case generation method described in the first aspect and various possible designs of the first aspect.

[0134] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.

[0135] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.

[0136] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.

Claims

1. A test case generation method, characterized in that, The method includes: The function under test is parsed to obtain its function structure information, which is used to characterize the call relationship between the function under test and other functions; the function structure information includes an abstract syntax tree. Based on the function structure information, candidate functions are determined, and the candidate functions are the calling functions called inside the function to be tested; Determine the candidate set of declarations corresponding to the candidate functions; Based on reflection and abstract syntax trees, the predicate information of the candidate functions corresponding to the candidate declaration set is determined; the predicate information is used to characterize the triggering conditions within the kernel predicate of the candidate function. Based on the predicate information, a target field and its corresponding field value are generated; the target field is the field in the kernel predicate of the candidate function that is referenced by the triggering condition. Based on the target field and its corresponding value, generate alternative test cases; Based on a heuristic search algorithm, the candidate test cases are iteratively evolved to generate target test cases, which are used to test the function under test.

2. The method according to claim 1, characterized in that, Based on reflection and abstract syntax trees, the predicate information of the candidate functions corresponding to the candidate declaration set is determined, including: Instrument the kernel predicates of the candidate functions corresponding to the candidate declaration set to generate stub functions; Based on the invocation of the stub function, the predicate information of the candidate functions corresponding to the candidate declaration set is obtained.

3. The method according to claim 1, characterized in that, The kernel predicate is a conditional statement declaration.

4. The method according to claim 1, characterized in that, The heuristic search algorithm includes a genetic algorithm. Based on the heuristic search algorithm, the candidate test cases are iteratively evolved to generate target test cases, including: The fitness value of the candidate test cases is calculated based on the fitness function of the genetic algorithm. Based on the fitness values ​​of the candidate test cases, iterative test cases are determined, wherein the iterative test cases are the candidate test cases with the highest fitness values. According to the genetic algorithm, the iterative test cases are mutated into a preset number of secondary candidate test cases; Based on the fitness function of the genetic algorithm, the fitness value of the secondary candidate test cases is iteratively calculated, and the iterative test cases corresponding to the secondary candidate test cases are determined until the iterative test cases meet the stopping condition of the genetic algorithm.

5. The method according to claim 1, characterized in that, The method further includes: Based on the candidate test cases, generate a suite of candidate test cases; The heuristic search algorithm includes a genetic algorithm. Based on the heuristic search algorithm, the candidate test cases are iteratively evolved to generate target test cases, including: Based on the genetic algorithm, the candidate test case suite is iteratively evolved to generate a target test case suite, which contains the target test cases.

6. The method according to claim 1, characterized in that, The function structure information includes a call graph; Analyze the function under test to obtain its structure information, including: Retrieve the preset call graph component; Based on the call graph component, the function under test is parsed to generate a call graph of the function under test, wherein the call graph is an abstract expression of the function under test.

7. The method according to any one of claims 1-6, characterized in that, The method further includes: Obtain simulation data of the candidate function, wherein the simulation data is used to characterize the simulated output value of the candidate function; Based on the simulation data of the candidate function, a virtual function corresponding to the candidate function is generated. The virtual function is executed when the candidate function is called and outputs the simulated output value to replace the call to the candidate function.

8. The method according to claim 7, characterized in that, The virtual function is generated using the mock component.

9. The method according to any one of claims 1-6, characterized in that, The function to be tested is based on the Go language; the method also includes: The target test case is compiled to generate an executable test case, which is used to test the function under test after being executed.

10. A test case generation device, characterized in that, include: The parsing module is used to parse the function under test and obtain function structure information, which is used to characterize the call relationship between the function under test and other functions; The function structure information includes an abstract syntax tree; The determination module is used to determine candidate functions based on the function structure information, wherein the candidate functions are the calling functions called inside the function to be tested; The generation module is used to determine the candidate set of declarations corresponding to the candidate functions; Based on reflection and abstract syntax trees, the predicate information of the candidate functions corresponding to the declaration candidate set is determined; The predicate information is used to characterize the triggering conditions within the kernel predicate of the candidate function; Based on the predicate information, a target field and its corresponding field value are generated; the target field is the field in the kernel predicate of the candidate function that is referenced by the triggering condition. Based on the target field and its corresponding value, generate alternative test cases; Based on a heuristic search algorithm, the candidate test cases are iteratively evolved to generate target test cases, which are used to test the function under test.

11. An electronic device, characterized in that, include: At least one processor and memory; The memory stores computer-executed instructions; The at least one processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the test case generation method as described in any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by the processor, implement the test case generation method as described in any one of claims 1 to 9.

13. A computer program product comprising a computer program that, when executed by a processor, implements the test case generation method of any one of claims 1 to 9.