Method and device for obtaining incremental code coverage rate, equipment and storage medium

By obtaining and compiling code file sets, calculating the incremental code coverage, solving the problem of inaccurate code coverage in the existing technology, and achieved higher coverage accuracy.

CN119357041APending Publication Date: 2025-01-24CHINA UNITED NETWORK COMM GRP CO LTD +2
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310907176.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-07-21
Publication Date
2025-01-24

AI Technical Summary

Technical Problem

The existing technology is inaccurate in obtaining incremental code coverage. Mainstream testing tools do not support obtaining incremental code coverage, and the minimum coverage granularity is file-level, so data on line coverage in incremental code cannot be directly provided.

Method used

By obtaining the code file set, the code file submitted by the user, and the third code file set consisting of the code and the previous version of the code; according to the first code file set and the second code file set, the incremental amount is obtained Code information, compile the third code file set, get abstract syntax trees, determine the number and total number of code rows in the target change file, and then calculate the incremental code coverage.

Benefits of technology

By changing the minimum particle size from the file level to the code level, the accuracy of the code coverage rate is improved, and the problem of leakage of the new code file caused by the leakage of the script configuration has improved the accuracy of the incremental code coverage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119357041A_ABST
    Figure CN119357041A_ABST
Patent Text Reader

Abstract

The invention provides a method, a device and equipment for obtaining an incremental code coverage rate and a storage medium. Comprising the steps that a current version of a target program is obtained as a first code file set, a previous version of the target program is obtained as a second code file set, and a third code file set comprising the second code file set and the first code file set is obtained; incremental code information is obtained; compiling the third code file set to obtain an abstract syntax tree of the third code file set; determining a target change file and a target change code line according to the incremental code information and the abstract syntax tree; determining the number of compiled target change code lines in the target change file and the total number of the target change code lines in the target change file; the incremental code coverage rate is determined according to the number of the compiled target change code lines in the target change file and the total number of the target change code lines in the target change file, and the accuracy of obtaining the incremental code coverage rate is improved through the abstract syntax tree and the incremental code information.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of Internet technology, and in particular to a method, apparatus, device and storage medium for obtaining incremental code coverage. Background Art

[0002] In order to ensure the quality of the code, the code repository administrator requires that the source code submitted by the program must be covered by test cases and has certain requirements for test coverage. The test coverage not only assesses the existing code coverage (i.e., the overall code coverage), but also considers the incremental code coverage. Among them, the incremental code coverage is more practical. The incremental code coverage ensures that each submitted code can be fully tested, which can expose problems in advance and avoid accidents caused by code going online and problems that cannot be delivered on time.

[0003] At present, mainstream testing tools generally do not support obtaining incremental code coverage. At the same time, the current code coverage is also calculated for the full code coverage, and the minimum coverage granularity is at the file level, which cannot directly provide data on line coverage in the incremental code. The existing method for obtaining incremental code coverage is: obtain all lines of incremental code through git instructions, obtain the test report generated by the test script, obtain the uncovered line number by finding the corresponding characters in the test report, and obtain the incremental code coverage through the uncovered lines and the total line number of the incremental code.

[0004] However, the incremental code coverage obtained by the prior art has the problem of inaccuracy. Summary of the invention

[0005] The present application provides a method, apparatus, device and storage medium for obtaining incremental code coverage, so as to solve the problem of inaccurate incremental code coverage.

[0006] In a first aspect, the present application provides a method for obtaining incremental code coverage, comprising:

[0007] Obtain a first code file set, a second code file set, and a third code file set, wherein the first code file set is a code file set of a current version of the target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set;

[0008] According to the first code file set and the second code file set, incremental code information is obtained, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type;

[0009] Compiling the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes a second change code line belonging to the target type;

[0010] Determine a target change file that exists in both the first change file subset and the second change file subset, and a target change code line in the target change file;

[0011] determining the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file;

[0012] The incremental code coverage is determined according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

[0013] In this application, obtaining a first code file set, a second code file set, and a third code file set includes:

[0014] Obtain a first code file set and a second code file set;

