A code coverage testing method, device, equipment and storage medium

CN122653979APending Publication Date: 2026-08-28SHANDONG YUNHAI GUOCHUANG CLOUD COMPUTING EQUIP IND INNOVATION CENT CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510237559.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-28
Publication Date
2026-08-28

AI Technical Summary

Technical Problem

然而,代码覆盖率获取工具依赖文件系统生成这些文件,在没有文件系统或文件系统资源受限的情况下则会无法使用

Benefits of technology

[0015] The code coverage testing method provided by this invention has the advantage of configuring the code coverage acquisition tool for the software under test with its file output address set to an address in the first storage space of memory, enabling the code coverage acquisition tool to write to memory without relying on the file system. Then, during the execution of the software under test, the method monitors the coverage record data written to the first storage space by the code coverage acquisition tool and moves this data from the first storage space to a second storage space on the disk. This ensures that the writing of coverage record data can continue even when file system resources are limited, without being interrupted by insufficient memory space. After the test is completed, the coverage record data is read from the second storage space to generate the coverage test results. Therefore, this invention solves the problem of being unable to obtain coverage using a code coverage acquisition tool when there is no file system or file system resources are limited. It also avoids the complexity and potential errors introduced by file system operations, improves the usability of the code coverage acquisition tool, and helps reduce testing costs, improve code quality, and enhance the maintainability and reliability of the software.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122653979A_ABST
    Figure CN122653979A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of software testing, and particularly discloses a code coverage testing method and device, equipment and a storage medium, wherein a code coverage acquisition tool is configured for a tested software, a file output address of the code coverage acquisition tool is configured as an address in a memory, the code coverage acquisition tool is enabled to write into the memory without depending on a file system; then, when the tested software is executed for testing, coverage record data written into a first storage space by the coverage acquisition tool during runtime is moved to a second storage space in a disk, so that the coverage record data can be written even when the file system resources are limited, and the writing cannot be continued due to insufficient memory space, so that the coverage record data can be read from the second storage space after the testing is completed to generate a coverage test result. The application solves the problem that the code coverage acquisition tool cannot be used to acquire the coverage when there is no file system or the file system resources are limited.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of software testing technology, and in particular to a method, apparatus, device, and storage medium for testing code coverage. Background Technology

[0002] Code coverage is a crucial parameter for evaluating software quality, especially during the software testing phase. It measures the extent to which test cases cover the source code of the software under test (SDT), essentially the proportion of the entire source code that is executed by the tests. Higher code coverage means more parts of the program are executed by the tests, and consequently, a lower risk of software defects and untested functionalities. Currently, many tools exist for measuring code coverage. These tools track and record the execution of the SDT during program runtime, generating files used to produce coverage test results. However, code coverage tools rely on file systems to generate these files, rendering them unusable in situations where a file system is unavailable or file system resources are limited.

[0003] How to perform software coverage testing in the absence of a file system or when file system resources are limited is a technical problem that needs to be solved by those skilled in the art. Summary of the Invention

[0004] The purpose of this invention is to provide a method, apparatus, device, and storage medium for testing code coverage, which can be used to achieve software coverage testing even when there is no file system or file system resources are limited.

[0005] To address the aforementioned technical problems, this invention provides a method for testing code coverage, comprising: Configure a code coverage acquisition tool for the software under test, and configure the file output address of the code coverage acquisition tool to an address in the first storage space of memory; When performing the test on the software under test, the coverage record data written to the first storage space by the code coverage acquisition tool is monitored, and the coverage record data is moved from the first storage space to the second storage space on the disk. After the test is completed, the coverage record data is read from the second storage space to generate the coverage test results.

[0006] On the one hand, monitoring the coverage record data written to the first storage space by the code coverage acquisition tool during runtime, and moving the coverage record data from the first storage space to a second storage space on the disk, includes: When it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in the memory in a loop, and the data in the third storage space is moved to the second storage space.

[0007] On the other hand, determining that the first storage space contains a coverage record data file includes: After detecting that the coverage record data file corresponding to a source file of the software under test in the first storage space is full, it is determined that there is a coverage record data file to be transmitted in the first storage space.

[0008] On the other hand, when it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space, including: When it is determined that the coverage record data file exists in the first storage space, the file header of the coverage record data file is directly moved from the first storage space to the second storage space; After the file header is moved, the data tags of the coverage record data file in the first storage space are read and parsed in a loop, and the data tags are moved to the third storage space in units of tag data blocks according to the data tags, until the end information of the coverage record data file is read. The tag data blocks that are found to be valid in the third storage space are moved to the second storage space until the end information of the coverage record data file is read.

[0009] On the other hand, when it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space, including: After detecting that the coverage record data file corresponding to a source file of the software under test in the first storage space is full, the first storage information of the coverage record data file in the first storage space is recorded, and the first storage information includes the current read / write position. Based on the first storage information, second storage information of the coverage record data file in the second storage space is generated, wherein the second storage information includes the file size; Based on the first storage information, the data in the coverage record data file is moved to the third storage space in the memory in a loop, and the current read / write position of the coverage record data file is updated until the end information is read; The valid tag data blocks in the third storage space are moved to the second storage space until the end information is read.

