Code coverage determination method, device, electronic device and storage medium

By testing and merging historical coverage for the changes in code, the problem of inefficiency of existing tools is solved, the efficiency and accuracy of code coverage is improved, and user-level testing of a single developer is realized.

CN114706749BActive Publication Date: 2024-12-24CHINANETCENT TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210204839.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-03
Publication Date
2024-12-24
Estimated Expiration
2042-03-03

AI Technical Summary

Technical Problem

The existing code coverage tool is inefficient, which affects the development progress, cannot be effectively tested against the code of a single developer, and requires re-determining of unmodified code parts.

Method used

By getting the source code and test the code, identifying the difference between it and the historical version, code testing only the changed parts, and combining it with the historical code coverage to get the full coverage of the source code.

Benefits of technology

Improves the efficiency of code coverage determination, reduces the amount and time required for testing, ensures the accuracy of coverage, and implements user-level code testing for individual developers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114706749B_ABST
    Figure CN114706749B_ABST
Patent Text Reader

Abstract

The embodiment of the present invention relates to the field of software testing technology, and discloses a method, device, electronic device and storage medium for determining code coverage. The method for determining code coverage includes: obtaining source code and test code; when the source code and test code have corresponding historical source code and historical test code, taking the basic blocks in the source code that are different from the historical source code and the basic blocks in the test code that are different from the historical test code as the first basic block; performing code testing according to the first basic block to obtain the first code coverage; merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code. This makes it possible to improve the efficiency of determining code coverage, thereby improving development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of software testing technology, and in particular to a method, device, electronic device and storage medium for determining code coverage. Background Art

[0002] Software testing is an important part of the software development process. Code coverage is a kind of software testing measurement result, which describes the proportion and degree of source code tested. At present, various code coverage tools have been produced, such as the GCOV tool provided by the GNU Compiler Collection (GCC).

[0003] However, the efficiency of developers using existing code coverage tools to determine code coverage is too low, which will seriously affect the development progress. Summary of the invention

[0004] The purpose of the embodiments of the present invention is to provide a method, device, electronic device and storage medium for determining code coverage, so as to improve the efficiency of determining code coverage and thus improve development efficiency.

[0005] To achieve the above-mentioned purpose, an embodiment of the present invention provides a method for determining code coverage, comprising: obtaining source code and test code, wherein the test code is used to perform code testing on the source code; when the source code and the test code have corresponding historical source code and historical test code, taking a basic block in the source code that is different from the historical source code and a basic block in the test code that is different from the historical test code as a first basic block; performing code testing according to the first basic block to obtain a first code coverage; merging the first code coverage with the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, wherein the second basic block is a basic block in which the source code is the same as the historical source code, and the corresponding test code part is also the same as the historical test code part, and the historical code coverage is a code coverage obtained by determining the code coverage of each basic block in the historical source code based on the historical test code.

[0006] To achieve the above-mentioned purpose, an embodiment of the present invention further provides a device for determining code coverage, comprising: an acquisition module, used to acquire source code and test code, wherein the test code is used to perform code testing on the source code; a determination module, used to, when the source code and the test code have corresponding historical source code and historical test code, take a basic block in the source code that is different from the historical source code and a basic block in the test code that is different from the historical test code as a first basic block; a testing module, used to perform code testing according to the first basic block to obtain a first code coverage; a merging module, used to merge the first code coverage with the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, wherein the second basic block is a basic block in which the source code is the same as the historical source code, and the corresponding test code part is also the same as the historical test code part, and the historical code coverage is a code coverage obtained by determining the code coverage of the historical source code based on the historical test code.

[0007] To achieve the above-mentioned purpose, an embodiment of the present invention also provides an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for determining code coverage as described above.

[0008] To achieve the above objectives, an embodiment of the present invention further provides a computer-readable storage medium storing a computer program, which implements the method for determining code coverage as described above when executed by a processor.