[0015] Determining different file subsets of the first code file set and the second code file set;

[0016] A third code file set is obtained according to the different file subsets and the second code file set.

[0017] In this application, incremental code information is obtained according to the first code file set and the second code file set, including:

[0018] In response to a first acquisition instruction sent by a user, a first change file subset is determined from the first code file set and the second code file set, wherein the first acquisition instruction is used to determine a change file from the first code file set and the second code file set, and determine the first change file subset according to the change file;

[0019] In response to a second acquisition instruction from a user, determining a first changed code line of a target type in the first changed file subset, the second acquisition instruction being used to determine the type of the changed code line in the first changed file subset, and determining the first changed code line of the target type according to the type of the changed code line;

[0020] Incremental code information is obtained according to the first changed file subset and the first changed code line of the target type in the first changed file subset.

[0021] In the present application, the third code file set is compiled to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, including:

[0022] Compile the third code file set to obtain an abstract syntax tree;

[0023] Traverse the abstract syntax tree to obtain a second subset of changed files;

[0024] All files of the second changed file subset are traversed to obtain second changed code lines of the target type in the second changed file subset.

[0025] In the present application, determining the target change files that exist in both the first change file subset and the second change file subset, and the target change code lines in the target change files, includes:

[0026] Perform file matching on the first changed file subset and the second changed file subset to obtain a target changed file;

[0027] According to the target change file, the first change code line, and the second change code line, a target change code line in the target change file is obtained.

[0028] In the present application, according to the target change file, the first change code line, and the second change code line, the target change code line in the target change file is obtained, including:

[0029] Determining an initial target change code line according to the first change code line and the second change code line;

[0030] According to the initial target change code line, determine the target change code line in the target change file.

[0031] In the present application, determining the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file includes:

[0032] Inject the probe into the target changed code line of the target changed file;

[0033] Determine a target probe in the probe, where the target probe is a probe corresponding to a target changed code line to be compiled;

[0034] the number of detection probes and the number of target probes;

[0035] According to the number of probes, determine the total number of target changed code lines in the target changed file set;

[0036] According to the number of target probes, the number of target change code lines compiled in the target change file is determined.

[0037] In the present application, after determining the incremental code coverage according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file, the method further includes:

[0038] Determine a preset incremental code coverage threshold;

[0039] If the incremental code coverage is greater than the incremental code coverage threshold, the third code file set is used as the second code file set of the next version;

[0040] If the incremental code coverage is less than or equal to the incremental code coverage threshold, the steps of obtaining the first code file set, the second code file set, and the third code file set are re-executed.

[0041] In a second aspect, the present application provides a device for obtaining incremental code coverage, comprising:

[0042] An acquisition module is provided to acquire a first code file set, a second code file set and a third code file set, wherein the first code file set is a code file set of a current version of the target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set;

[0043] A first obtaining module obtains incremental code information according to the first code file set and the second code file set, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type;

[0044] A second obtaining module compiles the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes a second change code line belonging to the target type;

[0045] A first determination module determines a target change file that exists in both the first change file subset and the second change file subset, and a target change code line in the target change file;

[0046] A second determination module determines the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file;

[0047] The third determination module determines the incremental code coverage according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

[0048] In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory communicatively connected to the processor;

[0049] Memory stores computer-executable instructions;

[0050] The processor executes the computer-executable instructions stored in the memory to implement the method in the present application.

[0051] In a fourth aspect, the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the method of the present application.

