Java-oriented multi-user and multi-branch coverage rate collection method and device
By modifying the jacoco plug-in and building a coverage rate collection platform, the problem that everyone cannot see their coverage rate information under the multi-branch code deployment is solved, and the coverage data independence and quality judgment is achieved when multiple people test at the same time.
Patent Information
- Application Number
- CN202411176566.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-26
- Publication Date
- 2024-11-12
AI Technical Summary
During the software development process, multi-branch code deployment results in everyone being able to see the full amount of code coverage data, but not seeing the branch coverage information they are concerned about, and being unable to judge the test quality.
Modify the open source plugin jacoco code, change the instrumentation format, convert the boolean array into a Map collection, build a coverage collection platform, use the jacoco plugin to generate the current user's test coverage information, and display it through the coverage collection platform.
Everyone can see the branch coverage information they are concerned about, effectively judge the test quality, and realize the independence of coverage data when multiple people test at the same time.
Smart Images

Figure CN118939558A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a coverage collection method, in particular to a Java multi-user and multi-branch coverage collection method, belonging to the technical field of software development. Background Art
[0002] Software development is a set of activities aimed at developing, designing, testing, and maintaining software. Software is a set of activities or data used to control computer hardware and support user operations. Software development involves the principles and practices of computer science, mathematics, and engineering.
[0003] In the process of software development, testing is an important part of ensuring software quality. However, code deployment is often multi-branch deployment, and multiple people test at the same time. Each person can only see the full code coverage data, but cannot see the coverage information of the branch they are concerned about. They cannot know whether this coverage data is really the result of their own test, and cannot judge the test quality of each person based on the coverage information. Therefore, a Java multi-user, multi-branch coverage collection method and device are proposed. Summary of the invention
[0004] In view of this, the present invention provides a Java multi-user, multi-branch coverage collection method and device to solve or alleviate the technical problems existing in the prior art, and at least provide a beneficial choice.
[0005] The technical solution of the embodiment of the present invention is implemented as follows: a Java multi-user, multi-branch coverage collection method, comprising the following steps:
[0006] S1. Modify the source code of the open source plug-in jacoco and change the instrumentation format of a single class;
[0007] S2. Convert the member attributes of the class in the source code from the original Boolean array to a Map collection;
[0008] S3. Use the tool class to set whether the current line is executed;
[0009] S4. Specify the jacoco plug-in to start;
[0010] S5. Build a coverage collection platform based on springboot to perform application management and task management;
[0011] S6. Use the coverage collection platform to execute the test task and parse the current user name;
[0012] S7. Obtain the IP address of the application under test according to the application information, and send information to the jacoco plug-in under the IP;
[0013] S8. Use the jacoco plug-in to traverse all the instrumented codes according to the information, retrieve the execution information of the current user from the Map collection, and feed it back to the coverage collection platform;
[0014] S9. Use the coverage collection platform to pull the code according to the git address and branch, and call the jacoco plug-in to generate coverage information.
[0015] Further preferably, in S2, the Map set includes a key and a value;
[0016] Among them, the key in the collection is the user name, and the value is the execution status of the code.
[0017] Further preferably, in the S3, the tool class is used to parse the user name when the test task is executed, put it into the ThreadLocal container, and during code execution, obtain the current request user name from the container, and then take out the Boolean array code execution status in jacocoMaps, and set the current line to have been executed.
[0018] Further preferably, in S4, it is set that when the service under test is started, the revised jacoco plug-in is started.
[0019] Further preferably, in S5, the coverage collection platform uses a MySQL database to store data.
[0020] Further preferably, in said S5, the coverage collection platform includes an application management module and a task management module;
[0021] Among them, the application management module is used to store the application name, git address and IP address information of the tested application, which is manually collected by the user;
[0022] Among them, the task management module includes application branch information and application information association, which is manually input by the user.
[0023] Further preferably, in S7, the information sent to the jacoco plug-in carries the user name;
[0024] In S8, the jacoco plug-in traverses all the plugged-in codes according to the user name and retrieves the execution information of the current user from the Map collection.
[0025] Further preferably, in S9, the generated coverage information is displayed to the user by using data visualization technology through the coverage collection platform.
[0026] A Java multi-user, multi-branch coverage collection device includes a memory, a processor, and a computer program stored in the memory and running on the processor. The processor executes the computer program to implement the above method.
[0027] Since the embodiment of the present invention adopts the above technical solution, it has the following advantages: the present invention modifies the source code of the open source plug-in jacoco code, changes the plug-in format of a single class, and specifies the revised jacoco plug-in to start when the service under test starts, and then builds a coverage collection platform based on springboot, and then uses the coverage collection platform to generate the test coverage information of the current user, which is not affected by other users, so that in the test task, everyone can see the coverage information of the branch they are concerned about, and can effectively judge the test quality of each person.
[0028] The above summary is for illustrative purposes only and is not intended to be limiting in any way. In addition to the illustrative aspects, embodiments and features described above, further aspects, embodiments and features of the present invention will be readily apparent by reference to the accompanying drawings and the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the embodiments of the present application 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 application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0030] Figure 1 is a flow chart of the steps of the present invention;
[0031] Figure 2 It is a schematic diagram of the architecture of the present invention;
[0032] Figure 3 A schematic diagram of a process for generating a coverage report by the coverage collection platform of the present invention;
[0033] Figure 4 This is a schematic diagram of the application of the second embodiment of the present invention;
[0034] Figure 5 This is the final effect diagram of the second embodiment of the present invention;
[0035] Figure 6 Schematic diagram of interaction between a device and an application according to Embodiment 3 of the present invention. DETAILED DESCRIPTION
[0036] In the following, only some exemplary embodiments are briefly described. As those skilled in the art will appreciate, the described embodiments may be modified in various ways without departing from the spirit or scope of the present invention. Therefore, the drawings and descriptions are considered to be exemplary and non-restrictive in nature.
[0037] It should be noted that the terms "first", "second", "symmetrical", "array", etc. are only used to distinguish between description and position description purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the definition of "first", "symmetrical", etc. can explicitly or implicitly include one or more of these features; similarly, when the number of certain features is not limited in the form of words such as "two" or "three", it should be noted that this feature also explicitly or implicitly includes one or more feature quantities.
[0038] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0039] Embodiment 1
[0040] like Figure 1-3 As shown, the embodiment of the present invention provides a Java multi-user, multi-branch coverage collection method, including the following steps:
[0041] S1. Modify the source code of the open source plug-in jacoco and change the instrumentation format of a single class;
[0042] S2. Convert the member attributes of the class in the source code from the original Boolean array to a Map collection;
[0043] S3. Use the tool class to set whether the current line is executed;
[0044] S4. Specify the jacoco plug-in to start;
[0045] S5. Build a coverage collection platform based on springboot to perform application management and task management;
[0046] S6. Use the coverage collection platform to execute the test task and parse the current user name;
[0047] S7. Obtain the IP address of the application under test according to the application information, and send information to the jacoco plug-in under the IP;
[0048] S8. Use the jacoco plug-in to traverse all the instrumented codes according to the information, retrieve the execution information of the current user from the Map collection, and feed it back to the coverage collection platform;
[0049] S9. Use the coverage collection platform to pull the code according to the git address and branch, and call the jacoco plug-in to generate coverage information.
[0050] In one embodiment, in S2, the Map set includes a key and a value;
[0051] Among them, the key in the collection is the user name, and the value is the execution status of the code.
[0052] The original code after stub insertion is:
[0053]
[0054]
[0055] When the code is executed, it is executed to jacoco[0] = true, which means that the 0th line of code has been executed. The position of the current line is found in the jacoco array. If it is set to true, it means that the current line has been executed. There is no record of which user has executed it.
[0056] The improved code is:
[0057]
[0058]
[0059] The member properties of the class are changed from the original Boolean array to a Map collection.
[0060] In one embodiment, in S3, the tool class is used to parse the user name when the test task is executed, put it into the ThreadLocal container, and during code execution, obtain the current request user name from the container, and then take out the Boolean array code execution status in jacocoMaps, and set the current line to have been executed.
[0061] Tool example code:
[0062]
[0063] In one embodiment, in S4, it is set that when the service under test is started, a modified jacoco plug-in is started.
[0064] When the jacoco plug-in is started, the jacoco plug-in obtains the code coverage of each class according to the user name and returns it to the coverage collection platform.
[0065] In one embodiment, in S5, the coverage collection platform uses a MySQL database to store data.
[0066] MySQL is a relational database management system that is widely used in various environments and is loved by developers for its high speed, flexibility and ease of use. As a relational database management system, MySQL stores data in different tables. The purpose of this design is to increase the speed of data access and increase flexibility. In addition, the SQL language used by MySQL is the most commonly used standardized language for accessing databases. Whether it is a small website or a large enterprise application, MySQL can be chosen as its website database.
[0067] In one embodiment, in S5, the coverage collection platform includes an application management module and a task management module;
[0068] Among them, the application management module is used to store the application name, git address and IP address information of the tested application, which is manually collected by the user;
[0069] Among them, the task management module includes application branch information and application information association, which is manually input by the user.
[0070] The application name, git address and IP address information of the tested application are managed by using the application management module of the coverage collection platform;
[0071] The branch information of the application and the association of the application information are managed by utilizing the task management module of the coverage collection platform.
[0072] In one embodiment, in S7, the information sent to the jacoco plug-in carries the user name;
[0073] In S8, the jacoco plug-in traverses all the instrumented codes according to the user name and retrieves the execution information of the current user from the Map collection.
[0074] Through the coverage collection platform, the current user name is first parsed, and the IP address of the tested application is found according to the application information. Then, information is sent to the jacoco plug-in under this IP, and the user name is carried in the information. Then, the jacoco plug-in traverses all the plugged-in codes according to the user name, and takes out the execution information of the current user from the Map collection and returns it to the coverage collection platform. The coverage collection platform calls the jacoco plug-in to generate coverage information based on the information returned by the plug-in, the git address, and the branch pull code;
[0075] After clicking the task execution button, a TCP request is sent to the application under test to obtain the application coverage information. The process of generating a coverage report is as follows: Figure 3As shown, user information is first sent to the application under test. The jacoco plug-in obtains the code coverage of each class based on the user name and returns it to the coverage collection platform. Then the code information of the branch of interest is downloaded locally, and the jacoco report plug-in is used to generate report information based on the coverage information and code information and display it to the user.
[0076] In one embodiment, in S9, the coverage collection platform uses data visualization technology to display the generated coverage information to the user.
[0077] By using data visualization technology, the generated coverage information is displayed to users in the form of watermarks so that users can view the information at any time.
[0078] Embodiment 2
[0079] like Figure 2-5 As shown, the embodiment of the present invention also provides a Java multi-user, multi-branch coverage collection method, comprising the following steps:
[0080] 1. Modify the source code of the open source plug-in jacoco code, change the instrumentation format of a single class, and modify the original instrumented code to the improved code
[0081] The original code after stub insertion is:
[0082]
[0083]
[0084] When the code is executed, it is executed to jacoco[0] = true, which means that the 0th line of code has been executed. The position of the current line is found in the jacoco array. If it is set to true, it means that the current line has been executed. There is no record of which user has executed it.
[0085] The improved code is:
[0086]
[0087] 2. Convert the member attributes of the class in the source code from the original Boolean array to a Map collection;
[0088] 3. Use the tool class to set whether the current line is executed. The tool code is as follows:
[0089]
[0090]
[0091] The tool class is used to parse the user name when the test task is executed, put it into the ThreadLocal container, and get the current request user name from the container during code execution, then take out the Boolean array code execution status in jacocoMaps and set the current line to have been executed;
[0092] 4. When the service under test starts, specify the revised jacoco plug-in to start;
[0093] 5. Build a coverage collection platform based on springboot to perform application management and task management;
[0094] Among them, the coverage collection platform consists of two modules, application management module and task management module;
[0095] The application management module mainly stores information such as application name, git address, and IP address of the tested application, which are manually collected by the user;
[0096] The task management module mainly includes the branch information of the application and the association with the application information, which is manually input by the user;
[0097] 6. Use the coverage collection platform to perform the test task. First, parse the current user name, find out the IP address of the application under test based on the application information, send information to the jacoco plug-in under this IP, and carry the user name. The jacoco plug-in traverses all the plugged-in codes based on the user name and takes out the execution information of the current user from the Map collection. Return it to the coverage platform. The coverage platform pulls the code based on the git address and branch based on the information returned by the plug-in, calls the jacoco plug-in to generate coverage information, and displays it to the user.
[0098] Specific as Figure 4 As shown in the figure, two users, A and B, test different required tasks on the same application. During the test, A and B send different http requests to the server to execute their respective test cases.
[0099] In this solution, the username information carried in the http is first parsed and stored in the local cache. When the code is executed, the coverage plug-in is used to record the executed code lines on the one hand, and obtain the username from the local cache on the other hand, and record it in the coverage result file at the same time.
[0100] When generating a coverage report, you can specify a user name to filter the user's coverage information from the coverage result file. This allows you to pull coverage reports by person when multiple people are testing at the same time, and more accurately obtain the code coverage of the required changes.
[0101] The final effect is as follows Figure 5As shown, the code is the program of our online car-hailing order grabbing platform. Assume that the user name is Shi Ke; the id is 184; the time is 20:18:38 on February 21, 2024;
[0102] Embodiment 3
[0103] like Figure 6 As shown, an embodiment of the present invention also provides a Java multi-user, multi-branch coverage collection device, including a memory, a processor, and a computer program stored in the memory and running on the processor. The processor executes the computer program to implement the method of embodiment one or embodiment two.
[0104] The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of various changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A Java multi-user, multi-branch coverage collection method, characterized in that: The following steps are involved: S1. Modify the source code of the open source plug-in jacoco and change the instrumentation format of a single class; S2. Convert the member attributes of the class in the source code from the original Boolean array to a Map collection; S3. Use the tool class to set whether the current line is executed; S4. Specify the jacoco plug-in to start; S5. Build a coverage collection platform based on springboot to perform application management and task management; S6. Use the coverage collection platform to execute the test task and parse the current user name; S7. Obtain the IP address of the application under test according to the application information, and send information to the jacoco plug-in under the IP; S8. Use the jacoco plug-in to traverse all the instrumented codes according to the information, retrieve the execution information of the current user from the Map collection, and feed it back to the coverage collection platform; S9. Use the coverage collection platform to pull the code according to the git address and branch, and call the jacoco plug-in to generate coverage information.
2. The Java multi-user, multi-branch coverage collection method according to claim 1 is characterized in that: In S2, the Map set includes key and value; The key in the collection is the user name, and the value is the execution status of the code.
3. The Java multi-user, multi-branch coverage collection method according to claim 1 is characterized in that: In the S3, the tool class is used to parse the user name and put it into the ThreadLocal container when the test task is executed. During the code execution, the tool class obtains the current request user name from the container, and then retrieves the Boolean array code execution status from jacocoMaps to set the current line to have been executed.
4. The Java multi-user, multi-branch coverage collection method according to claim 1 is characterized in that: In S4, it is set that when the service under test is started, the revised jacoco plug-in is started.
5. The Java multi-user, multi-branch coverage collection method according to claim 1 is characterized in that: In the S5, the coverage collection platform uses a MySql database to store data.
6. The Java multi-user, multi-branch coverage collection method according to claim 5, characterized in that: In S5, the coverage collection platform includes an application management module and a task management module; Among them, the application management module is used to store the application name, git address and IP address information of the tested application, which is manually collected by the user; Among them, the task management module includes application branch information and application information association, which is manually input by the user.
7. The Java multi-user, multi-branch coverage collection method according to claim 1 is characterized in that: In S7, the information sent to the jacoco plug-in carries the user name; In S8, the jacoco plug-in traverses all the plugged-in codes according to the user name and retrieves the execution information of the current user from the Map collection.
8. The Java multi-user, multi-branch coverage collection method according to claim 1, characterized in that: In S9, the coverage collection platform uses data visualization technology to display the generated coverage information to the user.
9. A Java multi-user, multi-branch coverage collection device, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: The processor executes the computer program to implement the method according to any one of claims 1 to 8.
