Code coverage rate obtaining method and device and electronic equipment
By determining and running the code snippets that can be supported by the test terminal, the problem of inaccurate code coverage statistics in the prior art is solved, and the accuracy and guidance of code coverage are improved.
Patent Information
- Application Number
- CN202310791685.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-29
- Publication Date
- 2024-12-31
AI Technical Summary
In the prior art, the accuracy and guidance of code coverage are affected in some scenarios due to the existence of code snippets that cannot be supported by the test terminal.
By obtaining the test code file and the code coverage statistics scenarios it belongs to, determine the target code snippets, including code snippets and incremental code snippets that the test terminal can support, run the code file to obtain the target code coverage, and avoid redundant code snippets being Included in the statistical scope.
The accuracy and guidance of code coverage are optimized, the reference of code coverage is improved, and the accuracy of statistical results is ensured.
Smart Images

Figure CN119226126A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of data processing, and in particular to a method, device and electronic device for acquiring code coverage. Background Art
[0002] In the implementation, the statistics of code coverage are of great reference for the testing and improvement of the test cases of the code. In the related technology, the code test can be performed on a test terminal of a set model and the statistics of code coverage can be performed.
[0003] In this scenario, the code file under test may contain code snippets that are not supported by the test terminal of the set model, which may affect the accuracy of the code coverage rate and the guidance of the code coverage rate in this scenario to a certain extent. Summary of the invention
[0004] The present disclosure aims to solve one of the technical problems in the related art at least to some extent.
[0005] To this end, a first aspect of the present disclosure proposes a method for obtaining code coverage.
[0006] A second aspect of the present disclosure provides a device for acquiring code coverage.
[0007] A third aspect of the present disclosure provides an electronic device.
[0008] A fourth aspect of the present disclosure provides a computer-readable storage medium.
[0009] In a first aspect, the present disclosure proposes a method for obtaining code coverage, the method comprising: obtaining a test code file and a code coverage statistical scenario to which the test code file belongs; based on the code coverage statistical scenario, determining a target code snippet in the test code file that participates in code coverage statistics, wherein the target code snippet comprises a code snippet in the test code file that can be supported by a test terminal that runs the test code file and an incremental code snippet in the test code file; running the test code file to obtain a running code snippet of the test code file running on the test terminal; and obtaining the target code coverage of the test code file based on the target code snippet and the running code snippet.
[0010] In addition, the method for obtaining code coverage proposed in the first aspect of the present disclosure may also have the following additional technical features:
[0011] According to an embodiment of the present disclosure, determining the target code snippets in the test code file that participate in code coverage statistics based on the code coverage statistics scenario includes: in response to the code coverage statistics scenario being a first coverage statistics scenario, obtaining a source code file of the test code file and scanning the source code file to obtain associated terminal configuration parameters of the test code blocks included in the test code file, wherein the first coverage statistics scenario is a scenario for counting the coverage of code snippets supportable by the test terminal in the test code file; obtaining the test terminal configuration parameters of the test terminal, and obtaining redundant configuration parameters that do not match the test terminal configuration parameters from all associated terminal configuration parameters to obtain redundant code blocks corresponding to the redundant configuration parameters; obtaining the remaining code blocks from all test code blocks except the redundant code blocks, and determining the code snippets in the remaining code blocks as the target code snippets participating in code coverage statistics in the test code file under the first coverage statistics scenario.
[0012] According to an embodiment of the present disclosure, determining the target code snippet in the test code file that participates in code coverage statistics based on the code coverage statistics scenario includes: in response to the code coverage statistics scenario being a second coverage statistics scenario, obtaining a comparison code file of the test code file, wherein the second coverage statistics scenario is a scenario of performing statistics on the coverage of an incremental code snippet of the test code file based on the comparison code file; and performing syntax tree parsing on the test code file and the comparison code file, respectively, to obtain an incremental code snippet of the test code file based on the comparison code file as the target code snippet.
[0013] According to an embodiment of the present disclosure, the method of performing syntax tree parsing on the test code file and the comparison code file respectively to obtain an incremental code fragment of the test code file based on the comparison code file as the target code fragment includes: performing syntax tree parsing on the test code file and the comparison code file respectively to obtain a first syntax tree parsing result of the test code file and a second syntax tree parsing result of the comparison code file; obtaining an incremental code fragment of the test code file based on the comparison code file based on the first syntax tree parsing result and the second syntax tree parsing result; and determining the incremental code fragment as the target code fragment in the test code file that participates in code coverage statistics.
[0014] According to an embodiment of the present disclosure, the method of obtaining an incremental code snippet of the test code file based on the comparison code file based on the first syntax tree parsing result and the second syntax tree parsing result includes: verifying the first syntax tree parsing result and the second syntax tree parsing result, and obtaining a verification result of the first syntax tree parsing result and the second syntax tree parsing result; in response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, obtaining difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determining an incremental code branch of the test code file based on the comparison code file based on the difference information; and obtaining the incremental code snippet of the test code file based on the comparison code file based on the incremental code branch.
[0015] According to one embodiment of the present disclosure, in response to the mismatch of the verification result, difference information between the first syntax tree parsing result and the second syntax tree parsing result is obtained, and based on the difference information, an incremental code branch of the test code file based on the comparison code file is determined, including: obtaining a code difference type of the first syntax tree parsing result based on the second syntax tree parsing result, and determining the code branches belonging to the code difference type in all test code blocks as the incremental code branches of the test code file based on the comparison code file.
[0016] According to one embodiment of the present disclosure, the target code coverage of the test code file is obtained based on the target code snippet and the running code snippet, including: obtaining a first code parameter of the target code snippet and a second code parameter of the running code snippet, and obtaining the target code coverage of the test code file based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter at least include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation, and code parameters required for conditional coverage calculation.
[0017] To achieve the above-mentioned purpose, the second aspect of the present disclosure proposes a device for obtaining code coverage, the device comprising: a first acquisition module, used to obtain a test code file and a code coverage statistical scenario to which the test code file belongs; a determination module, used to determine, based on the code coverage statistical scenario, a target code fragment in the test code file that participates in code coverage statistics, wherein the target code fragment includes one of a code fragment in the test code file that can be supported by a test terminal running the test code file and an incremental code fragment in the test code file; a running module, used to run the test code file to obtain a running code fragment of the test code file running on the test terminal; and a second acquisition module, used to obtain the target code coverage of the test code file based on the target code fragment and the running code fragment.
[0018] In addition, the device for acquiring code coverage proposed in the second aspect of the present disclosure may also have the following additional technical features:
[0019] According to one embodiment of the present disclosure, the determination module is further used to: in response to the code coverage statistics scenario being a first coverage statistics scenario, obtain the source code file of the test code file and scan the source code file to obtain associated terminal configuration parameters of the test code blocks included in the test code file, wherein the first coverage statistics scenario is a scenario for counting the coverage of code fragments supportable by the test terminal in the test code file; obtain the test terminal configuration parameters of the test terminal, and obtain redundant configuration parameters that do not match the test terminal configuration parameters from all associated terminal configuration parameters to obtain redundant code blocks corresponding to the redundant configuration parameters; obtain the remaining code blocks from all test code blocks except the redundant code blocks, and determine the code fragments in the remaining code blocks as the target code fragments participating in the code coverage statistics in the test code file under the first coverage statistics scenario.
[0020] According to one embodiment of the present disclosure, the determination module is further used to: in response to the code coverage statistics scenario being a second coverage statistics scenario, obtain a comparison code file of the test code file, wherein the second coverage statistics scenario is a scenario of performing statistics on the coverage of the test code file based on an incremental code snippet of the comparison code file; and perform syntax tree parsing on the test code file and the comparison code file respectively to obtain an incremental code snippet of the test code file based on the comparison code file as the target code snippet.
[0021] According to one embodiment of the present disclosure, the determination module is also used to: perform syntax tree parsing on the test code file and the comparison code file respectively, obtain a first syntax tree parsing result of the test code file, and a second syntax tree parsing result of the comparison code file; based on the first syntax tree parsing result and the second syntax tree parsing result, obtain an incremental code snippet of the test code file based on the comparison code file; and determine the incremental code snippet as the target code snippet in the test code file that participates in code coverage statistics.
[0022] According to one embodiment of the present disclosure, the determination module is also used to: verify the first syntax tree parsing result and the second syntax tree parsing result, and obtain the verification result of the first syntax tree parsing result and the second syntax tree parsing result; in response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, obtain the difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determine the incremental code branch of the test code file based on the comparison code file based on the difference information; based on the incremental code branch, obtain the incremental code fragment of the test code file based on the comparison code file.
[0023] According to one embodiment of the present disclosure, the determination module is also used to: obtain the code difference type of the first syntax tree parsing result based on the second syntax tree parsing result, and determine the code branches belonging to the code difference type in all test code blocks as the incremental code branches of the test code file based on the comparison code file.
[0024] According to one embodiment of the present disclosure, the second acquisition module is also used to: obtain a first code parameter of the target code snippet and a second code parameter of the running code snippet, and obtain the target code coverage of the test code file based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter at least include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation and code parameters required for conditional coverage calculation.
[0025] A third aspect of the present disclosure proposes an electronic device, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute instructions to implement the method for obtaining code coverage as proposed in the first aspect above.
[0026] A fourth aspect of the present disclosure proposes a computer-readable storage medium. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method for obtaining code coverage as proposed in the first aspect above.
[0027] The method and device for obtaining code coverage proposed in the present disclosure obtains a test code file and a code coverage statistical scenario to which the test code file belongs, determines the target code snippet in the test code file that participates in the code coverage statistics based on the code coverage statistical scenario, runs the test code file to obtain the running code snippet of the test code file running on the test terminal, and obtains the target code coverage of the test code file based on the target code snippet and the running code snippet. In the present disclosure, according to the requirements of the code coverage statistical scenario, the code snippet that the test code file can support on the test terminal or the incremental code snippet of the test code file is obtained as the target code snippet, avoiding the occurrence of abnormal situations in which redundant code snippets in the test code file are included in the code coverage statistical range, optimizing the code coverage acquisition method, improving the accuracy of the code coverage, and optimizing the guidance and reference of the code coverage.
[0028] It should be understood that the contents described in the present disclosure are not intended to identify the key or important features of the embodiments of the present disclosure, nor are they intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The above and / or additional aspects and advantages of the present disclosure will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:
[0030] Figure 1 A schematic diagram of a flow chart of a method for acquiring code coverage according to an embodiment of the present disclosure;
[0031] Figure 2 A schematic diagram of a flow chart of a method for acquiring code coverage according to another embodiment of the present disclosure;
[0032] Figure 3 A schematic diagram of a flow chart of a method for acquiring code coverage according to another embodiment of the present disclosure;
[0033] Figure 4 This is a schematic diagram of the structure of a device for acquiring code coverage according to an embodiment of the present disclosure;
[0034] Figure 5 A block diagram of an electronic device according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0035] Embodiments of the present disclosure are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present disclosure, and should not be construed as limiting the present disclosure.
[0036] A method, device and electronic device for obtaining code coverage proposed in an embodiment of the present disclosure are described below with reference to the accompanying drawings.
[0037] Figure 1 FIG. 1 is a flow chart of a method for obtaining code coverage according to an embodiment of the present disclosure. Figure 1 As shown, the method includes:
[0038] S101, obtaining a test code file and a code coverage statistics scenario to which the test code file belongs.
[0039] In the embodiments of the present disclosure, there is a need to test a code file, wherein the code file that needs to be tested can be determined as a test code file.
[0040] Optionally, after the test code file completes the current round of code testing, it is necessary to count the coverage of the test code file in the current round of code testing, and this scenario can be determined as the code coverage statistics scenario to which the test code file belongs.
[0041] S102, based on the code coverage statistics scenario, determining a target code snippet in the test code file that participates in the code coverage statistics, wherein the target code snippet includes one of a code snippet in the test code file that can be supported by a test terminal that runs the test code file and an incremental code snippet in the test code file.
[0042] In the embodiments of the present disclosure, for the same test code file in different code coverage statistics scenarios, there may be differences in the code snippets participating in the code coverage statistics. Based on the specific content included in the code coverage statistics scenario, the code snippets participating in the code coverage statistics in the code coverage statistics scenario can be obtained from the test code file as the target code snippet in the test code file.
[0043] Optionally, the code coverage statistics scenario may include a scenario in which coverage statistics are performed on code snippets that a test terminal that runs a test code file can support running, and may also include a scenario in which coverage statistics are performed on incremental code snippets of the test code file based on related historical code files, wherein the code coverage statistics scenarios are different and the code snippets in the test code file that participate in the code coverage statistics are also different.
[0044] In this scenario, in a scenario where coverage statistics are performed on code snippets that can be supported by a test terminal running a test code file, code snippets in the test code file that participate in code coverage statistics can be determined as target code snippets in this scenario.
[0045] Alternatively, in a scenario where coverage statistics are performed on the test code file based on incremental code snippets of related historical code files, the code snippets in the test code file that participate in the code coverage statistics are determined as target code snippets in this scenario.
[0046] S103, running the test code file to obtain an operating code fragment of the test code file running on the test terminal.
[0047] In the embodiment of the present disclosure, an execution instruction of a test code file may be obtained, and the test code file may be executed on a test terminal based on the received execution instruction.
[0048] The test code file may be compiled first to obtain a compiled file of the test code file, and then the compiled file may be run on the test terminal based on the obtained running instruction.
[0049] Optionally, during the running of the test code file, the code snippet in the test code file running on the test terminal may be monitored, and the monitored running code snippet may be determined as the running code snippet in the test code file.
[0050] S104, obtaining target code coverage of the test code file based on the target code snippet and the running code snippet.
[0051] Optionally, based on the code coverage algorithm in the related technology, algorithm processing can be performed on relevant parameters of the target code snippet and the running code snippet, and then the code coverage of the test code file is obtained according to the result of the algorithm processing, and marked as the target code coverage.
[0052] As an example, based on the code line coverage algorithm in the related art, algorithm processing can be performed on relevant parameters of the target code snippet and the running code snippet to obtain the line coverage of the test code file.
[0053] Among them, the number of code lines in the target code snippet and the number of code lines in the running code snippet can be obtained, and the ratio between the number of code lines in the running code snippet and the number of code lines in the target code snippet is determined as the line coverage of the test code file.
[0054] As another example, based on the code path coverage algorithm in the related art, algorithmic processing may be performed on relevant parameters of the target code snippet and the running code snippet to obtain the path coverage of the test code file.
[0055] Among them, the number of all paths through the code and expressions included in the target code snippet and the number of all paths through the code and expressions included in the running code snippet can be obtained, and then the values of the two quantities are algorithmically processed according to the code path coverage algorithm in the relevant technology to obtain the path coverage of the test code file.
[0056] Furthermore, based on the obtained line coverage, path coverage and other related coverage parameters, the target code coverage of the test code file is obtained.
[0057] The method for obtaining code coverage proposed in the present disclosure obtains a test code file and a code coverage statistical scenario to which the test code file belongs, determines the target code snippet in the test code file that participates in the code coverage statistics based on the code coverage statistical scenario, runs the test code file to obtain the running code snippet of the test code file running on the test terminal, and obtains the target code coverage of the test code file based on the target code snippet and the running code snippet. In the present disclosure, according to the requirements of the code coverage statistical scenario, the code snippet that the test code file can support on the test terminal or the incremental code snippet of the test code file is obtained as the target code snippet, avoiding the occurrence of abnormal situations in which redundant code snippets in the test code file are included in the code coverage statistical range, optimizing the code coverage acquisition method, improving the accuracy of the code coverage, and optimizing the guidance and reference of the code coverage.
[0058] In the above embodiment, the acquisition of the target code fragment can also be combined with Figure 2 Further understanding, Figure 2 FIG. 1 is a flow chart of a method for obtaining code coverage according to another embodiment of the present disclosure. Figure 2 As shown, the method includes:
[0059] S201, in response to the code coverage statistics scenario being the first coverage statistics scenario, obtain the source code file of the test code file and scan the source code file to obtain the associated terminal configuration parameters of the test code block included in the test code file, wherein the first coverage statistics scenario is a scenario for counting the coverage of code snippets supported by the test terminal.
[0060] In the embodiment of the present disclosure, a scenario of performing coverage statistics on code snippets that can be supported by the test terminal in the test code file may be determined as a first coverage statistics scenario in the code coverage statistics scenarios.
[0061] In the first coverage statistics scenario, all code blocks included in the test code file may be scanned, so as to determine the code segments in the test code file that are supported by the test terminal as the target code segments.
[0062] Among them, a static full scanning method can be used to scan all code blocks included in the test code file, and other scanning methods can also be used to scan all code blocks included in the test code file, which is not specifically limited here.
[0063] Optionally, based on a full code scanning method in the related art, a static full scan may be performed on all code blocks included in the test code file to determine a target code segment in the test code file that is supported by the test terminal.
[0064] The code blocks included in the test code file may be determined as the test code blocks included in the test code file. In this scenario, a static full scan may be performed on all the test code blocks included in the test code file.
[0065] It should be noted that the test code blocks in the test code file may include ordinary code blocks in the test code file, catch exception code blocks in the test code file, and other types of code blocks in the test code file, which are not specifically limited here.
[0066] Optionally, a test terminal supporting the running of a test code block in a test code file may be determined as an associated terminal of the test code file, and configuration parameters of the associated terminal may be determined as associated terminal configuration parameters of the test code block in the test code file.
[0067] S202: Acquire test terminal configuration parameters of the test terminal, and acquire redundant configuration parameters that do not match the test terminal configuration parameters among all associated terminal configuration parameters, so as to acquire redundant code blocks corresponding to the redundant configuration parameters.
[0068] In the embodiment of the present disclosure, for a certain model of test terminal, the code snippets in the test code file that can be supported by the test terminal may only include part of the code snippets in the test code file.
[0069] In this scenario, the code fragment in the test code file that is not supported by the test terminal of this model may be determined as a redundant code fragment in the test code file.
[0070] Optionally, the test terminal configuration parameters of the test terminal may be obtained and compared with the associated terminal configuration parameters of the test code block included in the test code file.
[0071] If the test terminal configuration parameters of the test terminal match the associated terminal configuration parameters of the test code block, it can be determined that the test code block is a code block that can be supported by the test terminal.
[0072] Accordingly, if the test terminal configuration parameters of the test terminal do not match the associated terminal configuration parameters of the test code block, the test code block can be determined to be a code block not supported by the test terminal, wherein it can be determined to be a redundant code block in the test code file.
[0073] Optionally, parameters in the associated configuration parameters that do not match the test terminal configuration parameters may be determined as redundant configuration parameters in the associated terminal configuration parameters.
[0074] Furthermore, the corresponding code blocks of the redundant configuration parameters in all the test code blocks may be determined as redundant code blocks in the test code file.
[0075] S203, obtaining remaining code blocks except redundant code blocks from all test code blocks, and determining code snippets in the remaining code blocks as target code snippets participating in code coverage statistics in the test code file in the first coverage statistics scenario.
[0076] In the disclosed embodiment, after the redundant code blocks included in the test code file are determined, the remaining code blocks except the redundant code blocks among all the included test code blocks can be determined as target code blocks in the test code file that can be supported by the test terminal.
[0077] Further, code snippets in all target code blocks are obtained, and the code snippets are determined as target code snippets in the test code file that are supportable by the test terminal and participate in code coverage statistics in the first coverage statistics scenario.
[0078] S204, obtaining target code coverage of the test code file based on the target code snippet and the running code snippet.
[0079] Optionally, a first code parameter of the target code snippet and a second code parameter of the running code snippet are obtained, and the target code coverage of the test code file is obtained based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation and code parameters required for conditional coverage calculation.
[0080] It can be understood that based on the specific coverage that needs to be calculated for the test code file, the code parameters corresponding to the target code snippet and the corresponding code parameters of the running code snippet are counted, wherein the code parameters corresponding to the target code snippet can be identified as the first code parameters, and the code parameters corresponding to the running code snippet can be identified as the second code parameters.
[0081] In the embodiment of the present disclosure, the code coverage that needs to be counted for the test code file may include instruction coverage, loop coverage, branch coverage, line coverage, condition coverage, etc.
[0082] In this scenario, for obtaining one of the coverage rates, the code parameters needed to be used in its statistical calculation process can be analyzed, and targeted acquisitions can be made from the corresponding code parameters of the target code fragment and the corresponding code parameters of the running code fragment, respectively, to obtain the first code parameters of the target code fragment and the second code parameters of the running code fragment.
[0083] Furthermore, based on a specific algorithm of the coverage rate, the obtained first code parameter and second code parameter are algorithmically processed to obtain a corresponding coverage rate of the test code file.
[0084] Optionally, the first code parameter of the target code snippet and the second code parameter of the running code snippet can be processed based on the code coverage acquisition tool test coverage framework (jacoco) in the related technology, and the target code coverage of the test code file can be obtained according to the output results of jacoco.
[0085] Among them, when jacoco parses class files in the bytecode manipulation framework (ASM), it will perform various coverage statistics, including instruction coverage, circle coverage, branch coverage, line coverage, and condition coverage.
[0086] In this scenario, various coverage statistics can be implemented based on bytecode instructions.
[0087] Optionally, jacoco can obtain the bytecode instructions corresponding to the running code snippet, only based on the bytecode instructions carrying the code identifier corresponding to the target code snippet, and perform coverage statistics based only on this part of the bytecode instructions, thereby obtaining the target code coverage of the test code file.
[0088] In the first coverage statistics scenario, when the test terminal runs the test code file, it will only run the code snippets it supports. In this scenario, the bytecode instructions corresponding to the running code snippets all carry the code identifier corresponding to the target code snippet. Therefore, the target code coverage statistics of the test code file in the first coverage statistics scenario can be implemented based on the bytecode instructions of all the running code snippets.
[0089] In the second coverage statistics scenario, it is necessary to perform coverage statistics on the test code file based on the incremental code snippet of the comparison code file. In this scenario, the bytecode instructions carrying the code identifier of the incremental code snippet as the target code snippet can be obtained from the bytecode instructions of the running code snippet, and coverage statistics can be performed only based on this part of the bytecode instructions, thereby obtaining the target code coverage of the test code file in the second coverage statistics scenario.
[0090] The method for obtaining code coverage proposed in the present disclosure scans the source code file of the test code file to obtain the associated terminal configuration parameters of the test code block included in the test code file when the code coverage statistical scenario is the first coverage statistical scenario. Accordingly, the test terminal configuration parameters of the test terminal running the test code file are obtained, and based on the associated terminal configuration parameters and the test terminal configuration parameters, the redundant code blocks not supported by the test terminal are obtained from the test code file, and then the remaining code blocks except the redundant code blocks in the test code file are determined as target code blocks supportable by the test terminal, and then the target code fragment included in the target code block is obtained. In the present disclosure, the code fragment that can be supported by the test code file on the test terminal is obtained as the target code fragment, which avoids the code fragment that is not supported by the test terminal from being included in the code coverage statistical scope, improves the accuracy of the code coverage, and optimizes the guidance and reference of the code coverage.
[0091] The method for obtaining code coverage provided in the present disclosure may also include: Figure 3 Obtaining code coverage for incremental code as shown, Figure 3 FIG. 1 is a flow chart of a method for obtaining code coverage according to another embodiment of the present disclosure. Figure 3 As shown, the method includes:
[0092] S301, in response to the code coverage statistics scenario being the second coverage statistics scenario, obtaining a comparison code file of a test code file, wherein the second coverage statistics scenario is a scenario of counting the coverage of an incremental code snippet of the test code file based on the comparison code file.
[0093] In the embodiment of the present disclosure, the code coverage statistics scenario to which the test code file belongs may also include a scenario of performing coverage statistics on incremental code snippets of the test code file, wherein the scenario may be determined as a second coverage statistics scenario.
[0094] In order to determine the incremental code snippet in the test code file, the historical code file that needs to be compared with the test code file can be obtained from the local code version library corresponding to the test code file as the comparison code file of the test code file.
[0095] As an example, set the code version of the test code file to version 2.0, and the version of the code file to be compared is the previous version 1.0 of version 2.0. Then, the historical revision code file of version 1.0 can be obtained from the historical revision code file of the test code file in the local code version library as the comparison code file for the test code file in this example.
[0096] S302 , performing syntax tree parsing on the test code file and the comparison code file respectively, to obtain an incremental code fragment of the test code file based on the comparison code file as a target code fragment.
[0097] Optionally, syntax tree parsing can be performed on the test code file and the comparison code file respectively to obtain a first syntax tree parsing result of the test code file and a second syntax tree parsing result of the comparison code file, and based on the first syntax tree parsing result and the second syntax tree parsing result, an incremental code snippet of the test code file based on the comparison code file is obtained.
[0098] In an embodiment of the present disclosure, by performing syntax tree analysis on a test code file and a comparison code file, difference information between the two can be obtained, wherein the result obtained by performing syntax tree analysis on the test code file can be identified as a first syntax tree parsing result, and the result obtained by performing syntax tree parsing on the comparison code file can be identified as a second syntax tree parsing result.
[0099] Optionally, the first syntax tree parsing result and the second syntax tree parsing result may be verified to obtain a verification result of the first syntax tree parsing result and the second syntax tree parsing result.
[0100] The first syntax tree parsing result and the second syntax tree parsing result may be verified based on a verification method (MD5) in the related art, thereby obtaining a verification result between the first syntax tree parsing result and the second syntax tree parsing result.
[0101] In some implementations, if the verification result indicates that the first syntax tree parsing result and the second syntax tree parsing result match, it can be determined that the test code file corresponding to the first syntax tree parsing result is the same as the comparison code file corresponding to the second syntax tree parsing result. In this scenario, the test code file does not have incremental code snippets based on the comparison code file.
[0102] In some other implementations, in response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, difference information between the first syntax tree parsing result and the second syntax tree parsing result is obtained, and based on the difference information, an incremental code branch of the test code file based on the comparison code file is determined.
[0103] It can be understood that if the verification result indicates that the first syntax tree parsing result does not match the second syntax tree parsing result, it can be determined that there is a difference between the test code file corresponding to the first syntax tree parsing result and the comparison code file corresponding to the second syntax tree parsing result.
[0104] In this scenario, the difference information between the two can be obtained, and based on the specific content carried by the difference information, the code branch newly added by the test code file based on the comparison code file can be determined and determined as the incremental code branch of the test code file based on the comparison code file.
[0105] As an example, the code difference type of the first syntax tree parsing result based on the second syntax tree parsing result can be obtained, and the code branches belonging to the code difference type in all test code blocks can be determined as incremental code branches of the test code file based on the comparison code file.
[0106] In the embodiment of the present disclosure, the type of the difference code between the first syntax tree parsing result and the second syntax tree parsing result can be obtained and identified as the code difference type of the first syntax tree parsing result based on the second syntax tree parsing result.
[0107] Further, code branches belonging to the code difference type in the test code file are obtained, and the part of the code branches is determined as an incremental code branch of the test code file based on the comparison code file.
[0108] Optionally, based on the incremental code branch, an incremental code snippet of the test code file based on the comparison code file is obtained.
[0109] Among them, all code snippets included in the incremental code branch in the test code file can be obtained, and the partial code snippets are determined as the incremental code snippets of the test code file based on the comparison code file.
[0110] The method for obtaining code coverage proposed in the present disclosure, when the code coverage statistical scenario is the second coverage statistical scenario, obtains a comparison code file of a test code file, and performs syntax tree parsing on the test code file and the comparison code file respectively, to obtain a first syntax tree parsing result of the test code file and a second syntax tree parsing result of the comparison code file. Furthermore, the first syntax tree parsing result and the second syntax tree parsing result are verified, and according to the verification result, an incremental code snippet of the test code file based on the comparison code file is obtained. In the present disclosure, the incremental code snippet of the test code file is obtained as the target code snippet, which avoids the occurrence of abnormal situations in which redundant code snippets in the test code file are included in the code coverage statistical range, improves the accuracy of the code coverage, and optimizes the guidance and reference of the code coverage.
[0111] Corresponding to the code coverage acquisition methods proposed in the above-mentioned embodiments, an embodiment of the present disclosure further proposes a code coverage acquisition device. Since the code coverage acquisition device proposed in the embodiment of the present disclosure corresponds to the code coverage acquisition methods proposed in the above-mentioned embodiments, the implementation methods of the above-mentioned code coverage acquisition methods are also applicable to the code coverage acquisition device proposed in the embodiment of the present disclosure, and will not be described in detail in the following embodiments.
[0112] Figure 4 FIG. 1 is a schematic diagram of a structure of a device for acquiring code coverage according to an embodiment of the present disclosure. Figure 4 As shown, the device 400 for acquiring code coverage includes a first acquisition module 41, a determination module 42, an operation module 43 and a second acquisition module 44, wherein:
[0113] A first acquisition module 41 is used to acquire a test code file and a code coverage statistics scenario to which the test code file belongs;
[0114] A determination module 42 is used to determine a target code segment in the test code file that participates in the code coverage statistics based on the code coverage statistics scenario, wherein the target code segment includes one of a code segment in the test code file that can be supported by a test terminal running the test code file and an incremental code segment in the test code file;
[0115] An operation module 43 is used to run the test code file and obtain an operation code fragment of the test code file running on the test terminal;
[0116] The second acquisition module 44 is used to acquire the target code coverage of the test code file based on the target code snippet and the running code snippet.
[0117] According to one embodiment of the present disclosure, the determination module 42 is further used to: in response to the code coverage statistics scenario being the first coverage statistics scenario, obtain the source code file of the test code file and scan the source code file to obtain the associated terminal configuration parameters of the test code blocks included in the test code file, wherein the first coverage statistics scenario is a scenario for counting the coverage of code snippets supportable by the test terminal in the test code file; obtain the test terminal configuration parameters of the test terminal, and obtain the redundant configuration parameters that do not match the test terminal configuration parameters in all the associated terminal configuration parameters to obtain the redundant code blocks corresponding to the redundant configuration parameters; obtain the remaining code blocks except the redundant code blocks in all the test code blocks, and determine the code snippets in the remaining code blocks as the target code snippets participating in the code coverage statistics in the test code file under the first coverage statistics scenario.
[0118] According to one embodiment of the present disclosure, the determination module 42 is further used to: in response to the code coverage statistics scenario being the second coverage statistics scenario, obtain a comparison code file of the test code file, wherein the second coverage statistics scenario is a scenario of performing statistics on the coverage of the test code file based on the incremental code snippet of the comparison code file; perform syntax tree parsing on the test code file and the comparison code file respectively to obtain the incremental code snippet of the test code file based on the comparison code file as the target code snippet.
[0119] According to one embodiment of the present disclosure, the determination module 42 is also used to: perform syntax tree parsing on the test code file and the comparison code file respectively, obtain a first syntax tree parsing result of the test code file, and a second syntax tree parsing result of the comparison code file; based on the first syntax tree parsing result and the second syntax tree parsing result, obtain an incremental code snippet of the test code file based on the comparison code file; and determine the incremental code snippet as a target code snippet in the test code file that participates in code coverage statistics.
[0120] According to one embodiment of the present disclosure, the determination module 42 is further used to: verify the first syntax tree parsing result and the second syntax tree parsing result, and obtain the verification result of the first syntax tree parsing result and the second syntax tree parsing result; in response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, obtain the difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determine the incremental code branch of the test code file based on the comparison code file based on the difference information; based on the incremental code branch, obtain the incremental code fragment of the test code file based on the comparison code file.
[0121] According to one embodiment of the present disclosure, the determination module 42 is also used to: obtain the code difference type of the first syntax tree parsing result based on the second syntax tree parsing result, and determine the code branches belonging to the code difference type in all test code blocks as incremental code branches of the test code file based on the comparison code file.
[0122] According to one embodiment of the present disclosure, the second acquisition module 44 is further used to: obtain a first code parameter of a target code snippet, and a second code parameter of a running code snippet, and obtain a target code coverage of a test code file based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter at least include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation, and code parameters required for conditional coverage calculation.
[0123] The device for acquiring code coverage proposed in the present disclosure acquires a test code file and a code coverage statistical scenario to which the test code file belongs, determines the target code snippet in the test code file that participates in the code coverage statistics based on the code coverage statistical scenario, runs the test code file to acquire the running code snippet of the test code file running on the test terminal, and obtains the target code coverage of the test code file based on the target code snippet and the running code snippet. In the present disclosure, according to the requirements of the code coverage statistical scenario, the code snippet that the test code file can support on the test terminal or the incremental code snippet of the test code file is acquired as the target code snippet, avoiding the occurrence of abnormal situations in which redundant code snippets in the test code file are included in the code coverage statistical range, optimizing the code coverage acquisition method, improving the accuracy of the code coverage, and optimizing the guidance and reference of the code coverage.
[0124] To achieve the above embodiments, the present disclosure also provides an electronic device, a computer-readable storage medium, and a computer program product.
[0125] Figure 5 1 shows a schematic block diagram of an example electronic device 500 that can be used to implement an embodiment of the present disclosure. Figure 5 As shown, the device 500 includes a memory 51, a processor 52, and a computer program stored in the memory 51 and executable on the processor 52. When the processor 52 executes the program instructions, the method for obtaining code coverage provided in the above embodiment is implemented.
[0126] The method for obtaining code coverage proposed in the present disclosure obtains a test code file and a code coverage statistical scenario to which the test code file belongs, determines the target code snippet in the test code file that participates in the code coverage statistics based on the code coverage statistical scenario, runs the test code file to obtain the running code snippet of the test code file running on the test terminal, and obtains the target code coverage of the test code file based on the target code snippet and the running code snippet. In the present disclosure, according to the requirements of the code coverage statistical scenario, the code snippet that the test code file can support on the test terminal or the incremental code snippet of the test code file is obtained as the target code snippet, avoiding the occurrence of abnormal situations in which redundant code snippets in the test code file are included in the code coverage statistical range, optimizing the code coverage acquisition method, improving the accuracy of the code coverage, and optimizing the guidance and reference of the code coverage.
[0127] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0128] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0129] The program code for implementing the method itself can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code can be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.
[0130] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0131] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0132] The systems and techniques described herein can be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a grid browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication grid). Examples of communication grids include: a local area network (LAN), a wide area network (WAN), the Internet, and a blockchain grid.
[0133] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication grid. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or a cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server for a distributed system, or a server combined with a blockchain.
[0134] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present disclosure. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.
[0135] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of the features. In the description of the present disclosure, "plurality" means at least two, such as two, three, etc., unless otherwise clearly and specifically defined.
[0136] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, segment or portion of code that includes one or more executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present disclosure includes additional implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present disclosure belong.
[0137] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute the instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), a fiber optic device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or otherwise processing in a suitable manner if necessary, and then stored in a computer memory.
[0138] It should be understood that the various parts of the present disclosure can be implemented in hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented in software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, any one of the following technologies known in the art or a combination thereof can be used to implement: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0139] A person skilled in the art may understand that all or part of the steps in the above-mentioned embodiment method may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.
[0140] In addition, each functional unit in each embodiment of the present disclosure may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.
[0141] The storage medium mentioned above may be a read-only memory, a disk or an optical disk, etc. Although the embodiments of the present disclosure have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limitations of the present disclosure. A person of ordinary skill in the art may change, modify, replace and modify the above embodiments within the scope of the present disclosure.
[0142] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.
[0143] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A method for obtaining code coverage, characterized in that: The method comprises: Obtain a test code file and a code coverage statistics scenario to which the test code file belongs; Based on the code coverage statistics scenario, determine a target code segment in the test code file that participates in code coverage statistics, wherein the target code segment includes one of a code segment in the test code file that can be supported by a test terminal running the test code file and an incremental code segment in the test code file; Running the test code file to obtain an operating code fragment of the test code file running on the test terminal; Based on the target code snippet and the running code snippet, the target code coverage of the test code file is obtained.
2. The method according to claim 1, characterized in that The determining, based on the code coverage statistics scenario, a target code segment in the test code file that participates in code coverage statistics includes: In response to the code coverage statistics scenario being the first coverage statistics scenario, a source code file of the test code file is acquired and the source code file is scanned to obtain associated terminal configuration parameters of the test code blocks included in the test code file, wherein the first coverage statistics scenario is a scenario of counting the coverage of the code snippets supportable by the test terminal in the test code file; Acquire the test terminal configuration parameters of the test terminal, and acquire the redundant configuration parameters that do not match the test terminal configuration parameters among all associated terminal configuration parameters, so as to acquire the redundant code blocks corresponding to the redundant configuration parameters; Remaining code blocks except the redundant code blocks in all the test code blocks are obtained, and code snippets in the remaining code blocks are determined as the target code snippets participating in code coverage statistics in the test code file in the first coverage statistics scenario.
3. The method according to claim 1, characterized in that The determining, based on the code coverage statistics scenario, a target code segment in the test code file that participates in code coverage statistics includes: In response to the code coverage statistics scenario being a second coverage statistics scenario, obtaining a comparison code file of the test code file, wherein the second coverage statistics scenario is a scenario of performing statistics on the coverage of the test code file based on an incremental code snippet of the comparison code file; The test code file and the comparison code file are respectively parsed by syntax trees to obtain an incremental code fragment of the test code file based on the comparison code file as the target code fragment.
4. The method according to claim 3, characterized in that The performing syntax tree parsing on the test code file and the comparison code file respectively to obtain an incremental code fragment of the test code file based on the comparison code file as the target code fragment includes: Performing syntax tree parsing on the test code file and the comparison code file respectively, obtaining a first syntax tree parsing result of the test code file and a second syntax tree parsing result of the comparison code file; Based on the first syntax tree parsing result and the second syntax tree parsing result, obtaining an incremental code snippet of the test code file based on the comparison code file; The incremental code snippet is determined as the target code snippet in the test code file that participates in code coverage statistics.
5. The method according to claim 4, characterized in that The obtaining, based on the first syntax tree parsing result and the second syntax tree parsing result, an incremental code snippet of the test code file based on the comparison code file includes: Verify the first syntax tree parsing result and the second syntax tree parsing result, and obtain a verification result of the first syntax tree parsing result and the second syntax tree parsing result; In response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, obtaining difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determining an incremental code branch of the test code file based on the comparison code file based on the difference information; Based on the incremental code branch, the incremental code fragment of the test code file based on the comparison code file is obtained.
6. The method according to claim 5, characterized in that In response to the verification result not matching, obtaining difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determining, based on the difference information, an incremental code branch of the test code file based on the comparison code file, comprising: Obtain a code difference type of the first syntax tree parsing result based on the second syntax tree parsing result, and determine the code branches belonging to the code difference type in all test code blocks as the incremental code branches of the test code file based on the comparison code file.
7. The method according to any one of claims 1 to 6, characterized in that: The obtaining the target code coverage of the test code file based on the target code snippet and the running code snippet includes: Obtain a first code parameter of the target code snippet and a second code parameter of the running code snippet, and obtain the target code coverage of the test code file based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter at least include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation, and code parameters required for conditional coverage calculation.
8. A device for obtaining code coverage, characterized in that: The device comprises: A first acquisition module is used to acquire a test code file and a code coverage statistical scenario to which the test code file belongs; A determination module, configured to determine, based on the code coverage statistics scenario, a target code segment in the test code file that participates in code coverage statistics, wherein the target code segment comprises one of a code segment in the test code file that can be supported by a test terminal running the test code file and an incremental code segment in the test code file; A running module, used for running the test code file to obtain a running code fragment of the test code file running on the test terminal; The second acquisition module is used to acquire the target code coverage of the test code file based on the target code fragment and the running code fragment.
9. The device according to claim 8, characterized in that The determining module is further used for: In response to the code coverage statistics scenario being the first coverage statistics scenario, a source code file of the test code file is acquired and the source code file is scanned to obtain associated terminal configuration parameters of the test code blocks included in the test code file, wherein the first coverage statistics scenario is a scenario of counting the coverage of the code snippets supportable by the test terminal in the test code file; Acquire the test terminal configuration parameters of the test terminal, and acquire the redundant configuration parameters that do not match the test terminal configuration parameters among all associated terminal configuration parameters, so as to acquire the redundant code blocks corresponding to the redundant configuration parameters; Remaining code blocks except the redundant code blocks in all the test code blocks are obtained, and code snippets in the remaining code blocks are determined as the target code snippets participating in code coverage statistics in the test code file in the first coverage statistics scenario.
10. The device according to claim 8, characterized in that The determining module is further used for: In response to the code coverage statistics scenario being a second coverage statistics scenario, obtaining a comparison code file of the test code file, wherein the second coverage statistics scenario is a scenario of performing statistics on the coverage of the test code file based on an incremental code snippet of the comparison code file; The test code file and the comparison code file are respectively parsed by syntax trees to obtain an incremental code fragment of the test code file based on the comparison code file as the target code fragment.
11. The device according to claim 10, characterized in that The determining module is further used for: Performing syntax tree parsing on the test code file and the comparison code file respectively, obtaining a first syntax tree parsing result of the test code file and a second syntax tree parsing result of the comparison code file; Based on the first syntax tree parsing result and the second syntax tree parsing result, obtaining an incremental code snippet of the test code file based on the comparison code file; The incremental code snippet is determined as the target code snippet in the test code file that participates in code coverage statistics.
12. The device according to claim 11, characterized in that The determining module is further used for: Verify the first syntax tree parsing result and the second syntax tree parsing result, and obtain a verification result of the first syntax tree parsing result and the second syntax tree parsing result; In response to the verification result indicating that the first syntax tree parsing result and the second syntax tree parsing result do not match, obtaining difference information between the first syntax tree parsing result and the second syntax tree parsing result, and determining an incremental code branch of the test code file based on the comparison code file based on the difference information; Based on the incremental code branch, the incremental code fragment of the test code file based on the comparison code file is obtained.
13. The device according to claim 12, characterized in that The determining module is further used for: Obtain a code difference type of the first syntax tree parsing result based on the second syntax tree parsing result, and determine the code branches belonging to the code difference type in all test code blocks as the incremental code branches of the test code file based on the comparison code file.
14. The device according to any one of claims 8 to 13, characterized in that: The second acquisition module is further used for: Obtain a first code parameter of the target code snippet and a second code parameter of the running code snippet, and obtain the target code coverage of the test code file based on the first code parameter and the second code parameter, wherein the first code parameter and / or the second code parameter at least include at least one of code parameters required for instruction coverage calculation, code parameters required for loop coverage calculation, code parameters required for branch coverage calculation, code parameters required for line coverage calculation, and code parameters required for conditional coverage calculation.
15. An electronic device, characterized in that: include: processor; a memory for storing executable instructions for the processor; The processor is configured to execute instructions to implement the method as claimed in any one of claims 1 to 7.
16. A computer-readable storage medium, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method according to any one of claims 1 to 7.
Citation Information
Cited By
Coverage rate database merging method, electronic equipment and storage medium
CN121070889A
