Test case generation method and related system
Through the test case generation method and system, the test cases and compilation configuration files of the checker are automatically generated, which solves the problems of development complexity and cumbersome test case development in traditional static analysis tools, and improves development efficiency and verification efficiency.
Patent Information
- Application Number
- CN202310544891.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-03-06
- Filing Date
- 2023-05-15
- Publication Date
- 2024-09-13
AI Technical Summary
In traditional static analysis tools, the coupling between the checker and the underlying framework is high, resulting in developers needing to master complex underlying framework logic, which increases the difficulty of checker development, and test case development is cumbersome, becoming a bottleneck in development efficiency.
Provide a test case generation method and system that automatically generates code snippets for verifying the correctness of the checker by parsing the checker code, and helps users automatically mark erroneous code snippets, reducing development complexity and testing costs. This method supports generating compilation configuration files and simplifies the testing process.
It improves test development efficiency and verification efficiency, reduces checker development and testing costs, simplifies the test case generation process, and improves development efficiency.
Smart Images

Figure CN118642937A
Abstract
Description
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on March 6, 2023, with application number 202310207427.8 and invention name “A test case generation method and related system”, the entire contents of which are incorporated by reference in this application. Technical Field
[0002] The present application relates to the field of testing technology, and in particular to a test case generation method, system, computing device cluster, computer-readable storage medium, and computer program product. Background Art
[0003] With the rise of new generation technologies such as mobile Internet, cloud computing, and artificial intelligence, the scale and complexity of software are constantly expanding, and the pressure on software security is also increasing. Among the many security assurance technologies, static analysis technology can discover potential security risks in software at a low cost by analyzing the software code logic without running the software, and has therefore been widely used.
[0004] Static analysis tools include an underlying framework and multiple checkers. Each checker can correspond to a check rule. The underlying framework is responsible for parsing the input code and storing the code in a specific data structure, such as an abstract syntax tree or a control flow graph. Based on this, the checker detects the input code according to the check rules to find hidden errors.
[0005] In traditional static analysis tools, the coupling between the checker and the underlying framework is high. Developers usually need to master the principles of the underlying framework to write the checker, and the logic of the underlying framework is often complex, which increases the difficulty of writing. In order to improve development efficiency and decouple the checker from the underlying framework, most new static analysis tools use a checker development model based on domain-specific language (DSL). Compared with the underlying framework, the learning cost of DSL is lower, and developers only need to master the syntax of DSL to develop the checker.
[0006] Based on software engineering specifications, after the checker is developed, it is usually necessary to test it. Although the above DSL-based solution reduces the development cost of the checker, the cumbersome test case development gradually becomes an efficiency bottleneck in the entire development process. Summary of the invention
[0007] The present application provides a test case generation method, which can improve the test development efficiency and help users automatically mark the error code fragments that conform to the checker check logic, thereby improving the verification efficiency. The present application also provides a test case generation system, a computing device cluster, a computer-readable storage medium, and a computer program product.
[0008] In a first aspect, the present application provides a test case generation method. The method can be executed by a test case generation system. The test case generation system is used to generate test cases for a checker, and the checker is used to perform static analysis on the input code. The test case generation system can be a software system, and the software system can be independent software, such as an independent component, an independent cloud service, and the software system can also be integrated into other software in the form of a plug-in, such as an integrated program analysis engine. The software system can be deployed in a computing device cluster, and the computing device cluster executes the program code of the software system, thereby executing the test case generation method. The test case generation system can also be a hardware system with a test case generation function, and when the hardware system is running, it executes the test case generation method of the present application.
[0009] The test case generation system automatically generates code snippets for verifying the correctness of the checker based on the error program nodes defined in the code and the associated query conditions by parsing the checker code, thereby improving the test development efficiency and helping users to automatically mark the error code snippets that conform to the checker's check logic to improve the verification efficiency. Furthermore, the language to be tested is a compiled language (also called a compiled language), that is, the input code is written in a compiled language, such as a general compiled language. The test case generation system can also automatically generate the compilation configuration files corresponding to the external components by identifying the external components involved in the code (such as external library), thereby helping users to generate a complete project project (test case) for testing.
[0010] In this method, the test case generation system parses the checker code and automatically generates code snippets for verifying the correctness of the checker based on the erroneous program nodes defined in the code and the associated query conditions, thereby improving test development efficiency and helping users automatically mark erroneous code snippets that conform to the checker's inspection logic, thereby improving verification efficiency.
[0011] In some possible implementations, the test case generation system may generate a test oracle for the test case according to the output format of the test oracle configured by the user, so as to generate a test oracle that meets the user's needs.
[0012] In some possible implementations, the test case generation system may generate an annotation of the first code snippet according to the output format of the test oracle configured by the user. The test oracle of the test case includes the annotation of the first code snippet. Alternatively, the test case generation system may generate a prophecy file independent of the first code snippet according to the output format of the test oracle configured by the user.
[0013] Among them, the test prophecy in the form of comments is located near the code snippet, which is convenient for users to test and verify. The independent prophecy file facilitates the quick location of the test prophecy and the user to modify and maintain the test prophecy.
[0014] In some possible implementations, the input code can be written in a compiled language. Compiled language is a type of programming language that is implemented by a compiler. Unlike an interpreted language (also called a literal translation language), where the program code is executed sentence by sentence by an interpreter (also called a translator), a compiler is used to compile the program code into machine code before executing it. For example, compiled languages may include but are not limited to JAVA, C, or C++. Accordingly, the test case generation system can also generate a compilation configuration file for the test case according to a compilation tool configured by the user.
[0015] This method can automatically generate compilation configuration files such as compilation configuration scripts, simplify the testing process, and improve testing efficiency.
[0016] In some possible implementations, when the test case generation system generates a compilation configuration file of the test case according to the compilation tool configured by the user, it can generate a template of the compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user. When the code of the checker includes reference information of an external component, the test case generation system obtains the compilation information of the external component, and adds the compilation information of the external component to the template to obtain the compilation configuration file.
[0017] This method identifies the external components involved in the checker code and automatically generates compilation configuration files corresponding to the external components, helping users generate a complete project for testing and improving testing efficiency.
[0018] In some possible implementations, when the checker code does not include reference information of the external component, for example, when the match with the external component fails, the test case generation system can determine the template of the compilation configuration file corresponding to the compilation tool as the compilation configuration file. This method supports identifying the external components involved in the checker code, and when the external components are not identified, directly determining the compilation configuration file according to the template of the compilation configuration file corresponding to the compilation tool, thereby improving the test efficiency.
[0019] In some possible implementations, the error program node includes one or more of a method declaration node, a variable use node, and an assignment expression node. Specifically, an error program node is a program node that can cause an alarm in the checker. The checker is used to perform static analysis on the input code (e.g., the code of the program to be tested). During static analysis, the checker can identify the program nodes in the code of the program to be tested and perform static analysis based on the program nodes. Taking into account that the inspection rules or inspection logics of different checkers are different, the error program nodes corresponding to the inspection rules or inspection logic can be defined in the code of the checker, such as one or more of the above-mentioned method declaration nodes (or function declaration nodes), variable use nodes, and assignment expression nodes.
[0020] The method can provide references for constructing positive example code snippets and counterexample code snippets by identifying method declaration nodes, variable use nodes, and assignment expression nodes, thereby realizing automatic generation of test cases.
[0021] In some possible implementations, the query condition associated with the error program node is a condition related to the error program node and used to determine whether to trigger an alarm. The query condition can be represented by a conditional expression, for example, the query condition can be that the function name is xxx and the number of parameters is not greater than n.
[0022] By identifying the query conditions associated with the erroneous program nodes, this method can provide a reference for constructing positive example code snippets and negative example code snippets, thereby realizing automatic generation of test cases.
[0023] In some possible implementations, the code of the checker is a domain specific language (DSL) code, wherein DSL is a computer language specifically for a specific application domain, including but not limited to HTML or Maven.
[0024] Since DSL has a low learning cost, developers only need to master the syntax of DSL to develop the checker, which reduces the development cost of the checker.
[0025] In a second aspect, the present application provides a test case generation system. The test case generation system is used to generate test cases for a checker, the checker is used to perform static analysis on input code, and the system includes:
[0026] A parsing subsystem, used to obtain the code of the checker, parse the code of the checker, obtain an error program node defined in the code of the checker and a query condition associated with the error program node, wherein the error program node is a program node that can cause the checker to issue an alarm;
[0027] A generating subsystem, configured to generate a source file of the test case according to the error program node and the query condition, wherein the source file of the test case includes a first code fragment that meets the query condition and a second code fragment that does not meet the query condition;
[0028] The generation subsystem is further configured to generate a test oracle for the test case, wherein the test oracle indicates that the first code snippet is a code snippet that causes the checker to raise an alarm.
[0029] In some possible implementations, the generating subsystem is specifically used for:
[0030] Generate a test oracle for the test case according to an output format of the test oracle configured by a user.
[0031] In some possible implementations, the generating subsystem is specifically used for:
[0032] Generate a comment for the first code snippet according to an output format of the test oracle configured by a user, wherein the test oracle of the test case includes the comment for the first code snippet; or
[0033] A prediction file independent of the first code snippet is generated according to an output format of the test prediction configured by a user.
[0034] In some possible implementations, the input code is written in a compiled language, and the generation subsystem is further used for:
[0035] Generate a compilation configuration file for the test case according to the compilation tool configured by the user.
[0036] In some possible implementations, the generating subsystem is specifically used for:
[0037] Generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user;
[0038] When the code of the checker includes reference information of an external component, the compilation information of the external component is obtained, and the compilation information of the external component is added to the template to obtain the compilation configuration file.
[0039] In some possible implementations, the erroneous program node includes one or more of a method declaration node, a variable use node, and an assignment expression node.
[0040] In some possible implementations, the code of the checker is a domain specific language (DSL) code.
[0041] In a third aspect, the present application provides a computing device cluster. The computing device cluster includes at least one computing device, and the at least one computing device includes at least one processor and at least one memory. The at least one processor and the at least one memory communicate with each other. The at least one processor is used to execute instructions stored in the at least one memory, so that the computing device or the computing device cluster executes the test case generation method described in the first aspect or any implementation of the first aspect.
[0042] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, wherein the instructions instruct a computing device or a computing device cluster to execute the test case generation method described in the first aspect or any one of the implementations of the first aspect.
[0043] In a fifth aspect, the present application provides a computer program product comprising instructions, which, when executed on a computing device or a computing device cluster, enables the computing device or the computing device cluster to execute the test case generation method described in the first aspect or any one of the implementations of the first aspect.
[0044] Based on the implementations provided in the above aspects, this application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical method of the embodiments of the present application, the drawings required for use in the embodiments are briefly introduced below.
[0046] Figure 1 A schematic diagram of the architecture of a test case generation system provided in an embodiment of the present application;
[0047] Figure 2 A flowchart of a test case generation method provided in an embodiment of the present application;
[0048] Figure 3 A schematic diagram of an application scenario of a test case generation method provided in an embodiment of the present application;
[0049] Figure 4 A schematic diagram of a source file and a test prediction provided in an embodiment of the present application;
[0050] Figure 5 A schematic diagram of a compilation configuration script provided in an embodiment of the present application;
[0051] Figure 6 A schematic diagram of the structure of a computing device provided in an embodiment of the present application;
[0052] Figure 7A schematic diagram of the structure of a computing device cluster provided in an embodiment of the present application;
[0053] Figure 8 A schematic diagram of the structure of another computing device cluster provided in an embodiment of the present application;
[0054] Fig. 9 A schematic diagram of the structure of another computer cluster provided in an embodiment of the present application. DETAILED DESCRIPTION
[0055] The terms "first" and "second" in the embodiments of the present application are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.
[0056] First, some technical terms involved in the embodiments of the present application are introduced.
[0057] Static analysis, also known as static code analysis, is a software verification activity that does not require code execution, but achieves quality, reliability, and security goals by analyzing source code. Static analysis can be used to identify defects and security vulnerabilities that may endanger application security at a low cost, so it is widely used in the field of software testing.
[0058] Static analysis tools include an underlying framework and multiple checkers. The underlying framework is responsible for parsing the input code (for example, the source code of the software to be tested) and storing the input code in a specific data structure, such as an abstract syntax tree or a control flow graph. The checker is a functional module in the static analysis tool that detects potential problems in the input code based on certain check rules. Each checker can correspond to one check rule.
[0059] The checker may be developed using a domain specific language (DSL). A DSL may be a computer language specifically designed for a specific application domain. In some examples, the DSL may include Hyper Text Markup Language (HTML) for displaying web pages, and in other examples, the DSL may include Maven for compiling configurations.
[0060] According to software engineering specifications, the project needs to be tested after the development work is completed, and the checker is no exception. Obviously, the test input of the checker can be an engineering project including source files (which can be written in the language to be tested, including error code snippets for the checker to detect). In addition, the information of the error code snippet can be specially marked to facilitate the test program to verify the results.
[0061] Currently, developers usually use a domain-specific language provided by a program analysis engine based on a DSL to write a checker. When testing the checker, the developer needs to set up the test package and test file according to the test framework provided by the program analysis engine, and then call the program analysis engine to run the test.
[0062] Although the program analysis engine provides a test framework, developers need to prepare the source files and test oracles of the test cases by themselves. The entire test process is very cumbersome and the development efficiency is low. In other words, the DSL-based program analysis engine reduces the development cost of the checker, but the testing cost is not significantly reduced. The cumbersome test case development gradually becomes an efficiency bottleneck in the entire checker development process, making it difficult to meet business needs.
[0063] In view of this, the present application provides a test case generation method. The method can be executed by a test case generation system. The test case generation system is used to generate test cases for a checker, and the checker is used to perform static analysis on the input code. The test case generation system can be a software system, and the software system can be an independent software, such as an independent component, an independent cloud service, and the software system can also be integrated into other software in the form of a plug-in, such as an integration in a program analysis engine. The software system can be deployed in a computing device cluster, and the computing device cluster executes the program code of the software system, thereby executing the test case generation method. The test case generation system can also be a hardware system with a test case generation function, and when the hardware system is running, it executes the test case generation method of the present application.
[0064] Specifically, the test case generation system can obtain the code of the checker, parse the code of the checker, and obtain the error program node defined in the code of the checker and the query condition associated with the error program node. The error program node is a program node that can cause the checker to alarm. The test case generation system can generate the source file of the test case based on the error program node and the query condition. The source file of the test case includes a first code fragment that meets the query condition (usually an error code fragment, also called a counterexample code fragment) and a second code fragment that does not meet the query condition (usually a correct code fragment, also called a positive example code fragment). The test case system also generates a test prediction for the test case, which indicates that the above-mentioned first code fragment is a code fragment that causes the checker to alarm.
[0065] In this method, the test case generation system automatically generates code snippets for verifying the correctness of the checker based on the error program nodes defined in the code and the associated query conditions by parsing the checker code, thereby improving the test development efficiency and helping users to automatically mark the error code snippets that conform to the checker's check logic, thereby improving the verification efficiency. Furthermore, the language to be tested is a compiled language (also called a compiled language), that is, the input code is written in a compiled language, such as a general compiled language. The test case generation system can also automatically generate a compiled configuration file corresponding to the external component by identifying the external component (such as an external library) involved in the code, thereby helping users to generate a complete project project (test case) for testing.
[0066] In order to make the technical solution of the present application clearer and easier to understand, the system architecture of the present application is introduced below with reference to the accompanying drawings.
[0067] See also Figure 1 The schematic diagram of the architecture of the test case generation system shown in the figure, the test case generation system 10 includes a parsing subsystem 100 and a generating subsystem 200. Among them, the parsing subsystem 100 is used to obtain the code of the checker (such as DSL code), parse the code of the checker, and obtain the error program node defined in the code of the checker and the query condition associated with the error program node. The generating subsystem 200 is used to generate the source file of the test case according to the error program node and the query condition. The source file of the test case includes a first code fragment that meets the query condition and a second code fragment that does not meet the query condition, as well as a test prediction for generating the test case, and the test prediction indicates that the first code fragment is the code fragment that causes the checker to alarm.
[0068] Specifically, the parsing subsystem 100 includes a node type identification module 102 and a query condition identification module 104. The node type identification module 102 is used to identify the code of the checker and obtain the error program node defined in the code of the checker. The query condition identification module 104 is used to identify the code of the checker and obtain the query condition associated with the error program node. The generation subsystem 200 includes a positive and negative example code fragment generation module 202, a source file generation module 204, and a test prediction generation module 206. The positive and negative example code fragment generation module 202 is used to generate a positive example code fragment and a negative example code fragment according to the error program node and the associated condition. The positive example code fragment is a first code fragment that meets the query condition, and the negative example code fragment is a second code fragment that does not meet the query condition. The source file generation module 204 is used to generate a source file of a test case according to the positive example code fragment and the negative example code fragment. The test prediction generation module 206 is used to generate a test prediction of the test case, and the test prediction indicates that the positive example code fragment is a code fragment that causes the checker to alarm.
[0069] In some possible implementations, the generation subsystem 200 may further include a compilation configuration generation module 208. The compilation configuration generation module 208 is used to generate a compilation configuration file for the test case. The compilation configuration file may be a compilation configuration script. Specifically, the compilation configuration generation module 208 may generate a compilation configuration file for the test case when the predicted language to be tested is a compilation language, such as when the input code of the checker is a code written in a compilation language. The compilation configuration generation module 208 may generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user, and obtain the compilation configuration file based on the template.
[0070] Furthermore, the parsing subsystem 100 also includes an external component identification module 106, which is used to identify the code of the checker and obtain the reference information of the external component in the code of the checker (if it exists). Among them, the reference information of the external component may include one or more of the package name, class name, and method name. The generation subsystem 200 also includes an external component matching module 210, which is used to match the external component in a warehouse (for example, a remote warehouse) according to the reference information of the external component to obtain the compilation information of the external component. Accordingly, the compilation configuration generation module 208 can add the compilation information of the external component to the template of the compilation configuration file to obtain the compilation configuration file.
[0071] The test case generation system 10 of the embodiment of the present application can be logically divided into three layers, including a parsing layer, an application layer, and a user layer from bottom to top. The parsing layer includes the above-mentioned parsing subsystem 100, which is the basis for test case generation. The parsing layer obtains various necessary elements for generating test cases by disassembling and analyzing the code of the checker. The application layer and the user layer include the above-mentioned generation subsystem 200. The application layer directly generates the various components that make up the test case using the necessary elements parsed by the parsing layer, and these components are directly displayed to the user in the form of a complete test case through the user layer.
[0072] The test case generation method provided by the present application is described in detail below in conjunction with the embodiments.
[0073] See also Figure 2 The flowchart of the test case generation method shown in FIG. 1 comprises the following steps:
[0074] S201: The test case generation system 10 obtains the code of the checker.
[0075] A checker can be a functional module that detects potential problems in input code based on certain checking rules. Checkers can be divided into different types according to different development methods. For example, a checker can be a checker developed based on a domain-specific language DSL. In other words, the code of the checker can be DSL code. Among them, DSL is a computer language specifically for a specific application field, including but not limited to HTML or Maven.
[0076] S203: The test case generation system 10 parses the code of the checker to obtain the error program nodes defined in the code of the checker and the query conditions associated with the error program nodes.
[0077] An error program node is a program node that can cause an alarm in the checker. The checker is used to perform static analysis on the input code (the code of the program to be tested). During static analysis, the checker can identify the program nodes in the code of the program to be tested and perform static analysis based on the program nodes. Taking into account the different inspection rules or inspection logic of different checkers, error program nodes corresponding to the inspection rules or inspection logic can be defined in the code of the checker. In some examples, the program error node may include one or more of a method declaration node (for example, a function declaration node), a variable use node, and an assignment expression node.
[0078] Specifically, the test case generation system 10 can obtain the error program node defined in the checker code by semantically identifying the checker code. In other examples, the test case generation system 10 can obtain the error program node defined in the checker code by identifying keywords in the checker code.
[0079] The query condition associated with the error program node is specifically a condition related to the error program node and used to determine whether to trigger an alarm. The query condition can be represented by a conditional expression, for example, the query condition can be that the function name is xxx and the number of parameters is not greater than n.
[0080] S205: The test case generation system 10 generates a source file of the test case according to the error program node and the query condition.
[0081] The source file of the test case includes a first code snippet that meets the query condition and a second code snippet that does not meet the query condition. Among them, the first code snippet is a positive example code snippet, and the second code snippet is a negative example code snippet. The test case generation system 10 can generate an initialized positive example code snippet and an initialized negative example code snippet according to the error program node and the query condition, and then based on the configured number of positive and negative examples and the keyword library for completing the positive and negative example code snippets, select keywords from the keyword library to complete the initialized positive column code snippet and the initialized negative example code snippet respectively, and generate a corresponding number of positive column code snippets and negative example code snippets. Among them, the number of positive and negative examples and the keyword library for completing the positive and negative example code snippets can be pre-set or set during use, and this embodiment does not limit this.
[0082] After generating the positive example code snippet and the counterexample code snippet, the test case generation system 10 can assemble the positive example code snippet and the counterexample code snippet to generate a source file of the test case. Further, the test case generation system 10 can also supplement the missing information, such as calling import statements, to obtain a complete source file. Among them, the calling import statements corresponding to different languages can be different. Taking Java as an example, the calling import statement can be an import statement.
[0083] S207: The test case generation system 10 generates a test oracle for the test case.
[0084] In the program analysis scenario, the test prediction refers to a judgment mechanism that determines whether the tested code snippet is a problematic code snippet. In this embodiment, the positive code snippet is a code snippet that can cause the checker to warn. Based on this, the test case generation system 10 can generate a test prediction based on the positive code snippet. The test prediction indicates that the positive code snippet (such as the first code snippet mentioned above) is a code snippet that causes the checker to warn.
[0085] Specifically, the test case generation system 10 can generate a test prophecy of the test case according to the output format of the test prophecy configured by the user. In some examples, the output format of the test prophecy configured by the user can be a comment, and the test case generation system 10 can generate a comment of the first code snippet according to the output format of the test prophecy configured by the user, and the comment indicates that the first code snippet is a code snippet that causes the checker alarm. Correspondingly, the test prophecy of the test case includes the comment of the above-mentioned first code snippet. In other examples, the output format of the test voice configured by the user can be an independent prophecy file. Further, the user can also configure the format or path of the prophecy file. For example, the user can configure the format of the language file to be Extensible Markup Language (XML), and the test case generation system 10 generates a prophecy file independent of the first code snippet according to the output format of the test prophecy configured by the user. It should be noted that the test case generation system 10 can also set a default format or default path for the prophecy file. When the user does not configure the format or path of the prophecy file, the test case generation system 10 can generate a prophecy file according to the default format or default path. Through the test prophecy, the developer can obtain the basic information of the alarm, such as: the location information of the positive example code snippet and the alarm description.
[0086] S209: The test case generation system 10 generates a compilation configuration file of the test case according to the compilation tool configured by the user.
[0087] The input code may be written in a compiled language or an interpreted language. When the input code is written in a compiled language, such as Java, the test case generation system 10 may generate a compilation configuration file of the test case according to the user's compilation tool.
[0088] A compile configuration file can be an executable script file, such as a compile configuration script. A compile configuration file is used to help develop programs to run on a variety of different computers. It matches the libraries on the user's computer with the libraries required by the program before compiling from the source code. Typically, the compile configuration script can be named configure.
[0089] Specifically, the test case generation system 10 can generate a template of a compilation configuration file (such as a compilation configuration script) corresponding to the compilation tool according to the compilation tool configured by the user. When the code of the checker (such as DSL code) includes reference information of an external component, the test case generation system 10 can obtain the compilation information of the external component, for example, searching for the external component in a remote warehouse according to the reference information of the outsourced component, obtaining the compilation information of the external component, and adding the compilation information of the external component to the template to obtain the compilation configuration script. When the code of the checker does not include reference information of the external component, the test case generation system 10 can determine the template as a compilation configuration file.
[0090] It should be noted that configuration items such as the number of positive and negative examples, the keyword library for completing positive and negative example code snippets, the format of test predictions, and the remote repository for external component matching can be pre-configured to facilitate one-click generation of test cases, or they can be configured according to actual scenarios at each step of generating test cases.
[0091] Based on the above description, it can be known that the test case generation method of the embodiment of the present application automatically generates code snippets for verifying the correctness of the checker based on the error program nodes defined in the code and the associated query conditions by parsing the code of the checker, thereby improving the test development efficiency and helping users to automatically mark the error code snippets that conform to the checker's check logic, thereby improving the verification efficiency. In addition, the method supports the automatic generation of compilation configuration files, thereby helping users to generate compilable test cases. In this way, the development cost of the checker is reduced, and the testing cost is significantly reduced by automatically generating test cases, such as automatically generating source files, test predictions, and compilation configuration files for test cases.
[0092] Next, the test case generation method of this application is introduced in combination with a specific application scenario.
[0093] See also Figure 3 The application scenario diagram of the test case generation method shown in the figure, the test case generation system 10 obtains the DSL code of the checker, performs node type analysis on the DSL code, obtains the erroneous program node defined in the DSL code, and performs query condition analysis on the DSL code to obtain the query condition associated with the erroneous program node defined in the DSL code, and performs external component analysis on the DSL code to obtain the external component analysis result.
[0094] Then the test case generation system 10 can construct positive example code snippets and negative example code snippets according to the error program node and the query conditions associated with the error program node. Figure 3As shown, the test case generation system 10 can construct positive example code snippets 1 to n and counterexample code snippets 1 to n. It should be noted that when the number of code snippets generated by the test case generation system 10 does not reach the set number of positive examples and counterexamples, the missing snippets can be supplemented. Then, the test case generation system 10 can assemble the positive example code snippets and the counterexample code snippets to generate the source file of the test case. The test case system 10 can further generate the test prediction of the test case based on the positive example code snippets in the source file.
[0095] For ease of understanding, the following is an example of the process of generating source files and generating test predictions. Figure 4 As shown, the error program node defined in the DSL code includes a function declaration node, and the query condition associated with the function declaration node is that the function name is calculate and the number of function parameters is 1. The test case generation system 10 can construct two positive example code fragments and two negative example code fragments according to the above function declaration node and the associated query condition, such as Figure 4 Positive example 1 (function name and number of parameters meet the conditions), positive example 2 (function name and number of parameters meet the conditions), and counterexample 1 (function name does not meet the conditions), counterexample 2 (number of parameters does not meet the conditions). Then the test case generation system 10 assembles the above positive example code snippets and counterexample code snippets to generate a source file. Among them, the class name and package name in the source file can be configured by the user. In addition, the source file can also include a copyright statement (in the form of comments), and the user can configure the copyright statement so that the test case generation system 10 adds the copyright statement to the source file in the form of comments. Then the test case generation system 10 can add the test prediction to the positive example code snippet in the form of a comment, and the comment indicates the location of the problem code snippet (such as the positive example code snippet), for example, adding one to the current line.
[0096] The test case generation system 10 can also generate a compilation configuration script in different ways according to whether the external component analysis result involves an external component. When the external component analysis result indicates that the DSL code involves an external component, the test case generation system 10 can intercept the reference information of the external component in the DSL code, search for the corresponding external component in the remote repository, and add the compilation information of the successfully matched external component to the template of the compilation configuration script to form a compilation configuration script, thereby generating a complete test case. When the external component analysis result indicates that the DSL code does not involve an external component, the test case generation system 10 can directly determine the template of the compilation configuration script as the compilation configuration script.
[0097] For ease of understanding, the process of generating a compilation configuration script is described below with reference to an example. First, the test case generation system 10 generates a compilation script template according to the compilation tool configured by the user. Figure 5In the example, the compilation tool is Maven, and the compilation script template is the Maven compilation template. Then, the test case generation system 10 generates a compilation configuration script according to the external component analysis result of the DSL code. In this example, the external component analysis result indicates that the DSL code involves external components. The test case generation system 10 matches the external components corresponding to the reference information of the external components in the DSL code from the remote repository, and then adds the compilation information of the successfully matched external components to the Maven compilation template to generate the final compilation configuration script.
[0098] The present application also provides a test case generation system 10, such as Figure 1 As shown, the test case generation system 10 is used to generate test cases for a checker, and the checker is used to perform static analysis on input code. The system 10 includes:
[0099] The parsing subsystem 100 is used to obtain the code of the checker, parse the code of the checker, obtain the error program node defined in the code of the checker and the query condition associated with the error program node, wherein the error program node is a program node that can cause the checker to issue an alarm;
[0100] A generating subsystem 200, configured to generate a source file of the test case according to the error program node and the query condition, wherein the source file of the test case includes a first code segment that meets the query condition and a second code segment that does not meet the query condition;
[0101] The generating subsystem 200 is further configured to generate a test oracle for the test case, wherein the test oracle indicates that the first code snippet is a code snippet that causes the checker to raise an alarm.
[0102] Exemplarily, the above-mentioned parsing subsystem 100 and generating subsystem 200 may be implemented by hardware or by software.
[0103] Among them, when implemented by software, the parsing subsystem 100 and the generating subsystem 200 can be applications running on a computer device, such as a computing engine, etc. Further, the application can also be virtualized through a virtualization service. Virtualization services may include virtual machine (VM) services, bare metal server (BMS) services, and container services. Among them, the VM service can be a service that virtualizes a virtual machine (VM) resource pool on multiple physical hosts through virtualization technology to provide users with VMs on demand for use. The BMS service is a service that virtualizes a BMS resource pool on multiple physical hosts to provide users with BMS on demand for use. The container service is a service that virtualizes a container resource pool on multiple physical hosts to provide users with containers on demand for use. VM is a simulated virtual computer, that is, a logical computer. BMS is a high-performance computing service that can be elastically scalable, and its computing performance is no different from that of a traditional physical machine, and it has the characteristics of secure physical isolation. Containers are a kernel virtualization technology that can provide lightweight virtualization to achieve the purpose of isolating user space, processes, and resources. It should be understood that the VM service, BMS service and container service in the above-mentioned virtualization services are only specific examples. In actual applications, virtualization services can also be other lightweight or heavyweight virtualization services, which are not specifically limited here.
[0104] When implemented by hardware, the parsing subsystem 100 and the generating subsystem 200 may include at least one computing device, such as a server, etc. Alternatively, the parsing subsystem 100 and the generating subsystem 200 may also be implemented by using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof.
[0105] Furthermore, the parsing subsystem 100 includes a node type identification module 102 and a query condition identification module 104. The node type identification module 102 is used to identify the code of the checker and obtain the error program node defined in the code of the checker, and the query condition identification module 104 is used to identify the code of the checker and obtain the query condition associated with the error program node.
[0106] Similar to the parsing subsystem 100 , the node type identification module 102 and the query condition identification module 104 may be implemented by hardware or software.
[0107] When implemented by software, the node type identification module 102 and the query condition identification module 104 may be an application program running on a computer device, such as a computing engine, etc. Further, the application program may also be virtualized through a virtualization service. For example, the node type identification module 102 may be a container (service) for implementing node type identification, and the query condition identification module 104 may be a container (service) for implementing query condition identification. When implemented by hardware, the node type identification module 102 and the query condition identification module 104 may include at least one computing device, such as a server, etc. Alternatively, the node type identification module 102 and the query condition identification module 104 may be devices implemented by utilizing an ASIC or a PLD.
[0108] The generation subsystem 200 includes a positive and negative example code snippet generation module 202, a source file generation module 204, and a test prediction generation module 206. The positive and negative example code snippet generation module 202 is used to generate a positive example code snippet and a negative example code snippet according to the error program node and the associated conditions, the positive example code snippet is a first code snippet that meets the query condition, and the negative example code snippet is a second code snippet that does not meet the query condition. The source file generation module 204 is used to generate a source file of a test case according to the positive example code snippet and the negative example code snippet. The test prediction generation module 206 is used to generate a test prediction for the test case, and the test prediction indicates that the positive example code snippet is a code snippet that causes the checker to alarm.
[0109] Similar to the generation subsystem 200, the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206 may be implemented by hardware or by software.
[0110] When implemented by software, the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206 can be applications running on a computer device, such as a computing engine, etc. Further, the application can also be virtualized through a virtualization service. For example, the positive and negative example code snippet generation module 202 can be a container (service) for implementing positive and negative example generation, the source file generation module 204 can be a container (service) for implementing source file generation, and the test prediction generation module 206 can be a container (service) for implementing test prediction generation. When implemented by hardware, the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206 can include at least one computing device, such as a server, etc. Or the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206 can be devices implemented by using ASIC or PLD.
[0111] In some possible implementations, the generating subsystem 200 is specifically used for:
[0112] Generate a test oracle for the test case according to an output format of the test oracle configured by a user.
[0113] In some possible implementations, the generating subsystem 200 is specifically used for:
[0114] Generate a comment for the first code snippet according to an output format of the test oracle configured by a user, wherein the test oracle of the test case includes the comment for the first code snippet; or
[0115] A prediction file independent of the first code snippet is generated according to an output format of the test prediction configured by a user.
[0116] In some possible implementations, the input code is written in a compiled language, and the generation subsystem 200 is further used to:
[0117] Generate a compilation configuration file for the test case according to the compilation tool configured by the user.
[0118] Specifically, the generation subsystem 200 may further include a compilation configuration generation module 208. The compilation configuration generation module 208 is used to generate a compilation configuration file for the test case. The compilation configuration file may be a compilation configuration script. The compilation configuration generation module 208 may generate a compilation configuration file for the test case when the predicted language to be tested is a compilation language, such as when the input code of the checker is a code written in a compilation language. The compilation configuration generation module 208 may generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user, and obtain the compilation configuration file based on the template.
[0119] Similar to the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206, the compilation configuration generation module 208 can be implemented by hardware or software, which will not be described in detail here.
[0120] In some possible implementations, the generating subsystem 200 is specifically used for:
[0121] Generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user;
[0122] When the code of the checker includes reference information of an external component, the compilation information of the external component is obtained, and the compilation information of the external component is added to the template to obtain the compilation configuration file.
[0123] Specifically, the generation subsystem 200 further includes an external component matching module 210, and the external component identification module 210 is used to match the external component in a warehouse (e.g., a remote warehouse) according to the reference information of the external component to obtain the compilation information of the external component. Accordingly, the compilation configuration generation module 208 can add the compilation information of the external component to the template of the compilation configuration file to obtain the compilation configuration file.
[0124] Similar to the positive and negative example code snippet generation module 202, the source file generation module 204, and the test prediction generation module 206, the external component matching module 210 can be implemented by hardware or software, which will not be described in detail here.
[0125] In some possible implementations, the erroneous program node includes one or more of a method declaration node, a variable use node, and an assignment expression node.
[0126] In some possible implementations, the code of the checker is a domain specific language (DSL) code.
[0127] because Figure 1 The test case generation system 10 shown corresponds to Figures 2 to 5 The test case generation method shown in Figure 1 The specific implementation of the test case generation system 10 shown and its technical effects can be found in the relevant descriptions of the aforementioned embodiments, which will not be repeated here.
[0128] The present application also provides a computing device 600. Figure 6As shown, computing device 600 includes: bus 602, processor 604, memory 606 and communication interface 608. Processor 604, memory 606 and communication interface 608 communicate through bus 602. Computing device 600 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in computing device 600.
[0129] The bus 602 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 The bus 602 may include a path for transmitting information between various components of the computing device 600 (eg, the memory 606, the processor 604, and the communication interface 608).
[0130] The processor 604 may include any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0131] The memory 606 may include a volatile memory, such as a random access memory (RAM). The memory 606 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD) or a solid state drive (SSD). The memory 606 stores executable program code, and the processor 604 executes the executable program code to implement the aforementioned test case generation method. Specifically, the memory 606 stores instructions for the test case generation system 10 to execute the test case generation method.
[0132] The communication interface 608 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 600 and other devices or communication networks.
[0133] The embodiment of the present application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device can be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device can also be a terminal device such as a desktop computer, a laptop computer, or a smart phone.
[0134] like Figure 7 As shown, the computing device cluster includes at least one computing device 600. The memory 606 in one or more computing devices 600 in the computing device cluster may store the same test case generation system 10 for executing the test case generation method.
[0135] In some possible implementations, one or more computing devices 600 in the computing device cluster may also be used to execute some instructions of the test case generation system 10 for executing the test case generation method. In other words, a combination of one or more computing devices 600 may jointly execute the instructions of the test case generation system 10 for executing the test case generation method.
[0136] It should be noted that the memory 606 in different computing devices 600 in the computing device cluster may store different instructions for executing partial functions of the test case generation system 10 .
[0137] Figure 8 A possible implementation is shown. Figure 8 As shown, two computing devices 600A and 600B are connected via a communication interface 608. The memory in computing device 600A stores instructions for executing the functions of parsing subsystem 100. The memory in computing device 600B stores instructions for executing the functions of generating subsystem 200. In other words, the memories 606 of computing devices 600A and 600B jointly store instructions for test case generating system 10 to execute the test case generating method.
[0138] Figure 8 The connection mode between the computing device clusters shown can be considered that the test case generation method provided by the present application requires a large amount of computing power to generate positive code snippets and negative code snippets, and when external components are involved, a large amount of computing power is also required to search or match the compilation information of the external components. Therefore, it is considered that the functions implemented by the generation subsystem 200 are handed over to the computing device 600B for execution.
[0139] It should be understood that Figure 8 The functions of the computing device 600A shown in FIG. 6A may also be completed by multiple computing devices 600. Similarly, the functions of the computing device 600B may also be completed by multiple computing devices 600.
[0140] In some possible implementations, one or more computing devices in the computing device cluster may be connected via a network, which may be a wide area network or a local area network. Fig. 9 A possible implementation is shown. Fig. 9 As shown, two computing devices 600C and 600D are connected via a network. Specifically, they are connected to the network via a communication interface in each computing device. In this type of possible implementation, the memory 606 in the computing device 600C stores instructions for executing the functions of the parsing subsystem 100. At the same time, the memory 606 in the computing device 600D stores instructions for executing the functions of the generating subsystem 200.
[0141] Fig. 9 The connection mode between the computing device clusters shown can be considered that the test case generation method provided by the present application requires a lot of computing power to generate positive code snippets and negative code snippets, and when external components are involved, a lot of computing power is also required to search or match the compilation information of the external components. Therefore, it is considered that the functions implemented by the generation subsystem 200 are handed over to the computing device 600D for execution.
[0142] It should be understood that Fig. 9 The functions of the computing device 600C shown in FIG. 6A may also be completed by multiple computing devices 600. Similarly, the functions of the computing device 600D may also be completed by multiple computing devices 600.
[0143] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by a computing device or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions that instruct the computing device to execute the above-mentioned test case generation system 10 for executing the test case generation method.
[0144] The embodiment of the present application also provides a computer program product including instructions. The computer program product may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computer device, the at least one computer device executes the above test case generation method.
[0145] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of the present invention.
Claims
1. A test case generation method, characterized in that: Applied to a test case generation system, the test case generation system is used to generate test cases for a checker, the checker is used to perform static analysis on input code, the method comprises: Obtaining the code of the checker, parsing the code of the checker, obtaining an error program node defined in the code of the checker and a query condition associated with the error program node, wherein the error program node is a program node that can cause the checker to issue an alarm; Generate a source file of the test case according to the error program node and the query condition, wherein the source file of the test case includes a first code fragment that meets the query condition and a second code fragment that does not meet the query condition; A test oracle for the test case is generated, the test oracle indicating that the first code snippet is a code snippet that causes the checker to raise an alarm.
2. The method according to claim 1, characterized in that The test prediction for generating the test case comprises: Generate a test oracle for the test case according to an output format of the test oracle configured by a user.
3. The method according to claim 2, characterized in that The step of generating the test oracle of the test case according to the output format of the test oracle configured by the user comprises: Generate a comment for the first code snippet according to an output format of the test oracle configured by a user, wherein the test oracle of the test case includes the comment for the first code snippet; or A prediction file independent of the first code snippet is generated according to an output format of the test prediction configured by a user.
4. The method according to any one of claims 1 to 3, characterized in that: The input code is written in a compiled language, and the method further comprises: Generate a compilation configuration file for the test case according to the compilation tool configured by the user.
5. The method according to claim 4, characterized in that The compiling tool configured by the user generates a compiling configuration file of the test case, including: Generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user; When the code of the checker includes reference information of an external component, the compilation information of the external component is obtained, and the compilation information of the external component is added to the template to obtain the compilation configuration file.
6. The method according to any one of claims 1 to 5, characterized in that: The error program node includes one or more of a method declaration node, a variable use node, and an assignment expression node.
7. The method according to any one of claims 1 to 6, characterized in that: The code of the checker is a domain specific language DSL code.
8. A test case generation system, characterized in that: The test case generation system is used to generate test cases for a checker, and the checker is used to perform static analysis on input code. The system includes: A parsing subsystem, used to obtain the code of the checker, parse the code of the checker, obtain an error program node defined in the code of the checker and a query condition associated with the error program node, wherein the error program node is a program node that can cause the checker to issue an alarm; A generating subsystem, configured to generate a source file of the test case according to the error program node and the query condition, wherein the source file of the test case includes a first code fragment that meets the query condition and a second code fragment that does not meet the query condition; The generation subsystem is further configured to generate a test oracle for the test case, wherein the test oracle indicates that the first code snippet is a code snippet that causes the checker to raise an alarm.
9. The system according to claim 8, characterized in that The generation subsystem is specifically used for: Generate a test oracle for the test case according to an output format of the test oracle configured by a user.
10. The system according to claim 8, characterized in that The generation subsystem is specifically used for: Generate a comment for the first code snippet according to an output format of the test oracle configured by a user, wherein the test oracle of the test case includes the comment for the first code snippet; or A prediction file independent of the first code snippet is generated according to an output format of the test prediction configured by a user.
11. The system according to any one of claims 8 to 10, characterized in that: The input code is written in a compiled language, and the generation subsystem is further used for: Generate a compilation configuration file for the test case according to the compilation tool configured by the user.
12. The system according to claim 11, characterized in that The generation subsystem is specifically used for: Generate a template of a compilation configuration file corresponding to the compilation tool according to the compilation tool configured by the user; When the code of the checker includes reference information of an external component, the compilation information of the external component is obtained, and the compilation information of the external component is added to the template to obtain the compilation configuration file.
13. The system according to any one of claims 8 to 12, characterized in that: The error program node includes one or more of a method declaration node, a variable use node, and an assignment expression node.
14. The system according to any one of claims 8 to 13, characterized in that The code of the checker is a domain specific language DSL code.
15. A computing device cluster, characterized in that: The computing device cluster includes at least one computing device, and the at least one computing device includes at least one processor and at least one memory, wherein the at least one memory stores computer-readable instructions; the at least one processor executes the computer-readable instructions so that the computing device cluster executes the method as described in any one of claims 1 to 7.
16. A computer-readable storage medium, characterized in that: The method comprises computer-readable instructions; the computer-readable instructions are used to implement the method according to any one of claims 1 to 7.
17. A computer program product, characterized in that The method comprises computer-readable instructions; the computer-readable instructions are used to implement the method according to any one of claims 1 to 7.
