CN114706749A - Method and device for determining code coverage rate, electronic equipment and storage medium - Google Patents

Method and device for determining code coverage rate, electronic equipment and storage medium Download PDF

Info

Publication number
CN114706749A
CN114706749A CN202210204839.1A CN202210204839A CN114706749A CN 114706749 A CN114706749 A CN 114706749A CN 202210204839 A CN202210204839 A CN 202210204839A CN 114706749 A CN114706749 A CN 114706749A
Authority
CN
China
Prior art keywords
code
test
historical
basic block
source
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210204839.1A
Other languages
Chinese (zh)
Inventor
朱智佳
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Wangsu Science and Technology Co Ltd
Original Assignee
Wangsu Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Wangsu Science and Technology Co Ltd filed Critical Wangsu Science and Technology Co Ltd
Priority to CN202210204839.1A priority Critical patent/CN114706749A/en
Publication of CN114706749A publication Critical patent/CN114706749A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3692Test management for test results analysis

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The embodiment of the invention relates to the technical field of software testing, and discloses a method and a device for determining code coverage rate, electronic equipment and a storage medium. The method for determining the code coverage rate comprises the following steps: acquiring a source code and a test code; under the condition that the source code and the test code have corresponding historical source codes and historical test codes, taking a basic block which is different from the historical source codes in the source code and a basic block which is different from the historical test codes in the test code as a first basic block; performing code testing according to the first basic block to obtain a first code coverage rate; and combining the first code coverage rate and the historical code coverage rate corresponding to the second basic block to obtain the code coverage rate of the source code. So that the efficiency of determining code coverage can be improved, thereby improving development efficiency.

Description

