A code coverage determination method, apparatus, device and readable storage medium

CN115794606BActive Publication Date: 2026-10-30PING AN BANK CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211312299.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-10-25
Publication Date
2026-10-30
Estimated Expiration
2042-10-25

AI Technical Summary

Benefits of technology

[0012]本发明提供的代码覆盖率确定方法、装置、电子设备和可读存储介质,通过获取第一代码包以及第二代码包,第二代码包为第一代码包进行更新后得到的代码包,第一代码包与第二代码包均包括多行代码,每行代码按照代码排序具有唯一行号;将第一代码包以及第二代码包中的代码进行比较,得到第一代码包中被删除的代码的第一行号以及第二代码包中属于新增代码的第二行号; 根据第一行号、第二行号以及第一代码包中每行代码的行号,确定第二代码中目标代码的更新行号以及目标代码在第一代码包中的原始行号;目标代码表征第一代码包与第二代码包共同拥有的代码;根据第一代码包的覆盖率数据以及第二代码中目标代码的更新行号以及目标代码在第一代码包中的原始行号,得到第二代码包中目标代码的覆盖率。如此,通过获取目标代码在第一代码包中以及在第二代码包中的位置,根据目标代码在第一代码包中以及在第二代码包中的位置,将目标代码曾被测算过的代码覆盖率直接应用到第二代码包中,无需对第二代码包中的目标代码的代码覆盖率进行测试,减少了代码覆盖率测算的时间,以及重新测算代码覆盖率所消耗的成本,提高了重新测算代码覆盖率的效率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115794606B_ABST
    Figure CN115794606B_ABST
Patent Text Reader

Abstract

The application discloses a code coverage determination method and device, equipment and a readable storage medium. The embodiment of the application compares the codes in the first code package and the second code package by obtaining the first code package and the second code package, obtains the first line number of the deleted code in the first code package and the second line number of the added code in the second code package, determines the updated line number of the target code in the second code and the original line number of the target code in the first code package according to the first line number, the second line number and the line number of each line code in the first code package, and obtains the coverage of the target code in the second code package according to the coverage data of the first code package and the updated line number of the target code in the second code and the original line number of the target code in the first code package, so that the code coverage of the target code in the first code package is applied to the second code package, and the time for measuring and calculating the code coverage of the second code package is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and more specifically to a method, apparatus, device, and readable storage medium for determining code coverage. Background Technology

[0002] Code coverage, as a crucial metric for software product reliability, is applied in numerous testing scenarios. Because code coverage is directly related to the code itself, in practice, old code coverage is often difficult to adapt to new code packages. The release of new code packages is often accompanied by updates to code coverage, requiring re-running tests every time the code is updated, which is both time-consuming and labor-intensive. Summary of the Invention

[0003] This invention provides a method, apparatus, device, and readable storage medium for determining code coverage, aiming to solve the problem of time-consuming code coverage calculation in the prior art. Firstly, a method for determining code coverage, the method including: Obtain the first code package and the second code package. The second code package is the code package obtained by updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code order. Compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package; Based on the first line number, the second line number, and the line number of each line of code in the first code package, determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package; the target code represents the code shared by the first code package and the second code package; The coverage of the target code in the second code package is obtained based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0004] In one possible implementation of the present invention, determining the updated line number of the target code in the second code and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package includes: Sort the first line numbers in ascending order, and compare the line numbers of each line of code in the first code package with the sorted first line numbers in ascending order; Based on the comparison results, the temporary line numbers of the code that was not deleted in the first code package and the original line numbers of the code that was not deleted in the first code package are obtained; the code that was not deleted is the target code; Based on the temporary line number, the original line number, and the second line number, determine the update line number of the code that was not deleted in the second code package.

[0005] In one possible implementation of the present invention, the line numbers of each line of code in the first code package are compared sequentially with the sorted first line numbers in ascending order. Based on the comparison results, temporary line numbers of the code in the first code package that have not been deleted are obtained, including: For each line number in the first code package, if the first comparison result indicates that the line number is less than the target first line number, then it is determined that the line number has not been deleted. The current line number offset is obtained, and a temporary line number for the line number is obtained based on the line number and the current line number offset. Then, according to the line number order, the next line number adjacent to the current line number is obtained, and the next line number adjacent to the current line number is compared with the target first line number. The target first line number is the line number being compared among all the first line numbers. The line number offset indicates the number of deleted line numbers. If the comparison result indicates that the line number is equal to the target first line number, then it is determined that the line of code has been deleted. The current line number offset is updated to obtain the updated line number offset. According to the sorting order of the first line numbers, the next first line number adjacent to the target first line number is obtained and the next first line number is determined as the new target first line number. According to the line number order, the next line number adjacent to the current line number is obtained and compared with the new target first line number. In one possible implementation of the present invention, the temporary line number of the code is obtained based on the line number and the current line number offset, including: Determine the difference between the line number and the current line number offset, and use this difference as the temporary line number for this code.

[0006] In one possible implementation of the present invention, the second line numbers are sorted in ascending order, and the updated line number of the undeleted code in the second code package is determined based on the temporary line number, the original line number, and the second line number, including: For each original line number, determine the target second line number that is the same as that original line number; Starting from the second row number of the target row, determine the number of consecutively increasing second row numbers, and determine the number of row numbers as the displacement corresponding to the original row number; Based on the displacement corresponding to the original line number and the corresponding temporary line number, the updated line number of the non-deleted code corresponding to the original line number is obtained in the second code package.