[0009] The method for determining code coverage provided by the embodiment of the present invention determines the first basic block after obtaining the source code and the test code for code testing the source code, and then, in the case where the source code and the test code have corresponding historical source code and historical test code, since the first basic block includes the basic block in the source code that is different from the historical source code and the basic block in the test code that is different from the historical test code, the first code coverage obtained by code testing the first basic block includes: the code coverage of the first basic block in the source code, and the code coverage of the basic block of the source code corresponding to the first basic block in the test code. In addition, since the second basic block is a basic block whose source code is the same as the historical source code and whose corresponding test code part is also the same as the historical test code part, that is, the test statement corresponding to the second basic block in the process of determining the code coverage of the source code and the process of determining the code coverage of the historical source code are the same, therefore, the code coverage of the second basic block determined in the source code and the historical source code is the same, that is, the historical code coverage corresponding to the second basic block is the code coverage obtained by code testing the second basic block in the source code, that is, the historical code coverage corresponding to the second basic block is feasible and accurate as the code coverage of the second basic block in the source code. Therefore, merging the first code coverage and the historical code coverage corresponding to the second basic block means merging the code coverage of the first basic block in the source code, the code coverage of the basic block corresponding to the first basic block in the test code in the source code, and the code coverage of the second basic block. Moreover, according to the meaning of the basic blocks corresponding to the first basic block, the second basic block and the first basic block in the test code in the source code, it can be known that the basic blocks corresponding to the first basic block, the second basic block and the first basic block in the test code in the source code together constitute the source code, that is, the historical code coverage corresponding to the first code coverage and the second basic block are merged to obtain the complete code coverage of the source code. It can be seen that in this embodiment, when determining the code coverage of the currently acquired source code, code testing is only performed on the first basic block that has changed relative to the historical source code and the historical test code, that is, code testing is only performed on the source code portion whose code coverage may change, and the historical code coverage corresponding to the second basic block is merged on the basis of the first code coverage. What is actually merged is the accurate code coverage of the second basic block that has not changed relative to the historical source code and the historical test code, that is, the amount of code that needs to be code tested is reduced, thereby improving the efficiency of determining the code coverage and ensuring the accuracy of the determined code coverage.In addition, since only the modified code parts are tested without considering the implementation of other parts, the part modified by a developer can be used as the first basic block, and the code written by other developers (regardless of whether it is modified) can be used as the second basic block by default, and finally the code coverage determination for a single user is realized, that is, user-level code testing, rather than project-level code testing. The efficiency of determining the code coverage of a single developer is improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.

[0011] Figure 1 is a flow chart of a method for determining code coverage provided in one embodiment of the present invention;

[0012] Figure 2 is a flowchart of a method for determining code coverage including a step of outputting code coverage of source code provided in another embodiment of the present invention;

[0013] Figure 3 is a flowchart of a method for determining code coverage including a format conversion step provided in another embodiment of the present invention;

[0014] Figure 4 is a flowchart of a method for determining code coverage including a backup step provided in another embodiment of the present invention;

[0015] Figure 5 is a flowchart of a method for determining code coverage including a deletion step provided in another embodiment of the present invention;

[0016] Figure 6 is a schematic structural diagram of a device for determining code coverage provided in another embodiment of the present invention;

[0017] Figure 7 It is a schematic structural diagram of an electronic device provided in another embodiment of the present invention. DETAILED DESCRIPTION

[0018] As can be seen from the background technology, there is an urgent need to provide an efficient code coverage determination method to solve the problem that the existing code coverage tools are inefficient and affect the development progress.

[0019] After analysis, it was found that the main reason for the above-mentioned low efficiency problem is that the development process is likely to involve multiple developers and a large number of code files, and the code is usually modified many times. After each modification, the code coverage tool will clear the previously determined code coverage and then re-execute the steps of determining code coverage for all codes written by the developer. It cannot determine the code of a specific developer, and needs to re-determine the unchanged code part.

[0020] To solve the above problems, an embodiment of the present invention provides a method for determining code coverage, including: obtaining source code and test code, wherein the test code is used to perform code testing on the source code; when the source code and the test code have corresponding historical source code and historical test code, taking a basic block in the source code that is different from the historical source code and a basic block in the test code that is different from the historical test code as a first basic block; performing code testing according to the first basic block to obtain a first code coverage; merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, wherein the second basic block is a basic block in the source code that is not determined by the first code coverage, and the historical code coverage is a code coverage obtained by determining the code coverage of each basic block in the historical source code based on the historical test code.

[0021] The method for determining code coverage provided by the embodiment of the present invention, after obtaining source code and test code for code testing the source code, performs code testing based on the first basic block in the source code that is different from the historical source code and the first basic block in the test code that is different from the historical test code, that is, performs code testing based on the source code portion that has been changed relative to the historical source code and the test code portion that has been changed relative to the historical test code, to obtain a first code coverage, and then merges the first code coverage obtained based on the first basic block with the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code. That is, when determining the code coverage of the currently obtained source code, only the first basic block that has been changed relative to the historical source code and the historical test code is tested, and the second basic block that has not been changed is not tested. In order to obtain the complete code coverage of the source code, the historical code coverage of the second basic block obtained when determining the code coverage of the historical source code is used as the coverage of the currently obtained source code. Compared with performing code testing on the entire source code from beginning to end every time, the amount of code that needs to be tested in this embodiment is reduced, and thus the time required for code testing is reduced. Since the second basic block is a basic block in the source code that is not determined by the first code coverage, that is, the second basic block is not modified and the code portion in the test code corresponding to the second basic block is not modified, therefore, whether the second basic block is used as part of the historical source code or as part of the source code, the tested code and the test code are the same when determining the code coverage, and thus the obtained code coverage is also the same, that is, the historical code coverage corresponding to the second basic block is used as its code coverage in the source code. It is accurate. In addition, since only the modified code portion is tested, and the implementation of other portions does not need to be considered, the portion modified by a certain developer can be used as the first basic block, and the code written by other developers (whether modified or not) can be used as the second basic block, and finally the code coverage determination for a single user is achieved, that is, user-level code testing, rather than project-level code testing. The efficiency of determining the code coverage of a single developer is improved.