Method and device for determining code coverage rate, electronic equipment and storage medium
Technical Field
The embodiment of the invention relates to the technical field of software testing, in particular to a method and a device for determining code coverage rate, electronic equipment and a storage medium.
Background
Software testing is an important part of the software development process, wherein code coverage is one of the software testing metrics, which describes the proportion and degree of the source code being tested. Various code coverage tools have been generated, such as the GCOV tool provided by the GNU Compiler Collection (GCC), among others.
However, the efficiency of determining the code coverage by the developer by using the existing code coverage tool is too low, and the development progress is seriously influenced.
Disclosure of Invention
An object of the embodiments of the present invention is to provide a method and an apparatus for determining code coverage, an electronic device, and a storage medium, so that efficiency of determining code coverage can be improved, thereby improving development efficiency.
To achieve the above object, an embodiment of the present invention provides a method for determining code coverage, including: acquiring a source code and a test code, wherein the test code is used for carrying out code test on the source code; under the condition that the source code and the test code have corresponding historical source codes and historical test codes, taking a basic block which is different from the historical source codes in the source code and a basic block which is different from the historical test codes in the test code as a first basic block; performing code testing according to the first basic block to obtain a first code coverage rate; and combining the first code coverage rate and a historical code coverage rate corresponding to a 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, the corresponding test code part is a basic block in which the test code part is also the same as the historical test code part, and the historical code coverage rate is determined by performing code coverage rate on each basic block in the historical source code based on the historical test code.
In order to achieve the above object, an embodiment of the present invention further provides a device for determining code coverage, including: the system comprises an acquisition module, a test module and a control module, wherein the acquisition module is used for acquiring a source code and a test code, and the test code is used for carrying out code test on the source code; a determining module, configured to, when the source code and the test code have corresponding historical source codes and historical test codes, take a basic block in the source code that is different from the historical source codes and a basic block in the test code that is different from the historical test codes as a first basic block; the test module is used for carrying out code test according to the first basic block to obtain a first code coverage rate; a merging module, configured to merge the first code coverage and a historical code coverage corresponding to a second basic block to obtain a code coverage of the source code, where the second basic block is a basic block in which the source code is the same as the historical source code, the corresponding test code portion is also the same as the historical test code portion, and the historical code coverage is a code coverage determined by performing code coverage on the historical source code based on the historical test code.
To achieve the above object, an embodiment of the present invention further provides an electronic device, including: at least one processor; and a memory communicatively coupled to the at least one processor; wherein the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of code coverage determination described above.
To achieve the above object, an embodiment of the present invention further provides a computer-readable storage medium storing a computer program, which when executed by a processor, implements the code coverage determination method as described above.
In the method for determining code coverage provided in the embodiment of the present invention, after a source code and a test code for performing a code test on the source code are obtained, and then under the condition that the source code and the test code have a corresponding historical source code and a corresponding historical test code, a first basic block is determined, so that because the first basic block includes a basic block in the source code, which is different from the historical source code, and a basic block in the test code, which is different from the historical test code, the first code coverage obtained by performing the code test according to the first basic block includes: the code coverage rate of the first basic block in the source code, and the code coverage rate 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 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, that is, the second basic block is the same as the test statement corresponding to the historical source code in the process of determining the code coverage rate of the source code, the code coverage rates determined by the second basic block in the source code and the historical source code are the same, that is, the historical code coverage rate corresponding to the second basic block is the code coverage rate obtained by performing the code test on the second basic block in the source code, that is, the historical code coverage rate corresponding to the second basic block is feasible and accurate as the code coverage rate of the second basic block in the source code. Therefore, the merging of the first code coverage and the historical code coverage corresponding to the second basic block is to merge 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, and according to the meaning of the basic block corresponding to the first basic block in the source code, the first basic block, the second basic block, and the first basic block in the test code together form the source code, that is, the merging of the first code coverage and the historical code coverage corresponding to the second basic block is performed, so that the complete code coverage of the source code is obtained. Therefore, in the embodiment, when the code coverage rate of the currently acquired source code is determined, the code test is performed only on the first basic block which is changed relative to the historical source code and the historical test code, that is, only the source code part of which the code coverage rate may be changed is subjected to the code test, the historical code coverage rate corresponding to the second basic block is combined on the basis of the first code coverage rate, and what is actually combined is the accurate code coverage rate of the second basic block which is not changed relative to the historical source code and the historical test code, that is, the code amount required to be subjected to the code test is reduced, so that the efficiency of determining the code coverage rate is improved, and the accuracy of the determined code coverage rate is also ensured. In addition, since only the modified code part is subjected to code testing without considering the implementation conditions of other parts, the part modified by a certain developer can be used as a first basic block, and codes (whether modified or not) written by other developers are used as a second basic block by default, and finally, the code coverage rate determination for a single user, namely the code testing at the user level is realized instead of the code testing at the project level. The code coverage determination efficiency of a single developer is improved.
Drawings
One or more embodiments are illustrated by way of example in the accompanying drawings, which correspond to the figures in which like reference numerals refer to similar elements and which are not to scale unless otherwise specified.
FIG. 1 is a flow chart of a method of determining code coverage provided in an embodiment of the present invention;
FIG. 2 is a flow chart of a method of code coverage determination including a step of outputting code coverage of source code provided in another embodiment of the present invention;
FIG. 3 is a flow chart of a method of code coverage determination including a format conversion step provided in another embodiment of the present invention;
FIG. 4 is a flow chart of a method of determining code coverage including a backup step provided in another embodiment of the present invention;
FIG. 5 is a flow chart of a method of code coverage determination including a deletion step provided in another embodiment of the present invention;
fig. 6 is a schematic structural diagram of a code coverage rate determining apparatus provided in another embodiment of the present invention;
fig. 7 is a schematic structural diagram of an electronic device provided in another embodiment of the present invention.
Detailed Description
As can be seen from the background art, it is desirable to provide an efficient code coverage determination method to solve the problem that the development progress is affected due to the inefficiency of the existing code coverage tool.
The analysis shows that the main reasons of the low efficiency problem are as follows: the method is characterized in that multiple developers and a large number of code files are likely to be involved in the development process, codes are generally modified for multiple times, after each modification, a code coverage rate tool clears the previously determined code coverage rate and then re-executes the step of determining the code coverage rate on all codes written by the developers, so that the codes of a certain developer cannot be determined, and the unmodified code part needs to be re-determined.
To solve the above problem, an embodiment of the present invention provides a method for determining code coverage, including: acquiring a source code and a test code, wherein the test code is used for carrying out code test on the source code; under the condition that the source code and the test code have corresponding historical source codes and historical test codes, taking a basic block which is different from the historical source codes in the source code and a basic block which is different from the historical test codes in the test code as a first basic block; performing code testing according to the first basic block to obtain a first code coverage rate; and merging the first code coverage rate and historical code coverage rates corresponding to second basic blocks to obtain the code coverage rate of the source code, wherein the second basic blocks are basic blocks which are not determined by the first code coverage rate in the source code, and the historical code coverage rate is determined by performing code coverage rate determination on each basic block in the historical source code based on the historical test code.
According to the method for determining the code coverage rate, after the source code and the test code for performing the code test on the source code are obtained, the code test is performed according to the source code and the test code which is different from the historical test code, namely, the code test is performed on the basis of the source code part which is changed relative to the historical source code and the test code part which is changed relative to the historical test code, so that the first code coverage rate is obtained, and then the first code coverage rate obtained on the basis of the first basic block and the historical code coverage rate corresponding to the second basic block are combined, so that the code coverage rate of the source code is obtained. That is, when determining the code coverage of the currently acquired source code, only the first basic block that fluctuates with respect to the historical source code and the historical test code is subjected to the code test, and the second basic block that does not fluctuate is not subjected to the code test. Compared with the method that the whole source code is tested from beginning to end every time, the code amount needing to be tested is reduced, and further the time needed by the code test is reduced, wherein the second basic block is a basic block which is not determined by the first code coverage rate in the source code, namely the second basic block is not modified, and the code part in the test code corresponding to the second basic block is not modified, so that the second basic block is used as a part of the historical source code or a part of the source code, the tested code and the test code are the same when the code coverage rate is determined, and further the obtained code coverage rate is the same, namely the historical code coverage rate corresponding to the second basic block is accurate as the code coverage rate in the source code. In addition, since only the modified code part is subjected to code testing without considering the implementation condition of other parts, the part modified by a certain developer can be used as a first basic block, and the code (whether modified or not) written by other developers is used as a second basic block, and finally, the code coverage rate determination aiming at a single user, namely the code testing at the user level is realized instead of the code testing at the project level. The code coverage determination efficiency of a single developer is improved.
To make the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the embodiments of the present invention will be described in detail below with reference to the accompanying drawings. However, it will be appreciated by those of ordinary skill in the art that in various embodiments of the invention, numerous technical details are set forth in order to provide a better understanding of the present invention. However, the claimed invention may be practiced without these specific details or with various changes and modifications based on the following embodiments.
The following embodiments are divided for convenience of description, and should not constitute any limitation to the specific implementation manner of the present invention, and the embodiments may be mutually incorporated and referred to without contradiction.
The embodiment of the invention provides a method for determining code coverage rate on one hand, which is applied to electronic equipment such as computers, servers and the like. The flow of the method for determining code coverage rate provided by this embodiment is shown in fig. 1, and at least includes the following steps:
step 101, a source code and a test code are obtained, and the test code is used for performing code test on the source code.
In this embodiment, the source code may be any code that needs to determine the code coverage, and the test code is a code for performing code test on the source code. For example, the source code may be a code corresponding to a function implemented by one or several code developers in a code development project, may be a code file, and may also be a complete code corresponding to a development project.
The source code and the test code may be written in any programming language, and particularly, even if the programming languages corresponding to the source code and the test code are not the same, the source code and the test code may be represented in the same programming language by compiling or the like during code testing, and therefore, the programming languages of the source code and the test code are not limited.
In some examples, the source code and the test code are stored on a personal device of a code developer, and accordingly, obtaining the test code of the source code may be accomplished by: reading the local addresses of the source codes and the test codes uploaded by the code developers; and searching the source code and the test code in the local storage of the code developer according to the read local address.
In other examples, the code developer actively uploads the source code and the test code, and accordingly, obtaining the test code of the source code may be implemented as follows: and reading the source code and the test code uploaded by the code developer.
Of course, the above is only a specific example, in other examples, a code developer may also designate a code file where the source code is located, directly read the code file to obtain the source code, and search for the test code corresponding to the source code, which is not described herein any more.
And 102, under the condition that the source code and the test code have corresponding historical source codes and historical test codes, taking a basic block which is different from the historical source codes in the source code and a basic block which is different from the historical test codes in the test code as a first basic block.
It will be appreciated that the entire development process will likely require multiple code modifications and adjustments, and that the modifications and adjustments may be made to the source code, the test code, or both, and therefore multiple code tests, i.e., multiple determinations of software test metrics such as code coverage, may be required. Wherein each determination of code coverage is based on 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 is changed relative to the source code and test code used in any determination of code coverage prior to the currently determined code coverage. At this time, source code and test code used in a certain process of determining code coverage before the current determination of code coverage may be taken as historical source code and historical test code.
In this embodiment, the source code, the historical source code, the test code, and the historical test code all include a plurality of basic blocks, a basic block in the source code that is different from the historical source code is a basic block in the source code that cannot be matched in the historical source code to the basic block, and a basic block in the test code that is different from the historical test code is a basic block in the test code that cannot be matched in the historical test code to the basic block, where two basic blocks match indicates that statements in the two basic blocks are the same, and two basic blocks do not match indicates that statements in the two basic blocks are not completely the same.
That is to say, regarding basic blocks in the source code which are different from the historical source code and basic blocks in the test code which are different from the historical test code as the first basic blocks, the essence is: matching each basic block in the source code in the historical source code respectively to determine which basic blocks in the source code are successfully matched and which basic blocks in the source code are not successfully matched, and then taking the basic blocks in the source code, which are not successfully matched, as first basic blocks; and matching each basic block in the test codes in the historical test codes respectively to determine which basic blocks in the test codes are successfully matched and which basic blocks in the test codes are not successfully matched, and then taking the basic blocks which are not successfully matched in the test codes as first basic blocks.
It can be understood that, the source code and the historical source code have an association, the test code and the historical test code have an association, for example, the source code may be obtained by modifying and adjusting the historical source code, and therefore, in the case that the source code and the test code have the corresponding historical source code and the historical test code, determining the code coverage of the source code based on the source code and the test code may refer to the code coverage of the historical source code obtained based on the historical source code and the historical test code, while performing code test on a source code portion whose code coverage of the historical source code obtained based on the historical source code and the historical test code does not provide a reference, determining the code coverage, thereby reducing the amount of code to be tested and improving the determination efficiency of determining the code coverage of the source code, wherein the code coverage of the historical source code obtained based on the historical source code and the historical test code does not provide a reference, the method comprises the steps of obtaining basic blocks in source code, wherein the basic blocks are changed in the source code relative to historical source code, and corresponding basic blocks of test code in the source code relative to the basic blocks changed in the historical test code. In contrast, when the source code and the test code do not have the corresponding history source code and history test code, since any reference cannot be obtained, it is necessary to perform code testing from the beginning to the end of all the source codes. That is, when the source code and the test code do not have the corresponding history source code and history test code, the source code is subjected to a code test based on the test code, and the code coverage of the source code is obtained.
Therefore, in other embodiments, after the source code and the test code are currently acquired, it is determined whether the acquired source code and the test code have corresponding historical source code and historical test code, so that in a case where it is determined that the acquired source code and the 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; and under the condition that the obtained source code and the test code do not have the corresponding historical source code and historical test code, performing code test on the source code according to the test code to obtain the code coverage rate of the source code. Particularly, whether the obtained source code and the test code have corresponding historical source code and historical test code is judged, mainly whether the original source code corresponding to the source code already determines the code coverage rate is judged. Judging whether the obtained source code and the test code have corresponding historical source codes and historical test codes, and under the condition that the number of times of determining the code coverage rate of the source code obtained based on the same original source code is counted, judging whether the count value is 0; in the case where the code coverage determination result obtained based on the same source code is stored in the specified storage space, this can be achieved by determining whether or not the storage content of the storage space is empty.
It should be noted that, in this embodiment, the process of determining the code coverage corresponding to the historical source code and the historical test code is not limited to the last process of determining the code coverage of the current process of determining the code coverage, but is described as a certain process of determining the code coverage before the current process of determining the code coverage. This is because there is uncertainty in modifying and adjusting the source code and the test code, and therefore, the source code and the test code acquired in the last process of determining the code coverage rate are not necessarily the least different from the source code and the test code acquired in the current process of determining the code coverage rate, and the difference between the source code and the test code acquired in the current process of determining the code coverage rate may affect the code coverage rate efficiency provided by the embodiment. Taking the source code as an example, in the source code acquired in the 6 th code coverage determination process, in the source code acquired in the 1 st to 5 th code coverage determination processes, closest to the source code obtained in the process of determining the code coverage rate for the 1 st time, in this case, compared with the source code obtained in the process of determining the code coverage rate for 2-5 times as the historical source code, the source code obtained in the process of determining the code coverage rate for the 1 st time as the historical source code can obtain a smaller number of first basic blocks, so that code testing is performed based on the first basic block at step 103, fewer basic blocks need to be tested for code, the efficiency of determining the coverage of the first code is higher, the coverage rate of the first code is improved by selecting the proper historical source code, and finally, the efficiency of determining the coverage rate of the source code is improved by determining the coverage rate of the first code.
Therefore, in other embodiments, in the case that multiple historical source codes exist, the source codes and the multiple historical source codes may also be matched, and the historical source code with the highest matching degree and the historical test code corresponding to the historical source code are used as the historical source code and the historical test code used when the first basic block is determined; or, the source code and the test code are respectively matched with the historical source code and the historical test code at the same time, and the historical source code and the historical test code which have the highest matching degree as a whole are used as the historical source code and the historical test code and the like used when the first basic block is determined, which is not repeated here.
And 103, performing code testing according to the first basic block to obtain a first code coverage rate.
In this embodiment, the first basic block includes a first basic block existing in the source code and different from the historical source code, and a first basic block existing in the test code and different from the historical test code. For a first basic block which is different from a historical source code and exists in the source code, because the code of the first basic block is not contained by the historical source code, the code coverage rate of the basic block cannot be contained in the historical code coverage rate of the historical source code, and the code coverage rate of the basic block needs to be determined through code testing; for a first basic block which is different from the historical test code and exists in the test code, because the test logic of the first basic block is not included by the historical test code, the code test needs to be performed on the corresponding test code part based on the test logic which is different from the historical test code, and the code coverage rate of the corresponding basic block in the corresponding source code needs to be determined again.
Therefore, in some examples, in the case that a first basic block different from the historical source code exists in the source code, a code test is performed according to the first basic block to obtain a first code coverage rate, and the following steps are performed: and according to the corresponding code in the test code, performing code test on the first basic block in the source code to obtain the first code coverage rate in the first basic block in the source code. After searching the corresponding code in the test code according to the first basic block in the source code, performing code test on the first basic block in the corresponding source code by using the determined code to obtain the first code coverage rate of the first basic block in the source code.
In some examples, in the case that a first basic block different from the historical test code exists in the test code, performing a code test according to the first basic block to obtain a first code coverage rate, and implementing the following steps: and according to the first basic block in the test code, performing code test on the corresponding code in the source code to obtain the first code coverage rate of the corresponding code in the source code. After searching the corresponding code in the source code according to the first basic block in the test code, performing code test on the code in the correspondingly determined source code by using the first basic block in the test code to obtain the first code coverage rate of the code in the determined source code.
And step 104, combining the first code coverage rate and the historical code coverage rate corresponding to a second basic block to obtain the code coverage rate of the source code, wherein the second basic block is a basic block with the same source code and historical source code, the corresponding test code part is a basic block with the same historical test code part, and the historical code coverage rate is the code coverage rate determined by performing code coverage rate on each basic block in the historical source code based on the historical test code.
In this embodiment, the merged first code coverage includes all the first code coverage determined in step 103, that is, the merged first code coverage 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. And the second basic block is used as a second basic block in a basic block with the same source code and historical source code, and the corresponding test code part is also the same as the historical test code part, because the first basic block in the source code is a basic block with different source code and historical source code, and the basic block in the source code corresponding to the first basic block in the test code, which is different from the historical test code, is a basic block with a corresponding test code part in the source code, which is different from the historical code part, the union of the second basic block and the first basic block in the source code, which is different from the historical source code, and the basic block in the source code, which is corresponding to the first basic block in the test code, which is different from the historical test code, is the source code. And then, taking the historical code coverage rate corresponding to the second basic block as the current code coverage rate of the second basic block, and combining the first code coverage rate and the historical code coverage rate corresponding to the second basic block under the condition that the combined first code coverage rate comprises 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, which are actually the code coverage rates of different parts forming the source code, so as to obtain the complete code coverage rate of the source code. The second basic block is a basic block which is not determined by the first code coverage rate in the source code, namely the second basic block is not modified and a code part in the test code corresponding to the second basic block is not modified, so that whether the second basic block is used as a part of a historical source code or a part of the source code, the tested code and the test code are the same when the code coverage rate is determined, and further the obtained code coverage rate is the same, namely the historical code coverage rate corresponding to the second basic block is accurate as the code coverage rate in the source code.
The embodiment of the invention also provides a method for determining the code coverage rate, which is applied to electronic equipment such as computers, servers and the like. The flow of the method for determining code coverage rate provided in this embodiment is shown in fig. 2, and at least includes the following steps:
and 105, adding content for outputting the code coverage rate, wherein the content for outputting the code coverage rate comprises a code coverage rate result output code or an added code coverage rate display tool bag.
In this embodiment, the new addition of the code coverage rate result output code can be realized by adding a code for realizing the function of outputting the code coverage rate in the test code, or by adding a compiling parameter in the compiling process; the new code coverage rate display toolkit can be newly added by packaging the corresponding coverage rate toolkit in the compiling process, and the description is omitted here.
And 106, calling the content for outputting the code coverage rate, and outputting the code coverage rate of the source code.
The embodiment of the invention also provides a method for determining the code coverage rate, which is applied to electronic equipment such as computers, servers and the like. The flow of the method for determining code coverage rate provided by this embodiment is shown in fig. 3, and at least includes the following steps:
And step 107, converting the output result into a hypertext markup language (HTML) format.
In this embodiment, a preset HyperText Markup Language (HTML) formatting tool may be called, or a preset format conversion code may be called, so that an output result is presented to a code developer through a browser, so as to determine whether to modify and how to modify the source code and/or the test code.
It should be noted that, this embodiment does not limit the manner of implementing the HTML format conversion, and may be any implementation manner capable of converting the non-HTML format text into the HTML format.
And step 108, displaying the output result after format conversion.
In this embodiment, the data result after format conversion may be presented on a personal device of a code developer, or may be presented on an electronic device that implements the method for determining code coverage provided in this embodiment, and so on.
The embodiment of the invention also provides a method for determining the code coverage rate, which is applied to electronic equipment such as computers, servers and the like. The flow of the method for determining code coverage rate provided by this embodiment is shown in fig. 4, and at least includes the following steps:
Step 109, the source code is backed up.
In this embodiment, after the code coverage of the source code is determined, the source code essentially becomes historical source code, relative to the code coverage determination process that will occur next. It is also desirable to ensure the security of the historical source code in order to facilitate the provision of the historical source code in the next code coverage determination process. Therefore, the source code can be backed up to ensure the information security thereof.
In some examples, source code may be backed up on an electronic device that implements the code coverage determination method provided by the present embodiments.
In other examples, the source code may be backed up in a personal device of the code developer.
The embodiment of the invention also provides a method for determining the code coverage rate, which is applied to electronic equipment such as computers, servers and the like. The flow of the method for determining code coverage rate provided by this embodiment is shown in fig. 5, and at least includes the following steps:
at step 110, the historical source code, historical test code, and historical code coverage are deleted.
In this embodiment, after the code coverage of the currently acquired source code is determined, the source code essentially becomes the historical source code, which is relative to the code coverage determination process that will occur next time. That is, in the next code coverage determination process, historical source code may be provided that includes the currently acquired source code and historical source code for the currently acquired source code. The historical source code and the related information of the currently acquired source code, namely the historical test code and the historical code coverage rate, are deleted, so that the historical source code, the historical test code and the historical code coverage rate can still be ensured in the next code coverage rate determining process. Meanwhile, due to timely deletion of the historical source codes, the historical test codes and the historical code coverage rate, the occupied amount of the cache is reduced.
If the historical source code, the historical test code and the historical code coverage rate are deleted after the code coverage rate of the currently acquired source code is determined each time, only one group of the available historical source code, the available historical test code and the historical code coverage rate is needed each time the code coverage rate of the currently acquired source code is determined, namely the code coverage rate of the source code, the test code and the source code acquired in the last process of determining the code coverage rate of the source code before the current process of determining the code coverage rate of the source code.
Of course, in other embodiments, historical source codes, historical test codes, and historical code coverage may not be deleted, so that more historical source codes, historical test codes, and historical code coverage are reserved, and thus, when step 102 is implemented, a set of historical source codes and historical test codes that have a smaller difference from the currently acquired source codes and/or test codes may be selected when the first basic block is determined, thereby further improving the determination efficiency of the code coverage.
In order to facilitate those skilled in the art to better understand the method for determining the code coverage provided by the foregoing embodiment, the following description will take, as an example, a method for determining the code coverage based on GNU Compiler Collection (gcc) tools, in which both the source code and the test code are C language, and the lcov tools are used as contents for outputting the code coverage.
Adding a parameter "- - - - -coverage-fprofile-dir ═ var/gcov/test" of the gcc generated code coverage into the compiling parameter, wherein, - - - - -coverage-fprofile-dir ═ var/gcov/test "represents the directory where the coverage binary file is stored, i.e. the location where the source code and the test code are stored, and" - - - -coverage-fprofile-dir ═ var/gcov/test "represents writing the generated code coverage data into the file under the directory"/var/gcov/test ", thereby automatically adding the coverage function when the code test is performed. That is, as long as the code corresponding to gcov _ test.c is run, the binary coverage information file gcov _ test.gcda to which gcov _ test.c is executed is generated under the/var/gcov/test directory. It is understood that in other examples, the/var/gcov/test directory may be modified to other representations as needed, and will not be described in detail herein.
Further, under the assumption that the source code is a file with a file name gcov _ test.c, determining a first basic block, performing a code test based on the first basic block, and then calling a command line "lcov-c-d./-b./-o a.info", and converting the obtained first coverage into a text file with a coverage, namely a file named as a.info, where the file includes information of each statement in each recorded basic block at the time of executing the code test, and the information may specifically include the following:
SF:/home/test/gcov_test.c
FN:10,func1
FN:15,func2
FNDA:1,func1
FNDA:1,func2
...
DA:13,1
DA:16,1
DA:17,1
end_of_record
I.e. a determination of the first time implementation code coverage is obtained.
The first code coverage and the historical code coverage are then merged by, where the first code coverage is represented as an a.info file and the historical code coverage is represented as a b.info file: judging that a B.info file exists currently, wherein the B.info file represents history source codes and files corresponding to the history source codes, reading A.info, wherein the A.info file represents the data, traversing each SF, starting data lines, reading the SF, and continuing the following circulation of data between the SF and the end of the record. Traversing each FN, recording the line number after the current FN and the line number after the next FN, and reading the data at the beginning of all DA between the line numbers, assuming that the first FN is between [10,15 ], and does not contain the 15 th line. The DA data between 10 and 14 are: DA:13, 1. Continuing reading the file of the B.info, searching data beginning with DA:13 between SF:/home/test/gcov _ test.c and end _ of _ record, then respectively opening the gcov _ test.c and gcov _ test.c.bak files and reading the program code data of the 13 th row for comparison, if the data are the same, accumulating the numbers behind the DA:13 of the B.info into the DA:13 in the A.info, and simultaneously, the FNDA of the A.info: 1, the number of the inner 1 of func1 is added with the number of the B.info corresponding to FNDA:1, func 1. If the 13 th lines of the two code files are different, the accumulated data is not merged. And finally obtaining the A.info representing the code coverage rate of the test code. And then deleting the B.info and the gcov _ test.c.bak, renaming the B.info with the A.info, realizing timely deletion of the coverage rate of the historical codes, avoiding occupying storage space, and simultaneously copying the gcov _ test.c file to the gcov _ test.c.bak for backup, namely, for the current source code backup, the file is used when the coverage rate of the codes is determined next time. The command line 'genhtml-f B.info-o result' can be called, so that the newly generated B.info obtains a corresponding html file when the code coverage rate is determined every time, and the code coverage rate result can be visually checked in the browser.
The steps of the above methods are divided for clarity, and the implementation may be combined into one step or split some steps, and the steps are divided into multiple steps, so long as the same logical relationship is included, which are within the scope of the present patent; it is within the scope of this patent to add insignificant modifications or introduce insignificant designs to the algorithms or processes, but not to change the core designs of the algorithms and processes.
Another aspect of the embodiments of the present invention further provides a device for determining code coverage, as shown in fig. 6, including:
the obtaining module 601 is configured to obtain a source code and a test code, where the test code is used to perform a code test on the source code.
A determining module 602, configured to, in a case that 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.
The testing module 603 is configured to perform a code test according to the first basic block to obtain a first code coverage.
A merging module 604, configured to merge the first code coverage and a historical code coverage corresponding to a second basic block to obtain a code coverage of the source code, where the second basic block is a basic block in which a source code is the same as a historical source code, a corresponding test code portion is also the same as a historical test code portion, and the historical code coverage is a code coverage determined by performing code coverage on the historical source code based on the historical test code.
It should be understood that the present embodiment is an apparatus embodiment corresponding to the method embodiment, and the present embodiment can be implemented in cooperation with the method embodiment. Related technical details mentioned in the method embodiment are still valid in this embodiment, and are not described herein again in order to reduce repetition. Accordingly, the related technical details mentioned in the present embodiment can also be applied in the method embodiment.
It should be noted that, all modules involved in this embodiment are logic modules, and in practical application, one logic unit may be one physical unit, may also be a part of one physical unit, and may also be implemented by a combination of multiple physical units. In addition, in order to highlight the innovative part of the present invention, a unit which is not so closely related to solve the technical problem proposed by the present invention is not introduced in the present embodiment, but this does not indicate that no other unit exists in the present embodiment.
Another aspect of the embodiments of the present invention further provides an electronic device, as shown in fig. 7, including: at least one processor 701; and a memory 702 communicatively coupled to the at least one processor 701; the memory 702 stores instructions executable by the at least one processor 701, and the instructions are executed by the at least one processor 701 to enable the at least one processor 701 to perform the method for determining code coverage as described in any of the method embodiments.
The memory 702 and the processor 701 are coupled by a bus, which may include any number of interconnecting buses and bridges that couple one or more of the various circuits of the processor 701 and the memory 702 together. The bus may also connect various other circuits such as peripherals, voltage regulators, power management circuits, and the like, which are well known in the art, and therefore, will not be described any further herein. A bus interface provides an interface between the bus and the transceiver. The transceiver may be one element or a plurality of elements, such as a plurality of receivers and transmitters, providing a means for communicating with various other apparatus over a transmission medium. Data processed by the processor 701 is transmitted over a wireless medium through an antenna, which receives the data and transmits the data to the processor 701.
The processor 701 is responsible for managing the bus and general processing and may also provide various functions including timing, peripheral interfaces, voltage regulation, power management, and other control functions. And the memory 702 may be used for storing data used by the processor 701 in performing operations.
In another aspect, an embodiment of the present invention further provides a computer-readable storage medium, in which a computer program is stored. The computer program, when executed by a processor, implements the method of determining code coverage as described in any of the method embodiments above.
That is, those skilled in the art may understand that all or part of the steps in the method according to the foregoing embodiments may be implemented by a program instructing related hardware, where the program is stored in a storage medium and includes several instructions to enable a device (which may be a single chip, a chip, or the like) or a processor (processor) to execute all or part of the steps in the method according to various embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk, and various media capable of storing program codes.
It will be understood by those of ordinary skill in the art that the foregoing embodiments are specific to implementations of the invention, and that various changes in form and detail may be made therein without departing from the spirit and scope of the invention in practice.