[0007] In one possible implementation of the present invention, the updated line number of the undeleted code in the second code package is obtained based on the displacement corresponding to the original line number and the corresponding temporary line number, including: Get the displacement corresponding to each original row number that is sorted before this original row number; The total displacement is obtained by considering the displacement corresponding to the original row number and the displacements corresponding to each original row number preceding it. Based on the sum of the total displacement and the corresponding temporary line number, the updated line number of the original line number in the second code package corresponding to the non-deleted code is obtained.

[0008] In one possible implementation of the present invention, after obtaining the coverage of the target code in the second code package, the method further includes: Determine the coverage of the newly added code in the second code package, excluding the target code; The code coverage of the second code package is obtained based on the coverage of the newly added code and the coverage of the target code.

[0009] Secondly, the present invention also provides an apparatus for determining code coverage, the apparatus comprising: The module retrieves a first code package and a second code package. The second code package is obtained by updating the first code package. Both the first and second code packages contain multiple lines of code, and each line of code has a unique line number according to the code order. The comparison module compares the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package. The target line number generation module determines the updated line number of the target code in the second code and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package; the target code represents the code shared by the first code package and the second code package; The code coverage determination module obtains the coverage of the target code in the second code package based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0010] Thirdly, the present invention also provides a code coverage determination electronic device, the electronic device comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement a fatigue detection method of the first aspect.

[0011] Fourthly, the present invention provides a readable storage medium, the readable storage medium comprising: A computer program, when running, controls a calibration device located on a readable storage medium to perform the steps of a code coverage determination method in the first aspect.

[0012] The present invention provides a code coverage determination method, apparatus, electronic device, and readable storage medium. The method involves acquiring a first code package and a second code package, wherein the second code package is an updated version of the first code package. Both the first and second code packages contain multiple lines of code, each line having a unique line number according to its order. The code in the first and second code packages is compared to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package. Based on the first line number, the second line number, and the line numbers of each line of code in the first code package, the updated line number of the target code in the second code package and the original line number of the target code in the first code package are determined. The target code represents code shared by both the first and second code packages. Based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package, the coverage rate of the target code in the second code package is obtained. In this way, by obtaining the location of the target code in the first code package and the second code package, the code coverage previously measured for the target code can be directly applied to the second code package based on the location of the target code in the first code package and the second code package. There is no need to test the code coverage of the target code in the second code package, which reduces the time for code coverage measurement and the cost of recalculating code coverage, and improves the efficiency of recalculating code coverage. Attached Figure Description

[0013] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0014] Figure 1 This is a schematic diagram of a code coverage determination system provided in an embodiment of the present invention; Figure 2 This is a flowchart illustrating a code coverage determination method provided in an embodiment of the present invention; Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention; Figure 4 This is a block diagram of a code coverage determination device provided in an embodiment of the present invention. Detailed Implementation

[0015] The technical solutions of this invention will now be clearly and completely described with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this invention, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0016] In the description of this invention, it should be understood that the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, features defined as "first" and "second" may explicitly or implicitly include one or more features. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.

[0017] In this invention, the term "exemplary" is used to mean "serving as an example, illustration, or description." Any embodiment described as "exemplary" in this invention is not necessarily to be construed as being more preferred or advantageous than other embodiments. The following description is provided to enable any person skilled in the art to make and use the invention. Details are set forth in the following description for purposes of explanation. It should be understood that those skilled in the art will recognize that the invention can be made without using these specific details. In other instances, well-known structures and processes will not be described in detail to avoid obscuring the description of the invention with unnecessary detail. Therefore, the invention is not intended to be limited to the embodiments shown, but is consistent with the broadest scope of the principles and features disclosed herein.

[0018] The following section will first explain some of the terms used in the embodiments of this invention.

[0019] Unique line number: This is a sequential number assigned to the code within a code package based on its writing and arrangement. Code in a code package is usually organized by lines. Multiple lines of code arranged in an orderly manner form a code package. The unique line number corresponding to the code package is formed by sequentially numbering these ordered lines of code.

[0020] Target code: This is the code that exists in both the old code package (the first code package) and the new code package (the second code package). In other words, it is the code in the old code package that has not been deleted and the code in the new code package that has not been added.

[0021] This invention provides a method, apparatus, device, and storage medium for determining code coverage, which are described in detail below.

[0022] The code coverage determination method provided in this embodiment of the invention can be executed by an electronic device with executable instructions.

[0023] Among them, electronic devices can be terminal devices such as mobile phones, tablets, and laptops, or independent physical servers, or server clusters or distributed systems composed of multiple physical servers. They can also be cloud servers that provide basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms, but are not limited to these.

[0024] The following sections provide detailed descriptions of each example. It should be noted that the order in which the embodiments are described is not intended to limit the preferred order of the embodiments.

