Code coverage test method, device, equipment, medium and program product
By inserting probes into application class files, code coverage data is monitored in real time and automatically reported, solving the problems of lag and inaccurate version correlation in existing coverage testing technologies, and realizing dynamic and continuous coverage statistics and efficient data analysis.
Patent Information
- Application Number
- CN202611573398.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-12
- Publication Date
- 2026-11-27
AI Technical Summary
Existing code coverage testing technologies suffer from problems such as delayed data output, inaccurate version correlation, high resource consumption, low analysis efficiency, and deep coupling of the data collection process with the CI pipeline, making it impossible to achieve dynamic and continuous coverage statistics and result output.
By inserting probes into the class files of the target application, code coverage is monitored in real time. Combined with preset test conditions and time intervals, data is automatically reported to achieve dynamic coverage statistics. Furthermore, code versions are automatically associated through version metadata, supporting coverage data collection and analysis throughout the entire application lifecycle.
It enables real-time monitoring and continuous reporting of code coverage data during application operation, supports real-time analysis in high-frequency version deployment scenarios, avoids data loss, and improves the accuracy and efficiency of coverage data analysis.
Smart Images

Figure CN123178282A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of big data, specifically to the field of data acquisition, and more specifically to a code coverage testing method, apparatus, device, medium, and program product. Background Technology
[0002] Code coverage testing is a core testing method in the software quality assurance system. It is used to quantify the degree of coverage of the tested code during the test execution process, and to provide data support for test integrity assessment, code quality optimization and version iteration.
[0003] In related technologies, code coverage testing typically adopts an implementation model of static instrumentation during the build phase and batch statistical generation of reports after the test process is completed. The collection and generation of coverage data are highly dependent on the execution process of the software CI (Continuous Integration) pipeline.
[0004] In this type of implementation, coverage data can only be generated in batches after the testing process is completed, making it difficult to achieve dynamic and continuous coverage statistics and result output throughout the entire application lifecycle. Summary of the Invention
[0005] In view of the above problems, embodiments of this application provide a code coverage testing method, apparatus, device, medium, and program product.
[0006] According to a first aspect of this application, a code coverage testing method is provided, comprising: in response to determining that a class file loaded during the execution of a target application is a first target class file, obtaining at least one initial bytecode instruction sequence in the first target class file using a bytecode conversion module, wherein the first target class file is the first class file loaded among a plurality of class files to be loaded in the target application; inserting a probe into the at least one initial bytecode instruction sequence of the first target class file to obtain a second target class file; executing the second target class file to determine code coverage data of the first target class file through the probe; and in response to determining that the target application meets preset test conditions, determining a test result for the target application based on the code coverage data, wherein the preset test conditions include: abnormal shutdown of the target application.
[0007] According to an embodiment of this application, at least one initial bytecode instruction sequence includes multiple bytecode instructions, and the second target class file includes multiple bytecode instructions and multiple probes respectively targeting multiple instructions to be tested, wherein the instructions to be tested are the bytecode instructions into which probes are inserted; determining the code coverage data of the first target class file through probes includes: in response to the execution of the instructions to be tested in the second target class file, marking the probes targeting the instructions to be tested as executed, and obtaining the marking results of the instructions to be tested; determining the code coverage data based on the number of marking results and the number of bytecode instructions in the second target class file.
[0008] According to an embodiment of this application, a plurality of bytecode instructions include jump instructions for logical branches, and inserting a probe in at least one initial bytecode instruction sequence of a first target class file includes: inserting a probe for a first instruction to be tested among a plurality of instructions to be tested, the first instruction to be tested including a jump instruction.
[0009] According to embodiments of this application, the method further includes: reporting code coverage data to the target server at preset time intervals.
[0010] According to embodiments of this application, the method further includes: determining the code version identifier of the test code to which the first target class file belongs; and associating the code version identifier with code coverage data.
[0011] According to an embodiment of this application, determining the code version identifier of the test code to which the first target class file belongs includes: scanning the application package containing the test code to obtain the version file; parsing the version metadata of the test code from the version file; and determining the code version identifier based on the version metadata.
[0012] According to an embodiment of this application, the method further includes: generating a version file containing version metadata of the test code; and encapsulating the version file and all class files of the test code together into an application package.
[0013] According to an embodiment of this application, it further includes: when the target application receives a shutdown command, reporting code coverage data to the target server.
[0014] According to an embodiment of this application, the method further includes: retrying to report code coverage data when code coverage data cannot be reported to the target server; recording the number of times code coverage data is retried to obtain the retry count; storing code coverage data in a local cache directory when the retry count reaches a preset threshold to obtain cached data; and reporting cached data to the target server when code coverage data can be reported to the target server normally.
[0015] According to a second aspect of this application, a code coverage testing apparatus is provided, comprising: an instruction sequence determination module, configured to, in response to determining that a class file loaded during the execution of a target application is a first target class file, obtain at least one initial bytecode instruction sequence in the first target class file using a bytecode conversion module, wherein the first target class file is the first class file loaded among a plurality of class files to be loaded in the target application; a probe insertion module, configured to insert a probe into at least one initial bytecode instruction sequence of the first target class file to obtain a second target class file; a code coverage data determination module, configured to execute the second target class file to determine the code coverage data of the first target class file through the probe; and a test result determination module, configured to, in response to determining that the target application meets preset test conditions, determine a test result for the target application based on the code coverage data, wherein the preset test conditions include: abnormal offline of the target application.
[0016] According to a third aspect of this application, an electronic device is provided, comprising: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the method described above.
[0017] According to a fourth aspect of this application, a computer-readable storage medium is also provided, on which a computer program or instructions are stored, wherein the computer program or instructions, when executed by a processor, implement the steps of the above-described method.
[0018] According to a fifth aspect of this application, a computer program product is also provided, comprising a computer program or instructions that, when executed by a processor, implement the steps of the above-described method. Attached Figure Description
[0019] The above-mentioned contents, other objects, features and advantages of this application will become clearer from the following description of embodiments with reference to the accompanying drawings, in which:
[0020] Figure 1 The illustration schematically depicts an application scenario of a code coverage testing method, apparatus, device, medium, and program product according to embodiments of this application.
[0021] Figure 2 A flowchart illustrating a code coverage testing method according to an embodiment of this application is shown schematically.
[0022] Figure 3 A flowchart illustrating the determination of code coverage data according to an embodiment of this application is shown schematically.
[0023] Figure 4 A flowchart illustrating probe insertion according to an embodiment of this application is shown schematically.
[0024] Figure 5 A flowchart illustrating code coverage data reporting according to an embodiment of this application is shown.
[0025] Figure 6 A flowchart illustrating code version identifier associated data according to an embodiment of this application is shown.
[0026] Figure 7 A flowchart illustrating the determination of a code version identifier according to an embodiment of this application is shown schematically.
[0027] Figure 8 A flowchart illustrating version metadata encapsulation according to an embodiment of this application is shown.
[0028] Figure 9 A flowchart illustrating the data processing during graceful shutdown according to an embodiment of this application is shown.
[0029] Figure 10 A flowchart illustrating data processing when the target server is unavailable according to an embodiment of this application is shown.
[0030] Figure 11 The diagram illustrates the structure of a code coverage testing apparatus according to an embodiment of this application.
[0031] Figure 12 A block diagram schematically illustrates an electronic device suitable for implementing a code coverage testing method according to an embodiment of this application. Detailed Implementation
[0032] The embodiments of this application will now be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of this application. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of this application for ease of explanation. However, it will be apparent that one or more embodiments may be implemented without these specific details. Furthermore, descriptions of well-known structures and technologies are omitted in the following description to avoid unnecessarily obscuring the concepts of this application.
[0033] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of this application. The terms “comprising,” “including,” etc., as used herein indicate the presence of the stated features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0034] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0035] When using expressions such as "at least one of A, B and C", they should generally be interpreted in accordance with the meaning that is commonly understood by those skilled in the art (e.g., "a system having at least one of A, B and C" should include, but is not limited to, a system having A alone, a system having B alone, a system having C alone, a system having A and B, a system having A and C, a system having B and C, and / or a system having A, B and C, etc.).
[0036] It should be noted that in the embodiments of this application, certain software, components, models and other existing solutions in the industry may be mentioned. These should be regarded as exemplary and are only intended to illustrate the feasibility of implementing the technical solution of this application. However, it does not mean that the applicant has used or necessarily used the solution.
[0037] In agile development and integrated development and operations (IDE) processes, CI (Continuous Integration) and CD (Continuous Deployment) have become the mainstream models for application delivery. With frequent iterations of application versions in the testing environment, quickly obtaining and evaluating the code coverage of each test version is of great significance for measuring test adequacy, identifying uncovered code, and improving code delivery quality.
[0038] In related technologies, code coverage collection typically employs static instrumentation during the build phase and offline analysis after testing. A typical solution is based on integrating JaCoCo (Java Code Coverage) into a CI pipeline, and its specific process is as follows:
[0039] 1. Build Phase: Through the continuous integration of automated build tools, the build process of Java project source code is compiled with the help of Java project build tools. During the compilation process, coverage statistics probes are inserted into the bytecode files to generate instrumented bytecode files and package them into application packages, including formats such as JAR (Java Archive) and WAR (Web Application Archive).
[0040] 2. Test Execution Phase: Deploy the application package to the test environment and run automated test suites such as Java unit testing frameworks; JaCoCo Agent (code coverage agent) runs in the JVM (Java Virtual Machine) and collects coverage statistics, writing them to a local binary data file.
[0041] 3. Data Pull and Analysis Phase: The local data files are pulled from the test environment server to the server via the CI pipeline script, centrally stored, and then the coverage analysis task is triggered to generate a coverage report.
[0042] The overall process of this type of solution is a linear batch processing mode. The coverage analysis process and the application operation process are independent of each other, which belongs to the offline analysis mode.
[0043] The relevant technical solutions have the following shortcomings in practical applications:
[0044] 1. Data output is delayed, making real-time statistics during operation impossible. Coverage data collection, reporting, and analysis rely entirely on the completion of CI pipeline test tasks. Offline coverage reports are only generated at fixed points after test task completion, making real-time monitoring during application operation impossible. Furthermore, it cannot perform immediate coverage assessments for newly deployed versions in the test environment, failing to meet the real-time analysis requirements of high-frequency deployments in the test environment. Moreover, coverage data is only output uniformly after the test process ends. If the application goes offline abnormally during operation, coverage data that hasn't reached the output node will be lost, compromising the integrity of test data.
[0045] 2. Coverage data lacks precise version correlation, making data misalignment prone to occur. In related technologies, the correlation between coverage data and code version relies on external configuration, making it impossible to accurately bind coverage data to the currently running code version in the test environment. In scenarios involving parallel deployment of multiple versions and testing in multiple environments, misalignment between coverage data and code version can easily occur, leading to distorted analysis results.
[0046] 3. Repeated full-scale analysis results in high resource consumption and low feedback efficiency. Each coverage analysis is performed based on the entire codebase. Even if there are only minor code changes between two production deployments, the coverage of the entire codebase needs to be re-analyzed, resulting in high computational resource consumption, low analysis efficiency, and inability to quickly output analysis results.
[0047] 4. The data acquisition process is deeply coupled with the CI pipeline and cannot run independently. The entire coverage acquisition and analysis process is deeply bound to the CI pipeline and cannot run independently of it. If the test is performed outside the pipeline, coverage data cannot be effectively acquired and output, which can easily lead to data omissions.
[0048] Figure 1 The illustration schematically depicts an application scenario of a code coverage testing method, apparatus, device, medium, and program product according to embodiments of this application. For example... Figure 1 As shown, application scenario 100 according to an embodiment of this application may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 serves as a medium for providing a communication link between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables. For example, a user can use the first terminal device 101, the second terminal device 102, and the third terminal device 103 to interact with the server 105 through the network 104 to receive or send information, etc.
[0049] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be electronic devices such as smartphones, wearable devices, personal computers, intelligent voice interaction devices, smart home appliances, intelligent vehicles, in-vehicle terminals, aircraft, unmanned vending terminals, and extended reality devices. Extended reality devices can include virtual reality devices, augmented reality devices, and mixed reality devices. A client application for the target application can be installed and run on the terminal devices. This target application can include, but is not limited to, financial transaction applications, payment applications, shopping applications, web browser applications, search applications, instant messaging tools, email clients, and social media platform software (these are just examples). Furthermore, this application embodiment does not limit the form of the target application, and it can include, but is not limited to, applications, mini-programs, etc., installed on the terminal devices, and can also be in the form of web pages.
[0050] Server 105 can be a server providing various services, such as a backend management server supporting websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (this is just an example). The backend management server can analyze and process received user requests and other data, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal devices. The server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services such as cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks, and basic cloud computing services such as big data. The server can be the backend server of the aforementioned target application, used to provide backend services to the clients of the target application.
[0051] It should be noted that the code coverage testing method provided in this application embodiment can generally be executed by server 105 and / or terminal devices 101-103. Accordingly, the code coverage testing device provided in this application embodiment can generally be set in server 105 and / or terminal devices 101-103.
[0052] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0053] Figure 2 A flowchart illustrating a code coverage testing method according to an embodiment of this application is shown schematically.
[0054] like Figure 2 As shown, the code coverage testing method 200 may include operations S210 to S240, and the code coverage testing method can be executed by a code coverage testing device.
[0055] Operation S210: In response to determining that the class file loaded during the execution of the target application is the first target class file, the bytecode conversion module is used to obtain at least one initial bytecode instruction sequence in the first target class file. The first target class file is the class file that is loaded for the first time among the multiple class files to be loaded in the target application.
[0056] Operation S220 inserts a probe into at least one initial bytecode instruction sequence of the first target class file to obtain the second target class file;
[0057] Operation S230 executes the second target class file to determine the code coverage data of the first target class file through the probe;
[0058] Operation S240, in response to determining that the target application meets preset test conditions, determines the test results for the target application based on code coverage data. The preset test conditions include: abnormal shutdown of the target application.
[0059] This application discloses a code coverage testing method. By inserting probes into at least one initial bytecode instruction sequence of the first target class file during the execution of the target application, it can break away from the process limitations of static instrumentation during the build phase in related technologies and complete the collection and statistics of coverage data without relying on continuous integration pipelines. This method completes probe implantation during the class loading phase and uses probes to count the code coverage of the first target class file during the execution of the second target class file. It can continuously generate coverage data throughout the entire application runtime without waiting for the entire test process to end. At the same time, by pre-setting test trigger conditions including abnormal offline, it can output test results in a timely manner when the target application goes offline abnormally.
[0060] In the embodiments of this disclosure, the abnormal shutdown of the target application included in the preset test conditions refers to an unexpected process termination scenario that does not go through the normal application shutdown process, including but not limited to: the target application triggering an uncaught fatal runtime error during operation, causing the process to exit abnormally, the application process being forcibly terminated by the operating system due to exhaustion of system resources, the application interruption caused by the failure and restart of the server or container instance where the application is located, and the application process being forcibly terminated by external signals.
[0061] In the embodiments disclosed herein, this application can be applied to a variety of programming languages and execution environments that support dynamic runtime code conversion, for collecting runtime coverage of the executable code of the target application, and is not limited to a specific programming language or runtime platform.
[0062] In the embodiments of this disclosure, taking a Java environment as an example, the bytecode conversion module can be a bytecode converter that implements the class file converter interface. When the target application is running, the class file converter can be registered with the class loading system of the Java Virtual Machine (JVM) through a pre-built agent program to complete the preparatory work for subsequent bytecode enhancement processing. This registration process can be completed during the startup phase of the target application without intruding on the original business logic of the target application.
[0063] In the embodiments of this disclosure, when the Java Virtual Machine loads a class file during runtime, the registered class file converter is automatically triggered. The class file converter can intercept the original .class bytecode of the class file to be loaded, parse the initial bytecode instruction sequence, and insert probes at the corresponding executable code locations to be analyzed. After the probe insertion is completed, a modified second target class file is obtained, and the modified bytecode stream is then handed over to the Java Virtual Machine to complete the subsequent loading and execution process. The above instrumentation process is only executed once when the class file is loaded for the first time (i.e., when the class file is the first target class file). Subsequent calls or loading of the class file will not repeat the bytecode modification, minimizing the impact on the performance of the target application. Moreover, the entire instrumentation and analysis process is completely transparent to application developers and existing business logic.
[0064] Figure 3 A flowchart illustrating the determination of code coverage data according to an embodiment of this application is shown.
[0065] like Figure 3 As shown, in operation S230, the code coverage data of the first target class file determined by the probe includes:
[0066] Operation S231, in response to the execution of the instruction to be tested in the second target class file, marks the probe for the instruction to be tested as executed, and obtains the marking result of the instruction to be tested;
[0067] Operation S232 determines the code coverage data based on the number of results from the marking results and the number of bytecode instructions in the second target class file.
[0068] In embodiments of this disclosure, at least one initial bytecode instruction sequence includes multiple bytecode instructions, and the second target class file includes multiple bytecode instructions and multiple probes respectively targeting multiple instructions to be tested, wherein the instructions to be tested are the bytecode instructions into which the probes are inserted.
[0069] In the embodiments of this disclosure, during the execution of the second target class file, when a test instruction is actually executed, the probe corresponding to that test instruction can be triggered, and the probe's state is marked as executed, forming a marking result corresponding to the test instruction. When all test instructions have been executed or the test result output conditions are met, the number of all marked results is counted, and combined with the total number of bytecode instructions in the second target class file, the code coverage data of the first target class file is calculated. The probe can be stored in an array structure in the form of Boolean flags. When the test instruction is executed, the flag at the corresponding position is set to executed, thereby completing the recording of the execution state with extremely low runtime overhead.
[0070] In the embodiments of this disclosure, in actual instrumentation scenarios, the instruction to be tested can be either a bytecode instruction corresponding to each line of executable code to support line-level coverage statistics, or a jump instruction corresponding to a logical branch to support branch-level coverage statistics.
[0071] Figure 4 A flowchart illustrating probe insertion according to an embodiment of this application is shown schematically.
[0072] like Figure 4 As shown, operation S220, inserting a probe into at least one initial bytecode instruction sequence of the first target class file, includes:
[0073] Operation S221 inserts a probe for the first instruction to be tested among a plurality of instructions to be tested, the first instruction to be tested including a jump instruction.
[0074] In embodiments of this disclosure, multiple bytecode instructions include jump instructions for logical branches.
[0075] In the embodiments of this disclosure, by implanting probes at the branch jump instructions, the actual execution status of each logical branch during program operation can be accurately counted, providing data support for branch dimension coverage statistics and improving the coverage statistics' ability to cover complex branch logic.
[0076] In the embodiments of this disclosure, the jump-type bytecode instructions of the corresponding logical branches are taken as the key test instructions that must be probed, which can completely cover the execution status of the branch jump path during program execution. By marking the jump instructions with probes, the actual execution status of different logical branches can be independently identified, avoiding the omission of unexecuted branches when only counting code line coverage, and improving the ability of coverage statistics to identify complex program branch logic.
[0077] Figure 5 A flowchart illustrating code coverage data reporting according to an embodiment of this application is shown.
[0078] like Figure 5 As shown, method 200 may further include:
[0079] Operate S250 to report code coverage data to the target server at preset time intervals.
[0080] In the embodiments of this disclosure, during the initialization phase of the target application startup, the proxy component can read various collection and reporting configurations passed through command-line parameters or independent configuration files, and complete the determination of reporting and collection rules at program startup: including the access address of the target server and server authentication credentials. Based on the above parameters, the target server to be reported can be determined during the startup phase, and the initialization and authentication preparation of the reporting channel can be completed in advance. At the same time, it can read the application package matching rules that need to be included in the coverage analysis scope, clarify the boundary of the target application package that needs to be instrumented and statistically analyzed, filter irrelevant class files that do not need to be collected, and reduce unnecessary instrumentation performance overhead. It can also read the custom data reporting interval parameter as the preset time interval in this application, and the user can flexibly adjust the reporting frequency according to the real-time requirements of the test scenario.
[0081] In the embodiments of this disclosure, the collected code coverage data is continuously reported to the target server at preset time intervals during the execution of the target application. This mechanism eliminates the need to wait for the target application to reach a fixed test node or for the entire test process to be completed before exporting coverage data in a centralized manner. It enables continuous synchronization of coverage data during application execution, reducing the pressure of a single centralized data transmission after the test. It also allows testers to view the coverage collection progress in real time during the test, promptly identify coverage gaps during the test process, and adapt to the testing rhythm of high-frequency version deployment and rapid verification in the test environment.
[0082] Figure 6 A flowchart illustrating code version identifier associated data according to an embodiment of this application is shown.
[0083] like Figure 6 As shown, method 200 may further include:
[0084] Operation S260 determines the code version identifier of the test code to which the first target class file belongs;
[0085] Operation S270 associates the code version identifier with code coverage data.
[0086] In the embodiments of this disclosure, after collecting code coverage data, the code version identifier corresponding to the test code of the first target class file can be determined, and this code version identifier can be associated with the collected code coverage data. During the association process, a pre-agreed project identifier can be used to achieve accurate data attribution: the project identifier adopts a unified rule convention, and the project name corresponding to the code under test can be taken as the identifier value, used to distinguish different code projects in a multi-project parallel collection scenario.
[0087] After completing the tag binding, the coverage data carrying the project identifier and code version identifier can be synchronized to the target server. The target server is pre-configured with the initialization information corresponding to each project identifier. After receiving the data, it can locate the corresponding code project and the precise version currently deployed based on the project identifier and code version information. This achieves logical isolation and aggregation statistics of coverage data under different projects and different versions, avoiding misalignment and confusion of coverage data from different sources in multi-version parallel testing and multi-environment deployment scenarios.
[0088] Figure 7 A flowchart illustrating the determination of a code version identifier according to an embodiment of this application is shown schematically.
[0089] like Figure 7 As shown, operation S260 determines the code version identifier of the test code to which the first target class file belongs, including:
[0090] Operation S261 scans application packages containing test code to obtain version files;
[0091] Operation S262 parses the version metadata of the test code from the version file;
[0092] Operation S263 determines the code version identifier based on version metadata.
[0093] In the embodiments of this disclosure, to automatically obtain the code version identifier of the test code to which the first target class file belongs, the application package loading the test code is automatically scanned during the startup initialization phase to locate the version file used to store version information within the package. The scanning process can recursively scan the nested archive structure within the application package, and is compatible with various mainstream application package formats such as Jar, War, and Ear, as well as complex project packaging structures with multi-level dependency nesting.
[0094] Once the version file is scanned and identified, the test code version metadata stored in the version file will be automatically parsed. Typical metadata includes code commit hash value, code branch name, code commit timestamp and other information that uniquely corresponds to the code version. Based on the parsed version metadata, the code version identifier corresponding to the test code carried by the current application package can be directly determined. The entire version identification process does not rely on external configuration or manual input and can be completed automatically during the application startup phase.
[0095] Figure 8 A flowchart illustrating version metadata encapsulation according to an embodiment of this application is shown schematically.
[0096] like Figure 8 As shown, method 200 may further include:
[0097] Operate S280 to generate a version file, which contains version metadata of the test code;
[0098] Operation S290 encapsulates the version file and all class files of the test code into the application package.
[0099] In the embodiments of this disclosure, during the project building phase of the test code, a version file containing test code version metadata can be automatically generated by a build tool plugin: for example, a commit information generation plugin adapted to the Java project build tool ecosystem can automatically extract relevant metadata of the current version from the code repository and automatically write it into an independent version file, eliminating the need for manual editing and maintenance of version information and avoiding errors or omissions in version information caused by manual entry.
[0100] Once the version file is generated, it will be packaged together with all class files generated from the compiled test code into the final application package according to the standard directory structure of the application package. This allows the application package to contain metadata corresponding to the code version. This method of automatically generating and packaging during the build phase eliminates the need for additional version information configuration after application deployment. The application package itself carries the corresponding version identifier, providing a preliminary data foundation for automatic code version identification at runtime. This ensures consistency between version metadata and the code within the application package from the source, avoiding coverage data misalignment caused by mismatches between version information and code entities.
[0101] Figure 9 A flowchart illustrating the data processing during graceful shutdown according to an embodiment of this application is shown.
[0102] like Figure 9 As shown, method 200 may further include:
[0103] Operation S2100: When the target application receives a shutdown command, it reports the code coverage data to the target server.
[0104] In the embodiments of this disclosure, during the normal operation of the target application, the regular timed reporting logic is executed according to the configured reporting interval: the reporting interval duration can be flexibly adjusted according to the test scenario. In a regular test scenario, it can be set to 480 minutes by default. After a single round of coverage data is successfully reported, there is no need to persist the data to local disk storage, thus avoiding unnecessary local resource occupation.
[0105] In the embodiments of this disclosure, when the target application receives a normal shutdown command and enters the graceful shutdown process, it will not wait for the next preset reporting interval to arrive, but will immediately report all the currently collected code coverage data to the target server, so as to avoid the loss of the coverage data collected in the reporting interval period due to the application stopping running.
[0106] In the embodiments disclosed herein, it should be noted that the graceful shutdown reporting logic described above is only triggered when the application receives a normal shutdown command. If the application process is forcibly terminated (e.g., directly terminated via a command to force termination), the graceful shutdown process cannot be triggered, and in such abnormal scenarios, the data collected within the last reporting interval will be lost. In actual implementation, the default reporting interval can be set to 480 minutes based on experience from test scenarios to achieve a balance between reporting frequency, server load, and the risk of data loss.
[0107] Figure 10 The flowchart illustrating the data processing when the target server goes offline abnormally according to an embodiment of this application is shown.
[0108] like Figure 10 As shown, method 200 may further include:
[0109] Operation S2110: When code coverage data cannot be reported to the target server, retry reporting code coverage data.
[0110] Operation S2120 records the number of times code coverage data is retried to be reported, thus obtaining the number of retries;
[0111] Operation S2130: When the number of retries reaches a preset threshold, the code coverage data is stored in the local cache directory to obtain cached data.
[0112] Operation S2140: When code coverage data can be reported to the target server normally, report the cached data to the target server.
[0113] In the embodiments of this disclosure, if an abnormality occurs on the target server during the coverage data reporting process (such as the server process being forcibly terminated or the reporting link being interrupted due to a crash) and code coverage data cannot be reported normally, a data retry reporting mechanism will be automatically triggered.
[0114] In the embodiments of this disclosure, the retry process can adopt an exponential backoff strategy to gradually lengthen the retry interval. The longest interval for a single retry can be set to 5000ms. The retry action is executed according to a preset finite number of times to avoid infinite retries occupying local system resources.
[0115] In the embodiments of this disclosure, when the cumulative number of retries reaches a preset threshold, it is determined that the current target server is temporarily unreachable, and the code coverage data to be reported is temporarily stored in a local cache directory. The relative path of the cache directory supports custom configuration. When using the default naming rules, an independent directory can be generated according to the structure of "collection component-process identifier-random identifier / coverage data" to avoid confusion between the temporary storage data of different collection processes. The temporary storage content includes at least two parts: one part is the collected code coverage data file, which can be structurally named according to the collection timestamp; the other part records the code version information of the data to be reported. In addition, it can also include the target server address to be reported, authentication information, etc., for verifying the consistency of the configuration during subsequent supplementary reporting.
[0116] In the embodiments of this disclosure, when the reporting link is restored and the collection process can connect to the target server normally (including the scenario of restarting the collection component), all temporary files in the local cache directory will be automatically traversed to verify whether the reporting metadata in the files is consistent with the configuration of the current collection process. After the verification is passed, the temporary historical coverage data will be supplemented to the target server in the collection order. After the corresponding data is successfully reported, the corresponding temporary file can be deleted locally to complete the reliable delivery of data.
[0117] The entire process, without interrupting the operation of the target application, addresses abnormal scenarios such as temporary server unavailability and network interruption through mechanisms such as automatic retry, local temporary storage, and re-reporting after link recovery. This minimizes the loss of coverage data and improves the fault tolerance of the reporting link and the reliability of data collection.
[0118] Based on the above code coverage testing method, embodiments of this application also provide a code coverage testing apparatus. The following will be combined with... Figure 11 The device is described in detail.
[0119] Figure 11 The diagram illustrates the structure of a code coverage testing apparatus according to an embodiment of this application.
[0120] like Figure 11 As shown, the code coverage testing device 300 of this embodiment includes an instruction sequence determination module 310, a probe insertion module 320, a code coverage data determination module 330, and a test result determination module 340.
[0121] The instruction sequence determination module 310 is used to determine, in response to determining that a class file loaded during the execution of the target application is a first target class file, to obtain at least one initial bytecode instruction sequence in the first target class file using the bytecode conversion module. The first target class file is the class file that is loaded for the first time among a plurality of class files to be loaded in the target application. In one embodiment, the instruction sequence determination module 310 can be used to execute step S210 described above, which will not be repeated here.
[0122] The probe insertion module 320 is used to insert a probe into at least one initial bytecode instruction sequence of the first target class file to obtain a second target class file. In one embodiment, the probe insertion module 320 can be used to perform step S220 described above, which will not be repeated here.
[0123] The code coverage data determination module 330 is used to execute the second target class file to determine the code coverage data of the first target class file through a probe. In one embodiment, the code coverage data determination module 330 can be used to execute step S230 described above, which will not be repeated here.
[0124] The test result determination module 340 is used to determine the test result for the target application based on code coverage data in response to determining that the target application meets preset test conditions. The preset test conditions include: abnormal offline occurrence of the target application. In one embodiment, the test result determination module 340 can be used to execute step S240 described above, which will not be repeated here.
[0125] According to embodiments of this disclosure, the code coverage data determination module 330 further includes a probe marking unit and a coverage calculation unit.
[0126] The probe marking unit is used to mark the probes targeting the instruction to be tested as executed in response to the execution of the instruction to be tested in the second target class file, thereby obtaining the marking result of the instruction to be tested. In one embodiment, the probe marking unit can be used to perform step S231 described above, which will not be repeated here.
[0127] The coverage calculation unit is used to determine code coverage data based on the number of marking results and the number of bytecode instructions in the second target class file. In one embodiment, the coverage calculation unit can be used to perform step S232 described above, which will not be repeated here.
[0128] According to embodiments of the present disclosure, the probe insertion module 320 further includes a branch probe insertion unit.
[0129] A branch probe insertion unit is used to insert a probe targeting a first instruction to be tested among a plurality of instructions to be tested, the first instruction to be tested including a jump instruction. In one embodiment, the branch probe insertion unit can be used to perform step S221 described above, which will not be repeated here.
[0130] According to embodiments of this disclosure, the code coverage testing apparatus 300 further includes a timed reporting module.
[0131] The timed reporting module is used to report the collected code coverage data to the target server at preset time intervals. In one embodiment, the timed reporting module can be used to execute step S250 described above, which will not be repeated here.
[0132] According to embodiments of this disclosure, the code coverage testing apparatus 300 further includes a version identifier determination module and a version association module.
[0133] The version identifier determination module is used to determine the code version identifier of the test code to which the first target class file belongs. In one embodiment, the version identifier determination module can be used to perform the operation S260 described above, which will not be repeated here.
[0134] The version association module is used to associate code version identifiers with the collected code coverage data. In one embodiment, the version association module can be used to perform the operation S270 described above, which will not be repeated here.
[0135] According to embodiments of this disclosure, the version identifier determination module further includes an application package scanning unit, a metadata parsing unit, and a version generation unit.
[0136] The application package scanning unit is used to scan application packages containing test code to obtain version files. In one embodiment, the application package scanning unit can be used to perform the operation S261 described above, which will not be repeated here.
[0137] The metadata parsing unit is used to parse the version metadata of the test code from the version file. In one embodiment, the metadata parsing unit can be used to perform the operation S262 described above, which will not be repeated here.
[0138] The version generation unit is used to determine the code version identifier based on the parsed version metadata. In one embodiment, the version generation unit can be used to perform the operation S263 described above, which will not be repeated here.
[0139] According to embodiments of this disclosure, the code coverage testing apparatus 300 further includes a version file generation module and an application package encapsulation module.
[0140] The version file generation module is used to generate a version file containing test code version metadata. In one embodiment, the version file generation module can be used to perform the operation S280 described above, which will not be repeated here.
[0141] The application package encapsulation module is used to encapsulate the version file and all class files of the test code into an application package. In one embodiment, the application package encapsulation module can be used to perform the operation S290 described above, which will not be repeated here.
[0142] According to embodiments of this disclosure, the code coverage testing apparatus 300 also includes a graceful offline reporting module.
[0143] The graceful shutdown reporting module is used to immediately report the currently collected code coverage data to the target server when the target application receives a shutdown command. In one embodiment, the graceful shutdown reporting module can be used to perform the operation S2100 described above, which will not be repeated here.
[0144] According to embodiments of this disclosure, the code coverage testing apparatus 300 further includes a retry triggering module, a retry counting module, a local cache module, and a cache reporting module.
[0145] The retry triggering module is used to automatically trigger the retry reporting process when code coverage data cannot be reported to the target server. In one embodiment, the retry triggering module can be used to execute the operation S2110 described above, which will not be repeated here.
[0146] The retry counting module is used to record the number of retry reports to obtain the cumulative number of retries. In one embodiment, the retry counting module can be used to perform the operation S2120 described above, which will not be repeated here.
[0147] The local caching module is used to store the code coverage data to be reported into the local cache directory when the cumulative number of retries reaches a preset threshold, thus obtaining cached data. In one embodiment, the local caching module can be used to perform the operation S2130 described above, which will not be repeated here.
[0148] The cache reporting module is used to supplement locally stored cached data to the target server when code coverage data can be reported normally. In one embodiment, the cache reporting module can be used to perform the operation S2140 described above, which will not be repeated here.
[0149] According to embodiments of this application, any multiple modules among the instruction sequence determination module 310, probe insertion module 320, code coverage data determination module 330, and test result determination module 340 can be combined into one module, or any one of these modules can be split into multiple modules. Alternatively, at least part of the functionality of one or more of these modules can be combined with at least part of the functionality of other modules and implemented in one module. According to embodiments of this application, at least one of the instruction sequence determination module 310, probe insertion module 320, code coverage data determination module 330, and test result determination module 340 can be at least partially implemented as hardware circuitry, such as field-programmable gate arrays, programmable logic arrays, systems-on-a-chip, systems-on-a-substrate, systems-on-package, application-specific integrated circuits, or implemented in hardware or firmware by any other reasonable means of integrating or packaging circuitry, or implemented in software, hardware, or firmware, or in any suitable combination of any of these three implementation methods. Alternatively, at least one of the instruction sequence determination module 310, probe insertion module 320, code coverage data determination module 330, and test result determination module 340 may be implemented at least partially as a computer program module that can perform corresponding functions when the computer program module is run.
[0150] Figure 12 A block diagram schematically illustrates an electronic device suitable for implementing a code coverage testing method according to an embodiment of this application.
[0151] like Figure 12 As shown, an electronic device 400 according to an embodiment of this application includes a processor 401, which can perform various appropriate actions and processes according to a program stored in a read-only memory 402 or a program loaded from a storage portion 408 into a random access memory 403. The processor 401 may include, for example, a general-purpose microprocessor, an instruction set processor and / or an associated chipset and / or a dedicated microprocessor. The processor 401 may also include onboard memory for caching purposes. The processor 401 may include a single processing unit or multiple processing units for executing different steps of the method flow according to an embodiment of this application.
[0152] Random access memory 403 stores various programs and data required for the operation of electronic device 400. Processor 401, read-only memory 402, and random access memory 403 are interconnected via bus 404. Processor 401 executes various steps of the method flow according to embodiments of this application by executing programs stored in read-only memory 402 and / or random access memory 403. It should be noted that the programs may also be stored in one or more memories other than read-only memory 402 and random access memory 403. Processor 401 may also execute various steps of the method flow according to embodiments of this application by executing programs stored in said one or more memories.
[0153] According to embodiments of this application, the electronic device 400 may further include an input / output interface 405, which is also connected to a bus 404. The electronic device 400 may also include one or more of the following components connected to the input / output interface 405: an input section 406 including a keyboard, mouse, etc.; an output section 407 including a cathode ray tube, liquid crystal display, etc., and a speaker, etc.; a storage section 408 including a hard disk, etc.; and a communication section 409 including a network interface card, such as a local area network card, modem, etc. The communication section 409 performs communication processing via a network such as the Internet. A drive 410 is also connected to the input / output interface 405 as needed. A removable medium 411, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 410 as needed so that computer programs read from it can be installed into the storage section 408 as needed.
[0154] Embodiments of this application also provide a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the method according to the embodiments of this application.
[0155] According to embodiments of this application, the computer-readable storage medium can be a non-volatile computer-readable storage medium, such as including but not limited to: portable computer disks, hard disks, random access memory, read-only memory, erasable programmable read-only memory, portable compact disk read-only memory, optical storage devices, magnetic storage devices, or any suitable combination thereof. In embodiments of this application, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to embodiments of this application, the computer-readable storage medium may include the read-only memory 402 described above, and / or random access memory 403, and / or one or more memories other than read-only memory 402 and random access memory 403.
[0156] Embodiments of this application also include a computer program product comprising a computer program containing program code for performing the methods shown in the flowchart. When the computer program product is run on a computer system, the program code is used to cause the computer system to implement the methods provided in the embodiments of this application.
[0157] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and downloaded and installed via communication section 409, and / or installed from removable medium 411. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0158] In embodiments of this application, the computer program can be downloaded and installed from a network via communication section 409, and / or installed from removable medium 411. When the computer program is executed by processor 401, it performs the functions defined in the system of this application embodiment. According to embodiments of this application, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0159] According to embodiments of this application, program code for executing the computer programs provided in the embodiments of this application can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0160] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0161] Those skilled in the art will understand that the features described in the various embodiments of this application can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in this application. In particular, the features described in the various embodiments of this application can be combined and / or combined in various ways without departing from the spirit and teachings of this application. All such combinations and / or combinations fall within the scope of this application.
Claims
1. A code coverage testing method, characterized in that, include: In response to determining that the class file loaded during the execution of the target application is the first target class file, the bytecode conversion module is used to obtain at least one initial bytecode instruction sequence in the first target class file, wherein the first target class file is the first class file loaded among the multiple class files to be loaded in the target application; A probe is inserted into at least one of the initial bytecode instruction sequences of the first target class file to obtain a second target class file; Execute the second target class file to determine the code coverage data of the first target class file using the probe; In response to determining that the target application meets preset test conditions, the test results for the target application are determined based on the code coverage data, wherein the preset test conditions include: abnormal offline of the target application.
2. The code coverage testing method according to claim 1, characterized in that, The at least one initial bytecode instruction sequence includes multiple bytecode instructions, and the second target class file includes multiple bytecode instructions and multiple probes respectively targeting multiple instructions to be tested, wherein the instructions to be tested are the bytecode instructions into which the probes are inserted; The step of determining the code coverage data of the first target class file through the probe includes: In response to the execution of the instruction to be tested in the second target class file, the probe for the instruction to be tested is marked as executed, and the marking result of the instruction to be tested is obtained; The code coverage data is determined based on the number of results from the marking and the number of bytecode instructions in the second target class file.
3. The code coverage testing method according to claim 2, characterized in that, The plurality of bytecode instructions include jump instructions for logical branches, and inserting a probe into at least one of the initial bytecode instruction sequences in the first target class file includes: Insert a probe for the first instruction to be tested among a plurality of instructions to be tested, the first instruction to be tested including the jump instruction.
4. The code coverage testing method according to claim 1, characterized in that, Also includes: The code coverage data is reported to the target server at preset time intervals.
5. The code coverage testing method according to claim 1, characterized in that, Also includes: Determine the code version identifier of the test code to which the first target class file belongs; Associate the code version identifier with the code coverage data.
6. The code coverage testing method according to claim 5, characterized in that, The step of determining the code version identifier of the test code to which the first target class file belongs includes: Scan the application package containing the test code to obtain the version file; Parse the version metadata of the test code from the version file; Based on the version metadata, the code version identifier is determined.
7. The code coverage testing method according to claim 5, characterized in that, Also includes: Generate a version file, which contains version metadata of the test code; The version file and all class files of the test code are packaged together into the application package.
8. The code coverage testing method according to claim 4, characterized in that, Also includes: When the target application receives a shutdown command, it reports the code coverage data to the target server.
9. The code coverage testing method according to claim 4, characterized in that, Also includes: If the code coverage data cannot be reported to the target server, retry reporting the code coverage data; Record the number of times the code coverage data is retried to obtain the retry count; When the number of retries reaches a preset threshold, the code coverage data is stored in the local cache directory to obtain cached data; When the code coverage data can be reported to the target server normally, the cached data will be reported to the target server.
10. A code coverage testing device, characterized in that, include: The instruction sequence determination module is used to determine, in response to determining that the class file loaded during the execution of the target application is the first target class file, obtain at least one initial bytecode instruction sequence in the first target class file using the bytecode conversion module, wherein the first target class file is the class file that is loaded for the first time among a plurality of class files to be loaded in the target application; A probe insertion module is used to insert a probe into at least one of the initial bytecode instruction sequences of the first target class file to obtain a second target class file; The code coverage data determination module is used to execute the second target class file to determine the code coverage data of the first target class file through the probe; The test result determination module is used to determine the test result for the target application based on the code coverage data in response to determining that the target application meets the preset test conditions. The preset test conditions include: abnormal offline of the target application.
11. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs. The characteristic feature is that the one or more processors execute the one or more computer programs to implement the steps of the method according to any one of claims 1 to 9.
12. A computer-readable storage medium having a computer program or instructions stored thereon, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 9.
13. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 9.