Claims (10)

1. A method for determining code coverage, comprising:
Acquiring a source code and a test code, wherein the test code is used for carrying out code test on the source code;
under the condition that the source code and the test code have corresponding historical source code and historical test code, taking a basic block which is different from the historical source code in the source code and a basic block which is different from the historical test code in the test code as a first basic block;
performing code testing according to the first basic block to obtain a first code coverage rate;
and combining the first code coverage rate and historical code coverage rates corresponding to second basic blocks 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, the corresponding test code part is also the same as the historical test code part, and the historical code coverage rate is determined by performing code coverage rate on each basic block in the historical source code based on the historical test code.
2. The method for determining code coverage according to claim 1, wherein the performing a code test according to the first basic block to obtain a first code coverage includes:
Under the condition that the first basic block which is different from the historical source code exists in the source code, performing code testing on the first basic block in the source code according to a corresponding code in the testing code to obtain the first code coverage rate of the first basic block in the source code; or,
and under the condition that the first basic block which is different from the historical test code exists in the test code, performing code test on the corresponding code in the source code according to the first basic block of the test code to obtain the first code coverage rate of the corresponding code in the source code.
3. The method of claim 1 or 2, wherein after obtaining the source code and the test code, the method further comprises:
and under the condition that the source code and the test code do not have the corresponding historical source code and historical test code, performing code test on the source code according to the test code to obtain the code coverage rate of the source code.
4. The method for determining code coverage according to claim 1 or 2, wherein before the code test is performed according to the first basic block to obtain the first code coverage, the method further comprises:
Adding content for outputting code coverage rate, wherein the content for outputting the code coverage rate comprises a code coverage rate result output code or a newly-added code coverage rate display toolkit;
after the combining the first code coverage rate and the historical code coverage rate corresponding to the second basic block to obtain the code coverage rate of the source code, the method further includes:
and calling the content for outputting the code coverage rate, and outputting the code coverage rate of the source code.
5. The method of claim 4, wherein after outputting the code coverage of the source code, the method further comprises:
converting the output result into a hypertext markup language (HTML) format;
and displaying the output result after format conversion.
6. The method for determining code coverage according to claim 1 or 2, wherein after the combining 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 comprises:
and backing up the source code.
7. The method for determining code coverage according to claim 1 or 2, wherein after the combining 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 comprises:
Deleting the historical source code, the historical test code, and the historical code coverage.
8. An apparatus for determining code coverage, comprising:
the system comprises an acquisition module, a test module and a control module, wherein the acquisition module is used for acquiring a source code and a test code, and the test code is used for carrying out code test on the source code;
a determining module, configured to, when the source code and the test code have corresponding historical source codes and historical test codes, take a basic block in the source code that is different from the historical source codes and a basic block in the test code that is different from the historical test codes as a first basic block;
the test module is used for carrying out code test according to the first basic block to obtain a first code coverage rate;
a merging module, configured to merge the first code coverage and a historical code coverage corresponding to a second basic block to obtain a code coverage of the source code, where the second basic block is a basic block in which the source code is the same as the historical source code, the corresponding test code portion is also the same as the historical test code portion, and the historical code coverage is a code coverage determined by performing code coverage on the historical source code based on the historical test code.
9. An electronic device, comprising:
at least one processor; and (c) a second step of,
a memory communicatively coupled to the at least one processor; wherein,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of code coverage determination of any one of claims 1 to 7.
10. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out a method for determining code coverage according to any one of claims 1 to 7.
CN202210204839.1A 2022-03-03 2022-03-03 Method and device for determining code coverage rate, electronic equipment and storage medium Pending CN114706749A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210204839.1A CN114706749A (en) 2022-03-03 2022-03-03 Method and device for determining code coverage rate, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210204839.1A CN114706749A (en) 2022-03-03 2022-03-03 Method and device for determining code coverage rate, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN114706749A true CN114706749A (en) 2022-07-05

