File generation method and apparatus

CN116028340BActive Publication Date: 2026-09-25TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

现有技术的变异测试方法无法全面覆盖程序运行中的错误,即无法生成全面的变异文件,从而无法准确评估测试用例的质量;

Benefits of technology

[0019]本申请获取目标源文件的源代码以及变异规则;并基于预设开源工具解析所述目标源文件的源代码,构建抽象语法树;预设开源工具的编译命令中路径为绝对路径;提高了抽象语法树的完整性,从而能够将源代码完全解析成语法树,从而便于查找待变异节点;然后遍历所述抽象语法树,根据所述变异规则确定待变异节点;基于所述待变异节点以及所述变异规则对所述源代码进行修改,生成变异文件;从而实现了通过抽象语法树以及变异规则,确定待变异节点,并快速确定源代码中待变异元素,进而生成变异体,得到变异文件;通过生成的变异文件对测试用例进行变异测试,提高了测试用例的测试结果准确率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116028340B_ABST
    Figure CN116028340B_ABST
Patent Text Reader

Abstract

The application discloses a file generation method and device, which can be applied to various scenes such as cloud technology, artificial intelligence, intelligent transportation and Internet of Vehicles, and the method comprises the following steps: acquiring source code of a target source file and a mutation rule; the mutation rule is used for modifying elements in the source code; the source code of the target source file is parsed based on a preset open source tool to construct an abstract syntax tree; nodes in the abstract syntax tree represent the elements in the source code; the path in the compilation command of the preset open source tool is an absolute path; the abstract syntax tree is traversed to determine a node to be mutated according to the mutation rule; and the source code is modified based on the node to be mutated and the mutation rule to generate a mutation file. The application realizes rapid generation of the mutation file, and the mutation file is used for mutation testing of test cases, thereby improving the accuracy of test results of the test cases.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method and apparatus for generating documents. Background Technology

[0002] In related technologies, well-designed test cases can uncover more errors in test files compared to poor-quality test cases. To measure the error-detecting capability of test cases, the industry uses mutation testing. Mutation testing simulates real program errors by injecting errors into the program, and then uses the ability of existing test cases to find these injected errors to measure the current test cases' ability to detect real errors. However, existing mutation testing methods cannot comprehensively cover errors during program execution; that is, they cannot generate comprehensive mutation files, thus failing to accurately evaluate the quality of test cases.

[0003] Therefore, it is necessary to provide a file generation method and apparatus that can generate multiple variant files and use these variant files to perform variant testing on test cases, thereby improving the accuracy of test results. Summary of the Invention

[0004] This application provides a file generation method and apparatus, which can perform mutation testing on test cases through the generated mutation files, thereby improving the accuracy of test results.

[0005] On the one hand, this application provides a document generation method, the method comprising:

[0006] Obtain the source code and mutation rules of the target source file; the mutation rules are used to modify elements in the source code.

[0007] The source code of the target source file is parsed using a pre-defined open-source tool, and an abstract syntax tree is constructed; the nodes in the abstract syntax tree represent elements in the source code; the paths in the compilation commands of the pre-defined open-source tool are absolute paths;

[0008] Traverse the abstract syntax tree and determine the nodes to be mutated according to the mutation rules;

[0009] Based on the node to be mutated and the mutation rules, the source code is modified to generate a mutated text.

[0010] On the other hand, a file generation apparatus is provided, the apparatus comprising:

[0011] The mutation rule acquisition module is used to acquire the source code and mutation rules of the target source file; the mutation rules are used to modify elements in the source code.

[0012] An abstract syntax tree (AST) construction module is used to parse the source code of the target source file based on a preset open-source tool and construct an AST; the nodes in the AST represent elements in the source code; the paths in the compilation commands of the preset open-source tool are absolute paths;

[0013] The module for determining nodes to be mutated is used to traverse the abstract syntax tree and determine the nodes to be mutated according to the mutation rules.

[0014] The mutation file generation module is used to modify the source code based on the node to be mutated and the mutation rules to generate a mutation file.

[0015] On the other hand, a file generation device is provided, the device including a processor and a memory, the memory storing at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by the processor to implement the file generation method as described above.

[0016] On the other hand, a computer storage medium is provided that stores at least one instruction or at least one program, which is loaded and executed by a processor to implement the file generation method described above.

[0017] On the other hand, a computer program product or computer program is provided, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the file generation method as described above.

[0018] The document generation method and apparatus provided in this application have the following technical advantages:

[0019] This application obtains the source code and mutation rules of the target source file; and parses the source code of the target source file based on a preset open-source tool to construct an abstract syntax tree; the path in the compilation command of the preset open-source tool is an absolute path; this improves the completeness of the abstract syntax tree, thereby enabling the source code to be completely parsed into a syntax tree, which facilitates the search for nodes to be mutated; then, the abstract syntax tree is traversed, and the nodes to be mutated are determined according to the mutation rules; based on the nodes to be mutated and the mutation rules, the source code is modified to generate a mutation file; thus, it realizes the determination of nodes to be mutated through the abstract syntax tree and mutation rules, and the rapid identification of elements to be mutated in the source code, thereby generating a mutant and obtaining a mutation file; the generated mutation file is used to perform mutation testing on test cases, improving the accuracy of test case test results. Attached Figure Description

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

[0021] Figure 1 This is a schematic diagram of a document generation system provided in an embodiment of this application;