[0025] Please see Figure 1 , Figure 1 This is a schematic diagram of the code coverage determination system of the present invention. The code coverage determination system may include an electronic device 100 and a terminal device 200 communicatively connected to the electronic device 100. The terminal device 200 may provide the electronic device with a first code package and a second code package that need to be tested for code coverage. After the electronic device 100 obtains the first code package and the second code package, it can determine the code coverage of the code in the second code package that is shared with the first code package based on the coverage data of the existing first code package.

[0026] The server in this invention can be based on a CISC complex instruction set architecture or a reduced instruction set architecture. For example, the server in this invention includes, but is not limited to, IA architecture servers and RISC architecture servers.

[0027] In this invention, the server and electronic device 100 can communicate via network through any communication method, including but not limited to mobile communication based on the 3rd Generation Partnership Project (3GPP), Long Term Evolution (LTE), and Worldwide Interoperability for Microwave Access (WiMAX), or computer network communication based on the TCP / IP Protocol Suite (TCP / IP) and User Datagram Protocol (UDP). Terminal device 200 can upload planned code packets to electronic device 100 through the aforementioned communication methods. It is understood that the code packets uploaded by terminal device 200 to electronic device 100 have identification information, and different versions of code packets belonging to the same application have the same identification information.

[0028] Those skilled in the art will understand that the application environment is merely one application scenario adapted to the solution of this invention, and does not constitute a limitation on the application scenario of this invention. Other application scenarios may include more or fewer electronic devices, such as one electronic device 100. It is understood that the code coverage determination system may also include other devices communicatively connected to the electronic device 100, but this is not specifically limited here. It should be noted that the schematic diagram of the code coverage determination system is merely an example. The code coverage determination system and scenario described in this invention are for the purpose of more clearly illustrating the technical solution of this invention, and do not constitute a limitation on the technical solution provided by this invention. Those skilled in the art will understand that as code coverage determination systems evolve and new business scenarios emerge, the technical solution provided by this invention is equally applicable to similar technical problems.

[0029] First, the present invention provides a code coverage determination method, which is applied to an electronic device. The electronic device is equipped with a line shifting algorithm that adapts the coverage of old code to new code, and a coverage testing tool that specifically calculates the coverage of new code in a new code package that has not been measured before. The code coverage determination method provided by this invention includes: obtaining a first code package and a second code package, wherein the second code package is obtained by updating the first code package, and both the first and second code packages include multiple lines of code, each line of code having a unique line number according to the code order; comparing the code in the first and second code packages to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package; determining the updated line number of the target code in the second code package and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package; the target code represents the code shared by the first and second code packages; and obtaining the coverage of the target code in the second code package based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0030] like Figure 2 The diagram shown is a flowchart illustrating an embodiment of the code coverage determination method provided in this invention. It should be noted that although the logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than that shown here.

[0031] S201. Obtain the first code package and the second code package. The second code package is the code package obtained after updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code order.

[0032] The first code package is the code package data that has been tested by the code coverage tool, which belongs to the older version of the code package. The second code package is the new code package that has not been tested for coverage, which belongs to the new version of the code package.

[0033] In this embodiment of the invention, the electronic device can obtain the first code packet and the second code packet passively from the terminal device, whereby the user uploads the first code packet, the second code packet, and the code coverage data of the first code packet to the electronic device through the terminal device. Alternatively, the electronic device can actively obtain the first code packet and the second code packet from the terminal device or a storage device with storage function. For example, after the user publishes the second code packet and the first code packet through the terminal device, the terminal device itself stores the second code packet and the existing first code packet and the code coverage data of the first code packet, or the terminal device stores the second code packet and the existing first code packet and the code coverage data of the first code packet through a storage device with storage function connected to it. The electronic device accesses the terminal device or the storage device at preset time intervals, such as 30 seconds, to obtain the first code packet, the second code packet, and the code coverage data of the first code packet.

[0034] In this embodiment of the invention, the code contained in the code package can be organized into rows, columns, or groups according to writing conventions. Typically, the code in a code package is organized by lines; therefore, a corresponding code package can include multiple lines of code, for example, the first code package may contain 20 lines of code. It should be noted that each line of code in the code package must be sequentially ordered to a fixed position to achieve its intended function. Therefore, each line of code in the code package is sequentially numbered, and each line of code in the code package has a unique line number at that position.

[0035] S202. Compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package.

[0036] Comparing the code in the first code package with the code in the second code package, the difference lines in the second code package can be obtained using the git diff command. The difference may be due to code deletion in the first code package and / or code addition in the second code package. Based on the difference lines, the deleted code in the first code package and the added code in the second code package can be mapped.

[0037] In this embodiment of the invention, the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package are obtained by comparing the first code package and the difference line code. For example, the first code package consists of 4 lines of code "1", "2", "3", and "4", where the code in line "1" is composed of the character "a", the code in line "2" is composed of the character "b", the code in line "3" is composed of the character "c", and the code in line "4" is composed of the character "d". The second code package consists of 5 lines of code "1", "2", "3", "4", and "5", where the code in line "1" is composed of the character "b", the code in line "2" is composed of the character "c", the code in line "3" is composed of the character "d", the code in line "4" is composed of the character "e", and the code in line "5" is composed of the character "f".