[0010] On the other hand, reading the coverage record data from the second storage space to generate coverage test results includes: Create a storage directory on the target device; The storage start position and file size of the coverage record data file in the second storage space are determined based on the second storage information, and the first variable is configured based on the file size; The data in the coverage record data file is moved to the storage directory in a loop so that the target device generates the coverage test result based on the coverage record data file. The first variable is updated based on the remaining data of the coverage record data file in the second storage space until the first variable is 0, and then the second storage information is deleted.

[0011] On the other hand, reading the coverage record data from the second storage space to generate coverage test results includes: Create a storage directory on the target device; The coverage record data is moved from the second storage space to the storage directory, so that the target device can call the code coverage acquisition tool to generate the coverage test results based on the coverage record data and the source files of the software under test.

[0012] To address the aforementioned technical problems, the present invention also provides a code coverage testing apparatus, comprising: The first configuration module is used to configure a code coverage acquisition tool for the software under test, and to configure the file output address of the code coverage acquisition tool to an address in the first storage space of memory. The first migration module is used to monitor the coverage record data written to the first storage space by the code coverage acquisition tool when the test of the software under test is performed, and to migrate the coverage record data from the first storage space to the second storage space on the disk. The second transfer module is used to read the coverage record data from the second storage space to generate coverage test results after the test is completed.

[0013] To address the aforementioned technical problems, the present invention also provides an electronic device, comprising: Memory, used to store computer programs; A processor for implementing the code coverage testing method as described in any of the preceding steps when executing the computer program.

[0014] To address the aforementioned technical problems, the present invention also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of the code coverage testing method as described in any of the preceding claims.

[0015] The code coverage testing method provided by this invention has the advantage of configuring the code coverage acquisition tool for the software under test with its file output address set to an address in the first storage space of memory, enabling the code coverage acquisition tool to write to memory without relying on the file system. Then, during the execution of the software under test, the method monitors the coverage record data written to the first storage space by the code coverage acquisition tool and moves this data from the first storage space to a second storage space on the disk. This ensures that the writing of coverage record data can continue even when file system resources are limited, without being interrupted by insufficient memory space. After the test is completed, the coverage record data is read from the second storage space to generate the coverage test results. Therefore, this invention solves the problem of being unable to obtain coverage using a code coverage acquisition tool when there is no file system or file system resources are limited. It also avoids the complexity and potential errors introduced by file system operations, improves the usability of the code coverage acquisition tool, and helps reduce testing costs, improve code quality, and enhance the maintainability and reliability of the software.

[0016] The code coverage testing device, electronic device, and computer-readable storage medium provided by this invention have the aforementioned beneficial effects, which will not be elaborated further here. Attached Figure Description

[0017] To more clearly illustrate the technical solutions of the embodiments of the present invention 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 the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0018] Figure 1 A flowchart of a first code coverage testing method provided in an embodiment of the present invention; Figure 2 An architecture diagram of a software testing system provided in an embodiment of the present invention; Figure 3 A schematic diagram of a first storage information table provided in an embodiment of the present invention; Figure 4 A schematic diagram of a second storage information table provided in an embodiment of the present invention; Figure 5 A schematic diagram of a third storage information table provided in an embodiment of the present invention; Figure 6 A flowchart illustrating the second code coverage testing method provided in this embodiment of the invention; Figure 7 A flowchart of a third code coverage testing method provided in an embodiment of the present invention; Figure 8 This is a schematic diagram of a code coverage testing device provided in an embodiment of the present invention. Detailed Implementation

[0019] The core of this invention is to provide a code coverage testing method, apparatus, device, and storage medium for achieving software coverage testing even in the absence of a file system or when file system resources are limited.

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

[0021] To facilitate understanding of the technical solutions provided in the embodiments of the present invention, some key terms used in the embodiments of the present invention will be explained here first.

[0022] Code coverage is an important parameter for evaluating software quality, especially during the software testing phase. It measures the extent to which test code covers the program's source code; that is, the ratio of the portion of the program executed by the test to the entire source code. In short, higher code coverage means more parts of the program are executed by the test, and the lower the risk of software defects and untested functionalities. Therefore, testing software code coverage is a crucial part of software performance testing.

[0023] There are many code coverage tools used in related fields, such as Jacoco, Cobertura, Clover, dotCover, OpenCover, Gcov, Lcov, and Ruby. Jacoco, Cobertura, and Clover support the Java programming language, while dotCover and OpenCover are .NET-oriented code coverage tools. Ruby offers a powerful configuration set that can generate HTML reports, and Gcov and Lcov support the C programming language.

[0024] Among them, JAVA, C, and C++ are computer programming languages.

[0025] .NET: A free and open-source development platform for building a variety of applications.

[0026] HTML: short for Hypertext Markup Language, is a markup language. It consists of a series of tags that unify the format of documents on the web, connecting scattered internet resources into a logical whole.

