White-box test coverage information extraction method, system, device and medium
Patent Information
- Application Number
- CN202310975981.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-04
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2043-08-04
AI Technical Summary
[0019]采用本申请实施例中提供的白盒测试覆盖信息提取方法,通过向待测程序中插入探针,编译运行插入探针的待测程序获得覆盖信息并写入临时文件,再通过对临时文件进行解析获得覆盖信息,最后通过SVG绘制矢量图将白盒测试覆盖信息表征出来。本申请通过SVG数据保存方式,能够使用Web浏览器、图片查看器或记事本等多种常用工具查看白盒测试覆盖信息,增加测试灵活性、降低工具使用成本、规范过程数据管理、辅助测试人员快速定位程序缺陷。
Smart Images

Figure CN117149620B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software testing, and in particular to a method, system, device, and medium for extracting white-box test coverage information. Background Technology
[0002] White-box testing, also known as logic-driven testing, focuses on how the unit under test (UTC) works internally. In white-box testing, dynamic testing requires testers to observe the code implementation beforehand, design test cases based on the program's control structure, run the program under test, and finally analyze any issues. Test case coverage is crucial data in white-box testing. Test coverage information reflects the adequacy of the tests and serves as a vital basis for testers to analyze problems. Therefore, extracting and preserving test coverage information is key to white-box testing.
[0003] Currently, some commercial testing tools exist that can visually display the code coverage of test cases. However, most of these commercial testing tools are expensive, resulting in high usage costs. Furthermore, the test coverage information from these tools is usually stored in the form of project scripts, making it inconvenient to view outside the test environment, leading to low efficiency, poor intuitiveness, and hindering testers from completing white-box testing quickly. Summary of the Invention
[0004] To address one of the aforementioned technical deficiencies, this application provides a method, system, device, and medium for extracting white-box test coverage information.
[0005] According to a first aspect of the embodiments of this application, a method for extracting white-box test coverage information is provided, the method comprising:
[0006] Insert probes into the program under test;
[0007] Compile and run the test program containing the inserted probe to generate a runtime coverage record;
[0008] Write overlay information into a temporary file based on the runtime overlay record;
[0009] The temporary file is parsed to obtain coverage information, and an SVG control flow graph is drawn based on the parsed coverage information. The white-box test coverage information is represented by the SVG control flow graph.
[0010] According to a second aspect of the embodiments of this application, a white-box testing coverage information extraction system is provided, the system comprising:
[0011] A probe insertion unit is used to insert probes into the program under test.
[0012] The compilation and execution unit is used to compile and run the test program of the inserted probe and generate a running coverage record;
[0013] An information writing unit is used to write overlay information into a temporary file according to the running overlay record;
[0014] The drawing unit is used to parse the temporary file to obtain coverage information, and draw an SVG control flow graph based on the parsed coverage information, thereby representing the white-box test coverage information through the SVG control flow graph.
[0015] According to a third aspect of the embodiments of this application, an electronic device is provided, comprising:
[0016] Memory, used to store computer programs;
[0017] A processor, used to implement the white-box test coverage information extraction method as described above when executing the computer program.
[0018] According to a fourth aspect of the embodiments of this application, a computer-readable storage medium is provided, wherein computer-executable instructions are stored in the computer-readable storage medium, and when the computer-executable instructions are loaded and executed by a processor, the white-box test coverage information extraction method described above is implemented.
[0019] The white-box test coverage information extraction method provided in this application involves inserting probes into the program under test (DUT), compiling and running the DUT with the inserted probes to obtain coverage information, which is then written to a temporary file. The coverage information is further obtained by parsing the temporary file, and finally, a vector graphic representation of the white-box test coverage information is created using SVG. This application utilizes SVG data storage, allowing users to view white-box test coverage information using various common tools such as web browsers, image viewers, or notepad. This increases testing flexibility, reduces tool usage costs, standardizes process data management, and assists testers in quickly locating program defects. Attached Figure Description
[0020] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0021] Figure 1 This is a flowchart of a white-box testing coverage information extraction method as described in Embodiment 1 of this application;
[0022] Figure 2 This is a schematic diagram of the structure of the temporary file described in Embodiment 1 of this application;
[0023] Figure 3An SVG control flow graph generated using the white-box test coverage information extraction method described in Embodiment 1 of this application;
[0024] Figure 4 This is a schematic diagram of the white-box test coverage information extraction system described in Embodiment 2 of this application. Detailed Implementation
[0025] To make the technical solutions and advantages of the embodiments of this application clearer, the exemplary embodiments of this application will be described in further detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not an exhaustive list of all embodiments. It should be noted that, unless otherwise specified, the embodiments and features in the embodiments of this application can be combined with each other.
[0026] Example 1
[0027] like Figure 1 This embodiment proposes a method for extracting white-box test coverage information, which includes:
[0028] S101. Insert a probe into the program under test.
[0029] Specifically, a software probe is a predefined code snippet or function that can be used to mark key information during program execution. This embodiment, based on the characteristics of software probes, inserts probes into the program under test, thereby marking key nodes in the program's execution process.
[0030] This embodiment employs a branch coverage strategy for inserting probes into the program under test. First, a decision node probe is inserted before the conditional statements in the program under test. A decision node is an element in the software control flow model, used to mark the locations where information branches during program execution. Then, branch probes are inserted into each branch of the program under test to mark the location of each branch. Finally, a merge node probe is inserted at the end of all branches. A merge node is an element in the software control flow model, used to mark the locations where information merges during program execution. Thus, this embodiment completes the probe insertion process in the program under test, a process equivalent to marking the trunk, branches, and apex of a tree, thereby connecting the entire execution process of the program under test in a complete chain.
[0031] For more complex programs under test, each branch may contain sub-branches. In such cases, we can extract the branches and sub-branches as independent programs under test. First, we insert sub-decision node probes before the secondary conditional statements in the branches of the program under test. The purpose is also to mark the locations where information flows during program execution, but the locations marked by the sub-decision node probes differ from those marked by the initial decision node probes. Then, we insert sub-branch probes in the sub-branches to mark their positions. If a sub-branch contains further sub-sub-branches, we continue the process of inserting decision node probes and branch probes until all branches are traversed. This ensures that all important nodes and branches in the program under test are marked, guaranteeing the logical integrity of the program during execution.
[0032] The following is a code snippet for inserting a probe:
[0033]
[0034]
[0035] By inserting probes, key information is marked in the above program segment. The marked objects are conditional statements or other code fragments. The purpose of the marking is to identify decision nodes and covered branches in the program control flow model.
[0036] S102. Compile and run the test program with the inserted probe to generate a running coverage record.
[0037] Specifically, in this embodiment, after inserting a probe into the program under test in S101, the program under test with the probe inserted is packaged into stub code. This stub code refers to temporary code used to replace the real code. It can satisfy the platform compatibility of the source code in the program under test, can be compiled and run more quickly, and improve the running efficiency of the program under test.
[0038] After compiling and running the test program packaged into stub code, software control flow node marking rules can be generated based on the positions of the probes inserted in the test program in S101. Then, the runtime coverage record of the test program can be generated based on the node marking rules.
[0039] S103. Write overlay information into the temporary file according to the running overlay record.
[0040] Specifically, after compiling and running the test program with the inserted probe in S102 and generating a runtime coverage record, runtime coverage information is written to a temporary file based on this runtime coverage record. The data encapsulation format used to record the runtime coverage information can be in the following form:
[0041] Node number 1 byte A pair of decision nodes and merging nodes have the same sequence number. Node depth 1 byte Corresponding branch nesting depth Branch type 1 byte 0x55 if branch; 0xAA else branch; 0xff no branch; 0xCC not covered Decision conditions 128 bytes Judgment conditions for extracting decision points Parent node sequence number 1 byte Determine the dependency relationship between nodes
[0042] Figure 2 This embodiment provides a temporary file for writing runtime coverage information. This temporary file, combined with the aforementioned data encapsulation format, has each information node being 133 bytes long, including a 5-byte node identifier and 128 bytes of decision information. Each node can be uniquely identified by its node depth and node sequence number. When node depths are the same, the node position can be determined based on the node sequence number; the smaller the node sequence number, the closer the corresponding node is to the upstream of the control flow, thus ensuring the uniqueness and accuracy of the node identifier.
[0043] S104. Parse the temporary file to obtain coverage information, and draw an SVG control flow graph based on the parsed coverage information. The white-box test coverage information is represented by the SVG control flow graph.
[0044] Specifically, Scalable Vector Graphics (SVG) is based on XML markup language and is a universal data storage format. It can be viewed using tools such as web browsers, image viewers, and notepad, offering strong convenience and intuitiveness. In this embodiment, after obtaining the temporary file containing the overlay information, the temporary file is first parsed to extract the overlay information. This parsing process can be performed using conventional methods, and this embodiment does not provide special explanations or limitations. Then, the extracted overlay information is plotted, causing the overlay information to be parsed into multiple overlay paths. These multiple overlay paths will be saved in the form of a queue. The storage medium can be memory or other storage media, and this embodiment does not provide special limitations.
[0045] After obtaining multiple coverage paths corresponding to the coverage information, all coverage paths are read sequentially according to the queue arrangement, starting from the first coverage path in the queue, to obtain key information such as coordinates, attributes, and decision information text corresponding to each coverage path. Since this embodiment uses SVG to represent coverage information, and SVG primarily represents the program's execution process graphically, this embodiment needs to predefine the representation of drawing elements using XML markup language before obtaining the coverage paths. These elements include shapes, styles, and colors. Shapes can be further defined as rectangles, circles, trapezoids, etc., styles can be further defined as line segments, curves, dashed lines, etc., and colors can be further defined as black, green, gray, etc. This embodiment is merely a simple example and does not impose excessive limitations.
[0046] After obtaining key information such as coordinates, attributes, and decision information text corresponding to each coverage path, the SVG control flow graph corresponding to each coverage path can be drawn by simply matching the above key information with the predefined drawing color representation. This SVG control flow graph allows for a visual overview of white-box test coverage information. The drawn SVG control flow graph can be viewed using tools such as web browsers, image viewers, and notepad, offering strong convenience and intuitiveness.
[0047] After each coverage path is drawn, it is stored in a drawing completion queue. Before drawing the next coverage path, the queue is first scanned to see if the next coverage path already exists. If it already exists, that coverage path is skipped, and the process moves on to the next-next coverage path. If it does not exist in the queue, that coverage path is drawn, and so on, until all coverage paths are drawn. This not only ensures comprehensive drawing of coverage paths but also improves drawing efficiency.
[0048] Finally, if all branches under a certain decision node are covered, then the decision node is identified as an uncovered branch and marked using a different representation than that of covered decision nodes.
[0049] like Figure 3 The SVG control flow graph shown is the one used by the test program of the probe inserted in S101. Figure 2 The diagram shown is drawn from a temporary file containing overlay information. In this SVG control flow graph, decision nodes are identified by rectangles, and merging points and branches are identified by dots and line segments. Overlaid decision points are filled with gray and their decision information is noted below. Uncovered decision points are identified by dashed borders without fill, and similarly, covered branches are identified by line segments, while uncovered branches are identified by dashed lines. This SVG control flow graph allows for a clear and intuitive view of the program under test's execution process. Of course, due to the requirements of the accompanying drawings, color cannot be added in this embodiment. In practical applications, different colors can be used to distinguish different node states for a more intuitive and obvious effect.
[0050] This embodiment inserts probes into the program under test (DUT), compiles and runs the DUT with the inserted probes to obtain coverage information, which is then written to a temporary file. The coverage information is then obtained by parsing the temporary file, and finally represented by a vector graphic using SVG. The SVG data saving method allows users to view the white-box test coverage information using various common tools such as web browsers, image viewers, or notepad, increasing testing flexibility, reducing tool usage costs, standardizing process data management, and assisting testers in quickly locating program defects.
[0051] Example 2
[0052] Corresponding to Embodiment 1, this embodiment proposes a white-box test coverage information extraction system, such as... Figure 4 As shown, the system includes:
[0053] A probe insertion unit is used to insert probes into the program under test.
[0054] The compilation and execution unit is used to compile and run the test program of the inserted probe and generate a running coverage record;
[0055] An information writing unit is used to write overlay information into a temporary file according to the running overlay record;
[0056] The drawing unit is used to parse the temporary file to obtain coverage information, and draw an SVG control flow graph based on the parsed coverage information, thereby representing the white-box test coverage information through the SVG control flow graph.
[0057] Since the embodiments of the white-box test coverage information extraction system correspond to the embodiments of the white-box test coverage information extraction method, please refer to the description of the embodiments of the white-box test coverage information extraction method for the embodiments of the white-box test coverage information extraction system, and will not be repeated here.
[0058] Example 3
[0059] This embodiment proposes an electronic device, including:
[0060] Memory, used to store computer programs;
[0061] A processor is configured to implement the white-box test coverage information extraction method as described above when executing the computer program.
[0062] Since the embodiments of the electronic device part correspond to the embodiments of the white-box test coverage information extraction method part, please refer to the description of the embodiments of the white-box test coverage information extraction method part for the embodiments of the electronic device part, and will not be repeated here.
[0063] Example 4
[0064] This embodiment provides a computer-readable storage medium storing computer-executable instructions. When the computer-executable instructions are loaded and executed by a processor, the white-box test coverage information extraction method described above is implemented.
[0065] Since the embodiments of the computer-readable storage medium portion correspond to the embodiments of the white-box test coverage information extraction method portion, the embodiments of the storage medium portion are described in the description of the embodiments of the white-box test coverage information extraction method, and will not be repeated here.
[0066] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. The solutions in the embodiments of this application can be implemented in various computer languages, such as the object-oriented programming language Java and the interpreted scripting language JavaScript.
[0067] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0068] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0069] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0070] In this application, unless otherwise expressly specified and limited, the terms "installation," "connection," "linking," and "fixing," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection, an electrical connection, or a connection that allows communication between them; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication between two components or the interaction between two components. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.
[0071] Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.
[0072] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.
Claims
1. A method for extracting coverage information in white-box testing, characterized in that, The method includes: Insert probes into the program under test; The process of compiling and running the test program with the inserted probes to generate a runtime coverage record includes: packaging the test program with the inserted probes into stub code, compiling and running it; after compilation and running, generating software control flow node marking rules based on the positions of the inserted probes in the test program; and generating a runtime coverage record based on the software control flow node marking rules. Coverage information is written to a temporary file based on the running coverage record; the temporary file contains node identifiers and decision information, the node identifiers include node depth and node sequence number; when the node depths are the same, the node position is determined based on the node sequence number; the smaller the node sequence number, the closer the corresponding node is to the upstream of the control flow; The temporary file is parsed to obtain coverage information, and an SVG control flow graph is drawn based on the parsed coverage information. The SVG control flow graph represents the white-box test coverage information. The process of drawing the SVG control flow graph based on the parsed coverage information includes: The representation of each element in the control flow graph is predefined using XML markup language; Based on the coverage information obtained from the parsing, points are plotted, multiple coverage paths corresponding to the coverage information are generated, and the multiple coverage paths are stored in the form of a queue; Starting from the coverage path of the first member of the queue, read sequentially to obtain the coordinates, attributes, and decision information text corresponding to each coverage path; After matching the coordinates, attributes, and decision information text with the representation of each element in the pre-defined control flow graph, the SVG control flow graph corresponding to each coverage path is drawn.
2. The method according to claim 1, characterized in that, The process of inserting probes into the program under test includes: Insert a decision node probe before the conditional statements in the program under test; Insert branch probes into each branch of the program under test; Insert an ingress node probe at the end of each branch of the program under test.
3. The method according to claim 2, characterized in that, The process of inserting probes into the program under test also includes: When any branch contains sub-branches, insert a sub-decision node probe before the secondary conditional statement of the program under test; Insert sub-branch probes into sub-branches until all branches have been traversed.
4. The method according to claim 1, characterized in that, The process of drawing the SVG control flow graph based on the coverage information obtained from the parsing also includes: Store the drawn overlay paths in the drawing completion queue; After reading the undrawn overlay path, the drawing completion queue is scanned. If the undrawn overlay path does not appear in the drawing completion queue, the SVG control flow graph corresponding to the undrawn overlay path is drawn.
5. A white-box testing coverage information extraction system, characterized in that, The system includes: A probe insertion unit is used to insert probes into the program under test. The compilation and execution unit is used to compile and run the test program with the inserted probes and generate a runtime coverage record; including: packaging the test program with the inserted probes into stub code for compilation and execution; after compilation and execution, generating software control flow node marking rules according to the positions of the inserted probes in the test program; and generating a runtime coverage record according to the software control flow node marking rules. The information writing unit is used to write coverage information to a temporary file according to the running coverage record; the temporary file contains node identifiers and decision information, and the node identifiers include node depth and node sequence number; when the node depths are the same, the node position is determined according to the node sequence number; the smaller the node sequence number, the closer the corresponding node is to the upstream of the control flow; A drawing unit is used to parse the temporary file to obtain coverage information, and draw an SVG control flow graph based on the parsed coverage information. The SVG control flow graph represents the white-box test coverage information. The process of drawing the SVG control flow graph based on the parsed coverage information includes: The representation of each element in the control flow graph is predefined using XML markup language; Based on the coverage information obtained from the parsing, points are plotted, multiple coverage paths corresponding to the coverage information are generated, and the multiple coverage paths are stored in the form of a queue; Starting from the coverage path of the first member of the queue, read sequentially to obtain the coordinates, attributes, and decision information text corresponding to each coverage path; After matching the coordinates, attributes, and decision information text with the representation of each element in the pre-defined control flow graph, the SVG control flow graph corresponding to each coverage path is drawn.
6. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor, configured to implement the white-box test coverage information extraction method as described in any one of claims 1 to 4 when executing the computer program.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when loaded and executed by a processor, implement the white-box test coverage information extraction method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
A test device and method for performing white-box testing on coverage calculation visualization
CN104331361A
Program static analysis based Java unit test teaching feedback method
CN106021094A
