Code coverage rate determination method and device, storage medium and program product
By inserting statistical code into the code coverage tool, real-time collection and processing of code execution behavior data, the problem of inability to view and analyze code coverage in real time in the existing technology is solved, and more accurate and comprehensive code coverage analysis is achieved, and testing efficiency and code quality are improved.
Patent Information
- Application Number
- CN202510082502.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-20
- Publication Date
- 2025-03-04
AI Technical Summary
When using the code coverage tool for code coverage analysis, the coverage results cannot be viewed in real time, and the coverage report is coarse in granularity and cannot provide finer-grained coverage information.
By inserting pre-compiled statistical code into each method of the target program, the behavior data of each method during the execution process is collected in real time, and the code coverage of different dimensions is obtained by processing the behavior data.
Real-time monitoring and fine-grained analysis of code coverage are realized, helping testers to deeply analyze the test coverage of the program, optimize the test results, improve code quality, and ensure that the execution paths of each part of the program are fully verified.
Smart Images

Figure CN119557231A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a method, device, storage medium and program product for determining code coverage. Background Art
[0002] Code coverage tools are used to analyze the execution of code. When using these tools for coverage analysis, they collect data during code execution and generate report files. Users need to manually open the report file to view this data, rather than viewing it in real time in the console or development environment. In addition, coverage reports are usually displayed at the granularity of methods and code lines, showing whether each method is executed and the execution status of each line of code, but cannot provide more fine-grained coverage information. Summary of the invention
[0003] In view of this, the present invention provides a method, device, storage medium and program product for determining code coverage to solve the problem that when using a code coverage tool to perform code coverage analysis, the coverage results cannot be viewed in real time and the coverage report has a coarse granularity.
[0004] In a first aspect, the present invention provides a method for determining code coverage, obtaining a target program to be executed, wherein the target program includes multiple methods; inserting pre-compiled statistical code into each method of the target program, and using the statistical code to collect behavioral data of each method during execution; processing the behavioral data to obtain code coverage of different dimensions.
[0005] The method for determining code coverage provided by an embodiment of the present invention inserts pre-compiled statistical code into each method of the target program to realize real-time collection and monitoring of behavioral data during program execution, so as to accurately track the execution status of each method, and obtain code coverage of different dimensions by processing the collected data, thereby helping testers to deeply analyze the test coverage of the program, optimize the test effect, improve the code quality, and ensure that the execution paths of various parts of the program are fully verified.
[0006] In an optional implementation, the behavior data is processed to obtain code coverage in different dimensions, including: extracting a first attribute feature corresponding to the execution code and a second attribute feature corresponding to the total code from the behavior data according to the dimension; and determining the code coverage in different dimensions according to the first attribute feature and the second attribute feature.
[0007] The method for determining code coverage provided by the embodiment of the present invention can analyze the code coverage in more detail by extracting attribute features of different dimensions (attribute features of execution code and attribute features of total code) from behavior data. By combining these features with coverage of different dimensions, a more comprehensive and accurate code coverage analysis can be provided, helping testers to better identify test blind spots and potential risks, optimize code testing strategies, and improve test effectiveness and coverage.
[0008] In an optional embodiment, the dimension includes one or more of a method dimension, an interface dimension, a program dimension and an incremental dimension; according to the first attribute feature and the second attribute feature, the code coverage of different dimensions is determined, including: for any method, based on the ratio of the first number of execution code lines included in the first attribute feature and the first total number of code lines included in the second attribute feature, the coverage of the method dimension corresponding to any method is determined; and / or, for any interface, the target method corresponding to any interface is obtained, and the coverage of the interface dimension corresponding to any interface is determined based on the ratio of the second number of execution code lines included in the first attribute feature and the second total number of code lines included in the second attribute feature; and / or, the target interface corresponding to the target program is obtained, and the coverage of the program dimension corresponding to the target program is determined based on the ratio of the third number of execution code lines included in the first attribute feature and the third total number of code lines included in the second attribute feature; and / or, if there is a code change in the target program, the coverage of the incremental dimension corresponding to the changed code is determined based on the ratio of the fourth number of execution code lines included in the first attribute feature and the fourth total number of code lines included in the second attribute feature.
[0009] The method for determining code coverage provided by the embodiment of the present invention can refine the coverage analysis and accurately identify the coverage at different levels by calculating the ratio of executed code to total code lines according to different dimensions (method execution, interface call, program as a whole, and code change). This detailed analysis not only helps to find weak links in the test, but also provides accurate test feedback for code changes and interface adjustments, thereby improving test efficiency and code quality, and ensuring that the software is fully verified at all levels.
[0010] In an optional implementation, the process of determining the first total number of code lines includes: obtaining the number of insertions of the statistical code corresponding to any method; and determining the number of insertions as the first total number of code lines corresponding to any method.
[0011] The method for determining code coverage provided by an embodiment of the present invention indirectly determines the total number of lines of code by counting the number of insertions of the code, thereby simplifying the calculation process of the code coverage. By recording the number of insertions of the statistical code in each method, the amount of code of the method can be efficiently measured, complex static analysis or code parsing can be avoided, and computational overhead can be reduced. At the same time, this method can accurately reflect the actual number of lines of code during the execution of the method, which facilitates more accurate measurement in coverage evaluation and improves the efficiency and accuracy of test analysis.
[0012] In an optional embodiment, the process of determining the first number of execution code lines includes: when executing any code line corresponding to any method, obtaining the line number of any executed code line; and counting the line numbers to obtain the first number of execution code lines corresponding to any method.
[0013] The method for determining code coverage provided by the embodiment of the present invention can accurately determine the number of code lines actually executed by the method by real-time tracking and counting the execution of each code line. By obtaining and counting the code line number of each execution, it can directly reflect which code lines are actually executed during the test process, thereby avoiding the complexity of static analysis.
[0014] In an optional embodiment, pre-compiled statistical code is inserted into each method of the target program, including: using the statistical code to create an object corresponding to any method when any method starts executing; the object is used to store behavioral data corresponding to any method.
[0015] The method for determining code coverage provided by the embodiment of the present invention helps to efficiently and accurately record the execution of each method by creating a special object to store relevant data at the beginning of the method, without the need for additional complex code analysis or manual intervention. Therefore, the method can dynamically obtain fine-grained execution data, which is convenient for subsequent analysis and evaluation, improves the accuracy of code coverage measurement and test efficiency, and also reduces performance overhead.
[0016] In an optional implementation, external interaction data of each method during execution is obtained; based on the behavior data and the external interaction data, a call link relationship between the methods is established; and the call link relationship is converted into a call link flow chart.
[0017] The method for determining code coverage provided by the embodiment of the present invention can fully capture the interaction and calling relationship between various methods by acquiring external interaction data and behavior data, thereby constructing a clear call link flow chart. Therefore, this method helps to intuitively display the dependencies and data flows between methods, and facilitates the analysis and optimization of the execution path of the program. By converting it into a flow chart, the tester can more clearly understand which methods and programs are called by the interface, simplifying the process of viewing the results.
[0018] In the second aspect, the present invention provides a device for determining code coverage, including: a first acquisition module, used to acquire a target program to be executed, the target program including multiple methods; a collection module, used to insert pre-compiled statistical code into each method of the target program, and use the statistical code to collect behavior data of each method during execution; a processing module, used to process the behavior data to obtain code coverage of different dimensions.
[0019] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the method for determining code coverage of the first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0020] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the method for determining code coverage of the above-mentioned first aspect or any corresponding embodiment thereof.
[0021] In a fifth aspect, the present invention provides a computer program product, comprising computer instructions for causing a computer to execute the method for determining code coverage of the first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0023] Figure 1 is a flow chart of a method for determining code coverage according to an embodiment of the present invention; Figure 2 is a flow chart of another method for determining code coverage according to an embodiment of the present invention; Figure 3 is a schematic diagram of a total coverage table according to an embodiment of the present invention; Figure 4 is a schematic diagram of a call link relationship according to an embodiment of the present invention; Figure 5 is a schematic diagram of a call link flow chart according to an embodiment of the present invention; Figure 6 is a schematic diagram of a snapshot creation page according to an embodiment of the present invention; Figure 7 is a structural block diagram of a device for determining code coverage according to an embodiment of the present invention; Figure 8 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0025] JaCoCo is a commonly used Java code coverage tool. When using JaCoCo for coverage analysis, it collects data during code execution and generates a report file (usually in HTML format). To view this data, users need to manually open the generated HTML report, and cannot view it in real time in the console or development environment. In addition, JaCoCo's coverage report is usually displayed at the method and code line granularity, showing whether each method is executed and the execution status of each line of code, but it cannot provide more fine-grained coverage information.
[0026] Based on this, the current JaCoCo usually uses Javassist as a bytecode instrumentation tool. Javassist is a tool that can insert coverage statistics code by modifying bytecode. However, due to the mechanism and performance limitations of Javassist, it may not be able to efficiently handle more complex or customized instrumentation requirements in some cases, resulting in insufficient flexibility.
[0027] In view of this, the technical solution of the present invention combines JaCoCo and ASM bytecode instrumentation technology to collect the behavior data of the target program through instrumentation and decoration execution. In JaCoCo, by inserting statistical code into the bytecode, the execution status of each executable code is marked, and the code coverage is calculated by counting these marks after execution. At the same time, this solution also uses ASM technology to modify the bytecode, supports modifying existing classes or dynamically generating classes, thereby improving the comprehensiveness and effectiveness of software testing.
[0028] According to an embodiment of the present invention, an embodiment of a method for determining code coverage is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0029] In this embodiment, a method for determining code coverage is provided, which can be used for computer devices, such as desktop computers, notebook computers, etc. Figure 1 is a flow chart of a method for determining code coverage according to an embodiment of the present invention, such as Figure 1 As shown, the process includes the following steps: Step S101, obtaining a target program to be executed, wherein the target program includes multiple methods.
[0030] The target program to be executed refers to the program that needs to be executed and the code coverage analysis is performed, for example, it can be an executable file after the source code is compiled. Multiple methods refer to different functions or logical units in the target program, and each method completes a specific task of the target program. Specifically, the target program to be executed can be obtained by compiling the source code or in the form of an executable file, bytecode, etc. generated from a build tool, such as using a build tool (such as Maven, Gradle, etc.) to compile the source code into a target program, or directly extracting the executable file to be executed from an existing program package, which is not limited here.
[0031] Step S102, inserting the pre-compiled statistical code into each method of the target program, and using the statistical code to collect the behavior data of each method during execution.
[0032] Precompiled statistical code refers to an additional code segment inserted into each method in the target program. These codes are used to track and record various information during the program execution, such as the execution status of each method or code line. Behavioral data refers to real-time data about code execution collected during the execution of the target program. Specifically, when starting, use the -javaagent parameter to specify a Java Agent. This Agent will be loaded and executed by the JVM before the Java virtual machine starts and executes the main function of the target program. The premain method uses bytecode manipulation tools such as ASM or Javassist to dynamically modify the bytecode of the target program and insert statistical code into each method. Through these inserted statistical codes, the program can collect behavioral data of each method at runtime, such as execution times, execution time, method calls, and other information, thereby realizing the monitoring and analysis of program behavior.
[0033] Step S103, processing the behavior data to obtain code coverage in different dimensions.
[0034] Code coverage in different dimensions refers to the degree of code execution measured from multiple perspectives, such as method coverage, interface coverage, and program coverage. Specifically, by counting the execution of each method, interface, or program itself during program execution, analyzing which parts have been executed and which have not been executed, the coverage of each dimension is calculated.
[0035] The method for determining code coverage provided by an embodiment of the present invention inserts pre-compiled statistical code into each method of the target program to realize real-time collection and monitoring of behavioral data during program execution, so as to accurately track the execution status of each method, and obtain code coverage of different dimensions by processing the collected data, thereby helping testers to deeply analyze the test coverage of the program, optimize the test effect, improve the code quality, and ensure that the execution paths of various parts of the program are fully verified.
[0036] In this embodiment, a method for determining code coverage is provided, which can be used for computer devices, such as desktop computers, notebook computers, etc. Figure 2 is a flow chart of a method for determining code coverage according to an embodiment of the present invention, such as Figure 2 As shown, the process includes the following steps: Step S201, obtain the target program to be executed, the target program includes multiple methods. Figure 1 Step S101 of the illustrated embodiment will not be described in detail here.
[0037] Step S202, inserting the pre-compiled statistical code into each method of the target program, and using the statistical code to collect the behavior data of each method during execution.
[0038] Specifically, the above step S202 includes: using the statistical code to create an object corresponding to any method when any method starts to be executed; the object is used to store the behavior data corresponding to any method.
[0039] An object is an instance of a data structure used to store the behavioral data of any method during its execution (such as method name, parameters, number of execution lines, etc.). Specifically, when the program executes a method, the statistical code will insert a section of code at the beginning of the method, which will create an object related to the method. This object is used to store the behavioral data required by the method during its execution, such as method name, passed parameters, number of execution lines, execution time, etc. These data will be recorded and managed through linked lists or other data structures for subsequent analysis and statistics.
[0040] The method for determining code coverage provided by the embodiment of the present invention helps to efficiently and accurately record the execution of each method by creating a special object to store relevant data at the beginning of the method, without the need for additional complex code analysis or manual intervention. Therefore, the method can dynamically obtain fine-grained execution data, which is convenient for subsequent analysis and evaluation, improves the accuracy of code coverage measurement and test efficiency, and also reduces performance overhead.
[0041] Step S203: Process the behavior data to obtain code coverage in different dimensions.
[0042] Specifically, the above step S203 includes: Step S2031, extracting the first attribute feature corresponding to the execution code and the second attribute feature corresponding to the total code from the behavior data according to the dimension.
[0043] The first attribute feature refers to the feature related to the execution code, such as the actual number of execution lines of a method or interface, which indicates the actual execution status of the code. The second attribute feature refers to the feature of the total code, for example, it can refer to the total number of lines of code, reflecting the overall code size of the target program. Specifically, the first attribute feature corresponding to the execution code and the second attribute feature corresponding to the total code are extracted based on the execution status recorded in the behavior data (such as whether each line of code is executed or not) combined with the total number of lines of code of the target program. For example, the behavior data of the program execution is collected by statistical code, and the number of execution lines (first attribute feature) and the total number of lines of code (second attribute feature) of each dimension (such as method, interface, program, etc.) are extracted based on these data.
[0044] Step S2032: determine the code coverage of different dimensions according to the first attribute feature and the second attribute feature.
[0045] Extract the first attribute features related to the executed code (such as the number of execution lines of each method, interface or program) and the second attribute features of the total code (such as the total number of code lines of methods, interfaces or programs) from the behavioral data. Calculate the coverage of each dimension based on these features.
[0046] The method for determining code coverage provided by the embodiment of the present invention can analyze the code coverage in more detail by extracting attribute features of different dimensions (attribute features of execution code and attribute features of total code) from behavior data. By combining these features with coverage of different dimensions, a more comprehensive and accurate code coverage analysis can be provided, helping testers to better identify test blind spots and potential risks, optimize code testing strategies, and improve test effectiveness and coverage.
[0047] In some optional embodiments, the dimension includes one or more of a method dimension, an interface dimension, a program dimension, and an incremental dimension, and the above step S2032 includes: for any method, based on the ratio of the first number of execution code lines included in the first attribute feature to the first total number of code lines included in the second attribute feature, determining the coverage of the method dimension corresponding to any method; and / or, for any interface, obtaining the target method corresponding to any interface, and determining the coverage of the interface dimension corresponding to any interface based on the ratio of the second number of execution code lines included in the first attribute feature to the second total number of code lines included in the second attribute feature; and / or, obtaining the target interface corresponding to the target program, and determining the coverage of the program dimension corresponding to the target program based on the ratio of the third number of execution code lines included in the first attribute feature to the third total number of code lines included in the second attribute feature; and / or, if there is a code change in the target program, determining the coverage of the incremental dimension corresponding to the changed code based on the ratio of the fourth number of execution code lines included in the first attribute feature to the fourth total number of code lines included in the second attribute feature.
[0048] The first number of executed code lines refers to the number of code lines actually executed by a method during the execution of the target program, that is, the number of code lines that are run in the method. The first total number of code lines refers to all the code lines in the method, including lines that are not executed. Specifically, by calculating the ratio of the first number of executed code lines to the first total number of code lines, the coverage of the method can be determined, reflecting the proportion of the actual executed code in the method to the total number of code lines. For example, if a method has 10 lines of code, of which 8 lines are executed, then the code coverage of the method is 8 / 10 = 80%.
[0049] The target method refers to the specific implementation method associated with a specific interface, which is one or more method implementations defined by the interface. The second number of executed code lines refers to the number of code lines actually executed by the specific target method during the execution of the target program, that is, the number of code lines actually run in the method. The second total number of code lines refers to the total number of code lines of the target method, including all code lines, whether executed or not. In order to determine the coverage of the interface dimension corresponding to any interface, it is necessary to obtain the target method corresponding to the interface and calculate it based on the ratio of the second number of executed code lines extracted from the behavior data to the second total number of code lines. Specifically, by calculating the ratio of the number of code lines actually executed during the execution process (the second number of executed code lines) to the total number of code lines of the target method (the second total number of code lines), the coverage of the interface dimension can be obtained, which indicates the extent to which the target method code under the interface is executed.
[0050] The target interface refers to the interface definition related to the specific interface in the target program, for example, it can be a set of all methods in the target program. The third number of executed code lines refers to the number of code lines actually executed by a specific target interface when executing the target program, that is, the number of times the code lines related to the interface are executed. The third total number of code lines refers to all the code lines in the target interface, including the implementation code lines of all methods, regardless of whether they are executed. In order to determine the coverage of the program dimension corresponding to the target program, it is necessary to obtain the target interface related to the target program and calculate it based on the ratio of the third number of execution code lines extracted from the behavior data to the third total number of code lines. Specifically, by calculating the ratio of the number of code lines actually executed (the third number of execution code lines) to the total number of code lines of the target interface (the third total number of code lines), the code coverage of the program dimension is obtained, which reflects the execution level of the overall code of the target program itself.
[0051] In the behavior data, the fourth number of executed code lines refers to the number of lines related to the changed code during the execution process, that is, the specific number of code lines related to the code change that are triggered and executed during the execution process. The fourth total number of code lines refers to all the code lines involved in the code change, including all the modified code lines before and after the change. Specifically, the Agent program monitors and collects the branches and change logs in Git to identify which parts of the target program code have changed (that is, the parts that are different from the specified version). This difference can be determined by the difference comparison tool in Git (such as gitdiff), showing which lines of code have been modified, added or deleted. When the code of the target program changes, the coverage of the incremental dimension needs to evaluate the test coverage of the code change part, which is calculated by dividing the fourth number of executed code lines by the fourth total number of code lines. That is, if a part of the changed code is executed, the code coverage of the changed part can be determined by calculating the ratio of the number of lines actually executed (the fourth number of executed code lines) to the total number of lines of the changed part (the fourth total number of code lines). All calculated incremental dimension coverage information will be stored in the database for subsequent analysis and tracking.
[0052] In addition, the code coverage of different dimensions can be displayed in the form of a table on the display interface, such as Figure 3 As shown, the fields are: application number, overall coverage, overall incremental coverage, last request time, source branch, target branch, application name, and project number.
[0053] In the above implementation, by calculating the ratio of executed code to total code lines according to different dimensions (method execution, interface call, program as a whole, and code change), the coverage analysis can be refined and the coverage at different levels can be accurately identified. This detailed analysis not only helps to find weak links in the test, but also provides accurate test feedback for code changes and interface adjustments, thereby improving test efficiency and code quality, and ensuring that the software is fully verified at all levels.
[0054] In some optional implementations, the process of determining the first total number of code lines includes: Step a1, obtaining the insertion times of the statistical code corresponding to any method.
[0055] The number of insertions of the statistical code refers to the number of times the pre-compiled statistical code is inserted into a specific method in the target program. Specifically, by analyzing the statistical code inserted into each method in the target program, the insertion frequency of the statistical code in the method is calculated.
[0056] Step a2: determine the number of insertions as a first total number of code lines corresponding to any method.
[0057] By counting the number of inserted statistical codes, the total number of lines of code of the method can be indirectly determined, that is, the number of insertions is used as an indicator of the total number of lines of code. In other words, the number of insertions represents the number of lines of code involved in the method, which can be used as an estimate of the total number of lines of code of the method.
[0058] In the above implementation, the total number of lines of code is indirectly determined by counting the number of insertions of the code, which simplifies the calculation process of code coverage. By recording the number of insertions of the statistical code in each method, the amount of code in the method can be efficiently measured, avoiding complex static analysis or code parsing, and reducing computational overhead. At the same time, this method can accurately reflect the actual number of lines of code during the execution of the method, facilitating more accurate measurement in coverage evaluation and improving the efficiency and accuracy of test analysis.
[0059] In some optional implementations, the process of determining the first number of execution code lines includes: Step b1, when executing any code line corresponding to any method, obtaining the line number of any executed code line.
[0060] The line number of any code line refers to the location identifier of each line of code in the source code of the target program, that is, the number of lines of code. The line number of any code line is used to identify whether a specific code line is executed during the program execution. Specifically, when the target program executes to a code line in a method, the inserted statistical code will capture the line number of the current code line and record it. By counting these line numbers, it is possible to determine which code lines have been executed.
[0061] Step b2: Count the line numbers to obtain the number of first execution code lines corresponding to any method.
[0062] Whenever the program executes a line of code, the pre-inserted statistical code will capture the line number of the current code line (i.e. the line number of the executed code line), and these line numbers will be recorded and statistically analyzed. Specifically, by counting the frequency of the executed line number in each method, the number of code lines actually executed in the method can be calculated, thereby obtaining the first number of executed code lines corresponding to the method.
[0063] For example, when the statistical code is an overridden equals method, during the interface execution process, an overridden equals method is inserted under each line of code. Whenever the method is executed, the current line number is passed as a parameter to the equals method, thereby recording the current line number of the code being executed. When the method is executed, the number of line numbers recorded by the equals method is counted. These line numbers record which lines of code have been executed during the execution of the method. Therefore, these line numbers can be used as the number of lines executed by the method. In addition, the statistical code will also count how many equals method calls are inserted (that is, an equals method will be inserted when each line of code is executed) to obtain the total number of code lines, and save the result as a txt file in the specified directory.
[0064] In the above implementation, by real-time tracking and counting the execution of each line of code, the number of lines of code actually executed by the method can be accurately determined. By obtaining the line number of each execution and counting it, it can directly reflect which lines of code are actually executed during the test process, thereby avoiding the complexity of static analysis.
[0065] Step S204, obtaining external interaction data of each method during execution.
[0066] External interaction data refers to the data of interaction with external systems obtained in real time by different collectors during the execution of the interface. Specifically, when the tester requests the interface, the data collection process is automatically started, and external interaction data related to the interface is collected through different collectors. According to the function and technology stack of the interface, select the appropriate collector for data collection. The HTTP collector records the URL, source IP, request parameters and response status of the network request; the RocketMQ collector captures data that interacts with the message queue, such as the IP of the message queue, message content, Topic and message type; the MySQL collector records the relevant data of database operations, including database name, table name, SQL statement, query conditions and execution status. These real-time collected data help analyze the execution of the interface, the interaction process of external dependencies, and support subsequent call link analysis and problem diagnosis.
[0067] Step S205: establishing a call link relationship between various methods based on the behavior data and the external interaction data.
[0068] The call chain relationship includes the calling order of each method and their interaction process with external systems (such as databases and message queues). Specifically, the execution of each method is tracked by statistical code (such as the overridden equals method), and the execution status and line number of each line of code are recorded to help track the progress and number of method execution lines. At the same time, external interaction data (such as HTTP requests, message queue interactions, database queries, etc.) are collected during the method execution process, and these data are combined with the method execution information to provide the interaction background between methods. Based on this data, a complete call chain is constructed to describe the calling order of methods and their interactions with external systems (such as databases and message queues). For example, a method may first issue an HTTP request, then execute a database query, and then send a message through RocketMQ. Finally, the method call chain is stored in the form of a linked list, recording the execution path of each method and related external interaction data, such as Figure 4 shown.
[0069] Step S206: convert the call link relationship into a call link flow chart.
[0070] The call chain flowchart graphically displays the calling sequence between methods and their interaction with external systems. Specifically, after establishing the call chain relationship, dagre-d3 is used to convert the data into a flowchart to graphically display the calling sequence between methods and their interaction with external systems. Each method in the flowchart is a node, and the nodes are connected by arrows, indicating the calling sequence and dependency relationship of the methods, such as Figure 5External interaction data (such as HTTP requests, database queries, message queue interactions, etc.) are also presented in different ways in the diagram, helping developers, testers, and operation and maintenance personnel to clearly understand the dependencies between the interface execution process and methods.
[0071] Furthermore, the information will be stored in ConcurrentHashMap and will only be saved for five minutes. After five minutes, the data will be cleared. If you want to save it for a long time, you can create a snapshot. The snapshot information is the information of this call chain (call chain relationship). After creating a snapshot, you can view the specific method call relationship, such as Figure 6 shown.
[0072] The method for determining code coverage provided by the embodiment of the present invention can fully capture the interaction and calling relationship between various methods by acquiring external interaction data and behavior data, thereby constructing a clear call link flow chart. Therefore, this method helps to intuitively display the dependencies and data flows between methods, and facilitates the analysis and optimization of the execution path of the program. By converting it into a flow chart, the tester can more clearly understand which methods and programs are called by the interface, simplifying the process of viewing the results.
[0073] In this embodiment, a device for determining code coverage is also provided, which is used to implement the above embodiments and preferred implementation modes, and will not be repeated hereafter. As used below, the term "module" may be a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0074] This embodiment provides a device for determining code coverage, such as Figure 7 As shown, including: A first acquisition module 301 is used to acquire a target program to be executed, wherein the target program includes a plurality of methods; The collection module 302 is used to insert the pre-compiled statistical code into each method of the target program, and use the statistical code to collect the behavior data of each method during execution; The processing module 303 is used to process the behavior data to obtain code coverage in different dimensions.
[0075] In some optional implementations, the processing module 303 includes: An extraction submodule, used to extract a first attribute feature corresponding to the execution code and a second attribute feature corresponding to the total code from the behavior data according to the dimension; The determination submodule is used to determine the code coverage of different dimensions according to the first attribute feature and the second attribute feature.
[0076] In some optional implementations, the determining submodule includes: The first determination unit is used to determine, for any method, the coverage of the method dimension corresponding to any method based on the ratio of the first number of execution code lines included in the first attribute feature and the first total number of code lines included in the second attribute feature; and / or, for any interface, obtain the target method corresponding to any interface, and determine the coverage of the interface dimension corresponding to any interface based on the ratio of the second number of execution code lines included in the first attribute feature and the second total number of code lines included in the second attribute feature; and / or, obtain the target interface corresponding to the target program, and determine the coverage of the program dimension corresponding to the target program based on the ratio of the third number of execution code lines included in the first attribute feature and the third total number of code lines included in the second attribute feature; and / or, if there is a code change in the target program, determine the coverage of the incremental dimension corresponding to the changed code based on the ratio of the fourth number of execution code lines included in the first attribute feature and the fourth total number of code lines included in the second attribute feature.
[0077] In some optional implementations, the first determining unit includes: A first acquisition unit, used to acquire the number of insertions of the statistical code corresponding to any method; The second determining unit is used to determine the number of insertions as a first total number of code lines corresponding to any method.
[0078] In some optional implementations, the first determining unit further includes: A second acquisition unit is used to acquire the line number of any code line executed when any code line corresponding to any method is executed; The counting unit is used to count the line numbers to obtain the number of first execution code lines corresponding to any method.
[0079] In some optional implementations, the acquisition module 302 includes: A submodule is created to create an object corresponding to any method by using the statistical code when any method starts to be executed; the object is used to store the behavior data corresponding to any method.
[0080] In some optional embodiments, the above device further comprises: The second acquisition module is used to acquire external interaction data of each method during execution; Establish a module for establishing the call link relationship between various methods based on behavior data and external interaction data; The conversion module is used to convert the call link relationship into a call link flow chart.
[0081] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0082] The device for determining the code coverage in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.
[0083] The device for determining code coverage provided by the embodiment of the present invention inserts pre-compiled statistical code into each method of the target program to realize real-time collection and monitoring of behavioral data during program execution, so as to accurately track the execution status of each method, and obtain code coverage of different dimensions by processing the collected data, thereby helping testers to deeply analyze the test coverage of the program, optimize the test effect, improve the code quality, and ensure that the execution paths of various parts of the program are fully verified.
[0084] The embodiment of the present invention also provides a computer device having the above Figure 7 The code coverage determination device is shown.
[0085] See also Figure 8 , Figure 8 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 8 As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 8 A processor 10 is taken as an example.
[0086] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0087] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.
[0088] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0089] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0090] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Figure 8 The example of connecting through bus is taken in the following.
[0091] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device may be a touch screen.
[0092] The computer device also includes a communication interface for the computer device to communicate with other devices or a communication network.
[0093] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0094] A part of the present invention may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present invention through the operation of the computer. Those skilled in the art should understand that the existence of the computer program instruction in a computer-readable medium includes, but is not limited to, a source file, an executable file, an installation package file, etc., and accordingly, the way in which the computer program instruction is executed by the computer includes, but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium may be any available computer-readable storage medium or communication medium accessible to the computer.
[0095] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A method for determining code coverage, characterized in that: The method comprises: Obtaining a target program to be executed, wherein the target program includes a plurality of methods; Inserting precompiled statistical codes into each method of the target program, and using the statistical codes to collect behavioral data of each method during execution; The behavior data is processed to obtain code coverage in different dimensions.
2. The method according to claim 1, characterized in that The behavior data is processed to obtain code coverage in different dimensions, including: Extracting a first attribute feature corresponding to the execution code and a second attribute feature corresponding to the total code from the behavior data according to the dimension; The code coverage of the different dimensions is determined according to the first attribute feature and the second attribute feature.
3. The method according to claim 2, characterized in that The dimension includes one or more of a method dimension, an interface dimension, a program dimension, and an increment dimension; and determining the code coverage of the different dimensions according to the first attribute feature and the second attribute feature includes: For any method, based on the ratio of the first number of execution code lines included in the first attribute feature to the first total number of code lines included in the second attribute feature, determine the coverage of the method dimension corresponding to the any method; and / or, For any interface, obtain a target method corresponding to the any interface, and determine the coverage of the interface dimension corresponding to the any interface based on the ratio of the second number of execution code lines included in the first attribute feature and the second total number of code lines included in the second attribute feature; and / or, Acquire a target interface corresponding to the target program, and determine the coverage of a program dimension corresponding to the target program based on a ratio of a third number of execution code lines included in the first attribute feature and a third total number of code lines included in the second attribute feature; and / or, If there is a code change in the target program, the coverage of the incremental dimension corresponding to the changed code is determined based on the ratio of the fourth number of execution code lines included in the first attribute feature and the fourth total number of code lines included in the second attribute feature.
4. The method according to claim 3, characterized in that The process of determining the first total number of code lines includes: Obtain the number of insertions of the statistical code corresponding to any of the methods; The number of insertions is determined as the first total number of code lines corresponding to any one of the methods.
5. The method according to claim 3, characterized in that: The process of determining the first number of execution code lines includes: When executing any code line corresponding to any of the methods, obtaining the line number of the executed code line; The line numbers are counted to obtain the number of the first execution code lines corresponding to any one of the methods.
6. The method according to claim 1, characterized in that The method of inserting the pre-compiled statistical code into each method of the target program includes: When any method starts to be executed, the statistical code is used to create an object corresponding to the method; the object is used to store the behavior data corresponding to the method.
7. The method according to claim 1, characterized in that Also includes: Obtain external interaction data of each method during execution; Based on the behavior data and the external interaction data, establishing a call link relationship between various methods; The call link relationship is converted into a call link flow chart.
8. A device for determining code coverage, characterized in that: The device comprises: A first acquisition module is used to acquire a target program to be executed, wherein the target program includes a plurality of methods; A collection module, used to insert pre-compiled statistical codes into each method of the target program, and use the statistical codes to collect behavior data of each method during execution; The processing module is used to process the behavior data to obtain code coverage in different dimensions.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the method for determining code coverage according to any one of claims 1 to 7.
10. A computer program product, characterized in that The method comprises computer instructions, wherein the computer instructions are used to cause a computer to execute the method for determining the code coverage according to any one of claims 1 to 7.
Citation Information
Patent Citations
Code coverage rate analysis method and device, equipment and medium
CN113806231A
Coverage rate acquisition method and device, electronic equipment and storage medium
CN116955122A
Test method and device based on functional interface
CN117743167A
Coverage rate determination method and device, equipment, storage medium and program product
CN118132408A
Incremental code coverage analysis using automatic breakpoints
US20170132121A1