[0052] The present application provides a method, apparatus, device and storage medium for obtaining incremental code coverage, which obtains a first code file set of a previous version, a second code file set submitted by a user and a third code file set consisting of the user-submitted code and the previous version code; obtains a first change file subset according to the first code file set and the second code file set, the first change file subset includes a first change code line of an addition type or a modification type; compiles the third code file set to obtain an abstract syntax tree of the third code file set, and a second change file subset in the abstract syntax tree, the second change file subset includes a second change code line of an addition type or a modification type; determines a target change file that exists in both the first change file subset and the second change file subset, and a target Changed code lines; determining the number of target changed code lines compiled in the target changed file and the total number of target changed code lines in the target changed file; determining incremental code coverage based on the number of target changed code lines compiled in the target changed file and the total number of target changed code lines in the target changed file, determining incremental code lines through incremental code information and abstract syntax trees, changing the minimum coverage granularity from file level to code line level, which can improve the accuracy of code coverage compared with traditional coverage tools that obtain coverage at the file level, and at the same time, obtaining compiled changed code lines through pointer probing and using test scripts to find the corresponding characters in the test report to obtain uncompiled changed code lines for comparison can avoid the problem of missing coverage of new code files due to script configuration, thereby improving the accuracy of the acquired incremental code coverage. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0054] Figure 1 A schematic diagram of a process for obtaining incremental code coverage provided in an embodiment of the present application;

[0055] Figure 2 A schematic diagram of another process for obtaining incremental code coverage provided in an embodiment of the present application;

[0056] Figure 3 A schematic diagram of a structural device for obtaining incremental code coverage provided in an embodiment of the present application;

[0057] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.

[0058] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0059] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0060] In the prior art, all lines of incremental code are obtained through git instructions, a test report is generated through a test script, the uncovered line numbers are obtained by finding the characters corresponding to the test report, and the incremental code coverage is obtained by the uncovered line numbers and the total line numbers of the incremental code. This method may cause problems in the configuration file, resulting in omission of new files and failure to calculate the coverage of the new files, thereby causing inaccurate coverage calculation.

[0061] In order to solve the above problems, the present application provides a method, apparatus, device and storage medium for obtaining incremental code coverage, which obtains a first code file set of a previous version, a second code file set submitted by a user and a third code file set consisting of the user-submitted code and the previous version code; obtains a first change file subset according to the first code file set and the second code file set, and the first change file subset includes a first change code line of an addition type or a modification type; compiles the third code file set to obtain an abstract syntax tree of the third code file set, and a second change file subset in the abstract syntax tree, and the second change file subset includes a second change code line of an addition type or a modification type; determines a target change file that exists in both the first change file subset and the second change file subset, and the target change file The target changed code lines in the target changed file; determine the number of target changed code lines compiled in the target changed file and the total number of target changed code lines in the target changed file; determine the incremental code coverage according to the number of target changed code lines compiled in the target changed file and the total number of target changed code lines in the target changed file, determine the incremental code lines through incremental code information and abstract syntax tree, change the minimum coverage granularity from file level to code line level, and improve the accuracy of code coverage compared with traditional coverage tools that obtain coverage at file level. At the same time, obtaining compiled changed code lines through pointer probing and using test scripts to find corresponding characters in test reports to obtain uncompiled changed code lines can avoid the problem of missing coverage of new code files due to script configuration, thereby improving the accuracy of the obtained incremental code coverage.

[0062] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0063] The execution subject of the determination method provided in the embodiment of the present application may be a server. The server may be a mobile phone, a tablet, a computer and other devices. The present embodiment does not impose any special restrictions on the implementation method of the execution subject, as long as the execution subject can obtain the first code file set, the second code file set and the third code file set, the first code file set is the code file set of the current version of the target program, the second code file set is the code file set of the previous version of the target program, and the third code file set includes the second code file set and the first code file set; based on the first code file set and the second code file set, incremental code information is obtained, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type represents that the type of the change code line is an addition type or a modification type. type; compile the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes a second change code line belonging to the target type; determine the target change files that exist in the first change file subset and the second change file subset, and the target change code lines in the target change files; determine the number of target change code lines compiled in the target change files and the total number of target change code lines in the target change files; determine the incremental code coverage based on the number of target change code lines compiled in the target change files and the total number of target change code lines in the target change files.

[0064] Among them, code coverage is a metric in software testing, which describes the proportion and degree of source code tested in the program. The resulting proportion is called code coverage.