[0022] Figure 2 This is a flowchart illustrating a file generation method provided in an embodiment of this application;

[0023] Figure 3 This is a flowchart illustrating a method for determining a node to be mutated, as provided in an embodiment of this application.

[0024] Figure 4 This is a flowchart illustrating a method for generating variant files provided in an embodiment of this application;

[0025] Figure 5 This is a flowchart illustrating a method for obtaining modified source code according to an embodiment of this application;

[0026] Figure 6 This is a flowchart illustrating a method for obtaining test results of test cases according to an embodiment of this application;

[0027] Figure 7 This is a flowchart illustrating a method for generating a variant file according to an embodiment of this application;

[0028] Figure 8 This is a flowchart illustrating a method for determining a mutable node provided in an embodiment of this application;

[0029] Figure 9 This is a flowchart illustrating another method for generating variants provided in an embodiment of this application;

[0030] Figure 10 This is a schematic diagram of the structure of a document generation device provided in an embodiment of this application;

[0031] Figure 11 This is a schematic diagram of the structure of a server provided in an embodiment of this application. Detailed Implementation

[0032] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0033] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or server that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or devices.

[0034] Please see Figure 1 , Figure 1 This is a schematic diagram of a file generation system provided in an embodiment of this application, such as... Figure 1 As shown, the file generation system may include at least server 01 and client 02.

[0035] Specifically, in this embodiment, server 01 may include a standalone server, a distributed server, or a server cluster composed of multiple servers. It may also be a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. Server 01 may include a network communication unit, a processor, and a memory, etc. Specifically, server 01 can be used to generate variant files.

[0036] Specifically, in this embodiment, the client 02 may include physical devices such as smartphones, desktop computers, tablets, laptops, digital assistants, smart wearable devices, smart speakers, in-vehicle terminals, and smart TVs. It may also include software running on the physical device, such as web pages provided to users by service providers, or applications provided to users by those service providers. Specifically, the client 02 can be used to receive mutated files sent by the server 01 and execute test cases based on the mutated files.

[0037] The following describes a document generation method according to this application. Figure 2 This is a flowchart illustrating a file generation method provided in an embodiment of this application. This specification provides the operational steps of the method described in the embodiments or flowcharts, but based on conventional or non-inventive methods, more or fewer operational steps may be included. The order of steps listed in the embodiments is merely one possible execution order among many and does not represent the only possible execution order. In actual system or server product execution, the methods shown in the embodiments or drawings can be executed sequentially or in parallel (e.g., in a parallel processor or multi-threaded processing environment). Specifically, as shown in the flowchart... Figure 2 As shown, the method may include:

[0038] S201: Obtain the source code and mutation rules of the target source file; the mutation rules are used to modify elements in the source code.

[0039] In this embodiment, the target source file is a file in a preset language; the preset language can be Objective-C, often written as ObjC or OC, and less commonly Objective C or Obj-C, which is an object-oriented programming language that extends C. The target source file can be a file within a target program, which can include, but is not limited to, iOS (Apple's mobile operating system), iPad OS (iPad Operating System, a series of mobile operating systems developed by Apple based on iOS), and Mac OS X (Apple's proprietary operating system for Macintosh computers) platform operating system programs and their applications. The mutation rule is a conversion rule that generates a minimally different program (i.e., a mutant) from the original program while adhering to syntax rules. The mutant is the smallest unit for mutation testing, representing a simulated defect in the software. Running the mutant is used to evaluate the quality of the automated test cases for the current system. Mutation testing is a testing method that measures the completeness of automated test cases by generating mutations in the source code to simulate fault injection.

[0040] In this embodiment, the source code consists of various elements, which may include, but are not limited to, data types, functions, arrays, pointers, strings, constants, symbols, etc. The mutation rules are preset rules that can be used to modify various elements in the source code.

[0041] S203: Based on the preset open-source tool, parse the source code of the target source file and construct an abstract syntax tree; the nodes in the above abstract syntax tree represent the elements in the above source code; the paths in the compilation commands of the above preset open-source tool are absolute paths.

[0042] In this embodiment of the application, the above-mentioned method of parsing the source code of the target source file based on a preset open-source tool and constructing an abstract syntax tree includes:

[0043] Based on a preset compiler, lexical and syntactic analysis are performed on the source code of the target source file to obtain the target language code.

[0044] The absolute path of the attribute information of each element in the target language code is obtained and stored based on a pre-set open-source tool;

[0045] The abstract syntax tree is constructed based on the code of the target language.

[0046] In this embodiment, the source code is compiled to obtain an Abstract Syntax Tree (AST). In computer science, an Abstract Syntax Tree (AST), or simply a syntax tree, is an abstract representation of the syntactic structure of source code. It represents the syntactic structure of a programming language in a tree-like structure, where each node represents a structure in the source code. AST nodes are of three types: declarations (Decl), statements (Stmt), and types (Type). The information from these AST nodes allows for effective analysis of the program.

[0047] In this embodiment of the application, the default compiler can be Clang libtooling; Clang is a C, C++, Objective-C, and Objective-C++ compiler front-end based on LLVM, where LLVM is a framework system for building compilers and is written in C++; LibTooling is a library that supports writing independent tools based on Clang.

[0048] The default compiler may include a lexical analyzer, a syntax analyzer, and a semantic analyzer, wherein:

[0049] Lexical analyzer: used to convert a stream of characters into a stream of words;

