Code testing method, code testing device, storage medium and program product
By obtaining and comparing the target code and historical code data, determine the change line and generate a coverage report, solve the problem of incremental code coverage cannot accurately explain the content of the content of the change, and realize the accurate coverage statistics and software testing of the change line. improve.
Patent Information
- Application Number
- CN202411531884.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-30
- Publication Date
- 2025-01-28
AI Technical Summary
In the prior art, the incremental code coverage cannot accurately explain the coverage of the changed content, and the coverage of the specific changed line cannot be counted.
By obtaining the target code data and historical code data, the change line is determined, and the result of the change line covers is determined based on whether the coverage line is the change line. Generate code coverage report, including changing row coverage, which is used to evaluate the degree of coverage of the change code for testing cases.
The statistics of the coverage of specific changes are achieved, and the statistics of the incremental code coverage of the incremental code are lacking, and the accuracy and effectiveness of software testing are improved.
Smart Images

Figure CN119377117A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a code testing method, a code testing device, a computer-readable storage medium, and a computer program product. Background Art
[0002] Code coverage is an important indicator that reflects the coverage of the tested program by test cases, and is also a reference value for measuring test completeness. Whether it is unit testing, API (Application Program Interface) testing, or functional testing, the program code is ultimately called. After the test is completed, the code coverage data can be used to determine whether the test is sufficient and where the weaknesses of the test are, and then the developer or auxiliary tester can be guided to increase the coverage of test cases.
[0003] The statistical granularity of incremental coverage is at the class level, and it provides feedback on the coverage of methods, lines, branches, etc. in the changed class. For a changed method, even if the incremental coverage reaches 90%, it may not cover the changed lines. Similarly, an incremental coverage of 10% does not necessarily mean that the changed lines are not covered.
[0004] Suppose a 100-line method actually changes two lines. After executing the test case, the coverage statistics are performed and the incremental coverage of this method is 98%. However, the actual changed lines are exactly 2% that are not covered. Obviously, this incremental coverage data cannot indicate that the coverage of this test meets the standard. This test does not cover the changed code, so a missed test occurs.
[0005] Coverage statistics are an important assessment of test effectiveness and code quality. Currently, the types of coverage statistics in the industry are mainly divided into full coverage and incremental coverage. For a change, incremental coverage can reflect the coverage of the changed class and method. However, the above two types cannot count the coverage of specific changed lines. When there are many methods in a changed class, and each method with dozens of lines has only two or three small changes, the incremental coverage cannot fully explain the coverage of the changed content.
[0006] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0007] Embodiments of the present invention provide a code testing method, a code testing device, a computer-readable storage medium, and a computer program product to at least solve the technical problem in the prior art that incremental code coverage cannot accurately describe the coverage of changed content.
[0008] According to one aspect of an embodiment of the present invention, a code testing method is provided, comprising: obtaining target code data and historical code data, and determining a changed line based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the changed behavior is a code line corresponding to the code in the target code data that has changed compared to the historical code data; obtaining a plurality of coverage lines corresponding to the target code data, and determining a changed line coverage result based on whether the coverage line is the changed line, wherein the coverage line is a code line corresponding to the code data actually executed when the target code data executes a test case, and the changed line coverage result includes at least a changed coverage line; generating a code coverage report based at least on the changed line coverage result, wherein the code coverage report includes a changed line coverage rate of the target code data, and the changed line coverage rate is a ratio of the total number of the changed coverage lines to the total number of the changed lines; and determining that the target code data has passed the test when the changed line coverage rate is greater than a threshold.
[0009] Optionally, determining the change line according to the target code data and the historical code data includes: comparing the target code data and the historical code data to obtain difference data, wherein the difference data includes code data newly added to the target code data relative to the historical code data and code data modified in the target code data relative to the historical code data; obtaining class files corresponding to the difference data to obtain multiple first-class files; and segmenting the first-class files to obtain multiple change lines.
[0010] Optionally, obtaining multiple coverage lines corresponding to the target code data includes: testing the target code data using the test case to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; inserting code stubs into the target code data to obtain the executable file; obtaining code lines corresponding to the executable file to obtain multiple coverage lines.
[0011] Optionally, the change row coverage result also includes a non-change coverage row, and the change row coverage result is determined according to whether the coverage row is the change row, including: when the coverage row is the change row, determining that the change row coverage result is the change coverage row; when the coverage row is not the change row, determining that the change row coverage result is the non-change coverage row.
[0012] Optionally, the code coverage report includes a statistical data report and a code coloring report, and the code coverage report is generated at least based on the changed line coverage result, including: obtaining the class file corresponding to the changed line to obtain multiple second-category files; classifying the multiple second-category files according to the package path of the second-category files to obtain multiple classification results; generating the code coloring report based on the changed line coverage result; generating the statistical data report based on the classification result.
[0013] Optionally, the code coloring report includes a first display result and a second display result. The code coloring report is generated according to the changed line coverage result, including: performing a first highlighting on the code data corresponding to the changed line to obtain the first display result; performing a second highlighting on the code data corresponding to the covered line to obtain the second display result, wherein the first highlighting and the second highlighting are different.
[0014] Optionally, the statistical data report is generated based on the classification result, including: displaying the changed line coverage of the target code data corresponding to each of the package paths to obtain the statistical data report.
[0015] According to another aspect of an embodiment of the present invention, a code testing device is also provided, comprising: an acquisition unit, used to acquire target code data and historical code data, and determine a changed line based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the changed behavior is a code line corresponding to the code in the target code data that has changed compared to the historical code data; a second acquisition unit, used to acquire multiple coverage lines corresponding to the target code data, and determine a changed line coverage result based on whether the coverage line is the changed line, wherein the coverage behavior is a code line corresponding to the code data actually executed when the target code data executes a test case, and the changed line coverage result at least includes a changed coverage line; a generation unit, used to generate a code coverage report based on at least the changed line coverage result, wherein the code coverage report includes a changed line coverage rate of the target code data, and the changed line coverage rate is a ratio of the total number of the changed coverage lines to the total number of the changed lines; a determination unit, used to determine that the target code data has passed the test when the changed line coverage rate is greater than a threshold.
[0016] Optionally, the acquisition unit includes: a comparison subunit, used to compare the target code data and the historical code data to obtain difference data, wherein the difference data includes code data newly added to the target code data relative to the historical code data and code data modified in the target code data relative to the historical code data; a first acquisition subunit, used to obtain class files corresponding to the difference data to obtain multiple first class files; and a segmentation subunit, used to segment the first class files to obtain multiple change lines.
[0017] Optionally, the second acquisition unit includes: a testing subunit, used to test the target code data using the test case to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; an insertion subunit, used to insert code stubs into the target code data to obtain the executable file; and a second acquisition subunit, used to obtain code lines corresponding to the executable file to obtain a plurality of coverage lines.
[0018] Optionally, the second acquisition unit includes: a first determination subunit, used to determine that the change line coverage result is the change coverage line when the coverage line is the change line; and a second determination subunit, used to determine that the change line coverage result is the non-change coverage line when the coverage line is not the change line.
[0019] Optionally, the generation unit includes: a third acquisition sub-unit, used to acquire the class file corresponding to the changed line to obtain multiple second-category files; a classification sub-unit, used to classify the multiple second-category files according to the package paths of the second-category files to obtain multiple classification results; a first generation sub-unit, used to generate the code coloring report according to the changed line coverage results; and a second generation sub-unit, used to generate the statistical data report according to the classification results.
[0020] Optionally, the first generation subunit includes: a first display subunit, used to perform a first highlight on the code data corresponding to the changed line to obtain the first display result; a second display subunit, used to perform a second highlight on the code data corresponding to the overwritten line to obtain the second display result, wherein the first highlight and the second highlight are different.
[0021] Optionally, the second generating subunit includes: a display subunit, used to display the changed line coverage of the target code data corresponding to each package path to obtain the statistical data report.
[0022] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is further provided, wherein the computer-readable storage medium includes a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to execute a testing method of any one of the codes described above.
[0023] According to another aspect of an embodiment of the present invention, a processor is provided, wherein the processor is used to run a program, wherein the program executes any one of the above-mentioned code testing methods when running.
[0024] According to another aspect of an embodiment of the present invention, a computer program product is provided, including a computer program, wherein when the computer program is executed by a processor, the steps of any one of the above-mentioned code testing methods are implemented.
[0025] In an embodiment of the present invention, target code data and historical code data are obtained, and change lines are determined based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the change line is the code line corresponding to the code that has changed in the target code data compared to the historical code data; multiple coverage lines corresponding to the target code data are obtained, and the change line coverage results are determined based on whether the coverage lines are change lines, wherein the coverage lines are the code lines corresponding to the code data actually executed when the target code data executes the test case, and the change line coverage results include at least the change coverage lines; a code coverage report is generated based on at least the change line coverage results, wherein the code coverage report includes the change line coverage rate of the target code data, and the change line coverage rate is the ratio of the total number of change coverage lines to the total number of change lines; when the change line coverage rate is greater than a threshold, it is determined that the target code data has passed the test. Through the technical solution provided by the present invention, it is achieved that the coverage of specific changed lines is counted on the basis of incremental code coverage statistics, the statistical deficiency of incremental code coverage on changed lines is compensated, and the proportion of executed code lines in all changed lines is intuitively fed back, thereby achieving the technical effect of accurately displaying change coverage in line dimension, improving the accuracy and effectiveness of software testing, and further solving the technical problem in the prior art that incremental code coverage cannot accurately describe the coverage of changed content. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0027] Figure 1 It is a hardware structure block diagram of a mobile terminal of a code testing method according to an embodiment of the present invention;
[0028] Figure 2 is a flow chart of a code testing method according to an embodiment of the present invention;
[0029] Figure 3 is a flowchart of an optional code testing method according to an embodiment of the present invention;
[0030] Figure 4 is a schematic diagram of a coverage coloring report according to an embodiment of the present invention;
[0031] Figure 5 is a schematic diagram of a code testing device according to an embodiment of the present invention.
[0032] The above drawings include the following reference numerals:
[0033] 102, processor; 104, memory; 106, transmission device; 108, input and output devices. DETAILED DESCRIPTION
[0034] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0035] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0036] As described in the background technology, the incremental code coverage in the prior art cannot accurately describe the coverage of the changed content. In view of the above defects, a code testing method, a code testing device, a computer-readable storage medium and a computer program product are provided in the embodiments of the present invention.
[0037] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0038] The method embodiments provided in the embodiments of the present invention can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a mobile terminal as an example, Figure 1 FIG. 1 is a hardware structure block diagram of a mobile terminal of a code testing method according to an embodiment of the present invention. Figure 1 As shown, the mobile terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the mobile terminal may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is for illustration only and does not limit the structure of the mobile terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0039] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer programs corresponding to the code testing method in the embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, that is, the above method is implemented. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the mobile terminal via a network. Examples of the above networks include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof. The transmission device 106 is used to receive or send data via a network. The specific examples of the above networks may include a wireless network provided by a communication provider of the mobile terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0040] According to an embodiment of the present invention, a method embodiment of a code testing method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0041] Figure 2 is a flow chart of a method for testing code according to an embodiment of the present invention, such as Figure 2 As shown, the method comprises the following steps:
[0042] Step S202, obtain the target code data and the historical code data, and determine the change line based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is performed, and the change line is the code line corresponding to the code in the target code data that has changed compared to the historical code data.
[0043] In this embodiment, the changed lines can be determined, that is, which codes have changed after the code is changed, so that targeted testing can be performed to ensure the correctness and stability of the new code. By testing the changed lines, the correctness of the code changes can be effectively verified to avoid introducing potential bugs and problems. Problems can also be located and solved more quickly, thereby improving testing efficiency and quality.
[0044] Step S204, obtain multiple coverage lines corresponding to the target code data, and determine the changed line coverage result according to whether the coverage line is a changed line, wherein the coverage line is the code line corresponding to the code data actually executed when the target code data executes the test case, and the changed line coverage result at least includes the changed coverage line.
[0045] In this embodiment, obtaining multiple coverage lines corresponding to the target code data and determining the changed line coverage results can evaluate the coverage and effect of the test case. By analyzing the relationship between the coverage lines and the changed lines, it can be determined which changed codes have been covered by the test cases and which changed codes have not yet been covered, thereby optimizing the test cases and improving the test coverage and quality. At the same time, potential problems introduced by code changes can be discovered in time, repaired in time, and the risks in the software development and maintenance process can be reduced.
[0046] Step S206, generating a code coverage report at least based on the changed line coverage result, wherein the code coverage report includes the changed line coverage rate of the target code data, and the changed line coverage rate is the ratio of the total number of changed coverage lines to the total number of changed lines.
[0047] It should be noted that the above code coverage report is a report used to evaluate test coverage, which shows which parts of the code are covered by the test and which parts are not covered by the test. It usually displays information such as the number of code lines, code blocks, and branches covered by the test, as well as the coverage of each code block and branch.
[0048] In this embodiment, it is possible to understand how much target code data is covered by the test case, and the change line coverage can evaluate the coverage of the test case for the latest code changes, which helps to check the quality and effectiveness of the test case and discover possible omissions or loopholes. By analyzing the code coverage report, the test strategy and use cases can be adjusted in time to ensure the quality and stability of the code.
[0049] Step S208: When the changed line coverage is greater than the threshold, it is determined that the target code data passes the test.
[0050] In this embodiment, determining whether the target code data has passed the test can ensure that when the change line coverage is greater than a threshold, the target code has passed the corresponding test case, thereby improving the quality and stability of the software, and can promptly discover potential problems and defects and repair them in a timely manner, ensuring that the software has been fully tested and verified before release. At the same time, it can also improve the maintainability and readability of the code, making the code more robust and reliable.
[0051] As can be seen from the above, in an embodiment of the present invention, target code data and historical code data are obtained, and the changed lines are determined based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the changed behavior is the code line corresponding to the code that has changed in the target code data compared to the historical code data; multiple coverage lines corresponding to the target code data are obtained, and the changed line coverage results are determined based on whether the covered lines are changed lines, wherein the covered behavior is the code line corresponding to the code data actually executed when the target code data executes the test case, and the changed line coverage results at least include the changed coverage lines; at least based on the changed line coverage results, a code coverage report is generated, wherein the code coverage report includes the changed line coverage rate of the target code data, and the changed line coverage rate is the ratio of the total number of changed coverage lines to the total number of changed lines; when the changed line coverage rate is greater than the threshold, it is determined that the target code data has passed the test, thereby achieving the purpose of statistically analyzing the coverage rate of specific changed lines on the basis of the incremental code coverage rate statistics, making up for the statistical deficiency of the incremental code coverage rate for the changed lines, and intuitively feeding back the proportion of the executed code lines in all changed lines, thereby achieving the technical effect of accurately displaying the change coverage rate in the line dimension, and improving the accuracy and effectiveness of software testing.
[0052] The technical solution provided by the embodiment of the present invention solves the technical problem in the prior art that the incremental code coverage cannot accurately describe the coverage of the changed content.
[0053] According to the above embodiment of the present invention, the change line is determined based on the target code data and the historical code data, including: comparing the target code data and the historical code data to obtain difference data, wherein the difference data includes the code data newly added in the target code data relative to the historical code data and the code data modified in the target code data relative to the historical code data; obtaining the class files corresponding to the difference data to obtain multiple first-class files; and segmenting the first-class files to obtain multiple change lines.
[0054] In this embodiment, by comparing the target code data and the historical code data to obtain the difference data, the changes of the target code relative to the historical code can be understood, so as to perform targeted testing. By obtaining the class files corresponding to the difference data and obtaining multiple first-class files, the specific class files that need to be tested can be determined, thereby improving the testing efficiency. The first-class files are segmented to obtain multiple change lines, which can more accurately locate the specific location of the code change, thereby performing testing and verification more effectively. By comparing and analyzing the code data, the impact of the code change can be better understood, thereby improving the accuracy and efficiency of the test.
[0055] Figure 3 is a flowchart of an optional code testing method according to an embodiment of the present invention, such as Figure 3 As shown in the figure, after the tester executes all the test cases of this change, the coverage statistics are triggered. The environment for executing the test cases needs to be completely consistent with the actual running environment. First, the difference information between the current branch and the comparison branch is obtained through git d iff, in order to obtain the changed lines. The newly added or modified code lines will be marked in the difference information, and deleted code does not need to be marked. When git merges two branches, it will retain as much context as possible. If there is no conflict in the merging process, the difference will be automatically resolved. If there is a conflict, manual intervention is required to resolve these conflicts. Then, the class file of the service code (that is, the first type of file) is obtained, the file is filtered and screened, and the code is cut into line dimensions.
[0056] It should be noted that the above-mentioned git d iff command is used to view the differences between the current file in the working directory and the file in the staging area. It will show which files have been modified and the specific content of these modifications. git d iff is not only based on regular expressions, but also implemented by comparing the "longest common subsequence" algorithm of two tree objects. Special cases such as multi-line changes, lines added again after deletion, and code after merge conflicts are resolved will be marked normally; the class file of the above-mentioned service code refers to the compiled Java source code file, which contains the definition, properties, methods and related logic of the service class. In server-side applications, class files are usually deployed on the server to provide specific service functions.
[0057] According to the above embodiment of the present invention, obtaining multiple coverage lines corresponding to the target code data includes: testing the target code data with a test case to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; inserting a code stub into the target code to obtain the executable file; obtaining the code lines corresponding to the executable file to obtain multiple coverage lines.
[0058] In this embodiment, the target code data is tested through test cases to verify the correctness and stability of the code. Inserting stubs into the target code can track the path of code execution and find out possible problems or vulnerabilities in the code. The code lines corresponding to the executable file are obtained, and multiple coverage lines are obtained to determine which codes are executed and which codes are not executed, so as to perform targeted testing and optimization, which can improve the test coverage of the code, reduce potential bugs (errors or failures in software or systems), and improve the quality and stability of the code, which is helpful to evaluate the coverage and quality of the code.
[0059] like Figure 3 As shown, the target code data is tested using test cases. By inserting stubs in the service code, the executable file within the test period, that is, the specific execution data of the code, is pulled from the server, the code lines corresponding to the executable file are obtained, and multiple coverage lines are obtained.
[0060] It should be noted that the above-mentioned instrumentation is to insert additional code snippets into the code, put Jacoco-agent into the service code, and run it through the startup script during deployment. It is performed on the bytecode after compilation. Among them, Jacoco-agent is a Java code coverage tool used to collect coverage information during code execution.
[0061] According to the above embodiment of the present invention, the change row coverage result also includes non-change coverage rows. The change row coverage result is determined according to whether the covered row is a change row, including: when the covered row is a change row, determining the change row coverage result as a change coverage row; when the covered row is not a change row, determining the change row coverage result as a non-change coverage row.
[0062] In this embodiment, it is possible to determine which lines of code are affected by the change, thereby more effectively verifying whether the new change will affect the functionality and logic of the original code. By identifying the code whose change line coverage results are change coverage lines, more in-depth testing can be performed on these codes to ensure the correct implementation of the change and the stability of the functions. At the same time, identifying non-change coverage lines can exclude code that does not need to be tested, improve testing efficiency, and help to quickly and accurately locate the scope of the change on the code, thereby better ensuring code quality and stability.
[0063] Specifically, the traversal statistics cover lines and add them to the incremental coverage list. At the same time, the changed line coverage list is increased. The incremental coverage list contains the number of covered lines, and the changed line coverage list includes the number of changed lines. Combined with the changed line mark in the branch difference information, it is determined at the entrance of the loop traversal whether it is a changed line. If so, the line is added to the changed line coverage list, and the statistics of the number of changed line coverage lines are increased.
[0064] It should be noted that the above acquisition of changed rows is performed before counting coverage, and counting the coverage of changed rows is performed simultaneously with counting all coverage.
[0065] According to the above embodiment of the present invention, the code coverage report includes a statistical data report and a code coloring report. The code coverage report is generated at least based on the changed line coverage result, including: obtaining the class file corresponding to the changed line to obtain multiple second-category files; classifying the multiple second-category files according to the package path of the second-category files to obtain multiple classification results; generating a code coloring report based on the changed line coverage result; and generating a statistical data report based on the classification result.
[0066] In this embodiment, by obtaining the class file corresponding to the changed line and obtaining multiple second-category files, the impact scope of the change can be understood and the relevant code can be reviewed. At the same time, by classifying multiple second-category files according to the package path of the second-category files, code changes can be better organized and managed to ensure the correctness and completeness of the code changes. Generating a code coloring report can quickly locate the position of the changed line and perform related testing. The statistical data report generated according to the classification results can provide an overview and analysis of the code changes, better understand the code changes, and perform corresponding optimizations and improvements based on the statistical data report, which can better manage and review code changes, improve the maintainability and testability of the code, and ensure the quality and stability of code changes.
[0067] like Figure 3 As shown, the incremental coverage only analyzes the coverage of the changed class, obtains the changed class from the branch difference information, extracts the class files of the changed class (i.e., the second type of files), and aggregates the class files by package path to generate data in the report. The data is counted in dimensions such as package-class-method, and the code can be displayed by project path, which is more intuitive and clear, and can better organize and display the coverage data. The aggregation process will not affect the identification of changed lines, and all changed lines can be accurately found in the aggregated data. After that, a code coverage report is generated, which includes a statistical data report and a code coloring report. According to the coverage results of the changed lines, a code coloring report is generated, and according to the classification results, a statistical data report is generated.
[0068] In addition, when generating a code coverage report, a corresponding column is added to the report table to show the coverage of the changed lines.
[0069] In the above embodiment of the present invention, the code coloring report includes a first display result and a second display result. The code coloring report is generated according to the changed line coverage result, including: performing a first highlighting on the code data corresponding to the changed line to obtain a first display result; performing a second highlighting on the code data corresponding to the covered line to obtain a second display result, wherein the first highlighting and the second highlighting are different.
[0070] In this embodiment, the first highlight can quickly locate the code data corresponding to the changed line to perform targeted testing and verification, and the second highlight can quickly locate the code data corresponding to the covered line to confirm whether the test case covers all relevant code paths. This allows for more efficient testing, improved test coverage and quality, and reduced potential risk of missed tests.
[0071] Specifically, the changed line coverage will only highlight whether the marked changed lines are covered (i.e., the first highlight), and the incremental coverage will highlight whether the entire method is covered (i.e., the second highlight).
[0072] In the above embodiment of the present invention, a statistical data report is generated according to the classification result, including: displaying the changed line coverage of the target code data corresponding to each package path to obtain the statistical data report.
[0073] In this embodiment, displaying the changed line coverage of the target code data corresponding to each package path can understand the impact scope of the code change and the code coverage, so as to perform code review and testing more effectively. The statistical data report can provide detailed indicators and statistical information to evaluate the quality and completeness of the test, and determine which parts of the code require more test coverage. By analyzing the statistical data report of the changed line coverage, possible missing test cases or uncovered code paths can be discovered, so as to promptly fix potential problems and improve code quality and reliability. In addition, the statistical data report can also better allocate test resources and prioritize the code paths that are most affected by the change to improve test efficiency and coverage.
[0074] It should be noted that the line number, specific code content, and whether it is covered will all be displayed.
[0075] Figure 4 is a schematic diagram of a coverage coloring report according to an embodiment of the present invention, such as Figure 4 As shown in the figure, the code line marked with "+" is the changed line this time, the red background is the uncovered code, and the green background is the covered code. The coverage results this time are: incremental coverage 80%, and changed line coverage 0%, indicating that the test case does not cover the changed line and there is a missed test. This reflects that relying solely on incremental coverage as the standard for passing the test has a certain risk of missed tests. It can be used as a supplement to incremental coverage to further ensure test quality.
[0076] In addition, it should be noted that, assuming that a 100-line method actually changes two lines, after executing the test case, the incremental coverage and the changed line coverage are counted, and the incremental coverage of this method is 98%, while the changed line coverage is 0%. By checking the dye report, it is found that the two changed lines are not covered, and it is concluded that the test fails and the missing test cases need to be supplemented. After adding test cases and executing them, the coverage is counted again, and the incremental coverage of this method is 100%, while the changed line coverage is 100%, and it is concluded that the test passes.
[0077] The above shows that the embodiments of the present invention can find missed measurement points, thus making up for the deficiency that the incremental coverage does not count the granularity of the changed rows.
[0078] The technical solution provided by the above-mentioned embodiment of the present invention has a finer granularity than the existing coverage statistics method, and can more intuitively display the coverage of this change. It only makes slight changes based on the incremental coverage, which does not affect the original execution method. The changed line coverage can be added to the incremental coverage coloring report, which solves the lack of changed line statistics in the incremental code coverage. By requiring a minimum value for the changed line coverage before going online, the coverage of the changed code is improved to avoid missing the change point.
[0079] That is, the technical solution provided by the embodiment of the present invention takes various factors into comprehensive consideration, improves on the basis of incremental code coverage statistics, and counts the coverage of specific changed lines, which makes up for the lack of statistics on changed lines in incremental code coverage. It can intuitively feedback the proportion of executed code lines in all changed lines, and more accurately display the change coverage in the line dimension.
[0080] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0081] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.
[0082] According to an embodiment of the present invention, a code testing device for implementing the above code testing method is also provided. Figure 5 is a schematic diagram of a code testing device according to an embodiment of the present invention, such as Figure 5 As shown, the device includes: an acquisition unit 501, a second acquisition unit 503, a generation unit 505 and a determination unit 507. The following is an explanation of the code testing device.
[0083] The acquisition unit 501 is used to acquire the target code data and the historical code data, and determine the change line based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the change line is the code line corresponding to the code in the target code data that has changed compared to the historical code data.
[0084] The second acquisition unit 503 is used to obtain multiple coverage lines corresponding to the target code data, and determine the changed line coverage result according to whether the covered line is a changed line, wherein the covered line is the code line corresponding to the code data actually executed when the target code data executes the test case, and the changed line coverage result at least includes the changed coverage line.
[0085] The generation unit 505 is used to generate a code coverage report based on at least the changed line coverage result, wherein the code coverage report includes the changed line coverage rate of the target code data, and the changed line coverage rate is the ratio of the total number of changed coverage lines to the total number of changed lines.
[0086] The determination unit 507 is used to determine whether the target code data passes the test when the changed line coverage is greater than a threshold.
[0087] It should be noted here that the above-mentioned acquisition unit 501, the second acquisition unit 503, the generation unit 505 and the determination unit 507 correspond to steps S202 to S208 in the above-mentioned embodiments, and the four modules are the same as the instances and application scenarios implemented by the corresponding steps, but are not limited to the contents disclosed in the above-mentioned embodiments.
[0088] As can be seen from the above, in the scheme recorded in the above-mentioned embodiment of the present invention, firstly, an acquisition unit can be used to acquire target code data and historical code data, and based on the target code data and the historical code data, a changed line can be determined, wherein the target code data is the historical code data after the execution of the change, and the changed line is the code line corresponding to the code that has changed in the target code data compared to the historical code data; then a second acquisition unit can be used to acquire multiple coverage lines corresponding to the target code data, and based on whether the coverage line is a changed line, a changed line coverage result can be determined, wherein the coverage line is the code line corresponding to the code data actually executed when the target code data executes the test case, and the changed line coverage result includes at least the changed coverage line; secondly, The generation unit can be used to generate a code coverage report based on at least the changed line coverage result, wherein the code coverage report includes the changed line coverage rate of the target code data, and the changed line coverage rate is the ratio of the total number of changed coverage lines to the total number of changed lines; finally, the determination unit can be used to determine that the target code data has passed the test when the changed line coverage rate is greater than the threshold, thereby achieving the purpose of counting the coverage rate of specific changed lines on the basis of incremental code coverage rate statistics, making up for the statistical deficiency of incremental code coverage rate for changed lines, and intuitively feeding back the proportion of executed code lines in all changed lines, thereby achieving the technical effect of accurately displaying the change coverage rate in the line dimension, and improving the accuracy and effectiveness of software testing.
[0089] The technical solution provided by the embodiment of the present invention solves the technical problem in the prior art that the incremental code coverage cannot accurately describe the coverage of the changed content.
[0090] In an optional embodiment, the acquisition unit includes: a comparison subunit, used to compare the target code data and the historical code data to obtain difference data, wherein the difference data includes the code data newly added to the target code data relative to the historical code data and the code data modified in the target code data relative to the historical code data; a first acquisition subunit, used to obtain the class files corresponding to the difference data to obtain multiple first class files; and a segmentation subunit, used to segment the first class files to obtain multiple change lines.
[0091] In an optional embodiment, the second acquisition unit includes: a testing subunit, used to test the target code data using a test case to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; an insertion subunit, used to insert a code stub into the target code to obtain an executable file; and a second acquisition subunit, used to obtain code lines corresponding to the executable file to obtain multiple coverage lines.
[0092] In an optional embodiment, the second acquisition unit includes: a first determination subunit, used to determine that the coverage result of the changed row is a changed coverage row when the coverage row is a changed row; and a second determination subunit, used to determine that the coverage result of the changed row is a non-changed coverage row when the coverage row is not a changed row.
[0093] In an optional embodiment, the generation unit includes: a third acquisition sub-unit, used to acquire the class file corresponding to the changed line to obtain multiple second-category files; a classification sub-unit, used to classify the multiple second-category files according to the package path of the second-category files to obtain multiple classification results; a first generation sub-unit, used to generate a code coloring report according to the changed line coverage result; and a second generation sub-unit, used to generate a statistical data report according to the classification result.
[0094] In an optional embodiment, the first generating sub-unit includes: a first display sub-unit, used to perform a first highlighting on the code data corresponding to the changed line to obtain a first display result; a second display sub-unit, used to perform a second highlighting on the code data corresponding to the covered line to obtain a second display result, wherein the first highlighting and the second highlighting are different.
[0095] In an optional embodiment, the second generation subunit includes: a display subunit, which is used to display the changed line coverage of the target code data corresponding to each package path to obtain a statistical data report.
[0096] According to another aspect of an embodiment of the present invention, a processor is provided, and the processor is used to run a program, wherein the program executes any one of the above code testing methods when the program is running.
[0097] According to another aspect of an embodiment of the present invention, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps of any one of the above code testing methods are implemented.
[0098] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium including a stored program, wherein when the program is running, a test method for controlling a device where the computer-readable storage medium is located to execute any one of the above codes.
[0099] Optionally, in this embodiment, the computer-readable storage medium may be located in any one of the computer terminals in a computer terminal group in a computer network, or in any one of the communication devices in a communication device group.
[0100] Optionally, in this embodiment, a computer-readable storage medium is configured to store program code for performing the following steps: obtaining target code data and historical code data, and determining change lines based on the target code data and the historical code data, wherein the target code data is the historical code data after the change is executed, and the change line is the code line corresponding to the code that has changed in the target code data compared to the historical code data; obtaining multiple coverage lines corresponding to the target code data, and determining the change line coverage result based on whether the coverage line is a change line, wherein the coverage line is the code line corresponding to the code data actually executed when the target code data executes the test case, and the change line coverage result includes at least the change coverage line; generating a code coverage report based on at least the change line coverage result, wherein the code coverage report includes the change line coverage rate of the target code data, and the change line coverage rate is the ratio of the total number of change coverage lines to the total number of change lines; when the change line coverage rate is greater than a threshold, determining that the target code data has passed the test.
[0101] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for performing the following steps: comparing target code data and historical code data to obtain difference data, wherein the difference data includes code data newly added to the target code data relative to the historical code data and code data modified in the target code data relative to the historical code data; obtaining class files corresponding to the difference data to obtain multiple first-class files; and segmenting the first-class files to obtain multiple change lines.
[0102] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for performing the following steps: testing the target code data using a test case to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; inserting a code stub into the target code to obtain an executable file; obtaining the code lines corresponding to the executable file to obtain multiple coverage lines.
[0103] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for executing the following steps: in the case where the coverage line is a changed line, determining the changed line coverage result to be a changed coverage line; in the case where the coverage line is not a changed line, determining the changed line coverage result to be a non-changed coverage line.
[0104] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for executing the following steps: obtaining the class file corresponding to the changed line to obtain multiple second-category files; classifying the multiple second-category files according to the package path of the second-category files to obtain multiple classification results; generating a code coloring report based on the changed line coverage results; and generating a statistical data report based on the classification results.
[0105] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for executing the following steps: performing a first highlighting on the code data corresponding to the changed line to obtain a first display result; performing a second highlighting on the code data corresponding to the overwritten line to obtain a second display result, wherein the first highlighting and the second highlighting are different.
[0106] Optionally, in this embodiment, the computer-readable storage medium is configured to store program code for executing the following steps: displaying the changed line coverage of the target code data corresponding to each package path to obtain a statistical data report.
[0107] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0108] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0109] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0110] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0111] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0112] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the above-mentioned methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0113] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A code testing method, characterized in that: include: Obtaining target code data and historical code data, and determining a change line according to the target code data and the historical code data, wherein the target code data is the historical code data after the change is performed, and the change line is a code line corresponding to a code in the target code data that has changed compared to the historical code data; Acquire multiple coverage lines corresponding to the target code data, and determine the change line coverage result according to whether the coverage line is the change line, wherein the coverage line is the code line corresponding to the code data actually executed when the target code data executes the test case, and the change line coverage result at least includes the change coverage line; Generate a code coverage report at least according to the changed line coverage result, wherein the code coverage report includes the changed line coverage rate of the target code data, and the changed line coverage rate is the ratio of the total number of the changed coverage lines to the total number of the changed lines; When the changed line coverage is greater than a threshold, it is determined that the target code data passes the test.
2. The method according to claim 1, characterized in that Determining the changed line according to the target code data and the historical code data includes: Comparing the target code data with the historical code data to obtain difference data, wherein the difference data includes code data newly added to the target code data relative to the historical code data and code data modified to the target code data relative to the historical code data; Obtaining class files corresponding to the difference data to obtain a plurality of first class files; The first type of files are segmented to obtain a plurality of the changed lines.
3. The method according to claim 1, characterized in that Acquiring a plurality of coverage lines corresponding to the target code data, including: Using the test case to test the target code data, to obtain an executable file corresponding to the target code data, wherein the executable file is the code data actually executed; Inserting a code stub into the target code data to obtain the executable file; Obtain code lines corresponding to the executable file to obtain a plurality of coverage lines.
4. The method according to claim 1, characterized in that: The changed row coverage result also includes non-changed covered rows, and determining the changed row coverage result according to whether the covered row is the changed row includes: In the case where the coverage row is the changed row, determining the coverage result of the changed row to be the changed coverage row; In the case that the covered row is not the changed row, the changed row coverage result is determined to be the non-changed covered row.
5. The method according to claim 1, characterized in that The code coverage report includes a statistical data report and a code coloring report. The code coverage report is generated at least according to the changed line coverage result, including: Obtaining the class file corresponding to the changed line to obtain multiple second class files; Classifying a plurality of the second-category files according to the package paths of the second-category files to obtain a plurality of classification results; Generate the code coloring report according to the changed line coverage result; The statistical data report is generated according to the classification result.
6. The method according to claim 5, characterized in that The code coloring report includes a first display result and a second display result. The code coloring report is generated according to the changed line coverage result, including: Performing a first highlighting display on the code data corresponding to the changed row to obtain the first display result; A second highlighting is performed on the code data corresponding to the covered row to obtain the second display result, wherein the first highlighting is different from the second highlighting.
7. The method according to claim 5, characterized in that Generate the statistical data report according to the classification result, including: The coverage of changed lines of the target code data corresponding to each of the package paths is displayed to obtain the statistical data report.
8. A code testing device, characterized in that: include: an acquisition unit, configured to acquire target code data and historical code data, and determine a change line according to the target code data and the historical code data, wherein the target code data is the historical code data after the change is performed, and the change line is a code line corresponding to a code in the target code data that has changed compared to the historical code data; A second acquisition unit is used to acquire a plurality of coverage lines corresponding to the target code data, and determine a change line coverage result according to whether the coverage line is the change line, wherein the coverage line is a code line corresponding to the code data actually executed when the target code data executes the test case, and the change line coverage result includes at least the change coverage line; A generating unit, configured to generate a code coverage report at least according to the changed line coverage result, wherein the code coverage report includes a changed line coverage ratio of the target code data, and the changed line coverage ratio is a ratio of the total number of the changed coverage lines to the total number of the changed lines; A determination unit is used to determine that the target code data passes the test when the changed line coverage is greater than a threshold.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the computer-readable storage medium is located is controlled to execute the method according to any one of claims 1 to 7.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Incremental code coverage rate determination method and device, equipment and storage medium
CN120276998A
Coverage rate report generation method, test platform and equipment
CN121743224A