Incremental code test coverage statistics method, device and electronic device
By obtaining and processing incremental code information and generating test coverage reports, the existing incremental code test coverage statistics methods are solved, and simple and efficient coverage statistics are achieved.
Patent Information
- Application Number
- CN202311563287.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-22
- Publication Date
- 2025-04-25
- Estimated Expiration
- 2043-11-22
AI Technical Summary
The existing incremental code test coverage statistics method is complex and cumbersome to implement, and it is difficult to meet the simple and efficient needs of testers.
By obtaining the preconfigured build.gradle file, use the os library to directly call the preconfigured incremental code to obtain scripts, obtain incremental code information, and use the json library to store it as a json file. Finally, call the test coverage tool to generate an incremental code test coverage report.
Reduce the dependence on testers' technical requirements, simplify the configuration process, significantly reduce the amount of code, and achieve efficient statistics on incremental code test coverage.
Smart Images

Figure CN117539774B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to an incremental code test coverage statistics method, device and electronic equipment. Background Art
[0002] The primary goal of testers is to ensure the quality of testing existing functions and incremental functions, and code coverage statistics are an important indicator to achieve this goal. Code coverage refers to the ratio of the part of the code that is run to the total code when executing one or more test cases.
[0003] The commonly used code coverage statistics tool is jacoco. Jacoco needs to carry out secondary development to implement incremental code coverage statistics. The commonly used language for secondary development with jacoco is generally Java, which includes the following steps:
[0004] Step 1: Calculate the difference between the two versions (based on git);
[0005] Step 2: Pass the difference code to jacoco in the report stage of jacoco;
[0006] Step 3: Modify the jacoco source code and determine whether the code is incremental code when generating a report. Only incremental code will generate a report.
[0007] However, the existing Java code implementation requires adding many classes, methods, dependencies, and configurations, and the amount of code is large.
[0008] Therefore, a simple and efficient method for incremental code test coverage statistics is needed. Summary of the invention
[0009] 1. Technical issues to be solved
[0010] In view of the deficiencies in the prior art, the present invention provides an incremental code test coverage statistics method, device and electronic device, which solves the problem that the existing incremental code test coverage statistics method is complicated and cumbersome to implement.
[0011] (II) Technical solution
[0012] To achieve the above objectives, the present invention is implemented through the following technical solutions:
[0013] In a first aspect, a method for counting incremental code test coverage is provided, the method comprising:
[0014] S1. Get the pre-configured build.gradle file.
[0015] S2. Use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information;
[0016] The pre-configured incremental code acquisition script to obtain incremental code information includes:
[0017] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool;
[0018] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0019] Step 3: Use the json library to store the incremental code information as a json file;
[0020] And an interface for obtaining incremental code is added to the source code of the code difference comparison tool;
[0021] S3. Use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file.
[0022] Furthermore, configuring the build.gradle file includes:
[0023] Configure jacoco plugin settings, configure test tasks, and configure violation rules.
[0024] Furthermore, the incremental code acquisition script is a .sh script.
[0025] Furthermore, the generated incremental code test coverage report is an HTML test report, and a domain name for accessing the HTML test report is output through a Jenkins scheduled task.
[0026] In a second aspect, a device for counting incremental code test coverage is provided, the device comprising:
[0027] File configuration module, used to obtain pre-configured build.gradle files;
[0028] The incremental code acquisition module is used to directly call the pre-configured incremental code acquisition script using the os library to obtain incremental code information;
[0029] The pre-configured incremental code acquisition script to obtain incremental code information includes:
[0030] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool;
[0031] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0032] Step 3: Use the json library to store the incremental code information as a json file;
[0033] And an interface for obtaining incremental code is added to the source code of the code difference comparison tool;
[0034] The incremental code test coverage report output module is used to use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file.
[0035] Furthermore, configuring the build.gradle file includes:
[0036] Configure jacoco plugin settings, configure test tasks, and configure violation rules.
[0037] Furthermore, the incremental code acquisition script is a .sh script.
[0038] Furthermore, the generated incremental code test coverage report is an HTML test report, and the device also includes: a scheduled task module, which is used to output a domain name for accessing the HTML test report through a Jenkins scheduled task.
[0039] In a third aspect, an electronic device is provided, including:
[0040] one or more processors;
[0041] Memory; and
[0042] One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the programs including instructions for executing the above-mentioned incremental code test coverage statistics method.
[0043] (III) Beneficial effects
[0044] The present invention provides a method, device and electronic device for counting incremental code test coverage. Compared with the prior art, the present invention has the following beneficial effects:
[0045] The present invention uses Python language to implement the script start, stop, difference code comparison and report output related codes, eliminating the cumbersome Java language implementation and the strong coupling of the configuration file and the server, and reducing the technical requirements for testers. In addition, the present invention only needs to perform simple configuration of jacoco in the project build.gradle file and add an incremental code acquisition interface in the code-diff source code, which significantly reduces the amount of code. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0047] Figure 1 is a flow chart of an embodiment of the present invention;
[0048] Figure 2 This is a schematic diagram of some existing codes that implement functions through Java;
[0049] Figure 3 Schematic diagram of all codes of an embodiment of the present invention. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention are clearly and completely described. Obviously, the described embodiments are 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 are within the scope of protection of the present invention.
[0051] The embodiments of the present application solve the problem that the existing incremental code test coverage statistics methods are complicated and cumbersome to implement by providing an incremental code test coverage statistics method, device and electronic device.
[0052] In order to better understand the above technical solution, the above technical solution will be described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0053] Embodiment 1:
[0054] like Figure 1 As shown, the present invention provides an incremental code test coverage statistics method, which is executed by a computer and includes:
[0055] S1. Get the pre-configured build.gradle file.
[0056] S2. Use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information;
[0057] The pre-configured incremental code acquisition script to obtain incremental code information includes:
[0058] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool;
[0059] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0060] Step 3: Use the json library to store the incremental code information as a json file;
[0061] An interface for obtaining incremental code is added to the source code of the code difference comparison tool;
[0062] S3. Use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file.
[0063] Beneficial effects of the embodiments of the present invention:
[0064] In the embodiment of the present invention, the script start, stop, difference code comparison and report output related codes are implemented in Python language, eliminating the cumbersome Java language implementation and the strong coupling of the configuration file and the server, and reducing the technical requirements for testers. In addition, the present invention only needs to perform simple configuration of jacoco in the project build.gradle file and add an incremental code acquisition interface in the code-diff source code, which significantly reduces the amount of code.
[0065] The specific implementation of the embodiment of the present invention is described in detail below:
[0066] S1. Get the pre-configured build.gradle file.
[0067] Gradle is an open source tool for automated project building. Each gradle project or module has a build.gradle file, which is the entry point for project building and can configure information such as version, plugin dependency library, etc. Each build file has a corresponding project instance. Configuring the build.gradle file actually means setting properties in the project instance or calling methods in it.
[0068] In the specific implementation, the build.gradle file configuration includes: configuring the jacoco plugin settings, test tasks, and violation rules;
[0069] Configuring the jacoco plug-in settings includes: adding the jacoco plug-in to the build.gradle file and configuring the jacoco version, report output directory, and associated test tasks.
[0070] S2, based on Python implementation to obtain incremental code information;
[0071] In the specific implementation, the following steps are included:
[0072] Use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information;
[0073] Among them, Python's os library provides various functions for interacting with the operating system. These functions can be used to call some functions of the computer's underlying operating system to manage files and directories quickly and efficiently.
[0074] The incremental code acquisition script is a .sh script, which is a script file type executed by the operating system, also known as a Bash script or a Shell script. The .sh script contains a series of commands that can be executed in a command line.
[0075] And the pre-configured incremental code acquisition script is used to perform the following steps:
[0076] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool to run the code difference comparison tool;
[0077] Among them, Python's requests library is a commonly used HTTP request library, which can easily send HTTP requests to websites and obtain response results;
[0078] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0079] Among them, git is an open source distributed version control system. A version control system is a system that records changes in the content of one or more files so that the revision of a specific version can be checked in the future.
[0080] The code difference comparison tool adopts the existing code-diff tool and adds an interface for obtaining incremental code in its source code;
[0081] Step 3: Use the json library to store the incremental code information as a json file.
[0082] Among them, json is a lightweight data exchange format, and python's json library can generate and parse json files.
[0083] S3, based on Python implementation to obtain incremental code test coverage report;
[0084] In the specific implementation, the specific steps include:
[0085] Use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the json file obtained in step 3;
[0086] The test coverage tool is jacococli.jar; jar is a software package file format, usually used to aggregate a large number of Java class files, related metadata and resource (text, pictures, etc.) files into one file to develop Java platform application software or libraries. Generate incremental code test coverage report as html test report.
[0087] Sourcefile refers to a code file, which is the source file of the program code. It usually has a suffix of .c, .cpp, .py, etc., and contains all the source code of the program.
[0088] classfiles are class files, compiled Java source files.
[0089] Furthermore, in the present invention, the html test report is viewed through the Jenkins scheduled task. After the prompt task is executed on Jenkins, the domain name will be output and the test report can be directly accessed. Jenkins is an open source software project, a continuous integration tool developed based on Java, used to monitor continuous and repetitive work.
[0090] Finally, the script start, stop, difference code comparison and report output related codes are implemented in Python language.
[0091] Effect verification:
[0092] Some codes of existing methods are as follows Figure 2 As shown; the entire code of the method of this application is as follows Figure 3 As shown in the comparison results shown in Table 1, it is obvious that the method of the present application can significantly reduce the difficulty of development and reduce the amount of code.
[0093] Table 1
[0094] Code volume / line Currently implemented in Java 200+ This application is implemented in Python 20+
[0095] Embodiment 2:
[0096] An incremental code test coverage statistics device, the device comprising:
[0097] File configuration module, used to obtain pre-configured build.gradle files;
[0098] The incremental code acquisition module is used to directly call the pre-configured incremental code acquisition script using the os library to obtain incremental code information;
[0099] The pre-configured incremental code acquisition script to obtain incremental code information includes:
[0100] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool;
[0101] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0102] Step 3: Use the json library to store the incremental code information as a json file;
[0103] And an interface for obtaining incremental code is added to the source code of the code difference comparison tool;
[0104] The incremental code test coverage report output module is used to use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file.
[0105] In one embodiment, the generated incremental code test coverage report is an HTML test report, and the device also includes: a scheduled task module, which is used to output a domain name for accessing the HTML test report through a Jenkins scheduled task.
[0106] Embodiment 3:
[0107] An electronic device, comprising:
[0108] one or more processors;
[0109] Memory; and
[0110] One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the programs comprising steps for performing the following steps:
[0111] S1. Get the pre-configured build.gradle file.
[0112] S2. Use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information;
[0113] The pre-configured incremental code acquisition script to obtain incremental code information includes:
[0114] Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool;
[0115] Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port;
[0116] Step 3: Use the json library to store the incremental code information as a json file;
[0117] And an interface for obtaining incremental code is added to the source code of the code difference comparison tool;
[0118] S3. Use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file.
[0119] It can be understood that the incremental code test coverage statistics device and electronic device provided in the embodiments of the present invention correspond to the above-mentioned incremental code test coverage statistics method. The explanations, examples, beneficial effects and other parts of the relevant contents can refer to the corresponding contents in the incremental code test coverage statistics method, and will not be repeated here.
[0120] In summary, compared with the prior art, the present invention has the following beneficial effects:
[0121] The present invention uses Python language to implement the script start, stop, difference code comparison and report output related codes, eliminating the cumbersome Java language implementation and the strong coupling of the configuration file and the server, and reducing the technical requirements for testers. The present invention only needs to perform simple configuration of Jacoco in the project build.gradle file and add an incremental code acquisition interface in the code-diff source code, which significantly reduces the amount of code.
[0122] It should be noted that, through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment. In this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements that are not explicitly listed, or also includes elements inherent to such process, method, article or device. Without more constraints, an element defined by the phrase "comprising a..." does not exclude the existence of other identical elements in the process, method, article or apparatus comprising the element.
[0123] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A statistical method for incremental code test coverage, characterized in that: The method comprises: S1. Get the pre-configured build.gradle file. Configuring the build.gradle file includes: Configure jacoco plugin settings, configure test tasks, and configure violation rules; Configuring the jacoco plugin settings includes: adding the jacoco plugin to the build.gradle file and configuring the jacoco version, report output directory, and associated test tasks; S2, based on Python implementation to obtain incremental code information; Use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information; The pre-configured incremental code acquisition script to obtain incremental code information includes: Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool; Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port; Step 3: Use the json library to store the incremental code information as a json file; And an interface for obtaining incremental code is added to the source code of the code difference comparison tool; S3, based on Python implementation to obtain incremental code test coverage report; Use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate an incremental code test coverage report based on the obtained json file; Among them, the test coverage tool is jacococli.jar.
2. The incremental code test coverage statistics method according to claim 1, characterized in that: The incremental code acquisition script is a .sh script.
3. The incremental code test coverage statistics method according to claim 1, characterized in that: The generated incremental code test coverage report is an html test report, and the domain name for accessing the html test report is output through a Jenkins scheduled task.
4. An incremental code test coverage statistics device, characterized in that: The device comprises: The file configuration module is used to obtain a pre-configured build.gradle file. The configuration of the build.gradle file includes: Configure jacoco plugin settings, configure test tasks, and configure violation rules; Configuring the jacoco plugin settings includes: adding the jacoco plugin to the build.gradle file and configuring the jacoco version, report output directory, and associated test tasks; The incremental code acquisition module is based on Python to obtain incremental code information. It is used to use the os library to directly call the pre-configured incremental code acquisition script to obtain incremental code information; The pre-configured incremental code acquisition script to obtain incremental code information includes: Step 1: Use the get request of the requests library to call the incremental code acquisition interface of the code difference comparison tool; Step 2: The code difference comparison tool obtains the new version code and the old version code from git, obtains the incremental code information and specifies the corresponding port; Step 3: Use the json library to store the incremental code information as a json file; And an interface for obtaining incremental code is added to the source code of the code difference comparison tool; The incremental code test coverage report output module is based on Python to obtain the incremental code test coverage report. It is used to use the os library to obtain the classfiles and sourcefiles files of each functional module in the incremental code; and call the test coverage tool to generate the incremental code test coverage report according to the obtained json file; Among them, the test coverage tool is jacococli.jar.
5. The incremental code test coverage statistics device according to claim 4, characterized in that: The incremental code acquisition script is a .sh script.
6. The incremental code test coverage statistics device according to claim 4, characterized in that: The generated incremental code test coverage report is an HTML test report, and the device also includes: a scheduled task module, which is used to output a domain name for accessing the HTML test report through a Jenkins scheduled task.
7. An electronic device, characterized in that: include: one or more processors; Memory; as well as One or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by the one or more processors, the programs including a program for executing the incremental code test coverage statistics method as described in any one of claims 1-3.
Citation Information
Patent Citations
Method and device for automatic testing of code coverage rate, applied to Android system
CN106681912A
Interface test method, device and equipment in code iterative update
CN113626325A
Code test coverage rate statistics and analysis method and system based on Jacocos
CN114936147A