[0027] Furthermore, Gcov and Lcov support the C programming language. As a general-purpose programming language, C is highly efficient, flexible, and portable, making it ideal for developing embedded systems and hardware-related software. For example, in the development of Redundant Arrays of Independent Disks (RAID) card firmware, using C allows for better control of hardware resources, efficient data reading and writing, and error handling, while also ensuring firmware stability and compatibility. Therefore, C is the preferred language for RAID card firmware development.

[0028] Gcov is a C / C++ code coverage analysis tool included with the GNU Compiler Collection (GCC). For projects developed using C, Gcov is the most common method, and therefore widely used in embedded systems. The GNU Compiler Collection comprises programming language compilers developed by GNU. The GNU Compiler Suite includes front-ends for C, C++, Objective-C, Fortran, Java, Ada, and Go, as well as libraries for these languages ​​(such as libstdc++, libgcj, etc.).

[0029] Gcov is a code coverage acquisition tool. When using Gcov, the gcno file is short for gcovnotes (also known as "comment file" or "coverage comment file"), one of the files generated by gcov (GNU code coverage testing tool) during source code compilation. The gcno file contains the control flow graph of the program under test, function call information, and other data used to calculate code coverage. The gcda file (also known as "count data file" or "coverage data file") is generated by running a compiled file with the -fprofile-arcs compilation parameter. It contains the number of arc transitions and other summary information. The gcda file already includes function execution information; the remaining work is to further visualize the execution information and match it with the source code.

[0030] JaCoCo is a code coverage acquisition tool. Before testing, JaCoCo modifies the Java bytecode to insert coverage collection logic into the target code. This process is called "bytecode enhancement." The instrumented bytecode contains instructions for recording code execution behavior, such as which lines of code, branches, or methods were called. During test execution, JaCoCo collects coverage data in real time through the instrumented code. This data is typically stored in memory and eventually generates an .exec file that records which code was executed. After test execution is complete, JaCoCo summarizes the collected coverage data and generates a detailed coverage report. The report supports multiple formats, such as HTML, XML, and CSV, allowing developers to easily see which code was covered by tests and which was not.

[0031] Based on the timing of code coverage acquisition, code coverage testing methods can be divided into two types: dynamic testing and static code analysis. Dynamic testing refers to using code coverage acquisition tools to record code execution in real time during the operation of the software under test, and generating a coverage report after the test is completed. Static testing refers to evaluating code coverage by analyzing the code structure and test case coverage without running the software under test.

[0032] Dynamic testing solutions, such as those using tools like Gcov and Jacoco, can compile code coverage acquisition tools into the source files of the software under test (SDT). This allows the executable file of the code coverage acquisition tool to run concurrently with the test cases, collecting coverage-related data and generating a coverage report. However, these tools rely on a file system to store the coverage data. In embedded operating systems without a file system, such code coverage acquisition tools cannot be used to calculate coverage. Furthermore, the types of file systems used in actual testing vary significantly, potentially leading to incompatibility between the tool and the file system or introducing more complex program errors.

[0033] To address the aforementioned issues, this invention provides a code coverage testing method. When configuring a code coverage acquisition tool for the software under test, the tool's output address is set to an address in a first memory storage space, enabling the tool to write to memory independently of the file system. During the testing of the software under test, the method monitors the coverage recording data written to the first storage space by the tool and moves this data to a second storage space on the disk. This ensures that writing coverage recording data continues even with limited file system resources, preventing write interruptions due to insufficient memory. After the test is completed, the coverage recording data is read from the second storage space to generate the coverage test results. Therefore, this invention solves the problem of being unable to obtain coverage using a code coverage acquisition tool when there is no file system or limited file system resources. It also avoids the complexity and potential errors introduced by file system operations, improving the usability of the code coverage acquisition tool, reducing testing costs, improving code quality, and enhancing software maintainability and reliability.

[0034] Figure 1 A flowchart of a first code coverage testing method provided in an embodiment of the present invention; Figure 2 This is an architecture diagram of a software testing system provided in an embodiment of the present invention.

[0035] like Figure 1 As shown, the code coverage testing method provided in this embodiment of the invention may include: S101: Configure the code coverage acquisition tool for the software under test, and configure the file output address of the code coverage acquisition tool to the address in the first memory storage space; S102: When performing the test on the software under test, monitor the coverage data recorded in the first storage space when the code coverage acquisition tool is running, and move the coverage data from the first storage space to the second storage space on the disk. S103: After the test is completed, read the coverage record data from the second storage space to generate the coverage test results.

[0036] The code coverage testing method provided in this embodiment of the invention can be applied to, for example... Figure 2 The software testing system shown is designed for software testing. During testing, the system can be deployed on a single test host or include both a test host and a host computer. The host computer sends the source files and test cases of the software under test to the test host, which then executes the tests based on the test cases. Simultaneously, the executable file of a code coverage acquisition tool is run to collect coverage data. After the software testing is complete, the host computer collects coverage data while gathering the test results of the software under test, and generates coverage test results based on this data.