[0038] By comparing the codes at corresponding positions in the first code package and the second code package one by one, we can obtain the codes in the second code package that differ (including positional differences), namely the five characters "b", "c", "d", "e", and "f". Comparing the obtained codes that differ with the characters in the first code package, we can find that the deleted code in the first code package is "a", and the first line number of "a" in the first code package is "1". Similarly, we can obtain the newly added codes "e" and "f" in the second code package, where the second line number of "e" is "4" and the second line number of "f" is "5".

[0039] S203. Based on the first line number, the second line number, and the line number of each line of code in the first code package, determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package; the target code represents the code shared by the first code package and the second code package.

[0040] In this embodiment of the invention, the target code represents the code shared by the first code package and the second code package, namely the code that was not deleted in the old code package and the code that was not added in the new code package.

[0041] The original line number of the target code in the first code package can be understood as the line number of the common code in the second code package and the first code package in the first code package. The updated line number of the target code in the second code package can be understood as the new line number obtained after the target code is shifted and reordered according to the insertion of the new code in the second code package.

[0042] S204. Based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package, obtain the coverage of the target code in the second code package.

[0043] In this embodiment of the invention, since the coverage data and the original line number are consistent, and each coverage data corresponds to a unique original line number, the coverage data of the target code in the first code package is obtained based on the coverage data line number of the first code package and the original line number of the target code in the first code package. Then, based on the coverage data of the target code in the first code package and the updated line number of the target code in the second code package, the coverage data of the target code in the first code package can be specifically applied to the specific line of the target code in the second code package to obtain the coverage of the target code in the second code package.

[0044] In the embodiments of the present invention, by shifting the coverage of the two sets of code in the manner of line numbers to correspond to the accuracy, the problem that the coverage of the old code cannot adapt to the new code and that the accuracy and precision of the old code coverage is not high by simply shifting it is better solved.

[0045] In some embodiments of the present invention, determining the updated line number of the target code in the second code and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package may further include: (1) Sort each first line number in ascending order, and compare the line number of each line of code in the first code package with the sorted first line number in ascending order. Based on the comparison results, obtain the temporary line number of the code that has not been deleted in the first code package and the original line number of the code that has not been deleted in the first code package; the code that has not been deleted is the target code. (2) Determine the updated line number of the code that was not deleted in the second code package based on the temporary line number, the original line number, and the second line number. Since the first line number represents the line number of the deleted code in the first code package, and there are multiple lines of code (contiguous or non-contiguous) deleted, there are multiple first line numbers.

[0046] After sorting the first line numbers in ascending order, the line numbers of each line of code in the first code package can be compared with the sorted first line numbers in ascending order. For example, first compare the smallest line number in the first code package with the smallest first line number among all the first line numbers, then compare the smallest line number in the first code package with the second smallest first line number among all the first line numbers, and so on. After comparing the smallest line number in the first code package, compare the second smallest line number in the first code package with the smallest first line number among all the first line numbers, and so on.

[0047] In this embodiment, the line numbers in the first code package can be used as the main loop list. The first line numbers are traversed to compare the line number of each line of code in the first code package with the first line number, thereby determining the temporary line number of the code in the first code package that has not been deleted. Specifically: (1) For each line number in the first code package, if the first comparison result indicates that the line number is less than the target first line number, then it is determined that the line number has not been deleted. The current line number offset is obtained, and the temporary line number of the code is obtained based on the line number and the current line number offset. The next line number adjacent to the line number is obtained according to the line number order, and the next line number adjacent to the line number is compared with the target first line number. The target first line number is the line number compared among the first line numbers. The line number offset indicates the number of deleted line numbers. (2) If the comparison result indicates that the line number is equal to the target first line number, then the line code is determined to be deleted, the current line number offset is updated to obtain the updated line number offset, and according to the sorting order of each first line number, the next first line number adjacent to the target first line number is obtained, the next first line number is determined as the new target first line number, and according to the line number order, the next line number adjacent to the line number is obtained, and the next line number adjacent to the line number is compared with the new target first line number; In this embodiment, the temporary line number of the code is obtained based on the line number and the current line number offset, including: Determine the difference between the line number and the current line number offset, and use this difference as the temporary line number for that line of code.

[0048] Table 1 provides an example:

[0049] Table 1. Code Line Number Table Sort the first row numbers in ascending order, as shown in Table 1. The first row numbers are "2", "3", "4", and "7". Assuming that the first row number "2" is used for comparison, then the first row number 2 is the target first row number. At this time, compare the first code package line number "1" with the first row number "2" in ascending order. The comparison result shows that the first code package line number is less than the first row number. It is determined that the line code corresponding to line number "1" in the first code package has not been deleted. At this time, the code corresponding to line number "1" is the target code, and line number "1" is the original line number of the target code. The current line number offset is 0, meaning no line number has been deleted. The difference "1" between the line number "1" in the first code package and the line number offset "0" is determined as the temporary line number of the code (the code with line number "1"). That is, the temporary line number is 1. Then, according to the line number order, that is, the order of the line numbers in the first code package, the next line number adjacent to the line number "1" is obtained, which is the line number "2".