[0050] Parser: Used to convert a stream of words into a syntax tree;

[0051] Semantic analyzer: Used to collect attribute information of identifiers, such as type, species, storage location, length value, scope, element and return value information, etc.

[0052] In this embodiment, the default open-source tool can be Xcpretty, an open-source tool for processing compilation logs. In related technologies, due to inherent defects in Xcpretty, there are issues with the precompiled header files in the replacement compilation commands, and the compilation commands contain relative paths. These issues cause Clang to parse the AST incompletely. Specifically, when parsing the target source file into an AST, the missing path information for some header files prevents the parsing of functions, variables, and other information defined within those header files, resulting in an incomplete AST. This embodiment addresses this issue by further developing Xcpretty. The main improvements are as follows: all relative paths in the compilation commands generated by Xcpretty are replaced with absolute paths, resolving the issue of Clang libtooling's inability to recognize relative paths; and the replacement of precompiled header files in Xcpretty is now handled by finding and replacing the corresponding files according to their path names.

[0053] In this embodiment of the application, the attribute information of an element can be obtained based on the absolute path of the element's attribute information, thereby determining the element's location information in the source code.

[0054] S205: Traverse the above abstract syntax tree and determine the nodes to be mutated according to the above mutation rules.

[0055] Specifically, in this embodiment of the application, the above-mentioned traversal of the abstract syntax tree and determination of the node to be mutated according to the above-mentioned mutation rules include:

[0056] A node matcher is constructed based on the above mutation rules;

[0057] Traverse the abstract syntax tree and find the node to be mutated based on the node matcher.

[0058] In this embodiment, one mutation rule corresponds to one node matcher, which can be an AST Matcher provided by Clang. Then, the AST is traversed, and the node to be mutated is matched according to the node matcher. If a node to be mutated is matched, its attribute information is obtained; otherwise, the AST is traversed again.

[0059] In this embodiment of the application, the number of mutation rules is at least two. The above-mentioned traversal of the abstract syntax tree and determination of the node to be mutated according to the above-mentioned mutation rules include:

[0060] Traverse the above abstract syntax tree to determine the initial node to be mutated for each mutation rule;

[0061] The nodes to be mutated are determined from at least two initial nodes to be mutated.

[0062] In this embodiment, one mutation rule corresponds to one initial node to be mutated. The node to be mutated can be selected from multiple initial nodes to be mutated according to a preset rule; alternatively, the mutation file of each initial node to be mutated can be constructed in sequence.

[0063] In this embodiment of the application, the test cases for the mutated files are constructed based on the preset code in the aforementioned source code, such as... Figure 3 As shown, determining the node to be mutated from at least two initial nodes to be mutated includes:

[0064] S301: Determine the initial element represented by each initial node to be mutated;

[0065] S303: Determine the first position information of each initial element in the above source code;

[0066] S305: Determine the second location information of the above-mentioned preset code in the above-mentioned source code;

[0067] S307: Determine whether the first position information corresponding to each initial element matches the aforementioned second position information;

[0068] S309: The first location information that matches the second location information mentioned above is determined as candidate location information;

[0069] S3011: Determine the initial element corresponding to the above candidate position information as the candidate element;

[0070] S3013: Determine the initial node to be mutated corresponding to the above candidate elements as the above node to be mutated.

[0071] Specifically, in this application's embodiments, a test case refers to a description of a testing task for a specific software product, embodying the test plan, methods, techniques, and strategies. Its content includes test objectives, test environment, input data, test steps, expected results, test scripts, etc., ultimately forming a document. Simply put, a test case is a set of test inputs, execution conditions, and expected results compiled for a specific objective, used to verify whether a specific software requirement is met.

[0072] Specifically, in this embodiment, a test case can correspond to a portion of code in the source code; based on the location information, it can be determined whether the code corresponding to the node to be mutated is in the code corresponding to the test case; if so, it is determined as the node to be mutated and a mutant is generated; the node represents an element in the code, and the position of the element in the source code can be determined; the position of the element is compared with the position of the preset code corresponding to the test case, and if the position of the code in the test case covers the position of the element, the node corresponding to the element is determined to be the node to be mutated; thereby ensuring that the element represented by the node to be mutated is part of the code in the test case.

[0073] S207: Modify the source code based on the nodes to be mutated and the mutation rules to generate a mutation file.

[0074] In the embodiments of this application, there can be one or more nodes to be mutated; if there are multiple nodes to be mutated, in order to facilitate the discovery of problems in the test cases, a mutant is generated each time based on a node to be mutated, that is, a mutant file is obtained; then the test case is executed, and after the execution is completed, a new mutant file is generated based on another node to be mutated, so as to perform the next test.

[0075] In the embodiments of this application, such as Figure 4 As shown, the above mutation rules include the mapping relationship between the element to be mutated and the mutated element, the above-mentioned node to be mutated represents the above-mentioned element to be mutated, and the above-mentioned source code is modified based on the above-mentioned node to be mutated and the above-mentioned mutation rules to generate a mutation file, including:

[0076] S2071: Obtain the attribute information of the node to be mutated; the attribute information represents the position information of the element to be mutated.

[0077] In this embodiment of the application, the attribute information of the node to be mutated may include, but is not limited to, the starting row, the starting column, the ending row, the ending column, the original code prototype before mutation, and the class to which it belongs.