[0037] Traditional code coverage acquisition tools rely on a file system to store coverage data. To enable the use of code coverage acquisition tools in scenarios where there is no file system or file system resources are limited, this embodiment of the invention simulates a file system in memory. When the executable file of the code coverage acquisition tool is run, the coverage data is recorded in memory, and then the code coverage acquisition tool in memory is sent to an external device (disk) so that the code coverage acquisition tool can be used on the test host or host computer to generate a coverage test report.

[0038] For S101, before the test, storage space is allocated in the memory of the test host for the coverage record data output by the code coverage acquisition tool, which is referred to as the first storage space in this embodiment of the invention.

[0039] In some optional embodiments of the present invention, the step of allocating the first storage space may include: obtaining the number of source files of the software under test; and allocating a corresponding first sub-storage space for each source file within the first storage space. Code coverage acquisition tools typically generate coverage record data on a per-source-file basis. By allocating a corresponding first sub-storage space for each source file, the storage of coverage record data for all source files is ensured. Each first sub-storage space can be configured for cyclic read / write, allowing the code coverage acquisition tool to write to consecutive addresses in the first sub-storage space in a cyclic manner, while the thread monitoring the first storage space reads from the first sub-storage space in a cyclic manner, achieving efficient utilization of memory storage space. To prevent unread coverage record data from being overwritten, the code coverage acquisition tool is configured to stop writing when the first sub-storage space corresponding to a source file is full.

[0040] In some alternative embodiments of the present invention, the step of allocating the first storage space may further include: constructing the first storage space as a memory pool to allocate memory blocks for the coverage record data to be written. A memory pool can be used to store the coverage record data, based on allocating an independent memory block for each coverage record data file, to reduce the problem of memory fragmentation. A memory pool is created in an accessible memory region. Before actually using memory, the memory pool requests and allocates a certain number of memory blocks of equal (or similar) size for backup. When there is a new memory requirement, a portion of memory blocks is allocated from the memory pool (the size of the memory pool and memory blocks is evaluated according to the actual needs of the test). During the test, memory blocks are requested from the memory pool. If there is a free memory block, the coverage record data of a source file is written into that memory block, and the data size is continuously calculated during the writing process; if not, the process waits for a free block before writing data.

[0041] In S101, configuring a code coverage acquisition tool for the software under test can include: inserting the code or instructions of the code coverage acquisition tool into the source files of the software under test and then compiling them to obtain an executable file containing the code coverage acquisition tool. For example, when using Gcov as the code coverage acquisition tool, during the compilation phase, the source code is compiled using the GCC compiler with the -fprofile-arcs (inserts counters between each basic block of the program to record the execution path during program runtime) and -ftest-coverage (used to generate auxiliary information files required for code coverage analysis). These options generate additional code to track which functions and branches are executed during program runtime and generate gcno files, one for each source file. Running the compiled executable file generates gcda files. These typically also follow the rule of one file corresponding to one source file.

[0042] In S101, configuring a code coverage acquisition tool for the software under test can also include modifying the compiled files based on the code coverage acquisition tool's code after compiling the source files of the software under test. For example, when using JaCoCo as the code coverage acquisition tool, it provides two configuration methods: online instrumentation (dynamically inserting code during class loading) and offline instrumentation (modifying bytecode after compilation).

[0043] For S102, by monitoring the data in the first storage space, the coverage record data in the first storage space is moved to the second storage space on the disk. After the move is completed, the corresponding memory block in the first storage space is released.

[0044] In this embodiment of the invention, the coverage recording data may include coverage-related data generated by the code coverage acquisition tool, or a complete coverage recording data file corresponding to a source file.

[0045] For S103, reading coverage record data from the second storage space to generate coverage test results may include: creating a storage directory on the target device; moving the coverage record data from the second storage space to the storage directory, so that the target device can call the code coverage acquisition tool to generate coverage test results based on the coverage record data and the source files of the software under test. The target device can be a test host or a host computer. That is, the host computer or test host reads binary data from the second storage space of the disk, parses it, and after obtaining the coverage record data, can use the code coverage acquisition tool to combine this coverage record data to generate a coverage test report, realizing the visualization of the coverage test results for testers to view. For example, when the code coverage acquisition tool uses Gcov, Gcov will analyze the data in the gcno and gcda files and generate a gcov file, which contains the number of times each line of code was executed and other coverage information.

[0046] The code coverage testing method provided in this invention configures a code coverage acquisition tool for the software under test, setting the tool's output address to an address in the first memory storage space. This allows the tool to write to memory independently of the file system. During the testing of the software under test, the method monitors the coverage data written to the first storage space by the tool and moves this data to a second storage space on the disk. This ensures that the writing of coverage data continues even with limited file system resources, preventing write interruptions due to insufficient memory. After the test is completed, the coverage data is read from the second storage space to generate the coverage test results. Therefore, this invention solves the problem of being unable to obtain coverage data using a code coverage acquisition tool when there is no file system or limited file system resources. It also avoids the complexity and potential errors introduced by file system operations, improving the usability of the code coverage acquisition tool and helping to reduce testing costs, improve code quality, and enhance software maintainability and reliability.