Family

ID=82166440

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210204839.1A Pending CN114706749A (en) 2022-03-03 2022-03-03 Method and device for determining code coverage rate, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN114706749A (en)

Citations (16)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20030093716A1 (en) * 2001-11-13 2003-05-15 International Business Machines Corporation Method and apparatus for collecting persistent coverage data across software versions
JP2006268777A (en) * 2005-03-25 2006-10-05 Nec Corp Source code control system, source code control method, and source code control program
CN104298587A (en) * 2013-07-15 2015-01-21 腾讯科技(深圳)有限公司 Code coverage test method, device and system
CN105988926A (en) * 2015-02-13 2016-10-05 腾讯科技(深圳)有限公司 Method and device for processing multi-version test data
US20160291970A1 (en) * 2015-03-31 2016-10-06 Ca, Inc. Effective Defect Management Across Multiple Code Branches
CN106708721A (en) * 2015-11-13 2017-05-24 阿里巴巴集团控股有限公司 Realization method and apparatus for code coverage testing
CN108519945A (en) * 2018-03-21 2018-09-11 腾讯科技(深圳)有限公司 Coverage rate test method, apparatus and storage device
US20190018761A1 (en) * 2017-07-17 2019-01-17 Red Hat Israel, Ltd. Source code test consolidation
CN109388566A (en) * 2018-09-27 2019-02-26 北京城市网邻信息技术有限公司 A kind of code coverage rate analysis method, apparatus, equipment and storage medium
CN109766261A (en) * 2018-12-14 2019-05-17 中国平安财产保险股份有限公司 Coverage test method, device, computer equipment and storage medium
CN111078563A (en) * 2019-12-18 2020-04-28 上海品顺信息科技有限公司 Coverage rate data processing method, terminal device and computer readable storage medium
CN111782516A (en) * 2020-06-24 2020-10-16 Oppo广东移动通信有限公司 Code testing method and device and storage medium
CN112363936A (en) * 2020-11-12 2021-02-12 广州品唯软件有限公司 Method and device for testing differential coverage rate, computer equipment and storage medium
CN112559337A (en) * 2020-12-10 2021-03-26 平安普惠企业管理有限公司 Code coverage rate testing method and device, electronic equipment and storage medium
CN113342635A (en) * 2020-03-02 2021-09-03 腾讯科技(深圳)有限公司 Acquisition of differential code coverage rate and differential code file sending method and device
CN113568839A (en) * 2021-08-03 2021-10-29 京东科技控股股份有限公司 Method, device, equipment and medium for software testing and statistical test coverage rate