[0050] Next, compare the line number "2" of the first code package with the first line number "2". The comparison result shows that the line number of the first code package is equal to the first line number. It is determined that the code corresponding to line number "2" in the first code package has been deleted. Update the current line number offset by adding 1 to the original line number offset. At this time, the updated line number offset becomes 1. Then, according to the sorting order of the line numbers of the first code package, obtain the next first code package line number "3" adjacent to the first code package line number "2", and according to the sorting order of the first line numbers, obtain the next first line number "3" adjacent to the target first line number "2". Determine the next first line number "3" as the new target first line number. Compare the next line number "3" adjacent to this line number with the new target first line number "3", that is, determine the first line number "3" as the new target first line number. Compare the next first code package line number "3" with the target first line number "3". The comparison result shows that the first code package line number is equal to the first line number. It is determined that the code corresponding to the line number "3" in the first code package has been deleted. Then, update the current line number offset, that is, increase the original line number offset by 1. At this time, the updated line number offset becomes 2.

[0051] Then, based on the sorting order of the first line numbers, obtain the next first line number "4" adjacent to the target first line number "3", and determine the next first line number "4" as the new target first line number. Also, based on the line number order, obtain the next line number "4" adjacent to this line number, and compare the next line number "4" adjacent to this line number with the new target first line number "4". That is, compare the first code package line number "4" with the target first line number "4". If the line number "4" is equal to the target first line number "4", it is determined that the code corresponding to the line number "4" in the first code package has been deleted. Update the current line number offset by increasing the original line number offset by 1. At this time, the updated line number offset becomes 3.

[0052] Then, based on the sorting order of the line numbers in the first code package, obtain the next first code package line number "5" adjacent to the first code package line number "4", and based on the sorting order of the first line numbers, obtain the next first line number "7" adjacent to the target first line number "4". Determine the next first line number "7" as the new target first line number. Compare the next line number "5" adjacent to this line number with the new target first line number "7", that is, compare the first code package line number "5" with the target first line number "7". If the comparison result shows that the first code package line number is less than the first line number, it is determined that the code corresponding to line number "5" in the first code package has not been deleted. At this time, the code corresponding to line number "5" is the target code, and line number "5" is the original line number of the target code.

[0053] The current line number offset is 3. The difference "2" between the line number "5" in the first code package and the line number offset "3" is determined as the temporary line number of the code (the code with line number "5"), that is, the temporary line number is 2. And so on, to get the temporary line number 3 of the line number "6" in the first code package.

[0054] Understandably, when the first line number traversal is complete, i.e. there is no more first line number to compare, the remaining line numbers in the first code package determine the temporary line number based on the line number offset determined by the last first line number. For example, if the first line number "7" in Table 1 is the last first line number and the corresponding line number offset is 4, then the temporary line number of line number "8" in the first code package is 4, the temporary line number of line number "9" is 5, and so on. The offset of the corresponding line numbers after line number "7" in the first code package is 4, and the code after line number "7" in the first code package has not been deleted.

[0055] After obtaining the temporary line number of the undeletable code in the first code package and the original line number of the undeletable code in the first code package, the updated line number of the undeletable code in the second code package can be determined based on the temporary line number, the original line number, and the second line number.

[0056] In some embodiments of the present invention, the second line numbers are sorted in ascending order, and the updated line number of the code that has not been deleted in the second code package is determined based on the temporary line number, the original line number, and the second line number. This may further include: For each original line number, determine the target second line number that is the same as that original line number; Starting from the second row number of the target row, determine the number of consecutively increasing second row numbers, and determine the number of row numbers as the displacement corresponding to the original row number.

[0057] Based on the displacement corresponding to the original line number and the corresponding temporary line number, the updated line number of the non-deleted code corresponding to the original line number is obtained in the second code package.

[0058] The original line number is the line number of the code in the first code package that was not deleted.

[0059] The second line number is the newly added line number in the second code package. The original line number of the target code in the first code package becomes a temporary line number after being transformed by the first line number. The corresponding coverage data in the temporary line number needs to be considered when applying it to the second code package, which also needs to take into account the existence of newly added lines in the second code package, that is, the displacement of the newly added lines.

[0060] As shown in Table 1, the original row numbers include 1, 5, 6, 8, and 9. The temporary line number of the original line number "1" is "1". There is a second line number "1" with the same number, and there is a case where the second line number corresponds to the line "1" and there are consecutive additions. Therefore, the displacement corresponding to the original line number "1" is 3. According to the original line number "5", there is no case where the second line number corresponds to the line "5" and there is only one second line number "5". Therefore, the displacement corresponding to the original line number "5" is 1.

[0061] In some embodiments of the present invention, obtaining the updated line number of the undeleted code in the second code package based on the displacement corresponding to the original line number and the corresponding temporary line number may further include: Get the displacements corresponding to each original line number before the original line number; get the total displacement based on the displacements corresponding to the original line number and the displacements corresponding to each original line number before the original line number; get the updated line number of the original line number in the second code package based on the sum of the total displacement and the corresponding temporary line number.