[0078] S2073: Based on the above attribute information and the above mutation rules, modify the above-mentioned element to be mutated in the above source code to the above-mentioned mutated element to obtain the modified source code; the above-mentioned element to be mutated is the element represented by the above-mentioned node to be mutated.

[0079] In the embodiments of this application, such as Figure 5 As shown, based on the aforementioned attribute information and mutation rules, the elements to be mutated in the aforementioned source code are modified to the aforementioned mutated elements, resulting in the modified source code, including:

[0080] S20731: Based on the above attribute information, determine the location information of the element to be mutated in the above source code;

[0081] S20733: Based on the above location information, locate the element to be mutated in the above source code;

[0082] S20735: Based on the above mutation rules, modify the elements to be mutated in the above source code to the above mutated elements to obtain the modified source code.

[0083] Specifically, in this embodiment of the application, the elements to be mutated in the source code are modified to the mutated elements, which is to construct the mutant, and each node to be mutated matches a unique mutant.

[0084] S2075: Based on the modified source code described above, generate the aforementioned variant file.

[0085] In this embodiment of the application, the file corresponding to the modified source code is the variant file, which includes the variant.

[0086] In this embodiment, the location information of the element to be mutated can be quickly determined based on the attribute information, and the corresponding element to be mutated can be found based on the location information. Then, the mutant can be quickly constructed according to the mutation rules to generate the mutant file.

[0087] In a specific embodiment, such as Figure 7 As shown, the methods for generating mutation files include:

[0088] S710: Stage for finding variable sites;

[0089] Among them, the mutable site is the mutable node, and the above-mentioned stage of finding the mutable site includes:

[0090] S7101: Obtain the source code of the target source file;

[0091] S7102: Obtain the AST by parsing the source code using Clang;

[0092] S7103: Traverse the AST and obtain mutable sites according to the mutation rules;

[0093] S7104: Obtain information on all mutable sites in the AST; the information on mutable sites is the attribute information of the mutable sites.

[0094] S720: Mutant generation stage.

[0095] The mutant generation stage includes:

[0096] S7201: Identify a specified variable site from multiple variable sites;

[0097] S7202: Obtain the AST by parsing the source code using Clang;

[0098] S7203: Matches a specified variable site in the AST;

[0099] S7204: Modify the elements corresponding to the mutable sites in the source code according to the mutation rules;

[0100] S7204: Generate the mutated source code to obtain the mutated file.

[0101] In another specific embodiment, such as Figure 8 As shown, the methods for determining mutable nodes include:

[0102] S801: Obtain the source code of the target source file;

[0103] S803: Based on the Clang compiler, the source code is preprocessed, lexical analyzed, syntax analyzed, and semantic analyzed sequentially.

[0104] Preprocessing refers to the series of text transformations and manipulations performed on the input by a preprocessor. The preprocessor is a standalone program invoked by the compiler before the actual compilation begins. Its functions include reading the input file into memory and breaking lines; replacing comments with single spaces; and converting the input into a series of preprocessing tokens.

[0105] The entire lexical analysis process mainly involves dividing our code into many string sequences based on identifiers, numbers, string literals, and punctuation marks.

[0106] Syntax analysis refers to the process of analyzing the input file and converting the tokens generated by the preprocessor into a syntax tree.

[0107] Once the syntax tree is generated, semantic analysis, type checking, and code formatting checks will be performed. This stage is responsible for generating most compiler warnings and errors from the syntax analysis process. The final output is the AST (Abstract Syntax Tree).

[0108] S805: The compiler outputs an abstract syntax tree;

[0109] S807: Preset set of mutation rules;

[0110] S809: Constructing a node matcher (AST Matcher) based on mutation rules;

[0111] S811: Traverse the AST and match mutable nodes (sites) based on the node matcher;

[0112] S813: Determine whether each current node matches the node matcher;

[0113] S8151: If so, determine the current node as a mutable node and obtain the attribute information (mutant information) of the current node;

[0114] S8153: If not, continue matching the next node;

[0115] S817: After the traversal is complete, obtain all mutable nodes and their corresponding attribute information in the source file.

[0116] In one specific embodiment, after obtaining all mutable nodes and their corresponding attribute information corresponding to the source file, a mutant can be generated, such as... Figure 9 As shown, the methods for generating mutants include:

[0117] S901: Obtain the source code of the target source file;

[0118] S903: Based on the Clang compiler, the source code is preprocessed, lexical analyzed, syntax analyzed, and semantic analyzed sequentially.

[0119] S905: The compiler outputs an abstract syntax tree;

[0120] S907: Based on a pre-built node matcher, node matching is performed using a preset set of mutation rules and mutation site information;

[0121] S909: Traverse the abstract syntax tree and match the node to be mutated (which can be a specified mutated node);

[0122] S911: Modify the corresponding elements in the source code according to the corresponding mutation rules;

[0123] S913: Generate a mutant and obtain the mutant file.

[0124] In this embodiment of the application, after modifying the source code based on the node to be mutated and the mutation rules to generate the mutated file, the method further includes:

[0125] Based on the aforementioned variant files, the test cases are subjected to variant testing to obtain the test results of the aforementioned test cases; the aforementioned test results characterize the quality of the aforementioned test cases; the aforementioned test cases are constructed based on the aforementioned target source files.

[0126] In this embodiment, the node to be mutated can be quickly identified, thereby generating a mutation file quickly, and then the test cases can be tested, which improves the testing efficiency and accuracy of the test cases.