[0022] In order to make the purpose, technical scheme and advantages of the embodiments of the present invention clearer, the embodiments of the present invention will be described in detail below with reference to the accompanying drawings. However, it can be understood by those skilled in the art that in the embodiments of the present invention, many technical details are provided to enable readers to better understand the present invention. However, even without these technical details and various changes and modifications based on the following embodiments, the technical scheme claimed in the present invention can be implemented.

[0023] The following embodiments are divided for the convenience of description and shall not constitute any limitation on the specific implementation of the present invention. The embodiments may be combined with each other and referenced to each other without contradiction.

[0024] An embodiment of the present invention provides a method for determining code coverage, which is applied to electronic devices such as computers and servers. The method for determining code coverage provided in this embodiment is as follows: Figure 1 As shown, at least the following steps are included:

[0025] Step 101, obtaining source code and test code, where the test code is used to perform code testing on the source code.

[0026] In this embodiment, the source code may be any code for which code coverage needs to be determined, and the test code is code used to perform code testing on the source code. For example, the source code may be code corresponding to a function implemented by one or several code developers in a code development project, or may be a code file, or may be a complete code corresponding to a development project, etc.

[0027] It should be noted that the source code and test code can be written in any programming language. In particular, even if the programming languages ​​corresponding to the source code and test code are inconsistent, when performing code testing, they can be expressed in the same programming language through compilation and other methods. Therefore, there is no limitation on the programming language of the source code and test code.

[0028] In some examples, the source code and test code are stored on the personal device of the code developer. Accordingly, obtaining the test code of the source code can be achieved in the following way: reading the local address of the source code and test code uploaded by the code developer; and searching for the source code and test code in the local storage of the code developer according to the read local address.

[0029] In other examples, the code developer actively uploads the source code and the test code. Accordingly, obtaining the test code of the source code can be achieved in the following manner: reading the source code and the test code uploaded by the code developer.

[0030] Of course, the above is only a specific example. In other examples, the code developer can also specify the code file where the source code is located, directly read the code file to obtain the source code and find the test code corresponding to the source code, which will not be described here one by one.

[0031] Step 102 , when the source code and the test code have corresponding historical source code and historical test code, take the basic blocks in the source code that are different from the historical source code and the basic blocks in the test code that are different from the historical test code as the first basic blocks.

[0032] It is understandable that the entire development process is likely to require multiple code modifications and adjustments, and the modifications and adjustments may be to the source code, the test code, or both. Therefore, it is necessary to perform code testing multiple times, that is, to determine software test metrics such as code coverage multiple times. Each determination of code coverage is based on the currently acquired source code and test code, and at least one basic block in at least one of the currently acquired source code and test code will change relative to the source code and test code used in any code coverage determination process before the current code coverage determination. At this time, the source code and test code used in a code coverage determination process before the current code coverage determination can be used as historical source code and historical test code.

[0033] In this embodiment, the source code, historical source code, test code and historical test code all include several basic blocks. The basic blocks in the source code that are different from the historical source code refer to the basic blocks in the source code that cannot be matched to the basic blocks in the historical source code. The basic blocks in the test code that are different from the historical test code refer to the basic blocks in the test code that cannot be matched to the basic blocks in the historical test code. Among them, two basic blocks match means that the statements in the two basic blocks are the same, and two basic blocks do not match means that the statements in the two basic blocks are not completely the same.

[0034] That is to say, taking the basic blocks in the source code that are different from the historical source code and the basic blocks in the test code that are different from the historical test code as the first basic block essentially means: for each basic block in the source code, matching is performed in the historical source code respectively, so as to determine which basic blocks in the source code are matched successfully and which basic blocks in the source code are matched unsuccessfully, and then taking the basic blocks in the source code that are matched unsuccessfully as the first basic blocks; and, for each basic block in the test code, matching is performed in the historical test code respectively, so as to determine which basic blocks in the test code are matched successfully and which basic blocks in the test code are matched unsuccessfully, and then taking the basic blocks in the test code that are matched unsuccessfully as the first basic blocks.

