Unit test file generation method and device
By automatically extracting and generating pile functions, the cumbersome problem of manually writing pile functions is solved, and the efficiency of unit test files is improved.
Patent Information
- Application Number
- CN202411770425.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-02-28
AI Technical Summary
In JAVA program development, manually writing pile functions is cumbersome and error-prone, resulting in inefficient unit test files generation.
By extracting the program information in the file being tested, obtaining the pile driving function information, and applying the preset pile function value strategy and pile function template to generate pile function files, automatically introducing the pile function into the file being tested, and generating unit test files.
No need to write stake functions manually, reducing the time required to generate unit test files and improving the efficiency of generating unit test files.
Smart Images

Figure CN119537250A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for generating a unit test file. Background Art
[0002] In daily JAVA program development, unit testing is an important means to ensure code quality and functional correctness. Unit test files are used to implement corresponding unit tests. The purpose of unit testing is to discover various errors that may exist in each module. This is mainly based on the principle of white box testing, which uses testing to discover errors introduced during the coding process. When performing unit testing, the unit under test may call other sub-units or external dependencies. These called units are called stub units. Stub units are used to replace sub-units in the unit under test (called by the unit under test) to isolate test tasks and ensure the independence and accuracy of the test.
[0003] In traditional unit testing practices, developers often need to manually write stub functions to replace components that have not yet been implemented or are unstable, as well as external dependencies that need to be isolated. The process of manually writing stub functions is cumbersome and error-prone. As the complexity and scale of software projects continue to increase, the efficiency of manually writing stub functions becomes increasingly low, resulting in lower and lower efficiency in generating unit test files for unit testing. Summary of the invention
[0004] In view of this, the embodiment of the present application provides a method and device for generating a unit test file, and the scheme provided by the application can automatically generate a pile function, and the pile function is automatically inserted into the tested file, without the need to manually write the pile function to the tested file, reducing the time required for inserting the pile function into the tested file, and improving the efficiency of generating the unit test file.
[0005] To achieve the above objectives, the present application provides the following technical solutions:
[0006] A method for generating a unit test file, comprising:
[0007] Obtaining a file under test, and extracting program information from the file under test;
[0008] Obtaining piling function information from the program information, wherein the piling function information includes various functions that need to be piling;
[0009] Applying a preset pile function value strategy and a predefined pile function template, generating a pile function file corresponding to the described pile driving function information, wherein the described pile function file comprises the pile function of each function that needs to be piled in the described pile driving function information;
[0010] The stub function file is introduced into the file under test to generate a unit test file corresponding to the file under test.
[0011] In the above method, optionally, the extracting the program information in the tested file includes:
[0012] Using a preset syntax analysis tool to construct the file under test into a syntax tree;
[0013] Performing static program analysis on the syntax tree to extract function information of each function in the syntax tree;
[0014] Based on the function information of each function in the syntax tree, perform call relationship analysis on all functions in the syntax tree, determine each function that needs to be piled, and generate pile function information containing each function that needs to be piled;
[0015] The stub function information and function information of each function in the syntax tree are determined as program information.
[0016] The above method optionally uses a preset pile function value strategy and a predefined pile function template to generate a pile function file corresponding to the pile driving function information, including:
[0017] For each function that needs piling in the piling function information, determine the function that needs piling as a piling function;
[0018] Obtain function information of the piling function;
[0019] Based on a preset pile function value strategy and function information of the pile driving function, determining the pile function value of the pile driving function;
[0020] Use described pile function value and predefined pile function template to build the pile function of described piling function;
[0021] Generate stub function files based on the stub functions of all stub functions.
[0022] The above method, optionally, determines the pile function value of the pile driving function based on a preset pile function value strategy and function information of the pile driving function, including:
[0023] Based on the function information of the pile driving function, determining whether the pile driving function satisfies the application default value strategy in the pile function value strategy;
[0024] When the pile driving function satisfies the application default value strategy in the pile function value strategy, the preset default value is used as the pile function value of the pile driving function;
[0025] When the pile driving function does not satisfy the application default value policy in the pile function value policy, the introduced pile value is determined as the pile function value of the pile driving function.
[0026] The above method, optionally, the step of introducing the stub function file into the tested file comprises:
[0027] Determine the call point position of each function that needs to be stubbed in the file under test as the target position;
[0028] For each target position in the tested file, a target pile function of the target position is determined in the pile function file, and the function of the target position is replaced by the target pile function.
[0029] A device for generating a unit test file, comprising:
[0030] A program analysis unit, used to obtain a file under test and extract program information from the file under test;
[0031] A pile function file generating unit is used for obtaining pile driving function information from the program information, wherein the pile driving function information contains various functions that need to be piled; applying a preset pile function value strategy and a predefined pile function template to generate a pile function file corresponding to the pile driving function information, wherein the pile function file contains the pile function of each function that needs to be piled in the pile driving function information;
[0032] The pile function introduction replacement unit is used to introduce the pile function file into the tested file to generate a unit test file corresponding to the tested file.
[0033] In the above device, optionally, the program analysis unit comprises:
[0034] Static analyzer, symbol table storage and function call relationship analyzer;
[0035] The static analyzer is used to construct the tested file into a syntax tree using a preset syntax analysis tool; perform static program analysis on the syntax tree to extract function information of each function in the syntax tree;
[0036] The function call analyzer is used to perform call relationship analysis on all functions in the syntax tree based on the function information of each function in the syntax tree, determine each function that needs to be piled, and generate piled function information containing each function that needs to be piled;
[0037] The symbol table memory is used to determine the stub function information and the function information of each function in the syntax tree as program information.
[0038] Above-mentioned device, optionally, described pile function file generation unit comprises:
[0039] a stub file constructor and a first symbol table reader;
[0040] The first symbol table reader is used to read the stubbing function information from the program analysis unit, and for each function that needs stubbing in the stubbing function information, determine the function that needs stubbing as a stubbing function; and obtain function information of the stubbing function;
[0041] The pile file constructor is used to determine the pile function value of each pile driving function based on a preset pile function value strategy and function information of the pile driving function; use the pile function value and a predefined pile function template to construct the pile function of the pile driving function; and generate a pile function file based on the pile functions of all pile driving functions.
[0042] In the above-mentioned device, optionally, the pile file constructor performs a process of determining the pile function value of each pile driving function based on a preset pile function value strategy and the function information of the pile driving function, including:
[0043] Based on the function information of the pile driving function, determining whether the pile driving function satisfies the application default value strategy in the pile function value strategy;
[0044] When the pile driving function satisfies the application default value strategy in the pile function value strategy, the preset default value is used as the pile function value of the pile driving function;
[0045] When the pile driving function does not satisfy the application default value policy in the pile function value policy, the introduced pile value is determined as the pile function value of the pile driving function.
[0046] In the above device, optionally, the stub function introduces a replacement unit, comprising:
[0047] A second symbol table reader and stub function inserter;
[0048] The second symbol table reader is used to read the location information of each function that needs stubbing from the program analysis unit;
[0049] Described pile function inserter is used for determining the call point position of each function that needs pile driving in described tested file based on the positional information of each function that needs pile driving, and the call point position of each function that needs pile driving in described tested file is determined as target position; For each target position in described tested file, determine the target pile function of described target position in described pile function file, and replace the function of described target position with described target pile function.
[0050] Compared with the prior art, this application has the following advantages:
[0051] The present application provides a method and device for generating a unit test file, comprising: extracting program information from an acquired file under test, obtaining pile driving function information from the program information, wherein the pile driving function information includes various functions that need to be piled; applying a preset pile function value strategy and a pile function template, generating a pile function file corresponding to the pile driving function information, wherein the pile function file includes the pile function of each function that needs to be piled in the pile driving function information; introducing the pile function file into the file under test, generating a unit test file corresponding to the file under test. Applying the scheme provided by the present application, there is no need to manually write a pile function, after obtaining the pile driving function information, the pile function value strategy and the pile function module can be used to generate the pile function information corresponding to the pile driving function information, after the pile function information is introduced into the file under test, the unit test file can be obtained, thus, when generating the unit test file, there is no need to wait for the staff to manually write the pile function, shortening the time required for the pile function to introduce the file, thereby improving the efficiency of generating the unit test file. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0053] Figure 1 A flowchart of a method for generating a unit test file provided in this application;
[0054] Figure 2 A schematic diagram of the structure of a device for generating a unit test file provided in an embodiment of the present application;
[0055] Figure 3 A flow chart for generating another unit test file provided in an embodiment of the present application;
[0056] Figure 4 The workflow of the program analysis unit provided in the embodiment of the present application;
[0057] Figure 5 Workflow diagram of the pile function file generation unit provided for the embodiment of the application;
[0058] Figure 6 A workflow diagram of introducing a replacement unit into a stub function provided in an embodiment of the present application. DETAILED DESCRIPTION
[0059] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0060] In this application, the terms "comprises", "comprising" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element.
[0061] Terminology explanation:
[0062] Syntax analysis tools: Syntax analysis tools can generate lexical analyzers and syntax analyzers based on user-defined syntax files, build program files into a syntax tree, and provide listeners and visitors for traversing the syntax tree. Excellent projects such as Twitter and Hadoop use syntax analysis tools for syntax analysis and lexical analysis.
[0063] Program analysis: Program analysis takes programs written in a certain language as the object, analyzes the program's running process, and clarifies its internal calling relationships. Static program analysis directly analyzes the program code and extracts elements in the code, mainly including data flow analysis, symbolic execution and other technologies.
[0064] Abstract Syntax Tree (AST): Syntax tree is an intermediate representation of a program. It is generated by syntax tree generation tool and syntax file. It represents the syntax structure of the program in a tree form. It includes all the information of the source program. Each node in the syntax tree represents a part of the information in the program.
[0065] Lexical analysis: The process of converting the input program text into a sequence of lexical symbols is called lexical analysis. Lexical symbols include the type of lexical symbol and the corresponding text.
[0066] Syntax analysis: Identifying the sentence structure by consuming the lexical symbols output by lexical analysis is called syntax analysis. The syntax analyzer will construct a syntax analysis tree.
[0067] Symbol table: used to store basic information in the source program, including variable definitions, function definitions, etc. By traversing the syntax tree of the source program, the source program information is obtained and stored in the symbol table.
[0068] Stub function: A stub function is a virtual function that replaces the original function called in the program. Since external system call functions need to be shielded in unit testing, such as database reading and writing, interface calls, etc., it is necessary to simulate a function return value to achieve the purpose of shielding the original function. This is generally achieved by generating a fake function definition.
[0069] Instrumentation: Instrumentation technology in unit testing is a method of inserting custom functions into the program unit under test to achieve testing purposes. The purpose of inserting custom functions includes obtaining unit test coverage, statement execution times, program execution path, etc.
[0070] In order to solve the problems of the prior art, the application provides a generation scheme of a unit test file, extracts program information in a tested file, obtains pile driving function information from the program information, applies a pile function value strategy to generate a pile function file corresponding to the pile driving function information; and adds the pile function file to the tested file to generate a unit test file corresponding to the tested file. When generating a unit test file containing a pile function, there is no need to manually write, which reduces the time required for generating a unit test file and improves the efficiency of generating a unit test file.
[0071] The present application can be used in many general or special computing device environments or configurations. For example: personal computers, server computers, handheld devices or portable devices, tablet devices, multi-processor devices, distributed computing environments including any of the above devices or devices, etc. The solution provided by the present application can be applied to processors or servers.
[0072] Reference Figure 1 , which is a flowchart of a method for generating a unit test file provided by this application, and is specifically described as follows:
[0073] S101: Obtain a file under test, and extract program information from the file under test.
[0074] Get the file under test provided by the user, or get the file under test from the system.
[0075] The file under test can be a program file written in JAVA language. The file under test is parsed to extract program information from the file under test. The program information includes function information of each function in the file under test and stubbing function information. The function information includes definition information and calling information of the function.
[0076] By analyzing the function information, the piling function information is obtained, and the piling function information includes various functions that need to be piled.
[0077] S102: Obtain stubbing function information from the program information, wherein the stubbing function information includes various functions that need to be stubbing.
[0078] Read the stubbing function information in the program information; the stubbing function information contains relevant information of each function that needs to be stubbing, such as function definition information, calling information, etc. of each function that needs to be stubbing.
[0079] S103, applying preset pile function value strategy and predefined pile function template, generating a pile function file corresponding to the pile driving function information, the pile function file comprising the pile function of each function that needs to be piled in the pile driving function information.
[0080] The stub function value strategy includes a strategy for determining the source of the stub function value. The sources of the stub function values used by different functions that need stubbing may be different. The stub function value strategy can be used to determine the stub function value of each function that needs stubbing.
[0081] The stub function template is used to build the stub function.
[0082] The generated stub function file information contains the stub function of each function that needs to be stubbed.
[0083] S104, introducing the stub function file into the file under test, and generating a unit test file corresponding to the file under test.
[0084] The generated stub function file is introduced into the file under test, so that the functions in the file under test that need to be piled are replaced by the functions in the stub function file. Thus, there is no need to write corresponding stub functions one by one for each function that needs to be piled in the file under test, and the automatic replacement of the called function can be realized quickly, thereby improving the efficiency of generating unit test files.
[0085] In the method provided by the embodiment of the present application, program information is extracted from the obtained tested file, and pile driving function information is obtained from the program information, and each function that needs to be piled is included in the pile driving function information; a preset pile function value strategy and a pile function template are applied to generate a pile function file corresponding to the pile driving function information, and the pile function file includes the pile function of each function that needs to be piled in the pile driving function information; the pile function file is introduced into the tested file to generate a unit test file corresponding to the tested file. The scheme provided by the application does not need to manually write a pile function. After obtaining the pile driving function information, the pile function value strategy and the pile function module can be used to generate the pile function information corresponding to the pile driving function information. After the pile function information is introduced into the tested file, the unit test file can be obtained. Thus, when generating the unit test file, there is no need to wait for the staff to manually write the pile function, shorten the time required for the pile function to introduce the file, and then improve the efficiency of generating the unit test file.
[0086] Reference Figure 2 , is a schematic diagram of the structure of a device for generating a unit test file provided in an embodiment of the present application, and is specifically described as follows:
[0087] The device comprises a program analyzing unit 201, a stub function file generating unit 202 and a stub function introducing and replacing unit 203.
[0088] Wherein, the program analysis unit includes a static analyzer, a first symbol table memory, and a function call relation analyzer; the pile function file generation unit includes a pile file constructor and a symbol table reader; and the pile function introduction replacement unit includes a second symbol table reader and a pile function inserter.
[0089] Preferably, the program analysis unit is used to obtain the tested file and extract the program information in the tested file; the pile function file generation unit is used to obtain the pile driving function information from the program information, and the pile driving function information contains various functions that need to be piled; a preset pile function value strategy and a predefined pile function template are applied to generate a pile function file corresponding to the pile driving function information, and the pile function file contains the pile function of each function that needs to be piled in the pile driving function information; the pile function introduction replacement unit is used to introduce the pile function file into the tested file to generate a unit test file corresponding to the tested file.
[0090] Figure 2 The generating device of the unit test file shown can be regarded as an automated JAVA pile function generating device, and the device generates the process of the unit test file as follows:
[0091] The program analysis unit expands the file under test, uses a syntax analysis tool to analyze the expanded file under test, generates a syntax tree to be unit tested, and performs static analysis on the syntax tree to extract program information from the file under test;
[0092] The pile function generating unit utilizes the pile function value strategy to determine the pile function value, and uses the program information in the program analysis unit to carry out the structure of the pile function file then, thereby generates the pile function file;
[0093] The stub function introduction unit introduces the stub function file constructed by the stub function generation unit into the file under test by using program instrumentation technology, thereby obtaining a unit test file.
[0094] Reference Figure 3 , is a flow chart for generating another unit test file provided in an embodiment of the present application, which includes Figure 2 The program analysis unit in the shown device, the pile function file generation unit and the pile function are introduced into the workflow of the replacement unit, and are specifically described as follows:
[0095] The program analysis unit loads the JAVA file under test, expands the import information of the JAVA file under test to form a complete file under test, uses the syntax analysis tool to build the file under test into a syntax tree, performs static program analysis based on the syntax tree, and stores the extracted program information in the symbol table. At this point, the program analysis unit completes its work.
[0096] The pile function file generation unit reads the program information in the program analysis unit, obtains the definition and other information of each called function in the tested file, the called function is the function that needs to be piled, determines whether to use the default pile function value or the introduced pile function value through the pile function value strategy, and constructs the pile function file based on the determined pile function value and the pile function template defined in the early stage. The pile function file contains the pile functions of each called function, and the work of the pile function file generation unit is now completed.
[0097] The pile function is introduced into the replacement unit to read the program information obtained by the program analysis unit through static analysis, and the function call point information is identified. The function call point information includes the function call point of each called function in the tested file, and the function call point can be understood as the position of the called function in the tested file; when building the JAVA unit test file, the pile function built by the pile function file generation unit is introduced by the plug-in technology at the function call point, preferably, the pile function can be a virtual pile function, and the constructed JAVA unit test file (i.e. the unit test file above) is output for the unit test framework to execute, and this device completes the process of automatic generation of the JAVA pile function.
[0098] Reference Figure 4 , which is the workflow of the program analysis unit provided in the embodiment of the present application, is specifically described as follows:
[0099] The program analysis unit first initializes and loads the file under test, and then determines whether to simplify the file under test. If the file under test needs to be simplified, the file under test is simplified and the simplified file under test is determined as the first file; if the file under test does not need to be simplified, the file under test is determined as the first file; then the syntax analysis tool is used to analyze the first file to construct a syntax tree of the first file according to the JAVA syntax; the constructed syntax tree is traversed using the visitor pattern to extract the function information of each function in the syntax tree, and a symbol table is constructed according to the function information of each function, and the function definition information of each function is stored in the symbol table, and then the calling relationship between each function is analyzed to obtain the function that needs to be piled, and the information of the function that needs to be piled is stored.
[0100] The program analysis unit is mainly used to load and analyze the JAVA program under test (i.e. the file under test mentioned above), extract the program information such as function definition information in the program and store it for use by subsequent units. The program analysis unit consists of a static analyzer, a symbol table storage, and a function call relationship analyzer.
[0101] The static analyzer is used to use the preset syntax analysis tool to build the tested file into a syntax tree; perform static program analysis on the syntax tree, and extract the function information of each function in the syntax tree. The function information includes the function call information and function definition information of the function. The function definition information includes the function name, function return value type, function input parameter type, etc.
[0102] Exemplarily, the static analyzer expands the file under test, expands the imported packages and merges them into a separate file. When there are unused function or variable definitions in the file, the unused function or variable definitions in the file are simplified to obtain a first file. The file is simplified to prevent the subsequently constructed syntax tree from occupying too much memory. A syntax analysis tool is used to generate a syntax tree for the first file, and then the visitor pattern is used to traverse the syntax tree, that is, the syntax tree is statically analyzed to obtain function information of all functions.
[0103] Exemplarily, the process of using a syntax analysis tool to construct a syntax tree is as follows: first, the syntax analysis tool is used to perform lexical analysis and syntax analysis on the file to generate a syntax tree to be unit tested. In the process of lexical analysis, the input text program is converted into lexical symbols, and the lexical symbols contain at least two pieces of information: the type of lexical symbol (identifying the lexical structure) and the text corresponding to the lexical symbol. In the syntax analysis step, the lexical symbols are identified as sentence structures according to the rules, and the syntax analysis tool syntax analyzer constructs a syntax analysis tree data structure, which records the process of the syntax analysis tool identifying the input sentence structure, as well as the components, and outputs an abstract syntax tree.
[0104] Furthermore, after obtaining the syntax tree, the syntax tree is traversed to construct a symbol table, and the visitor pattern is used to traverse the syntax tree. The visitor pattern can separate operations from structural objects and provide operations that act on each element of a certain object structure. The syntax tree is composed of many Nodes. Node has an accept method to accept visits from the visitor object Visitor. The Visitor class is an interface with a visit method. The visit method can respond differently to different types of Nodes in the Tree. According to the Node type, the definition of the function unit, the function header definition, the return value type, the input parameters and other information are identified and stored in the constructed symbol table. Preferably, the symbol table stores the definition of the function unit, the function header definition, the return value type, the input parameters and other information of each Node node in the syntax tree. The function unit can be regarded as a function.
[0105] The function call analyzer is used to analyze the call relationship of all functions in the syntax tree based on the function information of each function in the syntax tree, determine the functions that need to be piled, and generate pile function information containing the functions that need to be piled. The function call analyzer is used to find the called functions in the tested function unit. Since the tested file has more than one function unit, there may be duplicate definition problems between the pile functions. The call relationship between the functions is analyzed to reduce the generation of unnecessary pile function definition files.
[0106] The symbol table memory is used to determine the function information of each function in the stubbing function information and the syntax tree as program information; preferably, the symbol table memory can store the function information of each function, and can also save the symbol table constructed by the static analyzer.
[0107] for Figure 2 The stub function file generation unit shown includes a stub file constructor and a first symbol table reader.
[0108] Reference Figure 5 , is the workflow diagram of the pile function file generation unit provided by the embodiment of the present application, and is specifically described as follows:
[0109] The pile function file generation unit first reads the function definition information of each function from the program analysis unit, then determines the function that needs to be piled, and then uses the pile function value strategy to determine whether to adopt the default pile function value or the externally introduced pile function value. After determining the pile function value, read the pile function template, and construct the pile function file according to the pile function template and the determined pile function value.
[0110] Further, the pile function file generation unit is used for reading information from the program analysis unit, for example reads the information of pile driving function information, each function etc.; Then generate the pile function file according to the pile function template and the pile function value strategy of using to determine. The pile function file generation unit is composed of the pile file constructor and the first symbol table reader.
[0111] The first symbol table reader is used to read the stubbing function information from the program analysis unit, and for each function that needs to be stubbing in the stubbing function information, determine the function that needs to be stubbing as the stubbing function; obtain the function information of the stubbing function; and then provide the function information of each function that needs to be stubbing to the stubbing file constructor.
[0112] The pile file constructor is used to determine the pile function value of each pile driving function based on the preset pile function value strategy and the function information of the pile driving function; the pile function of the pile driving function is constructed using the pile function value and the predefined pile function template; the pile function file is generated based on the pile functions of all pile driving functions. It should be noted that when determining the pile function value of the pile driving function, based on the function information of the pile driving function, it is determined whether the pile driving function satisfies the application default value strategy in the pile function value strategy; when the pile driving function satisfies the application default value strategy in the pile function value strategy, the preset default value is used as the pile function value of the pile driving function; when the pile driving function does not meet the application default value strategy in the pile function value strategy, the introduced pile value is determined as the pile function value of the pile driving function. Exemplarily, when the pile function value of the pile driving function is recorded in the function information of the pile driving function as an externally introduced value, it is determined that the pile driving function does not meet the application default value strategy; when the pile function value of the pile driving function is recorded in the function information of the pile driving function as a default value, it is determined that the pile driving function meets the application default value strategy.
[0113] for Figure 2 Shown pile function introduces replacement unit, pile function introduces replacement unit and is mainly used for the pile function file that generates is introduced into the unit test file that the automated unit test tool generates, generates the unit test file behind the shielding called function; The pile function introduces replacement unit and comprises the pile function inserter and the second symbol table reader.
[0114] Reference Figure 6 , a workflow diagram of a pile function introduction replacement unit provided in an embodiment of the present application, specifically described as follows: the pile function introduction replacement unit reads information in a program analysis unit, for example, reads function definition information of each function that needs to be piled in the program analysis unit, determines the function call point position in each tested function unit (i.e., the function that needs to be piled) in the tested file, and then uses program instrumentation technology to replace the function of the function call point with the generated pile function, and outputs a JAVA single test file for execution by an automated unit testing tool.
[0115] The second symbol table reader in the replacement unit is introduced into the stub function to read the position information of each function that needs stubbing from the program analysis unit; and then the read information is provided to the stub function inserter.
[0116] A stub function inserter in a replacement unit is introduced for stub functions, which is used to determine the call point position of each function that needs stubbing in the tested file (the function call point position mentioned above) based on the position information of each function that needs stubbing, and determine the call point position of each function that needs stubbing in the tested file as the target position; for each target position in the tested file, determine the target stub function of the target position in the stub function file, and replace the function at the target position with the target stub function.
[0117] Preferably, the call point position can be represented by the character flag bit by the position information; the pile function plug-in uses the technology of plug-in to replace the function call in the tested file, such as the called function (the function that needs to be piled) is at the 1000th character, and the function field length is 10 characters. So when reconstructing the file, the characters at the 1000-1009 positions are replaced with the function name of the pile function to realize the introduction of the pile function.
[0118] In the scheme that the application provides, can identify the called function (function that needs pile driving) of the tested function unit in the JAVA file (tested file) by the form of static program analysis, and can obtain the definition of called function, construct pile function file according to function definition, without manually writing pile function, realize the automatic generation of JAVA pile function.Identify function call point by the form of static program analysis, utilize the technology of program insertion to automatically introduce the pile function file generated into the JAVA unit test file, realize the automatic replacement of called function, without manually writing pile function, improve the generation efficiency of tested file.
[0119] It should be noted that the information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards in the relevant regions.
[0120] The specific implementation processes and derivative methods of the above-mentioned embodiments are all within the protection scope of the present invention.
[0121] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can refer to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system or system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can refer to the partial description of the method embodiment. The system and system embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without creative work.
[0122] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0123] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for generating a unit test file, characterized in that: include: Obtaining a file under test, and extracting program information from the file under test; Obtaining piling function information from the program information, wherein the piling function information includes various functions that need to be piling; Applying a preset pile function value strategy and a predefined pile function template, generating a pile function file corresponding to the described pile driving function information, wherein the described pile function file comprises the pile function of each function that needs to be piled in the described pile driving function information; The stub function file is introduced into the file under test to generate a unit test file corresponding to the file under test.
2. The method according to claim 1, characterized in that The extracting the program information from the tested file includes: Using a preset syntax analysis tool to construct the tested file into a syntax tree; Performing static program analysis on the syntax tree to extract function information of each function in the syntax tree; Based on the function information of each function in the syntax tree, perform call relationship analysis on all functions in the syntax tree, determine each function that needs to be piled, and generate pile function information containing each function that needs to be piled; The stub function information and function information of each function in the syntax tree are determined as program information.
3. The method according to claim 1, characterized in that Applying the preset pile function value strategy and the predefined pile function template, generating the pile function file corresponding to the pile driving function information, including: For each function that needs piling in the piling function information, determine the function that needs piling as a piling function; Obtain function information of the piling function; Based on a preset pile function value strategy and function information of the pile driving function, determining the pile function value of the pile driving function; Use described pile function value and predefined pile function template to build the pile function of described piling function; Generate stub function files based on the stub functions of all stub functions.
4. The method according to claim 3, characterized in that The method of determining the pile function value of the pile driving function based on a preset pile function value strategy and function information of the pile driving function comprises: Based on the function information of the pile driving function, determining whether the pile driving function satisfies the application default value strategy in the pile function value strategy; When the pile driving function satisfies the application default value strategy in the pile function value strategy, the preset default value is used as the pile function value of the pile driving function; When the pile driving function does not satisfy the application default value policy in the pile function value policy, the introduced pile value is determined as the pile function value of the pile driving function.
5. The method according to claim 1, characterized in that: The step of introducing the stub function file into the tested file comprises: Determine the call point position of each function that needs to be stubbed in the file under test as the target position; For each target position in the tested file, a target pile function of the target position is determined in the pile function file, and the function of the target position is replaced by the target pile function.
6. A device for generating a unit test file, characterized in that: include: A program analysis unit, used to obtain a file under test and extract program information from the file under test; A pile function file generating unit is used for obtaining pile driving function information from the program information, wherein the pile driving function information contains various functions that need to be piled; applying a preset pile function value strategy and a predefined pile function template to generate a pile function file corresponding to the pile driving function information, wherein the pile function file contains the pile function of each function that needs to be piled in the pile driving function information; The pile function introduction replacement unit is used to introduce the pile function file into the tested file to generate a unit test file corresponding to the tested file.
7. The device according to claim 6, characterized in that The program analysis unit comprises: Static analyzer, symbol table storage and function call relationship analyzer; The static analyzer is used to construct the tested file into a syntax tree using a preset syntax analysis tool; perform static program analysis on the syntax tree to extract function information of each function in the syntax tree; The function call analyzer is used to perform call relationship analysis on all functions in the syntax tree based on the function information of each function in the syntax tree, determine each function that needs to be piled, and generate piled function information containing each function that needs to be piled; The symbol table memory is used to determine the stub function information and the function information of each function in the syntax tree as program information.
8. The device according to claim 6, characterized in that The stub function file generating unit comprises: a stub file constructor and a first symbol table reader; The first symbol table reader is used to read the stubbing function information from the program analysis unit, and for each function that needs stubbing in the stubbing function information, determine the function that needs stubbing as a stubbing function; and obtain function information of the stubbing function; The pile file constructor is used to determine the pile function value of each pile driving function based on a preset pile function value strategy and function information of the pile driving function; use the pile function value and a predefined pile function template to construct the pile function of the pile driving function; and generate a pile function file based on the pile functions of all pile driving functions.
9. The device according to claim 8, characterized in that The pile file constructor performs a process of determining the pile function value of each pile driving function based on a preset pile function value strategy and function information of the pile driving function, including: Based on the function information of the pile driving function, determining whether the pile driving function satisfies the application default value strategy in the pile function value strategy; When the pile driving function satisfies the application default value strategy in the pile function value strategy, the preset default value is used as the pile function value of the pile driving function; When the pile driving function does not satisfy the application default value policy in the pile function value policy, the introduced pile value is determined as the pile function value of the pile driving function.
10. The device according to claim 6, characterized in that The stub function introduces a replacement unit, including: A second symbol table reader and stub function inserter; The second symbol table reader is used to read the location information of each function that needs stubbing from the program analysis unit; Described pile function inserter is used for determining the call point position of each function that needs pile driving in described tested file based on the positional information of each function that needs pile driving, and the call point position of each function that needs pile driving in described tested file is determined as target position; For each target position in described tested file, determine the target pile function of described target position in described pile function file, and replace the function of described target position with described target pile function.