[0062] Using Table 1 as an example, the original line numbers are "1", "5", "6", "8", and "9". The second line numbers are "1", "2", "3", "5", and "7", totaling 5 line numbers. The original line number is "1". The target second line number that matches this original line number is determined. The smallest target second line number is "1". The line numbers that can be consecutively increased are "1", "2", and "3". The total number of consecutively increasing line numbers is 3. The displacement corresponding to the original line number "1" is 3. It is known that the temporary line number in the original line number corresponding to this target second line number is "1". The update line number of the undeleted code corresponding to the original line number in the second code package is the temporary line number plus the displacement corresponding to the original line number. Therefore, the update line number of the undeleted code corresponding to the original line number in the second code package is the temporary line number "1" plus the corresponding displacement 3. The update line number of the undeleted code corresponding to the original line number in the second code package is 4, and so on. The target second line number "5" that matches this original line number is determined. The displacement corresponding to the original line number and the displacements corresponding to the original line numbers before it are added together to get the total displacement of 3. Since the temporary line number of the original line number "5" is 3, the updated line number in the second code package is the total displacement of 3 plus the displacement of 1 corresponding to the original line number "5" plus the temporary line number 3. Therefore, the updated line number of the second code package is 7.

[0063] It should be noted that, considering that the update line number in the second code package may be occupied by the second line number, when calculating, if the update line number in the second code package is equal to the second line number, the update line number in the second code package will be shifted to the right. As shown in Table 1, the update line number "6" in the first code package is 7 in the second code package. However, the update line number in the second code package is equal to the second line number 7, so the update line number in the second code package is shifted to the right, and finally becomes 8.

[0064] The code coverage determination method provided in this embodiment of the invention, after obtaining the coverage of the target code in the second code package, determines the coverage of the newly added code in the second code package other than the target code. The coverage of the target code in the second code package has been calculated. The newly added code in the second code package other than the target code is arranged in ascending order, packaged, and uploaded to the server for code coverage calculation. At the same time, the code coverage of the newly added code in the second code package other than the target code also has a unique identifier according to the unique identifier of the second code package. The unique identifier of the coverage of the newly added code is integrated with the line number of the coverage of the target code to obtain the complete code coverage number in the second code package corresponding to the code in the second code package in sequence. Therefore, the code coverage under the code coverage number can be adaptively applied to each line of code. Based on the coverage of the newly added code and the coverage of the target code, the code coverage of the second code package is obtained. In the embodiments of the present invention, not only is the coverage of the target code not calculated, but the coverage of the code other than the target code is specifically calculated. Then, the coverage of the two sets of code is shifted in the form of line numbers using a line shifting algorithm to accurately shift the coverage of the two sets of code. This better solves the problem that the coverage of the old code cannot adapt to the new code, and that the accuracy and precision of the coverage of the old code is not high when simply shifted.

[0065] Based on the same inventive concept, please refer to Figure 4 This invention also provides a code coverage determination device 40, applied to... Figure 1 The electronic devices shown, such as Figure 4 As shown, the code coverage determination device 40 includes an acquisition module 401, a comparison module 402, a target line number generation module 403, and a code coverage determination module 404.

[0066] The acquisition module 401 is used to acquire a first code package and a second code package. The second code package is the code package obtained by updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code sorting. Comparison module 402 is used to compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package. The target line number generation module 403 is used to determine the updated line number of the target code in the second code and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package; the target code represents the code shared by the first code package and the second code package; The code coverage determination module 404 is used to obtain the coverage of the target code in the second code package based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0067] In some embodiments of the present invention, the target row number generation module 403 is used for: Sort the first line numbers in ascending order, and compare the line numbers of each line of code in the first code package with the sorted first line numbers in ascending order; Based on the comparison results, the temporary line numbers of the code that was not deleted in the first code package and the original line numbers of the code that was not deleted in the first code package are obtained; the code that was not deleted is the target code; Based on the temporary line number, the original line number, and the second line number, determine the update line number of the code that was not deleted in the second code package.

[0068] In some embodiments of the present invention, the target row number generation module 403 is used for: For each line number in the first code package, if the first comparison result indicates that the line number is less than the target first line number, then it is determined that the line number has not been deleted. The current line number offset is obtained, and a temporary line number for the code is obtained based on the line number and the current line number offset. Then, according to the line number order, the next line number adjacent to the current line number is obtained, and the next line number adjacent to the current line number is compared with the target first line number. The target first line number is the line number being compared among all the first line numbers. The line number offset indicates the number of deleted line numbers. If the comparison result indicates that the line number is equal to the target first line number, then it is determined that the line of code has been deleted. The current line number offset is updated to obtain the updated line number offset. According to the sorting order of the first line numbers, the next first line number adjacent to the target first line number is obtained and determined as the new target first line number. According to the line number order, the next line number adjacent to the current line number is obtained and compared with the new target first line number.

[0069] In some embodiments of the present invention, the target row number generation module 403 is used for: Determine the difference between the line number and the current line number offset, and use this difference as the temporary line number for that line of code.

[0070] In some embodiments of the present invention, the target row number generation module 403 is used for: For each original line number, determine the target second line number that is the same as that original line number; Starting from the second row number of the target row, determine the number of consecutively increasing second row numbers, and determine the number of row numbers as the displacement corresponding to the original row number; Based on the displacement corresponding to the original line number and the corresponding temporary line number, the updated line number of the non-deleted code corresponding to the original line number is obtained in the second code package.

[0071] In some embodiments of the present invention, the target row number generation module 403 is used for: Get the displacement corresponding to each original row number that is sorted before this original row number; The total displacement is obtained by considering the displacement corresponding to the original row number and the displacements corresponding to each original row number preceding it. Based on the sum of the total displacement and the corresponding temporary line number, the updated line number of the original line number in the second code package corresponding to the non-deleted code is obtained.