[0035] It is understandable that there is a correlation between the source code and the historical source code, and there is a correlation between the test code and the historical test code. For example, the source code can be obtained by modifying and adjusting the historical source code. Therefore, when the source code and the test code have corresponding historical source code and historical test code, the code coverage of the source code can be determined based on the source code and the test code. The code coverage of the historical source code obtained based on the historical source code and the historical test code can be referred to, and the code coverage can be determined by performing code testing on the source code part that cannot provide a reference for the code coverage of the historical source code obtained based on the historical source code and the historical test code, thereby reducing the amount of code to be tested and improving the efficiency of determining the code coverage of the source code. The source code part that cannot provide a reference for the code coverage of the historical source code obtained based on the historical source code and the historical test code includes the basic blocks in the source code that have changed relative to the historical source code, and the basic blocks in the source code that the test code has changed relative to the basic blocks in the historical test code. Correspondingly, when the source code and the test code do not have corresponding historical source code and historical test code, since no reference can be obtained, it is necessary to perform code testing on all the source code from beginning to end. That is to say, when the source code and the test code do not have corresponding historical source code and historical test code, code testing is performed on the source code according to the test code to obtain the code coverage of the source code.

[0036] Therefore, in other embodiments, after the source code and test code are currently obtained, it is judged whether the obtained source code and test code have corresponding historical source code and historical test code, so that when it is judged that the obtained source code and test code have corresponding historical source code and historical test code, the first basic block is determined, and the implementation continues according to step 103 and step 104 provided in this embodiment; when it is judged that the obtained source code and test code do not have corresponding historical source code and historical test code, the source code is tested according to the test code to obtain the code coverage of the source code. In particular, it is judged whether the obtained source code and test code have corresponding historical source code and historical test code, mainly to judge whether the original source code corresponding to the source code has determined the code coverage. It is judged whether the obtained source code and test code have corresponding historical source code and historical test code, and when counting the number of times the code coverage is determined for the source code obtained based on the same original source code, it can be achieved by judging whether the count value is 0; when the code coverage determination result obtained based on the same source code is stored in a designated storage space, it can be achieved by judging whether the storage content of the storage space is empty.

[0037] It should be noted that in this embodiment, the process of determining code coverage corresponding to the historical source code and the historical test code is not limited to the process of determining code coverage last time before the current process of determining code coverage, but is described as a process of determining code coverage before the current process of determining code coverage. This is because the modification and adjustment of the source code and the test code are uncertain. Therefore, the source code and the test code obtained in the process of determining code coverage last time may not be the least different from the source code and the test code obtained in the process of determining code coverage currently. The difference between the source code and the test code obtained in the process of determining code coverage currently will affect the code coverage efficiency provided by this embodiment. Taking the source code as an example, the source code obtained in the sixth process of determining the code coverage is closest to the source code obtained in the first process of determining the code coverage among the source codes obtained in the first to fifth processes of determining the code coverage. At this time, compared with using the source code obtained in the 2nd to 5th processes of determining the code coverage as the historical source code, using the source code obtained in the first process of determining the code coverage as the historical source code will result in fewer first basic blocks. Therefore, when code testing is performed based on the first basic blocks in step 103, fewer basic blocks need to be tested, and the efficiency of determining the first code coverage is higher. That is, the first code coverage is improved by selecting a suitable historical source code. Finally, by improving the efficiency of determining the first code coverage, the efficiency of determining the code coverage of the source code is improved accordingly.

[0038] Therefore, in other embodiments, when there are multiple historical source codes, the source code can also be matched with the multiple historical source codes, and the historical source code with the highest matching degree and its corresponding historical test code can be used as the historical source code and historical test code used when determining the first basic block; or, the source code and the test code can be matched with the historical source code and the historical test code respectively at the same time, and the historical source code and the historical test code with the highest overall matching degree can be used as the historical source code and the historical test code used when determining the first basic block, etc., which will not be described in detail here.

[0039] Step 103: Perform code testing according to the first basic block to obtain a first code coverage rate.

[0040] In this embodiment, the first basic block includes a first basic block that exists in the source code and is different from the historical source code, and a first basic block that exists in the test code and is different from the historical test code. For the first basic block that exists in the source code and is different from the historical source code, since its own code is not included in the historical source code, the historical code coverage of the historical source code will not include the code coverage of the basic block, and it is necessary to determine the code coverage of the basic block through code testing; for the first basic block that exists in the test code and is different from the historical test code, since its test logic is not included in the historical test code, it is necessary to perform code testing on the corresponding test code part based on the test logic that is different from the historical test code, and re-determine the code coverage of the corresponding basic block in the corresponding source code.