[0047] Figure 3 A schematic diagram of a first storage information table provided in an embodiment of the present invention; Figure 4 A schematic diagram of a second storage information table provided in an embodiment of the present invention; Figure 5 This is a schematic diagram of a third storage information table provided in an embodiment of the present invention.

[0048] Based on the above embodiments, this embodiment of the invention further introduces the data structures and interfaces that need to be implemented in the testing method to achieve code coverage.

[0049] To maintain the data stored in the first storage space, embodiments of the present invention provide a first storage information table that can be used as follows: Figure 3 As shown, the first storage information table can be in the form of a linked list, where each linked list node is a file memory storage information structure. This file memory storage information structure can include the filename (or unique identifier) ​​of the coverage record data file, the file memory storage address, the current read / write position, and the file size, etc.

[0050] To maintain the data stored in the second storage space, embodiments of the present invention provide a second storage information table that can be used as follows: Figure 4 As shown, the second storage information table can be in the form of a linked list. Each linked list node is a file disk storage information structure. This file disk storage information structure can include the file name (or unique identifier) ​​of the coverage record data file, the starting address of the file disk storage, the file size, the integrity identifier (whether the data is complete), etc.

[0051] To improve the reliability of moving coverage record data from memory to disk, in this embodiment of the invention, the step S102, where the code coverage acquisition tool writes coverage record data to the first storage space and moves the coverage record data from the first storage space to the second storage space on the disk, may include: when it is determined that a coverage record data file exists in the first storage space, cyclically moving the data in the coverage record data file from the first storage space to the third storage space in memory, and then moving the data in the third storage space to the second storage space. Through the data structure of the third storage space provided in this embodiment of the invention, an orderly mechanism for moving coverage record data from memory to disk is achieved, avoiding data loss.

[0052] Determining the existence of coverage record data files in the first storage space can include: after detecting that the coverage record data file corresponding to a source file of the software under test in the first storage space is full, determining that there is a coverage record data file to be transferred in the first storage space. That is, whenever a coverage record data file in the first storage space is full, the coverage record data file is moved. In other embodiments, to avoid the first sub-storage space allocated to the source file being full, it can also be determined that there is a coverage record data file to be transferred in the first storage space when the occupancy of the first sub-storage space reaches a preset proportion.

[0053] To maintain the data in the third storage space, embodiments of the present invention provide a third storage information table that can be used as follows: Figure 5 As shown, the third storage information table can be in the form of a linked list, with each linked list node used to store data information as a moving unit.

[0054] Embodiments of the present invention can implement the following interface to perform code coverage testing methods:

[0055] The interface for writing data to memory adds nodes to the file memory storage information linked list, allocates memory blocks to store coverage record data, and updates the file memory storage information structure.

[0056] The interface for updating the read position from memory updates the current read / write position based on the memory address and offset of the data recorded by the storage coverage.

[0057] Data sending interface to disk: The firmware encodes the data and sends it to the specified address on the disk.

[0058] Disk Read Data Interface: The host reads data from the disk and decodes it.

[0059] Figure 6 A flowchart of a second code coverage testing method provided in an embodiment of the present invention.

[0060] Using the data structures and interfaces described in the above embodiments, the present invention provides a code coverage testing method with the following flow: Figure 6 As shown, the process mainly includes the code coverage acquisition tool writing coverage record data into the first storage space, moving the coverage record data from the first storage space to the second storage space (corresponding to S102), and the host reading and parsing data from the disk (corresponding to S103).

[0061] In the process of the code coverage acquisition tool writing coverage record data to the first storage space, a memory pool is created in the accessible memory area. Before actually using memory, the memory pool first requests and allocates a certain number of memory blocks of equal (or similar) size as reserves, i.e., the first storage space. When there is a new memory requirement, a portion of memory blocks is allocated from the memory pool (the size of the memory pool and memory blocks is evaluated based on the actual needs of the test). When the code coverage acquisition tool needs to write coverage record data, it requests the use of memory blocks from the memory pool. If there are free memory blocks, the coverage record data of a certain source file is written into that memory block, and the data size is continuously calculated during the writing process; if not, it waits for a free block to be available before writing data. After all the coverage record data of a certain source file has been written, a linked list node is created to store the memory storage information of the coverage record data file, and the file memory storage information structure is updated, including: file name, file memory storage address, file size, etc.

[0062] In the process of moving coverage record data from the first storage space to the second storage space, if the first storage information table (such as the one storing file memory storage information) contains such information, then... Figure 3 If the value shown is not empty, then create a linked list node. This node is used to store the file's disk storage information and basic file information. Add this node to the third storage information table (e.g., ...). Figure 4 As shown), traverse the first storage information table (such as... Figure 3 As shown), read the file name and file size, store the information in the file disk storage information structure, initialize the data integrity status to 0, read the memory block address of the file, and read the binary data from the corresponding memory block.

[0063] In this embodiment of the invention, when it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space. This may include: when it is determined that a coverage record data file exists in the first storage space, the file header of the coverage record data file is directly moved from the first storage space to the second storage space; after the file header is moved, the data tags of the coverage record data file in the first storage space are read and parsed in a loop, and the data tags are moved to the third storage space in units of tag data blocks according to the data tags, until the end information of the coverage record data file is read; the tag data blocks that are checked as valid in the third storage space are moved to the second storage space, until the end information of the coverage record data file is read.