[0065] An Abstract Syntax Tree (AST), or just syntax tree, is a tree representation of the abstract grammatical structure of a text (usually source code) written in a formal language. Each node on the tree represents a structure that occurs in the text. The syntax is abstract because it does not represent every detail that occurs in a real grammar, but only structural or content-related details. For example, grouping brackets are implicit in the tree structure, so these brackets do not need to be represented as separate nodes. Similarly, grammatical structures like if-condition-then statements can be represented by a node with three branches. This distinguishes abstract syntax trees from concrete syntax trees, which are traditionally called parse trees. Parse trees are often constructed by a parser during source code translation and compilation. Once built, additional information is added to the AST through subsequent processing, such as context analysis. Abstract syntax trees are also used in program analysis and program transformation systems.

[0066] Figure 1A schematic diagram of a process for obtaining incremental code coverage provided by an embodiment of the present application. Figure 1 As shown, the execution subject of the method can be a server or other servers, and this embodiment is not particularly limited here. Figure 1 As shown, the method includes:

[0067] S101, obtaining a first code file set, a second code file set and a third code file set, wherein the first code file set is a code file set of a current version of a target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set.

[0068] Among them, the first code file set is the code file set uploaded by the user this time, and the second code file set can be obtained through the git log command, or external input specifies the branch (branch), commit (note), and tag (tag) of the current requirement, but the first code file set and the second code file set must be in the same branch.

[0069] In the embodiment of the present application, obtaining the first code file set, the second code file set, and the third code file set includes:

[0070] Obtain a first code file set and a second code file set;

[0071] Determining different file subsets of the first code file set and the second code file set;

[0072] A third code file set is obtained according to the different file subsets and the second code file set.

[0073] Among them, the third code file set is obtained by combining the first code file set and the second code file set. If the two have the same file subset, it means that the code lines in the first code file set and the same file subset of the first code file set are different, and the corresponding subset in the second code file set is retained here.

[0074] S102. Obtain incremental code information according to the first code file set and the second code file set, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type.

[0075] Among them, the incremental code information can refer to changed files and changed file types, changed code lines in changed files and changed code line types in changed files. The incremental code information can be determined by comparing the first code file set with the second code file set to determine the changed files with code changes in the file set.

[0076] The changed files are obtained by comparing the user-uploaded code with the specified source code.

[0077] The first change file subset represents the files that have changed as recorded in the incremental code information. In some embodiments, the first change file subset may include change code lines. The change code lines can be divided into addition type, deletion type, and modification type according to type. Among them, the addition type can represent the code lines that are newly added by the change code behavior, the deletion type can represent the code lines that are deleted by the change code behavior, and the modification type can represent the code lines that are modified by the change code behavior. In the embodiment of the present application, the first change code line belonging to the target type can refer to the change code line belonging to the addition type or the modification type.

[0078] Therefore, since the changed code lines belonging to the deletion type cannot be compiled and the coverage cannot be calculated, it is necessary to filter the first changed code of the deletion type to ensure that the calculation result of the coverage is more accurate.

[0079] In the embodiment of the present application, the incremental code information is obtained according to the first code file set and the second code file set, including:

[0080] In response to a first acquisition instruction sent by a user, a first change file subset is determined from the first code file set and the second code file set, wherein the first acquisition instruction is used to determine a change file from the first code file set and the second code file set, and determine the first change file subset according to the change file;

[0081] In response to a second acquisition instruction from a user, determining a first changed code line of a target type in the first changed file subset, the second acquisition instruction being used to determine the type of the changed code line in the first changed file subset, and determining the first changed code line of the target type according to the type of the changed code line;

[0082] Incremental code information is obtained according to the first changed file subset and the first changed code line of the target type in the first changed file subset.

[0083] The first changed file subset types include addition, modification, and deletion. Addition indicates that the first changed file subset is added, deletion indicates that the first file subset is deleted, and addition indicates that the first file subset is added.

[0084] The first instruction is an instruction that can filter out changed files and changed file types, and can be a diff instruction or a grep instruction. In some embodiments, the changed files can be obtained by inputting the first instruction.

[0085] The second instruction is an instruction that can filter out changed code lines and changed code line types, and can be a git diff instruction or a git blame instruction. In some embodiments, the changed code lines can be obtained by inputting the first instruction.