[0041] Therefore, in some examples, when there is a first basic block in the source code that is different from the historical source code, code testing is performed according to the first basic block to obtain a first code coverage rate, which is achieved in the following manner: code testing is performed on the first basic block in the source code according to the corresponding code in the test code to obtain a first code coverage rate in the first basic block in the source code. That is, after searching for the corresponding code in the test code according to the first basic block in the source code, code testing is performed on the corresponding first basic block in the source code using the determined code to obtain a first code coverage rate for the first basic block in the source code.

[0042] In some examples, when there is a first basic block in the test code that is different from the historical test code, code testing is performed according to the first basic block to obtain a first code coverage rate, which is achieved in the following manner: code testing is performed on the corresponding code in the source code according to the first basic block in the test code to obtain a first code coverage rate of the corresponding code in the source code. That is, after searching for the corresponding code in the source code according to the first basic block in the test code, code testing is performed on the corresponding code in the source code using the first basic block in the test code to obtain a first code coverage rate of the determined code in the source code.

[0043] Step 104, merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, wherein the second basic block is a basic block whose source code is the same as the historical source code and whose corresponding test code part is also the same as the historical test code part, and the historical code coverage is the code coverage obtained by determining the code coverage of each basic block in the historical source code based on the historical test code.

[0044] In this embodiment, the first code coverage rate to be merged includes all the first code coverage rates determined by step 103, that is, the first code coverage rate to be merged includes: the code coverage rate of the first basic block in the source code and the code coverage rate of the basic block in the source code corresponding to the first basic block in the test code. The second basic block is a basic block whose source code is the same as the historical source code and whose corresponding test code part is also the same as the historical test code part. Since the first basic block in the source code is a basic block different from the source code and the historical source code, the basic block corresponding to the first basic block in the source code that is different from the test code and the historical test code is a basic block whose corresponding test code part is different from the historical code part in the source code. Therefore, the union of the second basic block and the first basic block in the source code that is different from the historical source code and the basic block corresponding to the first basic block in the source code that is different from the historical source code and the basic block in the source code that is different from the historical test code is the source code. Then, when the historical code coverage rate corresponding to the second basic block is used as the current code coverage rate of the second basic block and the merged first code coverage rate includes the code coverage rate of the first basic block in the source code and the code coverage rate of the basic block in the source code corresponding to the first basic block in the test code, merging the first code coverage rate and the historical code coverage rate corresponding to the second basic block is actually merging the code coverage rates of different parts of the source code, and the complete code coverage rate of the source code is obtained. Among them, since the second basic block is a basic block in the source code that is not determined by the first code coverage rate, that is, the second basic block has not been modified and the code part in the test code corresponding to the second basic block has not been modified, therefore, whether the second basic block is used as part of the historical source code or as part of the source code, the tested code and the test code are the same when determining the code coverage rate, and the obtained code coverage rate is also the same, that is, the historical code coverage rate corresponding to the second basic block is accurate as its code coverage rate in the source code.

[0045] Another aspect of the present invention is to provide a method for determining code coverage, which is applied to electronic devices such as computers and servers. The method for determining code coverage provided in this embodiment is as follows: Figure 2 As shown, it also includes at least the following steps:

[0046] Step 105: Add content for outputting code coverage, where the content for outputting code coverage includes code coverage result output code or a newly added code coverage display toolkit.

[0047] In this embodiment, the addition of code coverage result output code can be achieved by adding code to the test code to implement the output code coverage function, or by adding new compilation parameters during the compilation process; the addition of a new code coverage display toolkit can be achieved by packaging the corresponding coverage toolkit during the compilation process, which will not be described in detail here.

[0048] Step 106 , calling the content for outputting code coverage, and outputting the code coverage of the source code.

[0049] Another aspect of the present invention is to provide a method for determining code coverage, which is applied to electronic devices such as computers and servers. The method for determining code coverage provided in this embodiment is as follows: Figure 3 As shown, at least the following steps are included:

[0050] Step 107, converting the output result into a hypertext markup language HTML format.

[0051] In this embodiment, a preset HyperText Markup Language (HTML) formatting tool can be called, or a preset format conversion code can be called to output the result in a non-HTML format so that it can be presented to the code developer through a browser, etc., so that it can be determined whether to modify and how to modify the source code and / or test code.

[0052] It should be noted that this embodiment does not limit the method for implementing HTML format conversion, and it can be any implementation method that can convert non-HTML format text into HTML format.

[0053] Step 108: Display the output result after format conversion.

[0054] In this embodiment, the data results after format conversion can be presented on a personal device of a code developer, or on an electronic device that implements the method for determining code coverage provided in this embodiment, etc.

[0055] Another aspect of the present invention is to provide a method for determining code coverage, which is applied to electronic devices such as computers and servers. The method for determining code coverage provided in this embodiment is as follows: Figure 4 As shown, at least the following steps are included:

[0056] Step 109: back up the source code.

[0057] In this embodiment, after the code coverage of the source code is determined, the source code actually becomes the historical source code, which is relative to the code coverage determination process that will occur next time. In order to facilitate the provision of the historical source code in the next code coverage determination process, it is also desired to ensure the security of the historical source code. Therefore, the source code can be backed up to ensure its information security.

[0058] In some examples, source code may be backed up on an electronic device that implements the method for determining code coverage provided by this embodiment.

[0059] In other examples, source code may be backed up on the personal devices of the code developer.

[0060] Another aspect of the present invention is to provide a method for determining code coverage, which is applied to electronic devices such as computers and servers. The method for determining code coverage provided in this embodiment is as follows: Figure 5 As shown, it also includes at least the following steps:

[0061] Step 110, delete historical source code, historical test code and historical code coverage.

[0062] In this embodiment, after the code coverage of the currently acquired source code is determined, the source code actually becomes a historical source code, which is relative to the code coverage determination process that will occur next time. That is to say, in the next code coverage determination process, the historical source code that can be provided includes the currently acquired source code and the historical source code of the currently acquired source code. Deleting the historical source code of the currently acquired source code and its related information, i.e., the historical test code and the historical code coverage, can still ensure that in the next code coverage determination process, the historical source code, the historical test code and the historical code coverage are still available. At the same time, due to the timely deletion of the historical source code, the historical test code and the historical code coverage, the cache occupancy is reduced.

[0063] Among them, if the historical source code, historical test code and historical code coverage are deleted each time after the code coverage of the currently acquired source code is determined, then each time the code coverage of the currently acquired source code is to be determined, there is only one set of available historical source code, historical test code and historical code coverage, namely, the source code, test code and code coverage of the source code obtained in the last process of determining the code coverage of the source code before the current process of determining the code coverage of the source code.

[0064] Of course, in other embodiments, the historical source code, historical test code, and historical code coverage may not be deleted, thereby retaining more historical source code, historical test code, and historical code coverage. In this way, when implementing step 102, a group of historical source code and historical test code with a smaller difference from the currently acquired source code and / or test code can be selected when determining the first basic block, thereby achieving the effect of further improving the efficiency of determining the code coverage as described above.

[0065] In order to facilitate those skilled in the art to better understand the method for determining the code coverage provided in the above embodiment, the following will take the source code and the test code as both in C language, the lcov tool as the content for outputting the code coverage, and the method for determining the code coverage implemented based on the GNU Compiler Collection (gcc) tool as an example for explanation.

[0066] Add the parameter "--coverage-fprofile-dir= / var / gcov / test" for gcc to generate code coverage to the compilation parameters, where / var / gcov / test in "--coverage-fprofile-dir= / var / gcov / test" represents the directory where the coverage binary file is saved, that is, the location where the source code and test code are saved, and "--coverage-fprofile-dir= / var / gcov / test" means that the generated code coverage data is written to the files in the " / var / gcov / test" directory, so that the coverage function is automatically added when the code is tested. That is, as long as the code corresponding to gcov_test.c is run, the binary coverage information file gcov_test.gcda of gcov_test.c executed will be generated in the / var / gcov / test directory. It can be understood that in other examples, the / var / gcov / test directory can be adjusted to other representations as needed, which will not be repeated here.

[0067] Furthermore, assuming that the source code is a file named gcov_test.c, the first basic block is determined and code testing is performed based on the first basic block. Then, the command line "lcov -cd . / -b . / -o A.info" is called to convert the obtained first coverage rate into a coverage rate text file, namely, a file named A.info. The file will include information about the execution of each statement in each basic block during code testing, which may specifically include the following content:

[0068] SF: / home / test / gcov_test.c

[0069] FN:10,func1

[0070] FN:15,func2

[0071] FNDA:1,func1

[0072] FNDA:1,func2 ...

[0074] DA:13,1

[0075] DA:16,1

[0076] DA:17,1

[0077] end_of_record

[0078] That is, a determination of the code coverage of the first implementation is obtained.

[0079] Then the first code coverage and the historical code coverage are merged in the following way, where the first code coverage is represented as an A.info file and the historical code coverage is represented as a B.info file: determine that there is a B.info file currently, where the B.info file represents the file of the historical source code and its corresponding code coverage, read A.info, where the A.info file represents ***, traverse each data line starting with SF:, read the data between SF: and end_of_record to continue the following loop. Traverse each FN, record the line number after the current FN and the line number after the next FN, read all the data starting with DA between these line numbers, assuming that the first FN is between [10,15), excluding line 15. The DA data between 10-14 are: DA:13,1. Then continue to read the B.info file, find the data starting with DA:13 between SF: / home / test / gcov_test.c and end_of_record, then open the gcov_test.c and gcov_test.c.bak files respectively and read the 13th line of program code data for comparison. If they are the same, add the number after DA:13 in B.info to DA:13 in A.info, and add the 1 in FNDA:1, func1 in A.info to the number on the corresponding FNDA:1, func1 line in B.info. If the 13th lines of the two code files are different, the accumulated data will not be merged. Finally, A.info representing the code coverage of the test code is obtained. Then delete B.info and gcov_test.c.bak, rename A.info to B.info, and delete the historical code coverage in time to avoid occupying storage space. At the same time, copy the gcov_test.c file to gcov_test.c.bak for backup, that is, backup the current source code for the next time you determine the code coverage. You can also call the command line "genhtml-f B.info-o result" to get the corresponding html file for the newly generated B.info each time you determine the code coverage, so that you can intuitively view the code coverage results in the browser.

[0080] The step division of the above methods is only for the purpose of clear description. When implemented, they can be combined into one step or some steps can be split and decomposed into multiple steps. As long as they include the same logical relationship, they are all within the scope of protection of this patent; adding insignificant modifications to the algorithm or process or introducing insignificant designs without changing the core design of the algorithm and process are all within the scope of protection of this patent.

[0081] Another aspect of the present invention is to provide a device for determining code coverage. Figure 6 As shown, including:

[0082] The acquisition module 601 is used to acquire source code and test code, and the test code is used to perform code testing on the source code.

[0083] The determination module 602 is used to, when the source code and the test code have corresponding historical source code and historical test code, take the basic blocks in the source code that are different from the historical source code and the basic blocks in the test code that are different from the historical test code as the first basic blocks.

[0084] The testing module 603 is used to perform code testing according to the first basic block to obtain a first code coverage.

[0085] The merging module 604 is used to merge the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, wherein the second basic block is a basic block in which the source code is the same as the historical source code, and the corresponding test code part is also the same as the historical test code part, and the historical code coverage is the code coverage obtained by determining the code coverage of the historical source code based on the historical test code.

[0086] It is not difficult to find that this embodiment is a device embodiment corresponding to the method embodiment, and this embodiment can be implemented in conjunction with the method embodiment. The relevant technical details mentioned in the method embodiment are still valid in this embodiment, and in order to reduce repetition, they are not repeated here. Accordingly, the relevant technical details mentioned in this embodiment can also be applied in the method embodiment.

[0087] It is worth mentioning that all modules involved in this embodiment are logic modules. In practical applications, a logic unit can be a physical unit, a part of a physical unit, or a combination of multiple physical units. In addition, in order to highlight the innovative part of the present invention, this embodiment does not introduce units that are not closely related to solving the technical problem proposed by the present invention, but this does not mean that there are no other units in this embodiment.

[0088] Another aspect of the present invention is to provide an electronic device, such as Figure 7 As shown, it includes: at least one processor 701; and a memory 702 that is communicatively connected to the at least one processor 701; wherein the memory 702 stores instructions that can be executed by the at least one processor 701, and the instructions are executed by the at least one processor 701 so that the at least one processor 701 can execute the method for determining the code coverage described in any of the above method embodiments.

[0089] The memory 702 and the processor 701 are connected in a bus manner, and the bus may include any number of interconnected buses and bridges, and the bus connects various circuits of one or more processors 701 and the memory 702 together. The bus can also connect various other circuits such as peripheral devices, voltage regulators, and power management circuits, which are well known in the art and are therefore not further described herein. The bus interface provides an interface between the bus and the transceiver. The transceiver can be one element or multiple elements, such as multiple receivers and transmitters, providing a unit for communicating with various other devices on a transmission medium. The data processed by the processor 701 is transmitted on a wireless medium via an antenna, and further, the antenna also receives data and transmits the data to the processor 701.

[0090] The processor 701 is responsible for managing the bus and general processing, and can also provide various functions, including timing, peripheral interfaces, voltage regulation, power management and other control functions. The memory 702 can be used to store data used by the processor 701 when performing operations.

[0091] Another aspect of the present invention is to provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the method for determining code coverage described in any of the above method embodiments.

[0092] That is, those skilled in the art can understand that all or part of the steps in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a program, and the program is stored in a storage medium, including several instructions for making a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0093] Those skilled in the art will appreciate that the above embodiments are specific embodiments for implementing the present invention, and that in actual applications, various changes may be made in form and detail without departing from the spirit and scope of the present invention.

Claims

1. A method for determining code coverage, characterized in that: include: Obtaining source code and test code, wherein the test code is used to perform code testing on the source code; In a case where the source code and the test code have corresponding historical source codes and historical test codes, taking a basic block in the source code that is different from the historical source code and a basic block in the test code that is different from the historical test code as a first basic block; Perform code testing according to the first basic block to obtain a first code coverage rate; The first code coverage rate is combined with the historical code coverage rate corresponding to the second basic block to obtain the code coverage rate of the source code, wherein the second basic block is a basic block in which the source code is the same as the historical source code, and the corresponding test code part is also the same as the historical test code part, and the historical code coverage rate is a code coverage rate obtained by determining the code coverage rate of each basic block in the historical source code based on the historical test code; The basic blocks in the source code that are different from the historical source code refer to the basic blocks in the source code that cannot be matched to the basic blocks in the historical source code; the basic blocks in the test code that are different from the historical test code refer to the basic blocks in the test code that cannot be matched to the basic blocks in the historical test code; two basic blocks match means that the statements in the two basic blocks are the same; two basic blocks do not match means that the statements in the two basic blocks are not completely the same; The second basic block is not modified and a code portion in the test code corresponding to the second basic block is also not modified.

2. The method for determining code coverage according to claim 1, characterized in that: The performing code testing according to the first basic block to obtain a first code coverage rate includes: When the source code contains the first basic block which is different from the historical source code, code testing is performed on the first basic block in the source code according to the corresponding code in the test code to obtain the first code coverage of the first basic block in the source code; or In a case where the test code contains the first basic block that is different from the historical test code, code testing is performed on the corresponding code in the source code according to the first basic block of the test code to obtain the first code coverage of the corresponding code in the source code.

3. The method for determining code coverage according to claim 1 or 2, characterized in that: After obtaining the source code and the test code, the method further includes: In the case that the source code and the test code do not have corresponding historical source code and historical test code, code testing is performed on the source code according to the test code to obtain code coverage of the source code.

4. The method for determining code coverage according to claim 1 or 2, characterized in that: Before performing code testing according to the first basic block to obtain a first code coverage, the method further includes: Adding content for outputting code coverage, wherein the content for outputting code coverage includes code coverage result output code or a new code coverage display toolkit; After merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, the method further includes: The content for outputting code coverage is called to output the code coverage of the source code.

5. The method for determining code coverage according to claim 4, characterized in that: After outputting the code coverage of the source code, the method further includes: Convert the output results into the Hypertext Markup Language HTML format; Display the output result after format conversion.

6. The method for determining code coverage according to claim 1 or 2, characterized in that: After merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, the method further includes: Back up the source code.

7. The method for determining code coverage according to claim 1 or 2, characterized in that: After merging the first code coverage and the historical code coverage corresponding to the second basic block to obtain the code coverage of the source code, the method further includes: The historical source code, the historical test code, and the historical code coverage are deleted.

8. A device for determining code coverage, characterized in that: include: An acquisition module, used for acquiring source code and test code, wherein the test code is used for performing code testing on the source code; a determination module, configured to, when the source code and the test code have corresponding historical source codes and historical test codes, use a basic block in the source code that is different from the historical source code and a basic block in the test code that is different from the historical test code as a first basic block; A testing module, configured to perform code testing according to the first basic block to obtain a first code coverage rate; a merging module, configured to merge the first code coverage rate with the historical code coverage rate corresponding to the second basic block to obtain the code coverage rate of the source code, wherein the second basic block is a basic block in which the source code is the same as the historical source code and the corresponding test code portion is also the same as the historical test code portion, and the historical code coverage rate is a code coverage rate obtained by determining the code coverage rate of the historical source code based on the historical test code; The basic blocks in the source code that are different from the historical source code refer to the basic blocks in the source code that cannot be matched to the basic blocks in the historical source code; the basic blocks in the test code that are different from the historical test code refer to the basic blocks in the test code that cannot be matched to the basic blocks in the historical test code; two basic blocks match means that the statements in the two basic blocks are the same; two basic blocks do not match means that the statements in the two basic blocks are not completely the same; The second basic block is not modified and a code portion in the test code corresponding to the second basic block is also not modified.

9. An electronic device, characterized in that: include: at least one processor; as well as, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method for determining code coverage according to any one of claims 1 to 7.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method for determining code coverage according to any one of claims 1 to 7 is implemented.