Code coverage rate generation method and device
By obtaining the user ID and generating a dynamic call tree, and combining with the static call tree to calculate the code coverage, the problem of inability to count the code coverage according to the user dimension in the existing technology is solved, and accurate code coverage reporting and code delivery quality improvement is achieved.
Patent Information
- Application Number
- CN202310631900.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-30
- Publication Date
- 2024-12-03
AI Technical Summary
The existing code coverage statistics tool cannot count code coverage according to the user dimension, resulting in the inability to accurately count the code coverage of each tester, and the inability to reversely check the test cases through the code coverage report, affecting the quality of code delivery.
By obtaining the caller's user ID, marking and parsing the dynamic call relationship of the tested code, generating a dynamic call tree, combining the static call tree to calculate the code coverage corresponding to the user ID, and providing an accurate code coverage report.
It realizes accurate calculation of the code coverage of each tester, and provides each tester with accurate code coverage reports, which facilitates supplementary test cases through report counter-checking and improves the quality of code delivery.
Smart Images

Figure CN119065949A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for generating code coverage. Background Art
[0002] Code testing is an effective means to detect the correctness of service code, and is usually performed by writing test cases. At present, when performing code coverage statistics, most of them are performed through open source code coverage statistics tools (such as jacoco, etc.). However, existing code coverage statistics tools cannot count code coverage according to user dimensions. For example, after a version of service code is released, multiple testers may execute test cases of different business functions on the newly released service at the same time, which may cause cross-calling of the underlying code. The final code coverage report is the code call coverage of all testers, and it is impossible to count the code coverage of each tester executing the test case. If you want to accurately count the code coverage of each tester, you need to deploy a test environment for each tester, and the test execution process needs to be physically isolated, which wastes resources.
[0003] In the prior art, it is impossible to count the code coverage of the test cases executed by each tester, and it is impossible to check the supplementary test cases through the code coverage report to improve the quality of code delivery by supplementing the test cases. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a method and device for generating code coverage, which can accurately calculate the final code coverage of each user and provide an accurate code coverage report for each tester, thereby facilitating the reverse query of supplementary test cases through the code coverage report and improving the quality of code delivery through supplementary test cases.
[0005] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for generating code coverage is provided, comprising:
[0006] In response to a call instruction to the tested code, obtaining a user ID of the caller;
[0007] Marking the code called in the tested code according to the user identifier;
[0008] Parsing the marked code to obtain a dynamic call relationship of the tested code, and generating a dynamic call tree of the tested code according to the dynamic call relationship;
[0009] The code coverage rate corresponding to the user identifier is generated according to the pre-generated static call tree and the dynamic call tree of the tested code.
[0010] Optionally, the code called in the tested code is marked according to the user identifier, including: assigning a value to a variable object of the calling thread of the tested code according to the user identifier, marking the code called by the calling thread during its life cycle through the variable object, so as to mark the code called in the tested code.
[0011] Optionally, the code called in the tested code is marked according to the user identifier, including: obtaining a corresponding coloring rule according to the user identifier; assigning a value to a variable object of a calling thread of the tested code according to the coloring rule, and coloring the code called by the calling thread during its life cycle according to the value of the variable object, so as to mark the code called in the tested code.
[0012] Optionally, in response to a call instruction to the tested code, obtaining a user identifier of the caller includes: in response to the call instruction to the tested code, obtaining the user identifier of the caller by using bytecode enhancement technology.
[0013] Optionally, in the case where the caller makes multiple calls to the code under test, the code coverage corresponding to the user identifier is generated based on a pre-generated static call tree and a dynamic call tree of the code under test, including: merging multiple dynamic call trees generated by the caller's multiple calls to the code under test to generate a dynamic call tree of the code under test corresponding to the user identifier; generating the code coverage corresponding to the user identifier based on a pre-generated static call tree of the code under test and a dynamic call tree of the code under test corresponding to the user identifier.
[0014] Optionally, based on the pre-generated static call tree and the dynamic call tree of the tested code, the code coverage corresponding to the user identifier is generated, including: respectively obtaining the number of methods and the number of branches covered by the static call tree and the dynamic call tree of the tested code; calculating the method coverage corresponding to the user identifier based on the number of methods covered by the dynamic call tree and the number of methods covered by the static call tree; calculating the branch coverage corresponding to the user identifier based on the number of branches covered by the dynamic call tree and the number of branches covered by the static call tree; and generating the code coverage corresponding to the user identifier based on the method coverage and the branch coverage.
[0015] Optionally, the code under test has a version number, and the static call tree of the code under test is generated in the following manner: obtaining the code of the previous version of the code under test according to the version number of the code under test; comparing the code under test with the code of the previous version of the code under test to obtain incremental code; parsing the incremental code to obtain the calling relationship between methods; and generating the static call tree of the code under test according to the calling relationship between the methods.
[0016] According to another aspect of an embodiment of the present invention, a device for generating code coverage is provided, comprising:
[0017] An identification acquisition module, used for acquiring a user identification of a caller in response to a call instruction to the tested code;
[0018] A code marking module, used for marking the code called in the tested code according to the user identifier;
[0019] A code parsing module, used for parsing the marked code to obtain the dynamic call relationship of the tested code, and generating a dynamic call tree of the tested code according to the dynamic call relationship;
[0020] The coverage calculation module is used to generate the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code.
[0021] According to another aspect of an embodiment of the present invention, there is provided an electronic device, comprising: one or more processors; a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the method for generating code coverage provided by an embodiment of the present invention.
[0022] According to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored. When the program is executed by a processor, the method for generating code coverage provided by an embodiment of the present invention is implemented.
[0023] An embodiment of the above invention has the following advantages or beneficial effects: by responding to a calling instruction to the code under test, the user identification of the caller is obtained; the code called in the code under test is marked according to the user identification; the marked code is parsed to obtain the dynamic calling relationship of the code under test, and a dynamic call tree of the code under test is generated according to the dynamic calling relationship; a technical solution for generating code coverage corresponding to the user identification is generated according to the pre-generated static call tree and dynamic call tree of the code under test, which can form a dynamic call tree by dynamically parsing the called code and mark the link of the called code with the user dimension, so as to accurately calculate the final code coverage of each user and provide each tester with an accurate code coverage report, so as to facilitate the reverse query of supplementary test cases through the code coverage report and improve the quality of code delivery through supplementary test cases.
[0024] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0026] Figure 1 is a schematic diagram of main steps of a method for generating code coverage according to an embodiment of the present invention;
[0027] Figure 2 is a schematic diagram of a static call tree according to an embodiment of the present invention;
[0028] Figure 3 is a schematic diagram of a first dynamic call tree according to an embodiment of the present invention;
[0029] Figure 4 is a schematic diagram of a second dynamic call tree according to an embodiment of the present invention;
[0030] Figure 5 is a schematic diagram of a merged dynamic call tree according to an embodiment of the present invention;
[0031] Figure 6 is a schematic diagram of main modules of a device for generating code coverage according to an embodiment of the present invention;
[0032] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0033] Figure 8 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0034] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0035] It should be noted that the collection, collection, updating, analysis, processing, use, transmission, storage and other aspects of user personal information involved in the technical solution disclosed in the present invention are in compliance with the provisions of relevant laws and regulations, are used for legitimate purposes, and do not violate public order and good morals. Necessary measures are taken for user personal information to prevent illegal access to user personal information data and maintain the security of user personal information, network security and national security.
[0036] In the prior art, when counting code coverage, it is impossible to count the code coverage of each tester executing the test case, and it is impossible to check the supplementary test cases through the code coverage report, and improve the quality of code delivery by supplementing the test cases. For example, suppose that after a version of the service code is released, for the previous version of the service code, method A, method B and method C are modified, and tester AA is required to verify the correctness of method A calling method C, and tester BB is required to verify the correctness of method B calling method C, where both method A and method B call method C. The test case of tester AA may not call method C when it is executed, but the test case of tester BB calls method C when it is executed. According to the existing code coverage statistics method, when the code coverage report is finally generated, the code coverage of method ABC is 100%. However, in this case, the test purpose of tester AA is not achieved, and it is also impossible to know who called method A, method B or method C, and it is impossible to assist testers to check the test cases and improve the quality of code delivery by supplementing the test cases.
[0037] In order to solve the above problems, the present invention provides a method for generating code coverage, which can statically parse the incremental code to form a static call tree of the incremental code; dynamically parse the called code to form a dynamic call tree, and mark the links of the called code with user dimensions. It can provide each tester with an accurate code coverage report, thereby facilitating the reverse query of supplementary test cases through the code coverage report and improving the quality of code delivery by supplementing test cases.
[0038] Figure 1 FIG. 1 is a schematic diagram of the main steps of the method for generating code coverage according to an embodiment of the present invention. Figure 1As shown, the method for generating code coverage in the embodiment of the present invention mainly includes the following steps S101 to S104.
[0039] Step S101: In response to a call instruction to the tested code, the user ID of the caller is obtained. When the service code is updated, it needs to be tested. At this time, a test system will be deployed. After the user (i.e., the tester, the caller of the tested code) logs in to the test system, the tested code is called by executing the test case to test the tested code. In an embodiment of the present invention, a code coverage statistics platform is provided to perform the code coverage generation method of the embodiment of the present invention. When the user logs in to the test system, the code coverage statistics platform can obtain the user's login information through the user management component, for example, obtain the user ID.
[0040] According to one embodiment of the present invention, in response to a call instruction to the tested code, the user ID of the caller is obtained, which includes: in response to the call instruction to the tested code, the user ID of the caller is obtained through bytecode enhancement technology. Each time the tester calls the tested code, the code coverage statistics platform uses the aspect component to parse the user ID (for example, user account) corresponding to the call based on the bytecode enhancement technology.
[0041] Step S102: marking the code called in the tested code according to the user identifier.
[0042] According to an embodiment of the present invention, marking the code called in the tested code according to the user identifier may specifically include: assigning a value to a variable object of a calling thread of the tested code according to the user identifier, marking the code called by the calling thread during its life cycle by the variable object, so as to mark the code called in the tested code. For example, a user number may be generated according to the user identifier, and the user number may be used to mark the code called in the tested code.
[0043] According to another embodiment of the present invention, marking the code called in the tested code according to the user identifier may specifically include: obtaining a corresponding coloring rule according to the user identifier; assigning a value to the variable object of the calling thread of the tested code according to the coloring rule, and coloring the code called by the calling thread during the life cycle according to the value of the variable object, so as to mark the code called in the tested code. Wherein, the coloring rule is, for example, determining the color used for coloring after performing a hash operation on the user identifier, for example, user A uses red to color the code called in the tested code. Wherein, thread coloring is to set a unique object for the thread, and all operations performed during the life cycle of the thread can be queried according to the unique object, and all operations of the thread are strung into a call link through the set unique object, that is, thread coloring. Specifically, in one embodiment, all operations of the thread during the life cycle (for example: method call relationship) can be marked according to the user identifier. By setting the user identifier to the ThreadLocal object of the calling thread this time, the expected value is transparently transmitted during the life cycle of the thread with the help of the object, so as to realize the coloring of the thread life cycle in the account dimension.
[0044] Step S103: Parse the marked code to obtain the dynamic call relationship of the tested code, and generate the dynamic call tree of the tested code according to the dynamic call relationship. According to step S102, the marked code is obtained, that is, the method and branch data executed by this call are obtained. After that, the code coverage statistics platform will parse the marked code through the code analysis component to obtain the dynamic call relationship and form a dynamic call tree. Each leaf node is the called method, and the connection between the leaves is the call link, so that the dynamic call tree corresponding to the caller's test can be generated.
[0045] According to an embodiment of the present invention, when a caller makes multiple calls to the code under test, the dynamic call tree corresponding to each call of the caller is obtained according to the user identifier, and then these dynamic call trees are merged to obtain a merged dynamic call tree of the code under test corresponding to the caller.
[0046] Step S104: Generate the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code. According to an embodiment of the present invention, when the service code is updated and deployed to the test system, the code coverage statistics platform will directly generate the static call tree of the tested code through the code analysis component, so as to facilitate the subsequent code coverage calculation and code coverage report generation.
[0047] According to one embodiment of the present invention, the tested code has a version number, and the static call tree of the tested code is generated in the following manner: obtaining the code of the previous version of the tested code according to the version number of the tested code; comparing the tested code with the code of the previous version of the tested code to obtain the incremental code; parsing the incremental code to obtain the calling relationship between methods; and generating the static call tree of the tested code according to the calling relationship between the methods. Normally, in order to mark the update iteration process of the service code, the tested code is assigned a version number to facilitate comparative analysis of different versions. When generating a static call tree, different versions of code are usually compared to obtain the incremental code, and the incremental code is parsed to obtain the static calling relationship between methods, and then a static call tree of the incremental code is generated, where each leaf node is a method and the connection between leaves is a call link.
[0048] Those skilled in the art should understand that the static call tree may be pre-generated before executing the test, or may be generated during the test process, and the present invention is not limited to this.
[0049] According to an embodiment of the present invention, when the caller makes multiple calls to the tested code, the code coverage corresponding to the user identifier is generated based on the pre-generated static call tree and the dynamic call tree of the tested code, including: merging multiple dynamic call trees generated by the caller calling the tested code multiple times to generate a dynamic call tree of the tested code corresponding to the user identifier; generating the code coverage corresponding to the user identifier based on the pre-generated static call tree of the tested code and the dynamic call tree of the tested code corresponding to the user identifier. When merging the dynamic call trees, specifically, the methods and call branches of each dynamic call tree are merged separately.
[0050] According to another embodiment of the present invention, based on the pre-generated static call tree and the dynamic call tree of the tested code, the code coverage corresponding to the user identifier is generated, including: respectively obtaining the number of methods and the number of branches covered by the static call tree and the dynamic call tree of the tested code; calculating the method coverage corresponding to the user identifier according to the number of methods covered by the dynamic call tree and the number of methods covered by the static call tree; calculating the branch coverage corresponding to the user identifier according to the number of branches covered by the dynamic call tree and the number of branches covered by the static call tree; generating the code coverage corresponding to the user identifier according to the method coverage and the branch coverage. The code coverage statistics platform will generate the code coverage corresponding to the caller according to the static call tree and the dynamic call tree through the report analysis component. Specifically, the method coverage corresponding to the user identifier can be calculated by taking the number of methods covered by the dynamic call tree as the numerator and the number of methods covered by the static call tree as the denominator; the branch coverage corresponding to the user identifier can be calculated by taking the number of branches covered by the dynamic call tree as the numerator and the number of branches covered by the static call tree as the denominator; then, the method coverage and branch coverage are used as the code coverage corresponding to the user identifier to generate a coverage report.
[0051] Figure 2 FIG. 1 is a schematic diagram of a static call tree according to an embodiment of the present invention. Figure 2 As shown, in an embodiment of the present invention, incremental code is obtained by code comparison, and the incremental code is parsed to obtain a static call tree of the code. As shown in the figure, the calling relationship between methods is as follows: method A will call method B, method C, method D, method E, method F, method H and method J; method B will call method D; method C will call method F. Among them, each method is a leaf node, and these methods and the calling relationship between methods constitute the static call tree of the code.
[0052] Figure 3It is a schematic diagram of the first dynamic call tree of an embodiment of the present invention, which shows the dynamic call tree generated after two testers call the tested code at the same time for the first time. Among them, the calling methods of different testers are marked with different filling colors, and the link branches called are marked according to the user identification of the tester. As shown in the figure, tester AA calls method A, method B and method D, tester BB calls method A, method C and method F, and the common part called by the two testers is method A. The methods and branches that are not called in the figure are not filled and marked; the common part method A called by the two testers is filled with the darkest gray; in addition to the common part method A, the method B and method D called by tester AA are filled and marked with the lightest gray; the method C and method F called by tester BB are filled and marked with a darker gray. In the specific implementation, in addition to marking with user identification on each link branch, different colors can also be used to identify each link branch.
[0053] Figure 4 It is a schematic diagram of the second dynamic call tree of an embodiment of the present invention; it shows the dynamic call tree generated after two testers call the tested code for the second time at the same time. Among them, the calling methods of different testers are marked with different filling colors, and the called link branches are marked according to the user identification of the tester. As shown in the figure, tester AA calls method A and method H, tester BB calls method A and method J, and the common part called by the two testers is method A. The methods and branches that are not called in the figure are not filled and marked; the common part method A called by the two testers is filled with the darkest gray; in addition to the common part method A, the method H called by tester AA is filled and marked with the lightest gray; the method J called by tester BB is filled and marked with a darker gray. In the specific implementation, in addition to marking with user identification on each link branch, different colors can also be used to identify each link branch.
[0054] Figure 5 is a schematic diagram of a merged dynamic call tree according to an embodiment of the present invention, wherein Figure 3 The first dynamic call tree and Figure 4The second dynamic call tree in the embodiment shown is a dynamic call tree obtained after merging according to the user identification. Among them, the calling methods of different testers are marked with different filling colors, and the link branches called are marked according to the user identification of the tester. As shown in the figure, tester AA calls method A, method B, method D and method H, tester BB calls method A, method C, method F and method J, and the common part called by the two testers is method A. The methods and branches that are not called in the figure are not filled and marked; the common part method A called by the two testers is filled with the darkest gray; in addition to the common part method A, the method B, method D and method H called by tester AA are filled and marked with the lightest gray; the method C, method F and method J called by tester BB are filled and marked with a darker gray. In the specific implementation, in addition to marking with the user identification on each link branch, different colors can also be used to identify each link branch.
[0055] By merging the dynamic call tree generated each time the tester calls the tested code in user dimensions, the final code coverage of each user can be accurately calculated, and an accurate code coverage report can be provided to each tester, making it easier to check supplementary test cases through the code coverage report and improve the quality of code delivery through supplementary test cases.
[0056] Figure 6 Schematic diagram of the main modules of the device for generating code coverage according to an embodiment of the present invention. Figure 6 As shown, the code coverage generation device 600 of the embodiment of the present invention mainly includes:
[0057] The identification acquisition module 601 is used to obtain the user identification of the caller in response to the calling instruction of the tested code;
[0058] A code marking module 602, used for marking the code called in the tested code according to the user identifier;
[0059] A code parsing module 603 is used to parse the marked code to obtain the dynamic call relationship of the tested code, and generate a dynamic call tree of the tested code according to the dynamic call relationship;
[0060] The coverage calculation module 604 is used to generate the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code.
[0061] According to one embodiment of the present invention, the code marking module 602 can also be used to: assign a value to the variable object of the calling thread of the tested code according to the user identifier, and mark the code called by the calling thread during its life cycle through the variable object to mark the code called in the tested code.
[0062] According to another embodiment of the present invention, the code marking module 602 can also be used to: obtain the corresponding coloring rule according to the user identifier; assign a value to the variable object of the calling thread of the tested code according to the coloring rule, and color the code called by the calling thread during its life cycle through the value of the variable object, so as to mark the code called in the tested code.
[0063] According to another embodiment of the present invention, the identification acquisition module 601 may also be used to: in response to a call instruction to the tested code, acquire the user identification of the caller through bytecode enhancement technology.
[0064] According to another embodiment of the present invention, when the caller makes multiple calls to the tested code, the coverage calculation module 604 can also be used to: merge multiple dynamic call trees generated by the caller's multiple calls to the tested code to generate a dynamic call tree of the tested code corresponding to the user identifier; and generate code coverage corresponding to the user identifier based on a pre-generated static call tree of the tested code and a dynamic call tree of the tested code corresponding to the user identifier.
[0065] According to another embodiment of the present invention, the coverage calculation module 604 can also be used to: respectively obtain the number of methods and the number of branches covered by the static call tree and the dynamic call tree of the tested code; calculate the method coverage corresponding to the user identifier according to the number of methods covered by the dynamic call tree and the number of methods covered by the static call tree; calculate the branch coverage corresponding to the user identifier according to the number of branches covered by the dynamic call tree and the number of branches covered by the static call tree; generate the code coverage corresponding to the user identifier according to the method coverage and the branch coverage.
[0066] According to another embodiment of the present invention, the tested code has a version number, and the static call tree of the tested code is generated in the following manner: obtaining the code of the previous version of the tested code according to the version number of the tested code; comparing the tested code with the code of the previous version of the tested code to obtain incremental code; parsing the incremental code to obtain the calling relationship between methods; and generating the static call tree of the tested code according to the calling relationship between the methods.
[0067] According to the technical solution of the embodiment of the present invention, by responding to the calling instruction of the tested code, the user identification of the caller is obtained; the code called in the tested code is marked according to the user identification; the marked code is parsed to obtain the dynamic calling relationship of the tested code, and the dynamic calling tree of the tested code is generated according to the dynamic calling relationship; according to the pre-generated static calling tree and dynamic calling tree of the tested code, a technical solution of code coverage corresponding to the user identification is generated, which can form a dynamic calling tree by dynamically parsing the called code, and mark the link of the called code with the user dimension, so as to accurately calculate the final code coverage of each user and provide each tester with an accurate code coverage report, so as to facilitate the reverse query of supplementary test cases through the code coverage report, and improve the quality of code delivery through supplementary test cases.
[0068] Figure 7 An exemplary system architecture 700 is shown to which the method for generating code coverage or the apparatus for generating code coverage according to the embodiments of the present invention can be applied.
[0069] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0070] Users can use terminal devices 701, 702, 703 to interact with server 705 through network 704 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 701, 702, 703, such as code testing applications, data marking applications, data analysis software, etc. (only examples).
[0071] The terminal devices 701 , 702 , and 703 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0072] Server 705 may be a server that provides various services, such as a background management server that provides support for websites browsed by users using terminal devices 701, 702, and 703 (for example only). The background management server may obtain the user ID of the caller from the received call instructions of the tested code and other data; mark the code called in the tested code according to the user ID; parse the marked code to obtain the dynamic call relationship of the tested code, and generate a dynamic call tree of the tested code according to the dynamic call relationship; generate the code coverage corresponding to the user ID according to the pre-generated static call tree and dynamic call tree of the tested code, and feed back the processing results (such as the code coverage corresponding to the user ID - for example only) to the terminal device.
[0073] It should be noted that the method for generating code coverage provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the device for generating code coverage is generally arranged in the server 705 .
[0074] It should be understood that Figure 7 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0075] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device or server suitable for implementing an embodiment of the present invention. Figure 8 The terminal device or server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0076] like Figure 8 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0077] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0078] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.
[0079] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0080] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0081] The units or modules involved in the embodiments of the present invention may be implemented in software or hardware. The units or modules described may also be provided in a processor. For example, they may be described as: a processor including an identification acquisition module, a code marking module, a code parsing module, and a coverage calculation module. Among them, the names of these units or modules do not constitute limitations on the units or modules themselves in certain circumstances. For example, the coverage calculation module may also be described as "a module for generating the code coverage corresponding to the user identifier based on the pre-generated static call tree and the dynamic call tree of the tested code".
[0082] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: in response to a call instruction to the tested code, obtaining the user identification of the caller; marking the code called in the tested code according to the user identification; parsing the marked code to obtain the dynamic call relationship of the tested code, and generating the dynamic call tree of the tested code according to the dynamic call relationship; generating the code coverage corresponding to the user identification according to the pre-generated static call tree and the dynamic call tree of the tested code.
[0083] According to the technical solution of the embodiment of the present invention, by responding to the calling instruction of the tested code, the user identification of the caller is obtained; the code called in the tested code is marked according to the user identification; the marked code is parsed to obtain the dynamic calling relationship of the tested code, and the dynamic calling tree of the tested code is generated according to the dynamic calling relationship; according to the pre-generated static calling tree and dynamic calling tree of the tested code, a technical solution of code coverage corresponding to the user identification is generated, which can form a dynamic calling tree by dynamically parsing the called code, and mark the link of the called code with the user dimension, so as to accurately calculate the final code coverage of each user and provide each tester with an accurate code coverage report, so as to facilitate the reverse query of supplementary test cases through the code coverage report, and improve the quality of code delivery through supplementary test cases.
[0084] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for generating code coverage, It is characterized in that include: In response to a call instruction to the tested code, obtaining a user ID of the caller; Marking the code called in the tested code according to the user identifier; Parsing the marked code to obtain a dynamic call relationship of the tested code, and generating a dynamic call tree of the tested code according to the dynamic call relationship; The code coverage rate corresponding to the user identifier is generated according to the pre-generated static call tree and the dynamic call tree of the tested code.
2. The method according to claim 1, It is characterized in that Marking the code called in the tested code according to the user identifier includes: The variable object of the calling thread of the tested code is assigned a value according to the user identifier, and the code called by the calling thread during its life cycle is marked by the variable object to mark the code called in the tested code.
3. The method according to claim 1 or 2, It is characterized in that Marking the code called in the tested code according to the user identifier includes: Acquire a corresponding coloring rule according to the user identifier; The variable object of the calling thread of the tested code is assigned a value according to the coloring rule, and the code called by the calling thread during its life cycle is colored by the value of the variable object, so as to mark the code called in the tested code.
4. The method according to claim 1, It is characterized in that In response to a call instruction to the tested code, obtaining a user ID of the caller includes: In response to a call instruction to the code under test, a user identifier of the caller is obtained through bytecode enhancement technology.
5. The method according to claim 1, It is characterized in that In the case where the caller has called the tested code multiple times, generating the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code, including: Merging multiple dynamic call trees generated by the caller calling the tested code multiple times to generate a dynamic call tree of the tested code corresponding to the user identifier; The code coverage rate corresponding to the user identifier is generated according to the pre-generated static call tree of the tested code and the dynamic call tree of the tested code corresponding to the user identifier.
6. The method according to claim 1 or 5, It is characterized in that Generating the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code, including: Respectively obtaining the number of methods and the number of branches covered by the static call tree and the dynamic call tree of the tested code; Calculating the method coverage rate corresponding to the user identifier according to the number of methods covered by the dynamic call tree and the number of methods covered by the static call tree; Calculating the branch coverage rate corresponding to the user identifier according to the number of branches covered by the dynamic call tree and the number of branches covered by the static call tree; The code coverage corresponding to the user identifier is generated according to the method coverage and the branch coverage.
7. The method according to claim 1, It is characterized in that The tested code has a version number, and the static call tree of the tested code is generated in the following manner: Obtaining the previous version of the tested code according to the version number of the tested code; Compare the tested code with a previous version of the tested code to obtain an incremental code; Parsing the incremental code to obtain the calling relationship between methods; A static call tree of the tested code is generated according to the calling relationship between the methods.
8. A device for generating code coverage, It is characterized in that include: An identification acquisition module, used for acquiring a user identification of a caller in response to a call instruction to the tested code; A code marking module, used for marking the code called in the tested code according to the user identifier; A code parsing module, used for parsing the marked code to obtain the dynamic call relationship of the tested code, and generating a dynamic call tree of the tested code according to the dynamic call relationship; The coverage calculation module is used to generate the code coverage corresponding to the user identifier according to the pre-generated static call tree and the dynamic call tree of the tested code.
9. An electronic device, It is characterized in that include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 7.
10. A computer readable medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