[0064] Based on the third storage space, the reliability of moving coverage record data from memory to disk is further improved by moving coverage record data in units of file header and tag data blocks.

[0065] During the migration process, based on the three storage information tables mentioned above, when it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space. This can include: after detecting that the data in the coverage record data file corresponding to a source file of the software under test in the first storage space is full, recording the first storage information of the coverage record data file in the first storage space, the first storage information including the current read / write position; generating the second storage information of the coverage record data file in the second storage space based on the first storage information, the second storage information including the file size; moving the data in the coverage record data file to the third storage space in memory in a loop based on the first storage information, and updating the current read / write position of the coverage record data file until the end information is read; moving the tag data blocks that are checked as valid in the third storage space to the second storage space until the end information is read.

[0066] Based on the three storage information tables and the third storage space described in the above embodiments, the tracking and migration of all coverage record files are ensured, further improving the reliability of migrating coverage record data from memory to disk.

[0067] Specifically, in the process of moving coverage record data from the first storage space to the second storage space, the file header can be read and verified first, then the tags can be read and parsed in a loop. After the loop ends, the corresponding node is deleted from the first storage information table and the corresponding memory block is released from the first storage space.

[0068] Reading and verifying the file header can include:

[0069] (1) Read the “magic” value at the beginning of the binary data file (this is used to verify whether the file type is a data file or a comment file), update the value of the current read / write position of the file in the file memory storage information structure, call the data sending interface, and send the binary data to the disk.

[0070] (2) Read the binary data, which is the version number, and update the current file reading position. Send the binary data to the disk.

[0071] (3) Read the binary data as a timestamp, update the current file reading position (which is the file creation time), and send the binary data to the disk.

[0072] (4) Read the checksum, which is used to verify the integrity of the file content, update the current file reading position, and send the binary data to the disk.

[0073] Looping through and parsing tags can involve reading and parsing tags from the coverage record data file. Read data is temporarily stored in a third storage space; if a value in the third storage space is retrieved, the binary data is read and temporarily stored in the third storage space. Each tag data block contains an identifier (tag), a length, and associated coverage record data.

[0074] Looping through and parsing tags can further include:

[0075] (1) Read the binary data into the third storage space first, update the current reading position of the file, read the tag from the third storage space. The tag is an identifier used to indicate the type of the next data block. Create a linked list node, store the tag value in the linked list node, and add the node to the third storage information table.

[0076] (2) Then continue to read binary data into the third storage space, update the current reading position of the file, read the length of the tag data block from the third storage space, create a linked list node, store the length value in the linked list node, use a variable to record the length value, and add the node to the third storage information table.

[0077] (3) Continue reading binary data length times. During the reading process, the data is first read into the third storage space and then retrieved from the temporary storage space. The current reading position of the file is continuously updated, and linked list nodes are created to store the read data and then added to the third storage information table.

[0078] (4) Continue reading binary data into the third storage space, update the current reading position of the file, read the data in the third storage space, check whether the next binary data is a valid identifier. If it is, it means that the data stored in the current third storage information table is a complete label. Traverse the linked list nodes and send the data to the disk. During the process of traversing the third storage information table and sending, continuously update the data file size in the structure. If it is not, it means that the label is not a valid data format. Do not send data with incorrect format to the disk. Throw an exception message. This coverage record data is incorrect and cannot be parsed correctly. At the same time, set whether the data in the structure is complete to 1.

[0079] The loop can end when a tag with a value of 0 is read, indicating that the file has been read completely.

[0080] Based on the above embodiments, in the embodiments provided by the present invention, reading coverage record data from the second storage space to generate coverage test results may include: creating a storage directory on the target device; determining the starting storage position and file size of the coverage record data file in the second storage space according to the second storage information, configuring a first variable according to the file size; cyclically moving data from the coverage record data file to the storage directory so that the target device generates coverage test results based on the coverage record data file, and updating the first variable according to the remaining data of the coverage record data file in the second storage space until the first variable is 0, and deleting the second storage information.

[0081] Specifically, during the process of the host reading and parsing data from the disk, a directory is created on the host side to store coverage record data.

[0082] On the host side, the coverage record data on the disk is parsed through the second storage information table to ensure that it can be correctly identified and processed, and the following steps are executed repeatedly:

[0083] (1) Traverse the second storage information table, get the structure of the disk storage information of the storage file, first determine whether the data in the structure is complete and whether the element is 1. If it is not 1, read the file name and create a coverage record data file with the name as the file name on the host side; if it is 1, throw an exception message that the coverage of the source file cannot be obtained correctly.

[0084] (2) The host continues to read the second storage information table to obtain the starting position of the file stored on the disk and the file size. The file size is saved in the first variable. The value of the first variable is updated every time data is read. The first variable A = A - the size of the data read.

[0085] (3) The file is read when the size of the data to be read becomes 0.