[0086] S103. Compile the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes second change code lines belonging to the target type.

[0087] Among them, the second change code line types in the second change file subset include addition, deletion, and modification. Addition indicates that the second change code line is added, deletion indicates that the second change code line is deleted, and modification indicates that the second code line is modified.

[0088] In the embodiment of the present application, the third code file set is compiled to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, including:

[0089] Compile the third code file set to obtain an abstract syntax tree;

[0090] Traverse the abstract syntax tree to obtain a second subset of changed files;

[0091] All files of the second changed file subset are traversed to obtain second changed code lines of the target type in the second changed file subset.

[0092] The second change file subset is obtained through an abstract syntax tree, and the first change file subset is obtained by comparing the first code file set with the second code file set.

[0093] Traverse the abstract syntax tree of each file. If the file has not changed, skip processing. If the file has changed, put the file into the second changed file subset. Process all files in sequence to obtain the changed file subset.

[0094] When a changed file is identified, all lines of the file are traversed to determine whether it is a changed line and the type of the changed line. If a new or modified changed line is identified, the changed line is added to the second code changed line.

[0095] S104: Determine target change files that exist in both the first change file subset and the second change file subset, and target change code lines in the target change files.

[0096] The target change file is a newly added or modified file subset, the target change code line is a newly added or modified file subset, and the target change file contains the target change code line.

[0097] By traversing the second changed file subset, the same files in the first changed file subset are obtained, and after traversing all the second changed file subsets, the file subset consisting of the same files is obtained, which is the target changed file.

[0098] In the embodiment of the present application, determining the target change file that exists in both the first change file subset and the second change file subset, and the target change code line in the target change file includes:

[0099] Perform file matching on the first changed file subset and the second changed file subset to obtain a target changed file;

[0100] According to the target change file, the first change code line, and the second change code line, a target change code line in the target change file is obtained.

[0101] The second changed file subset is traversed to find the file subset that is the same as the first changed file subset, and the target file subset is obtained.

[0102] Traverse the second changed code line, find the code line that is the same as the first changed code line, and obtain the target changed code line.

[0103] In the embodiment of the present application, obtaining the target change code line in the target change file according to the target change file, the first change code line, and the second change code line includes:

[0104] Determining an initial target change code line according to the first change code line and the second change code line;

[0105] According to the initial target change code line, determine the target change code line in the target change file.

[0106] S105: Determine the number of compiled target change code lines in the target change file and the total number of target change code lines in the target change file.

[0107] Among them, the compiled target change code line represents valid code, and being able to be compiled means that the function of the target code line can finally take effect and is not redundant invalid code.

[0108] In the embodiment of the present application, determining the number of compiled target change code lines in the target change file and the total number of target change code lines in the target change file includes:

[0109] Inject the probe into the target changed code line of the target changed file;

[0110] Determine a target probe in the probe, where the target probe is a probe corresponding to a target changed code line to be compiled;

[0111] the number of detection probes and the number of target probes;

[0112] According to the number of probes, determine the total number of target changed code lines in the target changed file set;

[0113] According to the number of target probes, the number of target change code lines compiled in the target change file is determined.

[0114] Among them, the probe can identify and mark all target changed code lines, and can distinguish the compiled target changed code lines. Here, the probe can be a counter. The initial value of the timer marking the target changed line is 0. When the target changed line is compiled once, the timer counts automatically. The final total number of counters is the number of target code lines. All counters with counts greater than or equal to 1 are the total number of compiled target code lines.

[0115] S106 : Determine the incremental code coverage rate according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

[0116] The incremental code coverage is obtained by dividing the number of compiled target change code lines by the total number of target change code lines in the target change file.

[0117] After determining the incremental code coverage rate according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file, the method further includes:

[0118] Determine a preset incremental code coverage threshold;

[0119] If the incremental code coverage is greater than the code coverage threshold, the third code file set is used as the second code file set for the next version;

[0120] If the incremental code coverage is less than or within the code coverage threshold, the steps of obtaining the first code file set, the second code file set, and the third code file set are re-executed.