[0072] In some embodiments of the present invention, after obtaining the coverage of the target code in the second code package, the code coverage determination module 404 is used to: Determine the coverage of the newly added code in the second code package, excluding the target code; The code coverage of the second code package is obtained based on the coverage of the newly added code and the coverage of the target code.

[0073] It should be noted that in this invention, the relevant contents of the acquisition module 401, comparison module 402, target line number generation module 403, and code coverage determination module 404 correspond one-to-one with those described above. Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the code coverage determination device and its corresponding modules described above can be referred to as follows: Figures 1 to 2 The description of the code coverage determination module method in any embodiment will not be repeated here.

[0074] To better implement the code coverage determination method of the present invention, based on the code coverage determination method, the present invention also provides an electronic device that integrates the code coverage determination device provided by the present invention, such as... Figure 3 As shown, the device includes: One or more processors 301; Memory 302; and One or more applications, wherein the one or more applications are stored in memory 302 and configured to be executed by processor 301 of the steps in the code coverage determination method of any embodiment described above.

[0075] like Figure 3 As shown, it illustrates a schematic diagram of an embodiment of the electronic device involved in this invention, specifically: The device may include components such as a processor 301 with one or more processing cores, a memory 302 with one or more computer-readable storage media, a power supply 303, and an input unit 304. Those skilled in the art will understand that... Figure 3 The device structure shown does not constitute a limitation on the device and may include more or fewer components than illustrated, or combine certain components, or have different component arrangements. Wherein: The processor 301 is the control center of the device, connecting various parts of the device through various interfaces and lines. It performs various functions and processes data by running or executing software programs and / or modules stored in the memory 302, and by calling data stored in the memory 302, thereby providing overall monitoring of the device. Optionally, the processor 301 may include one or more processing cores; the processor 301 may be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor. Preferably, the processor 301 may integrate an application processor and a modem processor, wherein the application processor mainly handles the operating system, user interface, and application programs, and the modem processor mainly handles wireless communication. It is understood that the aforementioned modem processor may not be integrated into the processor 301.

[0076] Memory 302 can be used to store software programs and modules (such as...) Figure 4The processor 301 executes various functional applications and data processing by running software programs and modules stored in the memory 302, which determine the code coverage of all functional modules in the device. The memory 302 may primarily include a program storage area and a data storage area. The program storage area may store the operating system, applications required for at least one function, etc.; the data storage area may store data created based on the use of the device, etc. Furthermore, the memory 302 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. Accordingly, the memory 302 may also include a memory controller to provide the processor 301 with access to the memory 302.

[0077] The device also includes a power supply 303 that supplies power to the various components. Preferably, the power supply 303 can be logically connected to the processor 301 through a power management system, thereby enabling functions such as charging, discharging, and power consumption management through the power management system. The power supply 303 may also include one or more DC or AC power supplies, recharging systems, power fault detection circuits, power converters or inverters, power status indicators, and other arbitrary components.

[0078] The device may also include an input unit 304, which can be used to receive input numeric or character code information, and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function control.

[0079] Although not shown, the device may also include a display unit, etc., which will not be described in detail here. Specifically, in this invention, the processor 301 in the device loads the executable files corresponding to the processes of one or more application programs into the memory 302 according to the following instructions, and the processor 301 runs the application programs stored in the memory 302 to realize various functions, as follows: Obtain the first code package and the second code package. The second code package is the code package obtained by updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code order. Compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package; Based on the first line number, the second line number, and the line number of each line of code in the first code package, determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package; the target code represents the code shared by the first code package and the second code package; The coverage of the target code in the second code package is obtained based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0080] Therefore, the present invention provides a computer-readable storage medium, which may include: read-only memory (ROM), random access memory (RAM), magnetic disk or optical disk, etc. A computer program is stored thereon, and the computer program is loaded by a processor to execute the steps in any of the code coverage determination methods provided by the present invention. For example, the computer program loaded by the processor can execute the following steps: Obtain the first code package and the second code package. The second code package is the code package obtained by updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code order. Compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package; Based on the first line number, the second line number, and the line number of each line of code in the first code package, determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package; the target code represents the code shared by the first code package and the second code package; The coverage of the target code in the second code package is obtained based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package.

[0081] Because of the instructions stored in the computer-readable storage medium, the present invention can be executed as follows: Figures 1 to 3 Corresponding to the steps in the code coverage determination method in any embodiment, the present invention can be implemented as follows. Figures 1 to 3 For details on the beneficial effects that the code coverage determination method can achieve in any embodiment, please refer to the preceding description, which will not be repeated here.

[0082] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the detailed descriptions of other embodiments above, which will not be repeated here.

[0083] In practice, each of the above units or structures can be implemented as an independent entity or can be arbitrarily combined to be implemented as the same or several entities. For specific implementation of each of the above units or structures, please refer to the previous embodiments, which will not be repeated here.

[0084] The foregoing has provided a detailed description of the code coverage determination method, apparatus, device, and storage medium provided by the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The above description is only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, those skilled in the art will recognize that there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.

Claims