[0086] (4) Delete the node that stores the disk storage information of the file from the second storage information table.

[0087] The loop ends when the second storage information table is empty, and all coverage record files on the disk have been read.

[0088] If the code coverage tool used is Gcov, the host passes the contents of the `gcno` and `gcda` files (or pointers / file paths to them) to the Gcov tool. Gcov is configured to use these files. It reads the `.gcno` and `.gcda` files, along with the original source code files, then calculates the execution count for each line of code, each basic block, and each branch, generating a source code file (usually with a `.gcov` extension) containing coverage information. This file includes the execution count for each line of code and the percentage of coverage calculated based on that information.

[0089] The host data reading and parsing method provided in this embodiment of the invention ensures that the host can read coverage record data from the disk in an orderly and correct manner through a second storage information table, thereby improving the reliability of generating coverage test results.

[0090] Figure 7 A flowchart of a third code coverage testing method provided in an embodiment of the present invention.

[0091] Based on the above embodiments, this embodiment of the invention uses Gcov as an example to illustrate the process of the code coverage testing method.

[0092] In embodiments of the present invention, such as Figure 7 As shown, the source files and code coverage acquisition tool of the software under test are compiled into an executable file, generating a coverage annotation file (gcno file). Running the executable file continuously generates coverage record data (i.e., coverage data file gcda), which is first written to the first storage space in memory. Then, the coverage record data is moved from the first storage space in memory to the second storage space on disk. After the test, the coverage record data is moved from disk to the host computer. The host computer calls the coverage report generation tool to generate coverage test results (coverage test report) based on the source files, coverage annotation file, and coverage data file. The coverage testing tool can be lcov, ghtml, etc. lcov is a front-end tool based on gcov data, used to collect, filter, and integrate coverage information, and generate more readable reports. Using ghtml, the summary file generated by lcov for coverage data can be converted into a report in Hypertext Markup Language (HTML) format.

[0093] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.

[0094] Figure 8 This is a schematic diagram of a code coverage testing device provided in an embodiment of the present invention.

[0095] like Figure 8 As shown, embodiments of this application also provide a code coverage testing apparatus, including: The first configuration module 801 is used to configure the code coverage acquisition tool for the software under test, and to configure the file output address of the code coverage acquisition tool to an address in the first storage space of memory. The first transfer module 802 is used to monitor the code coverage acquisition tool and write the coverage record data into the first storage space when the test of the software under test is performed, and to transfer the coverage record data from the first storage space to the second storage space on the disk. The second transfer module 803 is used to read coverage record data from the second storage space to generate coverage test results after the test is completed.

[0096] In this embodiment of the invention, the first moving module 802 monitors the coverage record data written to the first storage space when the code coverage acquisition tool is running, and moves the coverage record data from the first storage space to the second storage space on the disk. This may include: when it is determined that there is a coverage record data file in the first storage space, cyclically moving the data in the coverage record data file from the first storage space to the third storage space in memory, and moving the data in the third storage space to the second storage space.

[0097] In this embodiment of the invention, the first transfer module 802 determines that there is a coverage record data file in the first storage space, which may include: after detecting that the data of the coverage record data file corresponding to a source file of the software under test in the first storage space is full, determining that there is a coverage record data file to be transferred in the first storage space.

[0098] In this embodiment of the invention, when the first moving module 802 determines that a coverage record data file exists in the first storage space, it cyclically moves the data in the coverage record data file from the first storage space to the third storage space in memory, and moves the data in the third storage space to the second storage space. This may include: when it is determined that a coverage record data file exists in the first storage space, directly moving the file header of the coverage record data file from the first storage space to the second storage space; after completing the file header movement, cyclically reading and parsing the data tags of the coverage record data file in the first storage space, and moving them to the third storage space in units of tag data blocks according to the data tags, until the end information of the coverage record data file is read; moving the tag data blocks that are checked as valid in the third storage space to the second storage space, until the end information of the coverage record data file is read.

[0099] In this embodiment of the invention, when the first moving module 802 determines that a coverage record data file exists in the first storage space, it cyclically moves the data in the coverage record data file from the first storage space to the third storage space in memory, and moves the data in the third storage space to the second storage space. This may include: after detecting that the data in the coverage record data file corresponding to a source file of the software under test in the first storage space is full, recording the first storage information of the coverage record data file in the first storage space, the first storage information including the current read / write position; generating the second storage information of the coverage record data file in the second storage space based on the first storage information, the second storage information including the file size; cyclically moving the data in the coverage record data file to the third storage space in memory based on the first storage information, and updating the current read / write position of the coverage record data file until the end information is read; moving the tag data blocks that are checked as valid in the third storage space to the second storage space until the end information is read.

[0100] In this embodiment of the invention, the second transfer module 803 reads coverage record data from the second storage space to generate coverage test results, which may include: creating a storage directory on the target device; determining the starting storage position and file size of the coverage record data file in the second storage space according to the second storage information, configuring a first variable according to the file size; cyclically transferring data from the coverage record data file to the storage directory so that the target device generates coverage test results based on the coverage record data file, and updating the first variable according to the remaining data of the coverage record data file in the second storage space until the first variable is 0, and deleting the second storage information.