[0121] The preset incremental code coverage threshold may be set by the user, and may be 60%, 80%, or 90%.

[0122] When the actual incremental code coverage is less than the preset threshold, it means that there is a missed test, and the tester needs to re-execute the first code file set, the second code file set, and the third code file set to obtain the incremental code coverage again; if the tester ensures that there is no missed test, it means that there is a problem with the code logic, and the programmer needs to modify the code before obtaining the incremental code coverage again.

[0123] When the actual incremental code rate is greater than the preset threshold, it means that the code quality has reached the indicator of this incremental code coverage. The first code file set is merged into the library, the code is put online, and the first code file set merged into the library this time, that is, the third code file set, is used as the second code file set for the next execution to obtain the incremental code coverage.

[0124] The embodiment of the present application provides a method for obtaining incremental code coverage, which obtains incremental code information through a first code file set and a second code file set, merges the first code file set and the second code file set to obtain a third file set, and compiles the third file set to obtain an abstract syntax tree. The incremental code information is compared with the abstract syntax tree to obtain the changed lines and changed files, and the compilation is started to obtain the compiled target code lines and target code lines, and the value of the compiled target code lines divided by the target code lines is calculated to obtain the incremental code coverage. This aspect solves the problem that mainstream coverage tools cannot obtain incremental code coverage, and avoids the problem of configuration file errors leading to the coverage of new code files, thereby improving the accuracy of obtaining incremental code coverage.

[0125] Figure 2 Another flow chart of obtaining incremental code coverage provided by the embodiment of the present application. Figure 2 As shown, the execution subject of the method can be a server or other servers, and this embodiment is not particularly limited here. Figure 2 As shown, the method includes:

[0126] S201, obtain a first code file set through user submission, and obtain a second code file set through local incremental git log and specified source branch / commit / tag;

[0127] S202, obtaining changed files and their types through diff and grep commands, obtaining changed code lines and their types through git diff and gitblame commands, where the types can be any one of addition, deletion, and addition, and the changed code files, changed code file types, changed code lines, and changed code line types constitute incremental code information;

[0128] S203, start compiling and generate AST Node, which contains incremental files and the number of code lines corresponding to the incremental files. By comparing ASTnode and incremental code information, determine whether it is a changed file. If so, determine whether it is a new or modified type of change line. If so, perform pointer exploration;

[0129] S204, clone the node information, add a timer for the newly added and modified code lines of the type, and then replace the current node information. The initial value of the counter is 0. If the changed line is compiled once, the counter counts up by 1;

[0130] S205, obtaining the number of all counters whose counts are greater than or equal to 1 as the number of lines of the compiled incremental code, obtaining the number of all counters as the number of all incremental code lines, and dividing the number of compiled incremental code lines by the number of all incremental code lines to obtain code coverage;

[0131] S206. Set a code coverage threshold. If the actual code coverage is greater than the code coverage threshold, the first code file set code is put into storage and put online. If the actual code coverage is less than the code coverage threshold, the tester determines whether there is any missed test. If the tester determines that there is no missed test, it is handed over to the developer. After the developer adjusts the code, the code coverage is tested again until the requirements are met and the first code file set code can be put into storage and put online.

[0132] Another method for obtaining incremental code coverage provided by an embodiment of the present application is to build an incremental code test coverage platform based on the existing mainstream code coverage tools, thereby reducing the testing costs of developers and testers and improving efficiency. Ensuring that sufficient testing can be carried out during the testing phase helps to expose problems in advance and avoid online accidents. At the same time, it is possible to objectively measure the test results of the current requirements, intuitively associate the code with the test results, and enable developers and testers to intuitively perceive the test status of the changed parts of this requirement, ensuring the completeness of the test and the quality of the code.

[0133] Figure 3 This is a structural example diagram of the device for obtaining incremental code coverage provided by an embodiment of the present application. Figure 3 As shown, the device 30 for obtaining incremental code coverage includes: an obtaining module 301, a first obtaining module 302, a second obtaining module 303, a first determining module 304, a second determining module 305, and a third determining module 306. Among them:

[0134] An acquisition module 301 acquires a first code file set, a second code file set, and a third code file set, wherein the first code file set is a code file set of a current version of a target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set;

[0135] A first obtaining module 302 obtains incremental code information according to the first code file set and the second code file set, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type;

[0136] A second obtaining module 303 compiles the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes a second change code line belonging to the target type;

[0137] A first determination module 304 determines a target change file that exists in both the first change file subset and the second change file subset, and a target change code line in the target change file;

[0138] The second determination module 305 determines the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file;

[0139] The third determination module 306 determines the incremental code coverage according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

[0140] In the embodiment of the present application, the acquisition module 301 is further specifically used for:

[0141] Obtain a first code file set and a second code file set;

[0142] Determining different file subsets of the first code file set and the second code file set;

[0143] A third code file set is obtained according to the different file subsets and the second code file set.

[0144] In the embodiment of the present application, the first obtaining module 302 is further specifically used for:

[0145] In response to a first acquisition instruction sent by a user, determining a first change file subset from the first code file set and the second code file set, the first acquisition instruction being used to acquire the first change file subset and the first change file subset type;

[0146] In response to a second acquisition instruction from a user, determining a first changed code line in the first changed file subset that belongs to a target type, the second acquisition instruction being used to acquire the first changed code line in the first changed file subset and the type of the first changed code line in the first changed file subset;

[0147] In the embodiment of the present application, the second obtaining module 303 is further specifically used for:

[0148] Compile the third code file set to obtain an abstract syntax tree;

[0149] Traverse the abstract syntax tree to obtain a second subset of changed files;

[0150] All files of the second changed file subset are traversed to obtain second changed code lines of the target type in the second changed file subset.

[0151] In the embodiment of the present application, the first determining module 304 is further specifically configured to:

[0152] Perform file matching on the first changed file subset and the second changed file subset to obtain a target changed file;

[0153] According to the target change file, the first change code line, and the second change code line, a target change code line in the target change file is obtained.

[0154] In the embodiment of the present application, the second determining module 305 is further configured to:

[0155] Inject the probe into the target changed code line of the target changed file;

[0156] Determine a target probe in the probe, where the target probe is a probe corresponding to a target changed code line to be compiled;

[0157] the number of detection probes and the number of target probes;

[0158] According to the number of probes, determine the total number of target changed code lines in the target changed file set;

[0159] According to the number of target probes, the number of target change code lines compiled in the target change file is determined.

[0160] In the embodiment of the present application, the third determining module 306 is further configured to:

[0161] Determine a preset incremental code coverage threshold;

[0162] If the incremental code coverage is greater than the code coverage threshold, the third code file set is used as the second code file set for the next version;

[0163] If the incremental code coverage is less than or within the code coverage threshold, the steps of obtaining the first code file set, the second code file set, and the third code file set are re-executed.

[0164] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 4 As shown, the electronic device 40 includes:

[0165] The electronic device 40 may include a processor 401 with one or more processing cores, a memory 402 with one or more computer-readable storage media, a communication component 403 and other components. The processor 401 , the memory 402 and the communication component 403 are connected via a bus 407 .

[0166] In a specific implementation process, at least one processor 401 executes computer-executable instructions stored in the memory 402, so that at least one processor 401 executes the above-mentioned method for obtaining incremental code coverage.

[0167] The specific implementation process of the processor 401 can be found in the above method embodiment, and its implementation principle and technical effect are similar, so this embodiment will not be repeated here.

[0168] In the above Figure 4 In the illustrated embodiment, it should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.

[0169] The memory may include a high-speed memory (Random Access Memory, RAM), and may also include a non-volatile memory (Non-volatile Memory, NVM), such as at least one disk memory.

[0170] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.

[0171] In some embodiments, a computer program product is also proposed, including a computer program or instructions, which implement the steps of any of the above-mentioned methods for obtaining incremental code coverage when executed by a processor.

[0172] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present application. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.

[0173] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

Claims