[0127] In the embodiments of this application, such as Figure 6 As shown, the test results obtained by performing mutation tests on the test cases based on the aforementioned mutation files include:

[0128] S601: Execute the above test cases in the above target source file to obtain the first execution result;

[0129] S603: Execute the above test cases in the above variant file to obtain the second execution result;

[0130] S605: If the first execution result is consistent with the second execution result, the test case is determined to be a test case to be improved.

[0131] In this embodiment of the application, the variant file can be generated on the server; the target source file can be a file on the client. After the server generates the variant file, it can send the variant file to the client and execute the test cases on the client. The test cases are executed in the source file and the variant file respectively, and the execution results of the two are compared to obtain the test results of the test cases.

[0132] In this embodiment of the application, after executing the test cases in the aforementioned variant file and obtaining the second execution result, the method further includes:

[0133] Determine whether the first execution result above is consistent with the second execution result above;

[0134] If the first execution result is inconsistent with the second execution result, the test case is determined to be a normal test case.

[0135] Specifically, in the embodiments of this application, if the first execution result is inconsistent with the second execution result, it indicates that the test case detected a variant, which means that the quality of the test case is good.

[0136] In this embodiment of the application, the number of the aforementioned variant files is a preset number, which is at least two. The execution of the aforementioned test cases within the aforementioned variant files to obtain the second execution result includes:

[0137] Execute the above test cases in each variant file to obtain the second execution result for each variant file.

[0138] In the embodiments of this application, a test case typically requires multiple variant files to test its quality. Different variant files correspond to different defects. If the test case can detect all variant files, it indicates that the quality of the test case is good and no improvement is needed; otherwise, its quality can be evaluated based on the proportion of variant files detected by the test case.

[0139] In this application embodiment, determining the above-mentioned test cases as test cases to be improved includes:

[0140] Each second execution result is compared with the first execution result to obtain the target number of second execution results that do not match.

[0141] Calculate the ratio of the target quantity to the preset quantity;

[0142] If the above ratio is less than a preset threshold, the above test case is determined to be a test case that needs improvement.

[0143] Specifically, in this embodiment, if the comparison between the second execution result and the first execution result is inconsistent, it indicates that the test case has found a variant corresponding to the execution result. By determining the target number of inconsistent second execution results, the proportion of variants found by the test case can be calculated. If the ratio is less than a preset threshold, it indicates that the test case is a test case to be improved. If the ratio is greater than or equal to the preset threshold, it indicates that the test case is a normal test case and can be applied to file testing.

[0144] In this embodiment of the application, after determining the test case as a test case to be improved, the method further includes:

[0145] Obtain the target second execution result that matches the first execution result mentioned above;

[0146] Determine the target variant file corresponding to the second execution result of the above objectives;

[0147] Determine the target mutation rules corresponding to the above target mutation files;

[0148] Based on the aforementioned target mutation rules, the above-mentioned use cases to be improved are improved.

[0149] In this embodiment of the application, for the test case to be improved, the target mutation rule can be determined according to its corresponding target mutation file, indicating that the test case to be improved cannot identify the variant corresponding to the target mutation rule; based on this, the test case to be improved can be improved, thereby improving the quality of the test case to be improved and making it meet the test requirements.

[0150] In this embodiment of the application, the problems existing in the use case to be improved can be quickly identified according to the mutation rules corresponding to the mutation file, so as to facilitate rapid improvement to make it meet the testing requirements.

[0151] Specifically, in this embodiment of the application, in the XX iOS project, this scheme is used to generate variants, and a total of 243,780 problematic test cases are scanned. Based on the variants corresponding to each test case, the repair unit can be driven to fix the defects in the test cases, improve the effectiveness of unit tests, and ensure the quality of the XX iOS product.

[0152] In iOS mini-games and mini-programs, this solution was used to generate variants, which identified 4,683 unit test validity issues in test cases. Based on the variants corresponding to each test case, the fix unit was driven to fix the defects in the test cases, thereby improving the unit test validity.

[0153] As can be seen from the technical solutions provided by the embodiments of this application above, the embodiments of this application obtain the source code and mutation rules of the target source file; and parse the source code of the target source file based on a preset open-source tool to construct an abstract syntax tree; the path in the compilation command of the preset open-source tool is an absolute path; this improves the completeness of the abstract syntax tree, thereby enabling the source code to be completely parsed into a syntax tree, which facilitates the search for nodes to be mutated; then, the abstract syntax tree is traversed, and the nodes to be mutated are determined according to the mutation rules; the source code is modified based on the nodes to be mutated and the mutation rules to generate a mutation file; thus, the nodes to be mutated are determined through the abstract syntax tree and mutation rules, and the elements to be mutated in the source code are quickly determined, thereby generating a mutant and obtaining a mutation file; the generated mutation file is used to perform mutation testing on test cases, improving the accuracy of test case test results.

[0154] This application also provides a document generation apparatus, such as... Figure 10 As shown, the device includes:

[0155] The mutation rule acquisition module 1010 is used to acquire the source code and mutation rules of the target source file; the mutation rules are used to modify elements in the source code.

[0156] Abstract syntax tree construction module 1020 is used to parse the source code of the target source file based on a preset open-source tool and construct an abstract syntax tree; the nodes in the abstract syntax tree represent the elements in the source code; the paths in the compilation commands of the preset open-source tool are absolute paths;