[0101] In this embodiment of the invention, the second transfer module 803 reads coverage record data from the second storage space to generate coverage test results, and may further include: creating a storage directory on the target device; transferring the coverage record data from the second storage space to the storage directory, so that the target device calls the code coverage acquisition tool to generate coverage test results based on the coverage record data and the source files of the software under test.

[0102] For a description of the features in the embodiment corresponding to the code coverage testing device, please refer to the relevant description of the embodiment corresponding to the code coverage testing method, which will not be repeated here.

[0103] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any of the code coverage testing method embodiments described above.

[0104] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the code coverage testing method embodiments described above when run.

[0105] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.

[0106] This invention provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the code coverage testing method provided in any of the above embodiments.

[0107] For a description of the computer program product provided in the embodiments of the present invention, please refer to the above method embodiments. The effects it achieves are the same as the code coverage testing method provided in the embodiments of the present invention, and the present invention will not repeat them here.

[0108] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0109] The foregoing has provided a detailed description of a code coverage testing method, apparatus, device, and storage medium provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are merely for the purpose of helping to understand the method and its core ideas. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from its principles, and these improvements and modifications also fall within the protection scope of this application.

Claims

1. A method for testing code coverage, characterized in that, include: Configure a code coverage acquisition tool for the software under test, and configure the file output address of the code coverage acquisition tool to an address in the first storage space of memory; When performing the test on the software under test, the coverage record data written to the first storage space by the code coverage acquisition tool is monitored, and the coverage record data is moved from the first storage space to the second storage space on the disk. After the test is completed, the coverage record data is read from the second storage space to generate the coverage test results.

2. The code coverage testing method according to claim 1, characterized in that, Monitoring the coverage record data written to the first storage space during the runtime of the code coverage acquisition tool, and moving the coverage record data from the first storage space to a second storage space on the disk, includes: When it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in the memory in a loop, and the data in the third storage space is moved to the second storage space.

3. The code coverage testing method according to claim 2, characterized in that, Determining that the first storage space contains a coverage record data file includes: After detecting that the coverage record data file corresponding to a source file of the software under test in the first storage space is full, it is determined that there is a coverage record data file to be transmitted in the first storage space.

4. The code coverage testing method according to claim 2, characterized in that, When it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space, including: When it is determined that the coverage record data file exists in the first storage space, the file header of the coverage record data file is directly moved from the first storage space to the second storage space; After the file header is moved, the data tags of the coverage record data file in the first storage space are read and parsed in a loop, and the data tags are moved to the third storage space in units of tag data blocks according to the data tags, until the end information of the coverage record data file is read. The tag data blocks that are found to be valid in the third storage space are moved to the second storage space until the end information of the coverage record data file is read.

5. The code coverage testing method according to claim 2, characterized in that, When it is determined that a coverage record data file exists in the first storage space, the data in the coverage record data file is moved from the first storage space to the third storage space in memory in a loop, and the data in the third storage space is moved to the second storage space, including: After detecting that the coverage record data file corresponding to a source file of the software under test in the first storage space is full, the first storage information of the coverage record data file in the first storage space is recorded, and the first storage information includes the current read / write position. Based on the first storage information, second storage information of the coverage record data file in the second storage space is generated, wherein the second storage information includes the file size; Based on the first storage information, the data in the coverage record data file is moved to the third storage space in the memory in a loop, and the current read / write position of the coverage record data file is updated until the end information is read; The valid tag data blocks in the third storage space are moved to the second storage space until the end information is read.

6. The code coverage testing method according to claim 5, characterized in that, Reading the coverage record data from the second storage space to generate coverage test results includes: Create a storage directory on the target device; The storage start position and file size of the coverage record data file in the second storage space are determined based on the second storage information, and the first variable is configured based on the file size; The data in the coverage record data file is moved to the storage directory in a loop so that the target device generates the coverage test result based on the coverage record data file. The first variable is updated based on the remaining data of the coverage record data file in the second storage space until the first variable is 0, and then the second storage information is deleted.

7. The code coverage testing method according to claim 1, characterized in that, Reading the coverage record data from the second storage space to generate coverage test results includes: Create a storage directory on the target device; The coverage record data is moved from the second storage space to the storage directory, so that the target device can call the code coverage acquisition tool to generate the coverage test results based on the coverage record data and the source files of the software under test.

8. A code coverage testing device, characterized in that, include: The first configuration module is used to configure a code coverage acquisition tool for the software under test, and to configure the file output address of the code coverage acquisition tool to an address in the first storage space of memory. The first migration module is used to monitor the coverage record data written to the first storage space by the code coverage acquisition tool when the test of the software under test is performed, and to migrate the coverage record data from the first storage space to the second storage space on the disk. The second transfer module is used to read the coverage record data from the second storage space to generate coverage test results after the test is completed.

9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for implementing the code coverage testing method as described in any one of claims 1 to 7 when executing the computer program.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the code coverage testing method as described in any one of claims 1 to 7.