1. A method for determining code coverage, characterized in that, The method includes: Obtain a first code package and a second code package. The second code package is obtained by updating the first code package. Both the first code package and the second code package contain multiple lines of code, and each line of code has a unique line number according to the code order. Compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package; Based on the first line number, the second line number, and the line number of each line of code in the first code package, determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package; the target code represents code shared by the first code package and the second code package; The coverage of the target code in the second code package is obtained based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package. The step of determining the updated line number of the target code in the second code package and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package includes: Sort each of the first line numbers in ascending order, and compare the line number of each line of code in the first code package with the sorted first line numbers in ascending order; Based on the comparison results, the temporary line number of the code that was not deleted in the first code package and the original line number of the code that was not deleted in the first code package are obtained; the code that was not deleted is the target code; Based on the temporary line number, the original line number, and the second line number, determine the updated line number of the non-deleted code in the second code package.

2. The code coverage determination method according to claim 1, characterized in that, The step of comparing the line numbers of each line of code in the first code package in ascending order with the sorted first line numbers, and obtaining the temporary line numbers of the code that has not been deleted in the first code package based on the comparison results, includes: For each line number in the first code package, if the comparison result indicates that the line number is less than the target first line number, it is determined that the line number has not been deleted. The current line number offset is obtained, and a temporary line number for the code is obtained based on the line number and the current line number offset. Then, according to the line number order, the next line number adjacent to the current line number is obtained, and the next line number adjacent to the current line number is compared with the target first line number. The target first line number is the line number being compared among all the first line numbers. The line number offset indicates the number of deleted line numbers. If the comparison result indicates that the line number is equal to the target first line number, then it is determined that the line of code has been deleted. The current line number offset is updated to obtain the updated line number offset. According to the sorting order of the first line numbers, the next first line number adjacent to the target first line number is obtained, and the next first line number is determined as the new target first line number. According to the line number order, the next line number adjacent to the first line number is obtained, and the next line number adjacent to the first line number is compared with the new target first line number.

3. The code coverage determination method according to claim 2, characterized in that, The process of obtaining the temporary line number of the code based on the line number and the current line number offset includes: Determine the difference between the line number and the current line number offset, and use the difference as the temporary line number for that line of code.

4. The code coverage determination method according to claim 1, characterized in that, The second line numbers are sorted in ascending order. The step of determining the updated line number of the undeletable code in the second code package based on the temporary line number, the original line number, and the second line numbers includes: For each of the original line numbers, determine a target second line number that is the same as the original line number; Taking the target second row number as the starting row number, determine the number of row numbers for which the row numbers of the second row number increase consecutively, and determine the number of row numbers as the displacement corresponding to the original row number; Based on the displacement corresponding to the original line number and the corresponding temporary line number, the updated line number of the non-deleted code corresponding to the original line number in the second code package is obtained.

5. The code coverage determination method according to claim 4, characterized in that, The step of obtaining the updated line number of the non-deleted code in the second code package based on the displacement corresponding to the original line number and the corresponding temporary line number includes: Get the displacement corresponding to each original row number that is sorted before this original row number; The total displacement is obtained by considering the displacement corresponding to the original row number and the displacements corresponding to each original row number preceding it. Based on the sum of the total displacement and the corresponding temporary line number, the updated line number of the original line number in the second code package corresponding to the non-deleted code is obtained.

6. The code coverage determination method according to any one of claims 1-5, characterized in that, After obtaining the coverage of the target code in the second code package, the method further includes: Determine the coverage of the newly added code in the second code package, excluding the target code; The code coverage of the second code package is obtained based on the coverage of the newly added code and the coverage of the target code.

7. A code coverage determination device, characterized in that, The code coverage line shift adaptive device includes: The acquisition module is used to acquire a first code package and a second code package. The second code package is a code package obtained by updating the first code package. Both the first code package and the second code package include multiple lines of code, and each line of code has a unique line number according to the code order. The comparison module is used to compare the code in the first code package and the second code package to obtain the first line number of the deleted code in the first code package and the second line number of the newly added code in the second code package. The target line number generation module is used to determine the updated line number of the target code in the second code package and the original line number of the target code in the first code package based on the first line number, the second line number, and the line number of each line of code in the first code package; the target code represents the code shared by the first code package and the second code package; The code coverage determination module is used to obtain the coverage of the target code in the second code package based on the coverage data of the first code package, the updated line number of the target code in the second code package, and the original line number of the target code in the first code package. The target line number generation module is used to sort the first line numbers in ascending order, and compare the line number of each line of code in the first code package with the sorted first line numbers in ascending order; based on the comparison result, obtain the temporary line number of the undeleted code in the first code package and the original line number of the undeleted code in the first code package; the undeleted code is the target code; and determine the updated line number of the undeleted code in the second code package based on the temporary line number, the original line number, and the second line number.

8. An electronic device, characterized in that, The method includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the code coverage determination method according to any one of claims 1 to 6.

9. A readable storage medium, characterized in that, The readable storage medium includes a computer program, which, when executed, controls the calibration device containing the readable storage medium to perform the code coverage determination method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method and device for obtaining code coverage rate

    CN106201863A

  • Coverage rate data processing method, terminal equipment and computer readable storage medium

    CN111078563A