[0157] The node to be mutated determination module 1030 is used to traverse the above abstract syntax tree and determine the node to be mutated according to the above mutation rules.

[0158] The mutation file generation module 1040 is used to modify the source code based on the nodes to be mutated and the mutation rules to generate mutation files.

[0159] In some embodiments, the mutation rule includes a mapping relationship between the element to be mutated and the mutated element, the node to be mutated represents the element to be mutated, and the mutation file generation module may include:

[0160] The attribute information acquisition unit is used to acquire the attribute information of the node to be mutated; the attribute information represents the position information of the element to be mutated.

[0161] The modified source code determination unit is used to modify the element to be mutated in the source code to the mutated element based on the above attribute information and the above mutation rules, so as to obtain the modified source code; the element to be mutated is the element represented by the node to be mutated.

[0162] The variant file generation unit is used to generate the aforementioned variant file based on the modified source code.

[0163] In some embodiments, the modified source code determination unit may include:

[0164] The location information determination subunit is used to determine the location information of the element to be mutated in the source code based on the above attribute information.

[0165] The subunit for finding the element to be mutated is used to find the element to be mutated in the source code based on the above location information.

[0166] The modified source code determines the sub-unit, which is used to modify the elements to be mutated in the source code to the mutated elements based on the above mutation rules, thus obtaining the modified source code.

[0167] In some embodiments, the number of the above-mentioned variation rules is at least two, and the apparatus may further include:

[0168] In some embodiments, the node to be mutated determination module may include:

[0169] The initial node to be mutated determination unit is used to traverse the above abstract syntax tree and determine the initial node to be mutated corresponding to each mutation rule.

[0170] The node to be mutated determination unit is used to determine the aforementioned node to be mutated from at least two initial nodes to be mutated.

[0171] In some embodiments, the apparatus may further include:

[0172] The test result determination module is used to perform mutation testing on the test cases based on the above-mentioned mutation files to obtain the test results of the above-mentioned test cases; the above-mentioned test results characterize the quality of the above-mentioned test cases; the above-mentioned test cases are constructed based on the above-mentioned target source files.

[0173] In some embodiments, the test cases described above are constructed based on preset code in the source code described above, and the node to be mutated determination unit may include:

[0174] The initial element determination subunit is used to determine the initial element represented by each initial node to be mutated;

[0175] The first position information determination subunit is used to determine the first position information of each initial element in the above source code.

[0176] The second location information determining subunit is used to determine the second location information of the preset code in the source code.

[0177] The judgment sub-unit is used to determine whether the first position information corresponding to each initial element matches the aforementioned second position information;

[0178] The candidate location information determination subunit is used to determine the first location information that matches the second location information as candidate location information.

[0179] The candidate element determination subunit is used to determine the initial element corresponding to the above candidate position information as a candidate element;

[0180] The node to be mutated determination sub-unit is used to determine the initial node to be mutated corresponding to the above candidate elements as the above node to be mutated.

[0181] In some embodiments, the test result determination module may include:

[0182] The first execution result determination unit is used to execute the above test cases in the above target source file to obtain the first execution result;

[0183] The second execution result determination unit is used to execute the above test cases in the above variant file to obtain the second execution result;

[0184] The test case determination unit is used to determine the test case as a test case to be improved if the first execution result is consistent with the second execution result.

[0185] In some embodiments, the number of the aforementioned mutated files is a preset number, which is at least two, and the second execution result determination unit may include:

[0186] The second execution result determination subunit is used to execute the above test cases in each variant file to obtain the second execution result corresponding to each variant file.

[0187] In some embodiments, the use case determination unit may include:

[0188] The target quantity determination subunit is used to compare each second execution result with the first execution result mentioned above to obtain the target quantity of second execution results that do not match.

[0189] The ratio calculation subunit is used to calculate the ratio of the target quantity to the preset quantity.

[0190] The sub-unit for determining test cases to be improved is used to determine the above test cases as test cases to be improved if the above ratio is less than a preset threshold.

[0191] In some embodiments, the apparatus may further include:

[0192] The result acquisition module is used to acquire the target second execution result that matches the first execution result mentioned above.

[0193] The target variant file determination module is used to determine the target variant file corresponding to the second execution result of the above target;

[0194] The target mutation rule determination module is used to determine the target mutation rules corresponding to the above-mentioned target mutation files;

[0195] The use case improvement module is used to improve the use cases to be improved based on the target mutation rules mentioned above.

[0196] The apparatus and method embodiments described herein are based on the same inventive concept.

[0197] This application provides a file generation device, which includes a processor and a memory. The memory stores at least one instruction or at least one program, which is loaded and executed by the processor to implement the file generation method provided in the above method embodiments.

[0198] Embodiments of this application also provide a computer storage medium, which can be disposed in a terminal to store at least one instruction or at least one program related to implementing a file generation method in the method embodiments. The at least one instruction or at least one program is loaded and executed by the processor to implement the file generation method provided in the above method embodiments.

[0199] Embodiments of this application also provide a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the file generation method provided in the above-described method embodiments.

[0200] Optionally, in this embodiment, the storage medium may be located at at least one of the multiple network servers in a computer network. Optionally, in this embodiment, the storage medium may include, but is not limited to, various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.

[0201] The memory described in this application embodiment can be used to store software programs and modules. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory. The memory may mainly include a program storage area and a data storage area. The program storage area may store the operating system, applications required for the functions, etc.; the data storage area may store data created according to the use of the device, etc. In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. Accordingly, the memory may also include a memory controller to provide the processor with access to the memory.