1. A method for obtaining incremental code coverage, characterized in that: The method comprises: Obtain a first code file set, a second code file set, and a third code file set, wherein the first code file set is a code file set of a current version of a target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set; Obtaining incremental code information according to the first code file set and the second code file set, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type; Compiling the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes second change code lines belonging to the target type; Determine a target change file that exists in both the first change file subset and the second change file subset, and a target change code line in the target change file; Determine the number of compiled target change code lines in the target change file and the total number of target change code lines in the target change file; The incremental code coverage is determined according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

2. The method according to claim 1, characterized in that The obtaining of the first code file set, the second code file set and the third code file set includes: Obtaining the first code file set and the second code file set; Determining different file subsets of the first code file set and the second code file set; The third code file set is obtained according to the different file subsets and the second code file set.

3. The method according to claim 1, characterized in that: The obtaining incremental code information according to the first code file set and the second code file set includes: In response to a first acquisition instruction sent by a user, determining the first change file subset from the first code file set and the second code file set, wherein the first acquisition instruction is used to determine a change file from the first code file set and the second code file set, and determining the first change file subset according to the change file; In response to a second acquisition instruction from a user, determining a first changed code line of a target type in the first changed file subset, wherein the second acquisition instruction is used to determine the type of the changed code line in the first changed file subset, and determining the first changed code line of the target type according to the type of the changed code line; Incremental code information is obtained according to the first change file subset and the first change code line of the target type in the first change file subset.

4. The method according to claim 1, characterized in that: The step of compiling the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree includes: Compiling the third code file set to obtain an abstract syntax tree; Traversing the abstract syntax tree to obtain the second changed file subset; All files of the second changed file subset are traversed to obtain second changed code lines of the target type in the second changed file subset.

5. The method according to claim 1, characterized in that The step of determining the target change files that exist in both the first change file subset and the second change file subset, and the target change code lines in the target change files, includes: Performing file matching on the first changed file subset and the second changed file subset to obtain a target changed file; According to the target change file, the first change code line, and the second change code line, a target change code line in the target change file is obtained.

6. The method according to claim 5, characterized in that The step of obtaining a target change code line in the target change file according to the target change file, the first change code line, and the second change code line includes: Determining an initial target change code line according to the first change code line and the second change code line; According to the initial target change code line, a target change code line in the target change file is determined.

7. The method according to claim 1, characterized in that The determining the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file comprises: Injecting the probe into the target changed code line of the target changed file; Determine a target probe among the probes, where the target probe is a probe corresponding to a target changed code line being compiled; Detecting the number of the probes and the number of the target probes; Determine the total number of target changed code lines in the target changed file set according to the number of the probes; The number of target change code lines compiled in the target change file is determined according to the number of the target probes.

8. The method according to claim 1, characterized in that After determining the incremental code coverage according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file, the method further includes: Determine a preset incremental code coverage threshold; If the incremental code coverage is greater than the incremental code coverage threshold, using the third code file set as the second code file set of the next version; If the incremental code coverage is less than or equal to the incremental code coverage threshold, the steps of obtaining the first code file set, the second code file set and the third code file set are re-executed.

9. A device for obtaining incremental code coverage, characterized in that: include: an acquisition module, acquiring a first code file set, a second code file set, and a third code file set, wherein the first code file set is a code file set of a current version of a target program, the second code file set is a code file set of a previous version of the target program, and the third code file set includes the second code file set and the first code file set; A first obtaining module obtains incremental code information according to the first code file set and the second code file set, wherein the incremental code information includes a first change file subset, the first change file subset includes a first change code line belonging to a target type, and the target type indicates that the type of the change code line is an addition type or a modification type; a second obtaining module, compiling the third code file set to obtain an abstract syntax tree of the third code file set and a second change file subset in the abstract syntax tree, wherein the second change file subset includes a second change code line belonging to the target type; A first determination module is configured to determine a target change file that exists in both the first change file subset and the second change file subset, and a target change code line in the target change file; A second determination module determines the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file; The third determination module determines the incremental code coverage according to the number of target change code lines compiled in the target change file and the total number of target change code lines in the target change file.

10. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 8 when executed by a processor.