Line coverage determination method and device, computer device and storage medium
By utilizing bitwise operations on the execution array and reference array during software testing to calculate row coverage in real time, the problem of insufficient timeliness of row coverage in traditional techniques is solved, achieving timeliness and efficiency at the end of each time period.
Patent Information
- Application Number
- CN202411976719.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-26
- Publication Date
- 2026-01-02
- Estimated Expiration
- 2044-12-26
AI Technical Summary
In traditional techniques, the determination of row coverage is not timely, and it cannot reflect the comprehensiveness of test cases in a timely manner during the software testing process.
By obtaining the execution array and reference array of the target time period and the previous adjacent time period, bitwise operations are used to determine the newly executed bytecode instructions and source code lines, and then the line coverage is calculated.
It improves the timeliness of row coverage, enabling the determination of the current row coverage at the end of each time period, which facilitates timely adjustment of test cases and improves testing efficiency.
Smart Images

Figure CN119690850B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a line coverage determination method and device, computer equipment and storage medium. BACKGROUND
[0002] Line coverage is an index used to measure the execution of code in a program to be tested in software testing. The line coverage refers to the proportion of the number of executed source code lines in the program to be tested to the total number of source code lines. The higher the line coverage, the more comprehensive the test cases for testing the program to be tested, and the better the potential errors and defects in the program to be tested can be revealed.
[0003] In the prior art, the line coverage is calculated by collecting the executed source code line identifiers after the testing of the program to be tested is completed, which results in low timeliness of line coverage determination. SUMMARY
[0004] Therefore, it is necessary to provide a line coverage determination method and device, computer equipment, computer readable storage medium and computer program product capable of improving the timeliness of line coverage.
[0005] In a first aspect, the present application provides a line coverage determination method. The method comprises:
[0006] obtaining an execution array corresponding to a target time period and a reference array; the reference array is the same as an execution array corresponding to a previous adjacent time period of the target time period, and an element in the execution array represents the execution of a bytecode instruction corresponding to the index of the element;
[0007] determining a target index corresponding to a newly executed bytecode instruction in the target time period based on the execution array and the reference array;
[0008] determining a target line identifier corresponding to a target source code line newly executed in the target time period based on an index table corresponding to the program to be tested and the target index;
[0009] determining the line coverage corresponding to the program to be tested based on the target line identifier.
[0010] In one embodiment, the determining a target index corresponding to a newly executed bytecode instruction in the target time period based on the execution array and the reference array comprises:
[0011] performing a bitwise NOT operation on the reference array to obtain an updated reference array;
[0012] performing a bitwise AND operation on the execution array and the updated reference array to obtain a new array;
[0013] determining an index corresponding to the element with the first identifier in the new array as a target index; the first identifier represents that a bytecode instruction corresponding to the target index is executed in the target time period.
[0014] In an embodiment, the target source code line corresponding to the target time period is determined based on the index table corresponding to the program to be tested and the target index, comprising:
[0015] For each target index, the source code line identifier corresponding to the target index in the index table is determined as the target line identifier corresponding to the target index; the index table includes the correspondence between the index and the source code line identifier, the index is the bytecode instruction identifier of the bytecode instruction in the bytecode file corresponding to the program to be tested, and the source code line identifier is the identifier of the source code line in the program to be tested.
[0016] In an embodiment, the line coverage corresponding to the program to be tested is determined based on the target line identifier, comprising:
[0017] The target line identifier corresponding to the target time period is counted to obtain the total number of new executions corresponding to the target time period;
[0018] The total number of new executions and the total number of each historical execution are counted to obtain the total number of current executions;
[0019] The line coverage corresponding to the program to be tested is determined based on the total number of current executions and the total number of code lines corresponding to the program to be tested.
[0020] In an embodiment, the execution array and the reference array corresponding to the target time period are obtained, comprising:
[0021] The execution array and the reference array corresponding to the last adjacent time period of the target time period are obtained;
[0022] The execution array and the reference array corresponding to the last adjacent time period are subjected to bitwise OR operation to obtain the reference array corresponding to the target time period;
[0023] The execution array corresponding to the last adjacent time period is updated based on the bytecode instruction identifier corresponding to the bytecode instruction executed in the target time period to obtain the execution array corresponding to the target time period.
[0024] In an embodiment, before the execution array and the reference array corresponding to the target time period are obtained, further comprising:
[0025] compile the program to be tested to obtain a bytecode file corresponding to the program to be tested;
[0026] determine a mapping relationship between a source code line identifier of a source code line in the program to be tested and a bytecode instruction identifier of a bytecode instruction in the bytecode file;
[0027] determine an index table corresponding to the program to be tested based on the mapping relationship.
[0028] In a second aspect, the present application further provides a line coverage determination apparatus. The apparatus comprises:
[0029] an acquisition module configured to acquire an execution array corresponding to a target time period and a reference array; the reference array is the same as an execution array corresponding to a time period adjacent to the target time period, and an element in the execution array represents an execution situation of a bytecode instruction corresponding to an index of the element;
[0030] an index determination module configured to determine a target index corresponding to a bytecode instruction newly executed in the target time period based on the execution array and the reference array;
[0031] a line identifier determination module configured to determine a target line identifier corresponding to a target source code line newly executed in the target time period based on an index table corresponding to the program to be tested and the target index;
[0032] a line coverage determination module configured to determine a line coverage corresponding to the program to be tested based on the target line identifier.
[0033] In a third aspect, the present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements steps of the method in any one of the first aspect when executing the computer program.
[0034] In a fourth aspect, the present application further provides a computer readable storage medium, which stores a computer program, and the computer program implements steps of the method in any one of the first aspect when executed by a processor.
[0035] In a fifth aspect, the present application further provides a computer program product comprising a computer program, and the computer program implements steps of the method in any one of the first aspect when executed by a processor.
[0036] The aforementioned line coverage determination method, apparatus, computer equipment, storage medium, and computer program product acquire the execution array corresponding to the target time period and reference data identical to the execution array corresponding to the adjacent time period preceding the target time period. Based on the execution array and reference array, the target index corresponding to the newly executed bytecode instructions within the target time period is determined. Since the index table includes the correspondence between the bytecode instruction identifiers of each bytecode instruction in the bytecode file corresponding to the program under test and the source code line identifiers of the source code lines in the program under test, the target line identifier corresponding to the newly executed target source code line within the target time period can be determined based on the index table and target index. The total number of new executions corresponding to the target time period can then be determined based on the target line identifier. Similarly, based on the above method, the total number of historical executions corresponding to each historical time period before the target time period is determined. Based on the total number of new executions and the total number of historical executions, the line coverage of the program under test at the end of the target time period can be determined. This can be understood as determining the current line coverage of the program under test once at the end of each time period, which improves the timeliness of line coverage determination compared to determining the line coverage at the end of the test. Furthermore, calculating the line coverage of the program under test during the testing process facilitates timely adjustments to the test cases, thereby improving testing efficiency. Attached Figure Description
[0037] Figure 1 This is a diagram illustrating the application environment of an implementation method for determining coverage.
[0038] Figure 2 This is a flowchart illustrating an implementation method for determining coverage.
[0039] Figure 3 This is a schematic diagram illustrating the execution of an array in one embodiment;
[0040] Figure 4 This is a schematic diagram of an index table in one embodiment;
[0041] Figure 5 This is a schematic diagram illustrating the process of determining new data in one embodiment;
[0042] Figure 6 This is a flowchart illustrating the process after determining the newly added array in one embodiment;
[0043] Figure 7 This is a schematic diagram illustrating the process of determining the reference array corresponding to the target time period in one embodiment;
[0044] Figure 8 This is a schematic diagram of the execution array corresponding to the target time period in one embodiment;
[0045] Figure 9A flowchart of a line coverage determination procedure in one embodiment;
[0046] Figure 10 A block diagram of a line coverage determination apparatus in one embodiment;
[0047] Figure 11 An internal structure diagram of a computer device in one embodiment. DETAILED DESCRIPTION
[0048] For the purposes of the present application, the technical solutions and advantages thereof are more clearly apparent, the following further describes the present application in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely intended to explain the present application, and are not intended to limit the present application.
[0049] The line coverage determination method provided by the embodiments of the present application can be applied in an application environment as shown in Figure 1 The terminal 102 communicates with the server 104 through a network. The data storage system can store data required to be processed by the server 104. The data storage system can be integrated on the server 104, or placed on a cloud or other network server. The terminal and the server can be individually used to execute the line coverage determination method provided in the embodiments of the present application. The terminal and the server can also be used in cooperation to execute the line coverage determination method provided in the embodiments of the present application. For example, the terminal 102 acquires an execution array corresponding to a target time period and a reference array, the reference array is the same as an execution array corresponding to a previous adjacent time period of the target time period, the elements in the execution array represent the execution situation of the bytecode instructions corresponding to the indexes of the elements, determines a target index corresponding to a bytecode instruction newly executed in the target time period based on the execution array and the reference array, determines a target line identifier corresponding to a target source code line newly executed in the target time period based on an index table corresponding to the program to be tested and the target index, and determines the line coverage corresponding to the program to be tested based on the target line identifier. The terminal 102 can be, but is not limited to, various personal computers, notebook computers, smart phones, tablet computers, Internet of Things devices and portable wearable devices. The Internet of Things device can be a smart speaker, a smart television, a smart air conditioner, a smart vehicle device, etc. The portable wearable device can be a smart watch, a smart bracelet, a head-mounted device, etc. The server 104 can be implemented by an independent server or a server cluster composed of multiple servers.
[0050] In one embodiment, as shown in Figure 2 A line coverage determination method is provided, and the embodiments take the method applied to a computer device as an example to illustrate the method, which includes steps 202 to 208.
[0051] In step 202, an execution array corresponding to a target time period and a reference array are obtained; the reference array is the same as an execution array corresponding to a previous adjacent time period of the target time period, and an element in the execution array represents an execution situation of a bytecode instruction corresponding to an index of the element.
[0052] The time period refers to a time period in a test process of the to-be-tested program, which can be understood as dividing the test time of the to-be-tested program into multiple time periods, for example, the first second in the test process of the to-be-tested program is a time period, and the second second is a time period. The length of the time period can be set according to actual needs, which is not limited here. The target time period refers to the current time period. The execution array refers to an array for storing bytecode execution situations, and the execution array includes multiple elements, each element corresponding to an index, and the index is a bytecode instruction identifier of a bytecode instruction. The element in the execution array can be a first identifier or a second identifier, the first identifier representing that the bytecode instruction corresponding to the index of the element is executed, and the second identifier representing that the bytecode instruction corresponding to the index of the element is not executed, the first identifier can be 1, and the second identifier can be 0. For example, as shown in the schematic diagram of the execution array Figure 3 The 0 in the execution array is an element in the execution array, the index 1, the index 2, and the index 3 in the execution array are indexes of the elements in the execution array, and the element and the index in the execution array correspond to each other. The reference array refers to an array for recording an execution array of a previous adjacent time, that is, the reference array corresponding to the target time period is the same as the execution array corresponding to the previous adjacent time period of the target time period.
[0053] The element refers to a value stored in the array, and the element in the execution array refers to a value stored in the execution array. The index refers to a position identifier of the element, and the index can be a bytecode instruction identifier of a bytecode instruction. The bytecode instruction refers to an intermediate representation form generated after a program source code is compiled, which is a machine-independent instruction, and is used for interpretation or execution after compilation by a virtual machine, and usually represents the operation and flow of a program in a concise form. The bytecode file is composed of multiple bytecode instructions, and the bytecode instruction corresponds to the bytecode instruction identifier one by one. The execution situation of the bytecode instruction refers to whether the bytecode instruction is interpreted or executed after compilation, and the execution situation of the bytecode instruction can be executed or not executed.
[0054] For example, at the end of the target time period, the computer device obtains the current execution array and the reference array to obtain the execution array corresponding to the target time period and the reference array.
[0055] In step 204, based on the execution array and the reference array, a target index corresponding to a newly executed bytecode instruction in the target time period is determined.
[0056] The target index refers to a bytecode identifier corresponding to a bytecode instruction executed in the target time period.
[0057] The computer device performs a logical operation on the execution data and the reference array to obtain a new array, and determines the target index corresponding to the newly executed bytecode instruction in the target time period based on the new array.
[0058] In step 206, the target line identifier corresponding to the newly executed target source code line in the target time period is determined based on the index table corresponding to the to-be-tested program and the target index.
[0059] The to-be-tested program refers to a program to be tested, which can be composed of multiple source code lines in a program file or multiple source code lines in multiple program files. The index table corresponding to the to-be-tested program refers to an index table generated according to the to-be-tested program. The index table is an array in a key-value pair structure, and can be a hash table. The index table includes an index and a source code line identifier. The index in the index table is a bytecode instruction identifier of a bytecode instruction in a bytecode file corresponding to the to-be-tested program, which can be an identifier of the position of the bytecode instruction in the bytecode file. The source code line identifier refers to an identifier of a source code line in the to-be-tested program, which can be a position identifier of the source code line in the to-be-tested program, or can be composed of a file identifier of a file where the source code line is located and a position identifier in the file. For example, as shown in the schematic diagram of the index table, Figure 4 The index 1, the index 2, and the index 3 are indexes in the index table. The index 1 corresponds to two source code line identifiers, which are “file 1+line number 1” and “file 4+line number 8”. “File 1+line number 1” represents the first line of source code in file 1 that constitutes the to-be-tested program, and “file 4+line number 8” represents the eighth line of source code in file 4 that constitutes the to-be-tested program. That is, the index 1 corresponds to the bytecode instruction associated with the first line of source code in file 1 and the eighth line of source code in file 4. The target line identifier refers to an identifier of a source code line executed in the target time period.
[0060] The computer device obtains the index table corresponding to the to-be-tested program, and determines the target line identifier corresponding to the newly executed target source code line in the target time period based on the index table and the target index.
[0061] In step 208, the line coverage corresponding to the to-be-tested program is determined based on the target line identifier.
[0062] The line coverage refers to a proportion of the number of source code lines executed in the to-be-tested program to the total number of code lines in the to-be-tested program at the end of the target time period.
[0063] Exemplarily, the computer device counts target line identifiers corresponding to a target time period to obtain a total number of newly executed lines corresponding to the target time period, and determines line coverage of the to-be-tested program corresponding to the target time period based on the total number of newly executed lines corresponding to the target time period and historical total numbers of executed lines corresponding to historical time periods. The historical time periods refer to time periods before the target time period in the testing process of the to-be-tested program. For example, if the target time period is the fifth time period, the first time period, the second time period, the third time period, and the fourth time period are all historical time periods.
[0064] In this embodiment, an execution array corresponding to a target time period and reference data same as an execution array corresponding to a time period adjacent to the target time period are obtained, and a target index corresponding to a bytecode instruction newly executed in the target time period is determined based on the execution array and the reference array. Since the index table includes a correspondence between bytecode instruction identifiers of each bytecode instruction in a bytecode file corresponding to the to-be-tested program and source code line identifiers of source code lines in the to-be-tested program, a target source code line identifier corresponding to a target source code line newly executed in the target time period can be determined based on the index table and the target index. The total number of newly executed lines corresponding to the target time period can be determined based on the target source code line identifier. Similarly, historical total numbers of executed lines corresponding to historical time periods before the target time period are determined based on the above method. The line coverage of the to-be-tested program corresponding to an ending moment of the target time period can be determined based on the total number of newly executed lines and the historical total numbers of executed lines. It can be understood that the line coverage of the to-be-tested program can be determined at the end of each time period, which improves the timeliness of determining the line coverage compared with determining the line coverage at the end of the testing of the to-be-tested program. Moreover, the line coverage of the to-be-tested program is calculated during the testing, which facilitates timely adjustment of test cases of the to-be-tested program to improve the testing efficiency.
[0065] In one embodiment, based on the execution array and the reference array, a target index corresponding to a bytecode instruction newly executed in the target time period is determined, including:
[0066] The reference array is subjected to a bitwise NOT operation to obtain an updated reference array, and the execution array and the updated reference array are subjected to a bitwise AND operation to obtain a new array. An index corresponding to an element with a first identifier in the new array is determined as the target index. The first identifier indicates that a bytecode instruction corresponding to the index is executed in the target time period.
[0067] The bitwise NOT operation involves inverting each element of the reference array. For example, if the reference array is 001010, inverting it results in an updated reference array of 110101. The bitwise AND operation involves performing a bitwise AND operation on elements at the same position in both arrays. For example, if the execution array is 000011 and the updated reference array is 110101, performing a bitwise AND operation on both arrays results in a new array of 000001.
[0068] For example, the computer device performs a bitwise NOT operation on the reference array to obtain an updated reference array, then performs a bitwise AND operation on the execution array and the updated reference array to obtain a new array. The index corresponding to the element with the first identifier in the new array is determined as the target index. For example, a schematic diagram of the new data determination process is shown below. Figure 5 As shown, LineBitOrigin is the execution array, LineBitTemp is the reference array, and LineBitDiff is the new array. The new array can be obtained by performing bitwise NAND operations on the elements with the same index in the execution array and the reference array.
[0069] In one embodiment, the flowchart after determining the addition of the array is as follows: Figure 6 As shown, the process includes: the computer device determining the index corresponding to the element with the first identifier in the newly added array as the target index; determining the source code line identifier corresponding to the target index in the index table as the target line identifier corresponding to the target index; and sending the target line identifier corresponding to the target time period to the server. The server counts the target line identifiers corresponding to the target time period to obtain the total number of new executions for the target time period; it counts the total number of historical executions and the total number of new executions to obtain the current total number of executions; based on the current total number of executions and the total number of lines of code corresponding to the program under test, it determines the line coverage rate of the program under test at the end of the target time period; and it generates a line coverage report based on the line coverage rate at the end of the target time period, which is used for testing personnel to view. The methods by which the computer device sends the target line identifier corresponding to the target time period to the server include, but are not limited to, using network transmission methods such as GRPC (Google Remote Procedure Call), HTTP (Hypertext Transfer Protocol), and RPC (Remote Procedure Call).
[0070] In the embodiment, the newly added array is obtained by performing a bitwise AND operation on the execution array and the updated reference array, and the index corresponding to the element with the first identifier in the newly added array is the bytecode instruction identifier of the bytecode instruction executed in the target time period, so that the index corresponding to the element with the first identifier in the newly added array can be determined as the target index, thereby providing accurate basic data for subsequent determination of the target line identifier. Moreover, only the bitwise NOT operation and the bitwise AND operation are included in the process of determining the target index, the operation amount of the entire process is small, and the operation speed is fast, thereby improving the determination efficiency of the target index.
[0071] In one embodiment, based on the index table corresponding to the program to be tested and the target index, a target line identifier corresponding to a target source code line newly executed in a target time period is determined, including:
[0072] For each target index, the source code line identifier corresponding to the target index in the index table is determined as the target line identifier corresponding to the target index; the index table includes the correspondence between the index and the source code line identifier, the index is the bytecode instruction identifier of the bytecode instruction in the bytecode file corresponding to the program to be tested, and the source code line identifier is the identifier of the source code line in the program to be tested.
[0073] In the index table, the number of indexes is the same as the number of bytecode instructions in the bytecode file corresponding to the program to be tested, the index is the bytecode instruction identifier of the bytecode instruction, and the index corresponds to the bytecode instruction one by one. For example, the bytecode file corresponding to the program to be tested includes 100 bytecode instructions, and the index table corresponding to the program to be tested includes 100 indexes.
[0074] For example, after the target index is determined, the computer device obtains the index table corresponding to the program to be tested, and for each target index, the source code line identifier corresponding to the target index in the index table is determined as the target line identifier corresponding to the target index.
[0075] In the embodiment, since the index table includes the correspondence between the bytecode instruction identifier of each bytecode instruction in the bytecode file corresponding to the program to be tested and the source code line identifier of the source code line in the program to be tested, the target line identifier corresponding to the target source code line newly executed in the target time period can be determined according to the index table and the target index.
[0076] In one embodiment, based on the target line identifier, a line coverage corresponding to the program to be tested is determined, including:
[0077] The target line identifier corresponding to the target time period is counted to obtain a total number of newly executed lines corresponding to the target time period; the total number of historical executions and the total number of newly executed lines are counted to obtain a current total number of executions; and the line coverage corresponding to the program to be tested is determined based on the current total number of executions and a total number of code lines corresponding to the program to be tested.
[0078] The total number of newly executed lines refers to a total number of source code lines executed in the target time period. The total number of historical executions refers to a total number of source code lines executed in a historical time period, and the historical time period refers to a time period before the target time period during the testing of the program to be tested. The length of the historical time period can be equal to the length of the target time period. The current total number of executions refers to a total number of source code lines executed up to the end of the target time period. The total number of code lines refers to a total number of source code lines included in the program to be tested.
[0079] For example, the computer device counts the target line identifier corresponding to the target time period to obtain the total number of newly executed lines corresponding to the target time period, obtains the total number of historical executions corresponding to each historical time period, counts the total number of historical executions and the total number of newly executed lines to obtain the current total number of executions, and divides the current total number of executions by the total number of code lines corresponding to the program to be tested to obtain the line coverage corresponding to the program to be tested at the end of the target time period.
[0080] In this embodiment, the current total number of executions is obtained by counting the total number of historical executions and the total number of newly executed lines, and the line coverage corresponding to the program to be tested at the end of the target time period is determined based on the current total number of executions and the total number of code lines corresponding to the program to be tested. It can be understood that the current line coverage of the program to be tested can be determined once at the end of each time period, which improves the timeliness of determining the line coverage compared to determining the line coverage at the end of the testing of the program to be tested.
[0081] In one embodiment, the execution array and the reference array corresponding to the target time period are obtained, including:
[0082] The execution array and the reference array corresponding to the last adjacent time period of the target time period are obtained; the execution array and the reference array corresponding to the last adjacent time period are subjected to a bitwise OR operation to obtain the reference array corresponding to the target time period; and the execution array corresponding to the last adjacent time period is updated based on the bytecode instruction identifier corresponding to the bytecode instruction executed in the target time period to obtain the execution array corresponding to the target time period.
[0083] The bitwise OR operation refers to an OR operation on elements at the same position in the two arrays.
[0084] Exemplarily, the computer device determines an execution array and a reference array corresponding to a last adjacent time period of a target time period, and performs a bitwise OR operation on the execution array and the reference array corresponding to the last adjacent time period to obtain an array as a reference array corresponding to the target time period after determining a new-added array corresponding to the last adjacent time period based on the execution array and the reference array corresponding to the last adjacent time period. For example, a determination process of the reference array corresponding to the target time period is shown in FIG. 8. Figure 7 The execution array LineBitOrigin corresponding to the last adjacent time period and the reference array LineBitTemp corresponding to the last adjacent time period are subjected to a bitwise OR operation to obtain the reference array LineBitTemp corresponding to the target time period.
[0085] The computer device assigns, based on a bytecode instruction identifier corresponding to a bytecode instruction executed in the target time period, an element value at an index same as the bytecode instruction identifier in the execution array corresponding to the last adjacent time period to a first identifier to obtain an execution array corresponding to the target time period. For example, a schematic diagram of the execution array corresponding to the target time period is shown in FIG. 9. Figure 8 The bytecode instruction corresponding to the index 4 and the bytecode instruction corresponding to the index 7 are executed in the target time period, and therefore the elements corresponding to the index 4 and the index 7 are respectively assigned to 1.
[0086] In this embodiment, the execution array and the reference array corresponding to the last adjacent time period are subjected to a bitwise OR operation to obtain the reference array corresponding to the target time period, and an index corresponding to all elements with the first identifier in the reference array corresponding to the target time period represents all bytecode instructions executed between the target time periods, which provides accurate basic data for subsequent determination of a new-added array corresponding to the target time period. The execution array corresponding to the last adjacent time period is updated based on a bytecode instruction identifier corresponding to a bytecode instruction executed in the target time period to obtain an execution array corresponding to the target time period, and an index corresponding to all elements with the first identifier in the execution array corresponding to the target time period represents all bytecode instructions executed before and in the target time period, which provides accurate basic data for subsequent determination of the new-added array corresponding to the target time period. Moreover, the execution data storage cup is used to store information of the bytecode instructions executed, which occupies a small storage space and improves resource utilization.
[0087] In one embodiment, before the execution array and the reference array corresponding to the target time period are obtained, the method further includes:
[0088] The program to be tested is compiled to obtain a bytecode file corresponding to the program to be tested, a mapping relationship between a source code line identifier of a source code line in the program to be tested and a bytecode instruction identifier of a bytecode instruction in the bytecode file is determined, and an index table corresponding to the program to be tested is determined based on the mapping relationship.
[0089] The mapping relationship refers to a corresponding relationship between the bytecode instruction identifier and the source code line identifier.
[0090] For example, the computer device compiles the program to be tested to obtain a bytecode file corresponding to the program to be tested, determines a mapping relationship between a source code line identifier of a source code line in the program to be tested and a bytecode instruction identifier of a bytecode instruction in the bytecode file, and determines an index table corresponding to the program to be tested based on all the mapping relationships.
[0091] In an embodiment, before the program to be tested is compiled, the method further includes: embedding a line coverage collection program into the program to be tested. The embedding manner can be a patching manner. The line coverage collection program is used to collect running information of the program to be tested, including but not limited to an execution path and a variable value, to support software testing, performance analysis or behavior monitoring.
[0092] In an embodiment, after the program to be tested is compiled to obtain a bytecode file corresponding to the program to be tested, the method further includes: generating an initial execution array based on bytecode instruction identifiers of bytecode instructions in the bytecode file; each element in the execution array is a second identifier, the number of elements in the execution array is the same as the number of bytecode instructions in the bytecode file, and the index of the element in the execution array is the bytecode instruction identifier in the bytecode file. The second identifier represents that the bytecode instruction corresponding to the index of the second identifier is not executed, and the second identifier can be 0.
[0093] In this embodiment, the index table corresponding to the program to be tested is determined based on the mapping relationship between the source code line identifier of the source code line in the program to be tested and the bytecode instruction identifier of the bytecode instruction in the bytecode file, and the index table includes a corresponding relationship between the bytecode instruction identifier of each bytecode instruction in the bytecode file corresponding to the program to be tested and the source code line identifier of the source code line in the program to be tested, which lays a foundation for subsequently determining the target line identifier according to the target index.
[0094] In an exemplary embodiment, a line coverage determination flowchart is shown in Figure 9 , which includes:
[0095] (1) The computer device embeds the line coverage collection program into the program to be tested by a patching manner.
[0096] (2) After the program to be tested is started, the program to be tested is compiled to obtain a bytecode file corresponding to the program to be tested, a mapping relationship between source code line identifiers of source code lines in the program to be tested and bytecode instruction identifiers of bytecode instructions in the bytecode file is determined, and an index table CodeMap corresponding to the program to be tested is determined based on all the mapping relationships. Based on the bytecode instruction identifiers of the bytecode instructions in the bytecode file, an initial execution array LineBitOrigin is generated, and all elements in the execution array LineBitOrigin are 0.
[0097] (3) After the program to be tested starts to be tested, the running bytecode instructions are collected by the line coverage collection program, and the element value of the index same as the bytecode instruction identifier in the execution array LineBitTemp is assigned as 1.
[0098] (4) The line coverage is periodically calculated, including:
[0099] At the end of the first test time period, an initial reference array LineBitTemp is created, and all elements in the reference array LineBitTemp are 0.
[0100] For each target time period, the reference array LineBitTemp corresponding to the target time period is subjected to a bitwise NOT operation to obtain an updated reference array LineBitTemp, and the execution array LineBitOrigin corresponding to the target time period and the updated reference array LineBitTemp are subjected to a bitwise AND operation to obtain a new array LineBitDiff corresponding to the target time period. The index corresponding to the element with 1 in the new array is determined as a target index. For each target index, the source code line identifier corresponding to the target index in the index table is determined as a target line identifier corresponding to the target index.
[0101] The execution array LineBitOrigin corresponding to the target time period and the reference array LineBitTemp are subjected to a bitwise OR operation to obtain a reference array LineBitTemp corresponding to the next time period.
[0102] (5) Based on a preset sending interval, the determined target line identifier is sent to the server, the server performs statistics on the received target line identifier to obtain a new execution total number; each historical execution total number and the new execution total number are counted to obtain a current execution total number; the current execution total number is divided by the total number of code lines corresponding to the program to be tested to obtain a line coverage rate currently corresponding to the program to be tested, and a line coverage report is updated based on the current corresponding line coverage rate. The line coverage report is used for the tester to view. The preset sending interval can be equal to the time length of the target time period, or can be a multiple of the time length of the target time period.
[0103] In this embodiment, the execution array corresponding to the target time period and the reference data same as the execution array corresponding to the time period adjacent to the target time period are obtained, the target index corresponding to the bytecode instruction newly executed in the target time period is determined according to the execution array and the reference array, since the index table includes the correspondence between the bytecode instruction identifiers of each bytecode instruction in the bytecode file corresponding to the program to be tested and the source code line identifiers of the source code lines in the program to be tested, the target source code line identifier corresponding to the target source code line newly executed in the target time period can be determined according to the index table and the target index, and the total number of new executions corresponding to the target time period can be determined according to the target line identifier. Similarly, the historical execution total number corresponding to each historical time period before the target time period is determined based on the above method, and the line coverage corresponding to the end moment of the target time period of the program to be tested can be determined according to the total number of new executions and each historical execution total number. It can be understood that the line coverage of the program to be tested can be determined at the end of each time period, which improves the timeliness of line coverage determination compared with determining the line coverage at the end of the program to be tested. Moreover, the line coverage of the program to be tested is calculated during the test, which facilitates timely adjustment of the test cases of the program to be tested to improve the test efficiency.
[0104] It should be understood that, although each step in the flowchart involved in each embodiment as described above is displayed in sequence according to the arrow, these steps are not necessarily executed in sequence according to the arrow. Unless otherwise specified herein, the execution of these steps is not strictly limited in sequence, and these steps can be executed in other sequences. Moreover, at least part of the steps in the flowchart involved in each embodiment as described above can include multiple steps or stages, which are not necessarily executed at the same time, but can be executed at different times, and the execution sequence of these steps or stages is not necessarily sequential, but can be alternately executed with other steps or steps or stages in other steps.
[0105] Based on the same inventive concept, the embodiments of the present application also provide a line coverage determination device for implementing the line coverage determination method described above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme described in the above method, so the specific limitations in one or more line coverage determination device embodiments provided below can refer to the limitations of the line coverage determination method in the above text, which will not be repeated here.
[0106] In one embodiment, as Figure 10As shown, a line coverage determination apparatus is provided, comprising: an acquisition module 1002, an index determination module 1004, a line identification determination module 1006, and a line coverage determination module 1008, wherein:
[0107] The acquisition module 1002 is configured to acquire an execution array corresponding to a target time period and a reference array; the reference array is the same as an execution array corresponding to a previous adjacent time period of the target time period, and an element in the execution array represents an execution situation of a bytecode instruction corresponding to an index of the element;
[0108] The index determination module 1004 is configured to determine a target index corresponding to a newly executed bytecode instruction in the target time period based on the execution array and the reference array.
[0109] The line identification determination module 1006 is configured to determine a target line identification corresponding to a newly executed target source code line in the target time period based on an index table corresponding to the program to be tested and the target index.
[0110] The line coverage determination module 1008 is configured to determine a line coverage corresponding to the program to be tested based on the target line identification.
[0111] In one embodiment, the index determination module 1004 is further configured to: perform a bitwise NOT operation on the reference array to obtain an updated reference array; perform a bitwise AND operation on the execution array and the updated reference array to obtain a new array; and determine an index corresponding to an element with a first identification in the new array as a target index; the first identification represents that a bytecode instruction corresponding to the index of the first identification is executed in the target time period.
[0112] In one embodiment, the line identification determination module 1006 is further configured to: for each target index, determine a source code line identification corresponding to the target index in the index table as a target line identification corresponding to the target index; the index table includes a correspondence between an index and a source code line identification, the index is a bytecode instruction identification of a bytecode instruction in a bytecode file corresponding to the program to be tested, and the source code line identification is an identification of a source code line in the program to be tested.
[0113] In one embodiment, the line coverage determination module 1008 is further configured to: count target line identifications corresponding to the target time period to obtain a total number of newly executed in the target time period; count each historical execution total number and the total number of newly executed to obtain a current execution total number; and determine a line coverage corresponding to the program to be tested based on the current execution total number and a total number of code lines corresponding to the program to be tested.
[0114] In an embodiment, the obtaining module 1002 is further configured to: obtain an execution array and a reference array corresponding to a last adjacent time period of the target time period; perform a bitwise OR operation on the execution array and the reference array corresponding to the last adjacent time period to obtain the reference array corresponding to the target time period; and update the execution array corresponding to the last adjacent time period based on the bytecode instruction identifiers corresponding to the bytecode instructions executed in the target time period to obtain the execution array corresponding to the target time period.
[0115] In an embodiment, the obtaining module 1002 is further configured to: compile the program to be tested to obtain a bytecode file corresponding to the program to be tested; determine a mapping relationship between source code line identifiers of source code lines in the program to be tested and bytecode instruction identifiers of bytecode instructions in the bytecode file; and determine an index table corresponding to the program to be tested based on the mapping relationship.
[0116] The above modules in the line coverage determination apparatus can be implemented by software, hardware, or a combination thereof. The above modules can be embedded in or independent of a processor in a computer device in a hardware form, or stored in a memory in the computer device in a software form, so as to be called and executed by the processor to perform operations corresponding to the above modules.
[0117] In an embodiment, a computer device is provided, which can be a terminal. An internal structure diagram of the computer device can be as shown in FIG. 8. Figure 11The computer device shown in the figure includes a processor, a memory, an input / output interface, a communication interface, a display unit and an input device. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capability. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium to run. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with external terminals in a wired or wireless manner. The wireless manner can be realized through WIFI, mobile cellular network, NFC (near field communication) or other technologies. The computer program is executed by the processor to realize a line coverage determination method. The display unit of the computer device is used to form a visually visible picture, which can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer overlaid on the display screen, or a key, trackball or touchpad arranged on the shell of the computer device, or an external keyboard, touchpad or mouse, etc.
[0118] Those skilled in the art can understand that, Figure 11 The structure shown in the figure is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. The specific computer device can include more or fewer components than those shown in the figure, or combine certain components, or have a different component arrangement.
[0119] In one embodiment, a computer device is provided, including a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the steps in each of the above method embodiments.
[0120] In one embodiment, a computer readable storage medium is provided, which stores a computer program, and the computer program is executed by a processor to implement the steps in each of the above method embodiments.
[0121] In one embodiment, a computer program product is provided, including a computer program, and the computer program is executed by a processor to implement the steps in each of the above method embodiments.
[0122] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or authorized by all parties.
[0123] It can be understood by those skilled in the art that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing related hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium. When the computer program is executed, it can include the processes of the above-mentioned embodiments of each method. Any reference to memory, database or other medium used in the embodiments provided by the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical storage, high-density embedded non-volatile memory, resistive memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration but not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The database involved in the embodiments provided by the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a block chain, etc., without being limited thereto. The processor involved in the embodiments provided by the present application can be a general-purpose processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.
[0124] The technical features of the above embodiments can be combined in any way. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described, but as long as the combination of the technical features does not exist contradictory, it should be considered as the scope of the present application.
[0125] The above-described embodiments are merely illustrative of several embodiments of the present application, and the description is relatively specific and detailed, but should not be understood as a limitation on the scope of the patent. It should be noted that for those skilled in the art, without departing from the concept of the present application, a number of modifications and improvements can be made, which are all within the scope of the present application. Therefore, the scope of protection of the present application should be subject to the appended claims.
Claims
1. A method for determining row coverage, characterized in that, The method includes: Obtain the execution array and reference array corresponding to the target time period; the reference array is the same as the execution array corresponding to the previous adjacent time period of the target time period, and the elements in the execution array represent the execution status of the bytecode instruction corresponding to the index of the element; Perform logical operations on the execution array and the reference array to obtain a new array; determine the index corresponding to the element with the first identifier in the new array as the target index; For each target index, the source code line identifier corresponding to the target index in the index table corresponding to the program to be tested is determined as the target line identifier corresponding to the target index; the index table includes the correspondence between indexes and source code line identifiers; The target row identifiers corresponding to the target time period are statistically analyzed to obtain the total number of new executions corresponding to the target time period. Based on the total number of new executions corresponding to the target time period and the total number of historical executions corresponding to each historical time period, the row coverage rate corresponding to the program to be tested is determined.
2. The method according to claim 1, characterized in that, The step of performing logical operations on the execution array and the reference array to obtain a new array includes: Perform a bitwise NOT operation on the reference array to obtain the updated reference array; Perform a bitwise AND operation on the execution array and the updated reference array to obtain the new array.
3. The method according to claim 1, characterized in that, The index is the bytecode instruction identifier of the bytecode instruction in the bytecode file corresponding to the program under test, and the source code line identifier is the identifier of the source code line in the program under test.
4. The method according to claim 1, characterized in that, The process of determining the line coverage of the program under test based on the total number of new executions corresponding to the target time period and the total number of historical executions corresponding to each historical time period includes: The total number of executions in each historical period and the total number of new executions are statistically analyzed to obtain the current total number of executions. Based on the current total number of executions and the total number of lines of code corresponding to the program under test, the line coverage of the program under test is determined.
5. The method according to claim 1, characterized in that, The process of obtaining the execution array and reference array corresponding to the target time period includes: Obtain the execution array and reference array corresponding to the previous adjacent time period of the target time period; Perform a bitwise OR operation on the execution array and reference array corresponding to the previous adjacent time period to obtain the reference array corresponding to the target time period; Based on the bytecode instruction identifiers corresponding to the bytecode instructions executed within the target time period, the execution array corresponding to the previous adjacent time period is updated to obtain the execution array corresponding to the target time period.
6. The method according to claim 1, characterized in that, Before obtaining the execution array and reference array corresponding to the target time period, the method further includes: The program to be tested is compiled to obtain the bytecode file corresponding to the program to be tested; Determine the mapping relationship between the source code line identifier of the source code line in the program to be tested and the byte code instruction identifier of the byte code instruction in the byte code file; Based on the mapping relationship, the index table corresponding to the program to be tested is determined.
7. A row coverage determination device, characterized in that, The device includes: The acquisition module is used to acquire the execution array and reference array corresponding to the target time period; the reference array is the same as the execution array corresponding to the previous adjacent time period of the target time period, and the elements in the execution array represent the execution status of the bytecode instruction corresponding to the index of the element; The index determination module is used to perform logical operations on the execution array and the reference array to obtain a new array; and to determine the index corresponding to the element with the first identifier in the new array as the target index. The line identifier determination module is used to determine, for each target index, the source code line identifier corresponding to the target index in the index table corresponding to the program to be tested as the target line identifier corresponding to the target index; the index table includes the correspondence between indexes and source code line identifiers; The row coverage determination module is used to count the target row identifiers corresponding to the target time period to obtain the total number of new executions corresponding to the target time period, and determine the row coverage of the program to be tested based on the total number of new executions corresponding to the target time period and the total number of historical executions corresponding to each historical time period.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Code coverage rate processing method and device, server and storage medium
CN111290943A
Code coverage rate detection method and device
CN113836006A