[0202] The file generation method embodiments provided in this application can be executed on mobile terminals, computer terminals, servers, or similar computing devices. Taking running on a server as an example, Figure 11 This is a hardware structure block diagram of a server for a file generation method provided in an embodiment of this application. For example... Figure 11 As shown, the server 1100 can vary significantly due to different configurations or performance. It may include one or more Central Processing Units (CPUs) 1110 (CPUs 1110 may include, but are not limited to, microprocessors (MCUs) or programmable logic devices (FPGAs), a memory 1130 for storing data, and one or more storage media 1120 (e.g., one or more mass storage devices) for storing application programs 1123 or data 1122. The memory 1130 and storage media 1120 may be temporary or persistent storage. The program stored in the storage media 1120 may include one or more modules, each module including a series of instruction operations on the server. Furthermore, the CPU 1110 may be configured to communicate with the storage media 1120 and execute the series of instruction operations stored in the storage media 1120 on the server 1100. Server 1100 may also include one or more power supplies 1160, one or more wired or wireless network interfaces 1150, one or more input / output interfaces 1140, and / or one or more operating systems 1121, such as Windows Server™, Mac OS X™, Unix™, Linux™, FreeBSD™, etc.

[0203] The input / output interface 1140 can be used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of server 1100. In one example, the input / output interface 1140 includes a network interface controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the input / output interface 1140 may be a radio frequency (RF) module for wireless communication with the Internet.

[0204] Those skilled in the art will understand that Figure 11 The structure shown is for illustrative purposes only and does not limit the structure of the aforementioned electronic device. For example, server 1100 may also include... Figure 11 The more or fewer components shown, or having the same Figure 11 The different configurations shown.

[0205] As can be seen from the embodiments of the file generation method, apparatus, server, or storage medium provided in this application, this application obtains the source code and mutation rules of the target source file; and parses the source code of the target source file based on a preset open-source tool to construct an abstract syntax tree; the path in the compilation command of the preset open-source tool is an absolute path; this improves the completeness of the abstract syntax tree, thereby enabling the source code to be completely parsed into a syntax tree, which facilitates the search for nodes to be mutated; then, the abstract syntax tree is traversed, and the nodes to be mutated are determined according to the mutation rules; the source code is modified based on the nodes to be mutated and the mutation rules to generate a mutated file; thus, it realizes the determination of nodes to be mutated through the abstract syntax tree and mutation rules, and the rapid determination of elements to be mutated in the source code, thereby generating a variant and obtaining a mutated file; the generated mutated file is used to perform mutation testing on test cases, improving the accuracy of test case test results.

[0206] It should be noted that the order of the embodiments described above is merely for descriptive purposes and does not represent the superiority or inferiority of the embodiments. Furthermore, specific embodiments have been described above. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims can be performed in a different order than that shown in the embodiments and still achieve the desired result. Additionally, the processes depicted in the drawings do not necessarily require a specific or sequential order to achieve the desired result. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0207] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, devices, and storage media are basically similar to the method embodiments, so the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.

[0208] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer storage medium, such as a read-only memory, a disk, or an optical disk.

[0209] The above description is only a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.

Claims

1. A method for generating a file, characterized in that, The method includes: Obtain the source code and mutation rules of the target source file; the mutation rules are used to modify elements in the source code; the target source file is an Objective-C language source file; the number of mutation rules is at least two. The source code of the target source file is parsed using a pre-defined open-source tool, and an abstract syntax tree is constructed. Nodes in the abstract syntax tree represent elements in the source code. The paths in the compilation commands of the pre-defined open-source tool are absolute paths, and the corresponding files are searched and replaced according to the path names of the pre-compiled header files during header file replacement. Traverse the abstract syntax tree to determine the initial node to be mutated for each mutation rule; Determine the first position information of the initial element represented by each initial node to be mutated in the source code; Obtain test cases, which are constructed based on preset code in the source code; determine the second location information of the preset code in the source code; The first location information that matches the second location information is determined as the candidate location information; The initial element corresponding to the candidate position information is determined as the candidate element; The initial node to be mutated corresponding to the candidate element is determined as the node to be mutated. Based on the mutation rules, the elements to be mutated represented by the nodes to be mutated in the source code are modified into mutated elements to generate a mutation file; the mutation rules include the mapping relationship between the elements to be mutated and the mutated elements.

2. The method according to claim 1, characterized in that, The method further includes: Obtain the attribute information of the node to be mutated; the attribute information represents the position information of the element to be mutated; Based on the attribute information and the mutation rules, the element to be mutated in the source code is modified into the mutated element to obtain the modified source code; the element to be mutated is the element represented by the node to be mutated. The mutated file is generated based on the modified source code.

3. The method according to claim 2, characterized in that, The process of modifying the element to be mutated in the source code to the mutated element based on the attribute information and the mutation rules to obtain the modified source code includes: Based on the attribute information, the location information of the element to be mutated in the source code is determined; Based on the location information, locate the element to be mutated in the source code; Based on the mutation rules, the element to be mutated in the source code is modified into the mutated element to obtain the modified source code.

4. The method according to claim 1, characterized in that, After modifying the source code based on the node to be mutated and the mutation rules to generate a mutation file, the method further includes: The test cases are subjected to mutation testing based on the mutated file to obtain the test results of the test cases; the test results characterize the quality of the test cases; the test cases are constructed based on the target source file.