Patent Citations (16)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20030093716A1 (en) * 2001-11-13 2003-05-15 International Business Machines Corporation Method and apparatus for collecting persistent coverage data across software versions
JP2006268777A (en) * 2005-03-25 2006-10-05 Nec Corp Source code control system, source code control method, and source code control program
CN104298587A (en) * 2013-07-15 2015-01-21 腾讯科技(深圳)有限公司 Code coverage test method, device and system
CN105988926A (en) * 2015-02-13 2016-10-05 腾讯科技(深圳)有限公司 Method and device for processing multi-version test data
US20160291970A1 (en) * 2015-03-31 2016-10-06 Ca, Inc. Effective Defect Management Across Multiple Code Branches
CN106708721A (en) * 2015-11-13 2017-05-24 阿里巴巴集团控股有限公司 Realization method and apparatus for code coverage testing
US20190018761A1 (en) * 2017-07-17 2019-01-17 Red Hat Israel, Ltd. Source code test consolidation
CN108519945A (en) * 2018-03-21 2018-09-11 腾讯科技(深圳)有限公司 Coverage rate test method, apparatus and storage device
CN109388566A (en) * 2018-09-27 2019-02-26 北京城市网邻信息技术有限公司 A kind of code coverage rate analysis method, apparatus, equipment and storage medium
CN109766261A (en) * 2018-12-14 2019-05-17 中国平安财产保险股份有限公司 Coverage test method, device, computer equipment and storage medium
CN111078563A (en) * 2019-12-18 2020-04-28 上海品顺信息科技有限公司 Coverage rate data processing method, terminal device and computer readable storage medium
CN113342635A (en) * 2020-03-02 2021-09-03 腾讯科技(深圳)有限公司 Acquisition of differential code coverage rate and differential code file sending method and device
CN111782516A (en) * 2020-06-24 2020-10-16 Oppo广东移动通信有限公司 Code testing method and device and storage medium
CN112363936A (en) * 2020-11-12 2021-02-12 广州品唯软件有限公司 Method and device for testing differential coverage rate, computer equipment and storage medium
CN112559337A (en) * 2020-12-10 2021-03-26 平安普惠企业管理有限公司 Code coverage rate testing method and device, electronic equipment and storage medium
CN113568839A (en) * 2021-08-03 2021-10-29 京东科技控股股份有限公司 Method, device, equipment and medium for software testing and statistical test coverage rate