5. The method according to claim 4, characterized in that, The step of performing mutation testing on the test cases based on the mutation file to obtain the test results of the test cases includes: The test cases are executed in the target source file to obtain the first execution result; The test cases are executed in the variant file to obtain the second execution result; If the first execution result is consistent with the second execution result, the test case is determined to be a test case to be improved.

6. The method according to claim 5, characterized in that, The number of variant files is a preset number, which is at least two. Executing the test cases in the variant files to obtain a second execution result includes: The test cases are executed in each variant file to obtain the second execution result corresponding to each variant file; The process of determining that a test case is a test case to be improved includes: Each second execution result is compared with the first execution result to obtain the target number of second execution results that do not match. Calculate the ratio of the target quantity to the preset quantity; If the ratio is less than a preset threshold, the test case is determined to be a test case that needs improvement.

7. The method according to claim 5, characterized in that, After determining that the test case is a test case to be improved, the method further includes: Obtain the target second execution result that matches the first execution result; Determine the target variant file corresponding to the second execution result of the target; Determine the target mutation rule corresponding to the target mutation file; The use case to be improved is improved based on the target mutation rule.

8. A document generation device, characterized in that, The device includes: The mutation rule acquisition module is used to acquire the source code of the target source file and the mutation rules; the mutation rules are used to modify elements in the source code; the target source file is an Objective-C language source file; the number of mutation rules is at least two. An abstract syntax tree (AST) construction module is used to parse the source code of the target source file based on a preset open-source tool and construct an AST. Nodes in the AST represent elements in the source code. The paths in the compilation commands of the preset open-source tool are absolute paths, and the corresponding files are searched and replaced according to the path names of the pre-compiled header files during header file replacement. An initial node to be mutated determination unit is used to traverse the AST and determine the initial node to be mutated corresponding to each mutation rule. The first position information determination subunit is used to determine the first position information of the initial element represented by each initial node to be mutated in the source code; The second location information determination subunit is used to obtain test cases, which are constructed based on preset code in the source code; and to determine the second location information of the preset code in the source code. A candidate location information determination subunit is used to determine the first location information that matches the second location information as candidate location information; The candidate element determination subunit is used to determine the initial element corresponding to the candidate position information as a candidate element; The node to be mutated determination subunit is used to determine the initial node to be mutated corresponding to the candidate element as the node to be mutated. The mutation file generation module is used to modify the element to be mutated represented by the node to be mutated in the source code into a mutated element based on the mutation rules, thereby generating a mutation file; the mutation rules include the mapping relationship between the element to be mutated and the mutated element.

9. The apparatus according to claim 8, characterized in that, The variant file generation module includes: An attribute information acquisition unit is used to acquire the attribute information of the node to be mutated; the attribute information represents the position information of the element to be mutated. The modified source code determination unit is used to modify the element to be mutated in the source code into the mutated element based on the attribute information and the mutation rules, so as to obtain the modified source code; the element to be mutated is the element represented by the node to be mutated. The variant file generation unit is used to generate the variant file based on the modified source code.

10. The apparatus according to claim 9, characterized in that, The modified source code determination unit includes: The location information determination subunit is used to determine the location information of the element to be mutated in the source code based on the attribute information. The element to be mutated search subunit is used to search for the element to be mutated in the source code based on the location information; The modified source code determines a subunit, which is used to modify the element to be mutated in the source code into the mutated element based on the mutation rule, thereby obtaining the modified source code.

11. The apparatus according to claim 8, characterized in that, The device further includes a test result determination module, which is used to perform mutation testing on the test cases based on the mutation file to obtain the test results of the test cases; The test results characterize the quality of the test cases; The test cases are constructed based on the target source file.

12. The apparatus according to claim 11, characterized in that, The test result determination module includes: The first execution result determination unit is used to execute the test cases in the target source file to obtain a first execution result; The second execution result determination unit is used to execute the test cases in the variant file to obtain a second execution result; The test case determination unit is used to determine the test case as a test case to be improved if the first execution result is consistent with the second execution result.

13. The apparatus according to claim 12, characterized in that, The number of the mutated files is a preset number, and the preset number is at least two. The second execution result determination unit includes: The second execution result determination subunit is used to execute the test cases in each variant file and obtain the second execution result corresponding to each variant file; The unit for determining the use cases to be improved includes: The target quantity determination subunit is used to compare each second execution result with the first execution result to obtain the target quantity of second execution results that do not match. A ratio calculation subunit is used to calculate the ratio of the target quantity to the preset quantity; The sub-unit for determining test cases to be improved is used to determine the test case as a test case to be improved if the ratio is less than a preset threshold.

14. The apparatus according to claim 12, characterized in that, The device further includes: The result acquisition module is used to acquire the target second execution result that matches the first execution result. The target variant file determination module is used to determine the target variant file corresponding to the second execution result of the target; The target mutation rule determination module is used to determine the target mutation rule corresponding to the target mutation file. The use case improvement module is used to improve the use case to be improved based on the target mutation rule.

15. A document generation device, characterized in that, The file generation device includes a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the file generation method as described in any one of claims 1-7.

16. A computer storage medium, characterized in that, The computer storage medium stores at least one instruction or at least one program, which is loaded and executed by a processor to implement the file generation method as described in any one of claims 1-7.