Similar Documents

Publication Publication Date Title
CN108829593B (en) Code coverage rate calculation and analysis method, device, equipment and storage medium
CN109086199B (en) Method, terminal and storage medium for automatically generating test script
US9632916B2 (en) Method and apparatus to semantically connect independent build and test processes
US6542167B1 (en) System and method for flexible software linking
US9015695B2 (en) Information processing apparatus and information processing method
JP4638484B2 (en) Data integrity in data processing equipment
US6665688B1 (en) Method and system for automatically regenerating data on-demand
CN115269544A (en) Database cluster upgrading method and device, electronic equipment and storage medium
CN114116505A (en) Code testing method and device
CN116501415B (en) Command execution method and device, electronic equipment and computer readable storage medium
US6915512B1 (en) Software editing with indication of format and processing state of each process of the software
CN114706749A (en) Method and device for determining code coverage rate, electronic equipment and storage medium
CN112181812A (en) Version testing method and device
CN111352764A (en) Chip repairing method, device, equipment and storage medium
US6854109B2 (en) Tool for converting .MAP file formats
CN113177020A (en) Data processing method and device
CN110764777B (en) ELF file generation method, ELF file, equipment and storage medium
CN111880803A (en) Software construction method and device applied to multiple platforms
US20020129350A1 (en) Method of automatic software installation in hand personal computer production line
CN112231229A (en) Web UI (user interface) automatic testing method and system, electronic equipment and readable storage medium
CN110058876B (en) Method, device, server and storage medium for expanding compatibility of wireshark software
CN114186531A (en) Intelligent terminal form data style processing method, system, device and storage medium
US8205186B1 (en) Incremental modification of instrumentation logic
CN117616387A (en) Incremental compiling processing method and device, electronic equipment and storage medium
IL145365A (en) Generating optimized computer data field conversion routines

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination