CN113760694A - Method and device for calculating code coverage rate - Google Patents

Method and device for calculating code coverage rate Download PDF

Info

Publication number
CN113760694A
CN113760694A CN202010603620.XA CN202010603620A CN113760694A CN 113760694 A CN113760694 A CN 113760694A CN 202010603620 A CN202010603620 A CN 202010603620A CN 113760694 A CN113760694 A CN 113760694A
Authority
CN
China
Prior art keywords
branch
target program
information
logic
code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010603620.XA
Other languages
Chinese (zh)
Inventor
甘明涛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Wodong Tianjun Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN202010603620.XA priority Critical patent/CN113760694A/en
Publication of CN113760694A publication Critical patent/CN113760694A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis

Abstract

The invention discloses a method and a device for calculating code coverage rate, and relates to the technical field of computers. One embodiment of the method comprises: positioning each logic branch of a target program by analyzing a syntax tree of the target program; respectively inserting the reported information code and the reported information into the code of each logic branch to generate an executable program of the target program; and uploading the reported information of each logic branch to a server. The implementation method can solve the technical problems that the using scene is single and the code coverage condition cannot be counted in real time.

Description

Method and device for calculating code coverage rate
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for calculating code coverage.
Background
Code coverage is a measure in software testing that describes the proportion and extent to which source code is tested in a program, and the resulting proportion is referred to as code coverage. At present, the common code coverage tool is mainly Jacoco, which is an open-source code coverage tool, and the function of counting the coverage is mainly realized by a code injection mode.
In the process of implementing the invention, the inventor finds that at least the following problems exist in the prior art:
the code coverage rate tool can only count the code coverage rate of the unit test case, the use scene is single, the code coverage rate cannot be counted in real time during the program operation period, a complete counting platform does not exist, and the use is complicated.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for calculating a code coverage rate, so as to solve the technical problems that a usage scenario is single and a code coverage condition cannot be counted in real time.
To achieve the above object, according to an aspect of the embodiments of the present invention, there is provided a method for calculating code coverage, applied to a client, including:
positioning each logic branch of a target program by analyzing a syntax tree of the target program;
respectively inserting the reported information code and the reported information into the code of each logic branch to generate an executable program of the target program;
and uploading the reported information of each logic branch to a server.
Optionally, the report information includes a name of the target program, a packet name of the logical branch, a class name of the logical branch, a method name of the logical branch, and a branch name of the logical branch.
Optionally, the method further comprises:
sending a coverage rate query request to the server, wherein the coverage rate query request carries the name of the target program;
and receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server.
In addition, according to another aspect of the embodiments of the present invention, there is provided a method for calculating code coverage, which is applied to a test terminal, and includes:
running an executable program of the target program, and reporting the report information to the server when the report information code is executed;
the reporting information code and the corresponding reporting information are respectively inserted into the codes of each logic branch of the target program.
Optionally, the executable program for running the object program includes:
running an executable program of the target program in the execution scene; the execution scene is a unit test execution scene, a function test execution scene, an automation scene execution scene or a manual execution scene of a tester.
Optionally, running an executable program of the target program, and reporting the report information to the server when executing the report information code, includes:
and running the executable program of the target program, and reporting the reporting information inserted in the logic branch to the server side in a network transmission mode when the reporting information code inserted in the logic branch is executed.
In addition, according to another aspect of the embodiments of the present invention, there is provided a method for calculating code coverage, which is applied to a server, and includes:
receiving the reported information of each logic branch of a target program uploaded by a client, and storing the reported information of each logic branch to a database;
receiving reporting information of the logic branch reported by the test terminal in the process of running the executable program of the target program on the test terminal;
and determining the code coverage state of each logic branch according to the reporting information of the logic branch reported by the test terminal and the reporting information of each logic branch stored in the database.
Optionally, for any one of the logical branches, the code coverage status of the logical branch is determined by the following method:
inquiring the code coverage state of the logic branch in the database according to the currently received report information of the logic branch;
if the code coverage state of the logic branch is uncovered, modifying the coverage state into coverage;
and if the code coverage state of the logic branch is coverage, ending the operation.
Optionally, the method further comprises:
receiving a coverage rate query request sent by the client, wherein the coverage rate query request carries the name of the target program;
querying the code coverage state of each logic branch of the target program from the database so as to calculate the code coverage rate of the target program;
and returning the code coverage rate of the target program and the code coverage state of each logic branch to the client.
In addition, according to another aspect of the embodiments of the present invention, there is provided an apparatus for calculating code coverage, which is provided at a client, and includes:
the grammar positioning module is used for positioning each logic branch of the target program by analyzing a grammar tree of the target program;
the inserting module is used for respectively inserting the reporting information codes and the reporting information into the codes of the logic branches to generate executable programs of the target programs;
and the uploading module is used for uploading the reported information of each logic branch to a server.
Optionally, the report information includes a name of the target program, a packet name of the logical branch, a class name of the logical branch, a method name of the logical branch, and a branch name of the logical branch.
Optionally, the system further comprises a query module, configured to:
sending a coverage rate query request to the server, wherein the coverage rate query request carries the name of the target program;
and receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server.
In addition, according to another aspect of the embodiments of the present invention, there is provided an apparatus for calculating a code coverage, which is disposed at a server, and includes:
the storage module is used for receiving the reported information of each logic branch of the target program uploaded by the client and storing the reported information of each logic branch into a database;
a receiving module, configured to receive reporting information of a logical branch reported by a test terminal in a process of running an executable program of the target program on the test terminal;
and the updating module is used for determining the code coverage state of each logic branch according to the currently received reporting information of the logic branch and the reporting information of each logic branch stored in the database.
Optionally, for any one of the logical branches, the code coverage status of the logical branch is determined by the following method:
inquiring the code coverage state of the logic branch in the database according to the currently received report information of the logic branch;
if the code coverage state of the logic branch is uncovered, modifying the coverage state into coverage;
and if the code coverage state of the logic branch is coverage, ending the operation.
Optionally, the system further comprises a computing module, configured to:
receiving a coverage rate query request sent by the client, wherein the coverage rate query request carries the name of the target program;
querying the code coverage state of each logic branch of the target program from the database so as to calculate the code coverage rate of the target program;
and returning the code coverage rate of the target program and the code coverage state of each logic branch to the client.
According to another aspect of the embodiments of the present invention, there is also provided an electronic device, including:
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 of any of the embodiments described above.
According to another aspect of the embodiments of the present invention, there is also provided a computer readable medium, on which a computer program is stored, which when executed by a processor implements the method of any of the above embodiments.
One embodiment of the above invention has the following advantages or benefits: because the technical means that the reporting information codes and the reporting information are respectively inserted into the codes of all the logic branches to generate the executable program of the target program and the reporting information of all the logic branches is uploaded to the server is adopted, the technical problems that the using scene is single and the code coverage condition cannot be counted in real time in the prior art are solved. According to the embodiment of the invention, the reporting information code and the reporting information are respectively inserted into the codes of all logic branches of the program, development operation is not needed after the executable program is generated, and the code coverage condition of the test case under each execution scene can be counted in real time during the program running. The vulnerability testing method can also be displayed through a front-end page for developers and testers to check, and the purpose of reducing the occurrence probability of vulnerabilities is achieved.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of the main flow of a method of calculating code coverage according to one embodiment of the invention;
FIG. 2 is a diagram illustrating logical branches before reporting information codes and reporting information are inserted according to an embodiment of the present invention;
FIG. 3 is a diagram illustrating a logical branch after inserting reporting information codes and reporting information according to an embodiment of the present invention;
FIG. 4 is a diagram illustrating a main flow of a method of calculating code coverage according to a referential embodiment of the present invention;
FIG. 5 is a schematic diagram of the main flow of a method of calculating code coverage according to another embodiment of the invention;
FIG. 6 is a diagram showing a main flow of a method of calculating code coverage according to another referential embodiment of the present invention;
FIG. 7 is a schematic diagram of the main modules of an apparatus for calculating code coverage according to one embodiment of the present invention;
FIG. 8 is a schematic diagram of the main modules of an apparatus for calculating code coverage according to another embodiment of the present invention;
FIG. 9 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 10 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server according to an embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a schematic diagram of a main flow of a method of calculating code coverage according to one embodiment of the present invention. As an embodiment of the present invention, as shown in fig. 1, the method for calculating code coverage applied to a client may include:
step 101, positioning each logic branch of the target program by analyzing a syntax tree of the target program.
Before the object program forms an executable program, various logic branches of the object program, such as method logic branches, judgment logic branches, circulation logic branches and other common logic branches, can be found by analyzing a syntax tree (namely an abstract syntax tree) of the object program through AOP (aspect programming) or a mode of modifying codes during compiling.
And 102, respectively inserting the report information code and the report information into the code of each logic branch to generate an executable program of the target program.
After each logic branch in the target program is found, the reporting information code and the reporting information are inserted into the codes of the logic branches. Optionally, the report information includes basic information of the target program, such as a program name, a packet name of a logical branch, a class name of a logical branch, a method name of a logical branch, and a branch name of a logical branch.
The reporting information code and the reporting information can be inserted into the logic branch in a tangent plane programming mode. Fig. 2 is a schematic diagram of a logical branch before a reporting information code and reporting information are inserted according to an embodiment of the present invention, and fig. 3 is a schematic diagram of a logical branch after a reporting information code and reporting information are inserted according to an embodiment of the present invention. As can be seen from fig. 2-3, the reporting information code and the reporting information are inserted into the code for determining the logical branch, and the reporting information includes information such as the packet name, the class name, the method name, and the branch name of the logical branch.
The method comprises the steps of obtaining a packet name of a logic branch, obtaining a category name of the logic branch, obtaining a method name of the logic branch, and obtaining an ifBranch.
Optionally, the reported information may further include information such as a user name and a communication mode, where the user name may be used to distinguish test results of different testers, and the communication mode may be an intranet or an internet and mainly includes information such as a server IP address, an interface name, and a parameter.
And 103, uploading the reported information of each logic branch to a server.
After all the logic branches of the target program are inserted into the reported information codes and the reported information, the reported information and the quantity of all the logic branches are obtained at the same time, and then the information is stored so as to be convenient for counting the code coverage condition in the subsequent steps. The reporting information of all the logical branches can be uploaded to the server side in various ways, for example: after the program generates an executable program, triggering to upload the reported information of all the logic branches to the server; or, when the program runs, the reporting information of all the logic branches is triggered to be uploaded to the server.
The first method is as follows: after the program generates the executable program
Taking the android platform as an example, after a packing platform (e.g., a common packing platform Jenkins) completes packing (apk generation), the client is triggered to upload the report information of all the logic branches to the server.
The second method comprises the following steps: program runtime
The triggering and reporting operation can be added at some time when the program runs, for example, the program with an operation interface can be added with a button for triggering and uploading. And after the user clicks a button for triggering uploading, triggering the test end to upload the reported information of all the logic branches to the server end.
According to the various embodiments described above, it can be seen that the technical means of the embodiments of the present invention respectively inserting the report information code and the report information into the code of each logical branch to generate the executable program of the target program and uploading the report information of each logical branch to the server solves the technical problems that the usage scenario is single and the code coverage condition cannot be counted in real time in the prior art. According to the embodiment of the invention, the reporting information code and the reporting information are respectively inserted into the codes of all logic branches of the program, development operation is not needed after the executable program is generated, and the code coverage condition of the test case under each execution scene can be counted in real time during the program running.
Fig. 4 is a schematic diagram of a main flow of a method of calculating code coverage according to one referential embodiment of the present invention. As another embodiment of the present invention, as shown in fig. 4, the method for calculating code coverage applied to the client may include:
step 401, by analyzing the syntax tree of the target program, each logical branch of the target program is located.
Before the target program forms an executable program, various logic branches of the target program, such as method logic branches, judgment logic branches, loop logic branches and other common logic branches, can be found by analyzing a syntax tree (namely an abstract syntax tree) of the target program through AOP or a mode of modifying codes during compiling.
And 402, respectively inserting the report information code and the report information into the code of each logic branch to generate an executable program of the target program.
Optionally, the report information includes a name of the target program, a packet name of the logical branch, a class name of the logical branch, a method name of the logical branch, and a branch name of the logical branch.
And step 403, uploading the reported information of each logic branch to a server.
And after the target program generates an executable program, triggering to upload the reported information of all the logic branches to the server.
Step 404, sending a coverage rate query request to the server, where the coverage rate query request carries the name of the target program.
And sending a coverage rate query request to the server side in the running process of the target program or after the running of the target program is finished so that a tester can check the code coverage condition in real time.
Step 405, receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server.
After receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server, the client can display the code coverage rate and the code coverage state of each logic branch through a front-end page for developers and testers to check, so that the purpose of reducing the occurrence probability of the loopholes is achieved.
After the program runs, the coverage rate does not reach 100% completely, the code can be checked back according to the information of the uncovered logic branches, the scenes corresponding to the uncovered logic branches can be found, and then the target program is operated again for covering aiming at the uncovered scenes. And the full coverage of the logic branch is achieved by reversely checking the code and re-running the target program.
In addition, in a reference embodiment of the present invention, the detailed implementation of the method for calculating code coverage is described in detail above, and therefore the repeated description is not repeated here.
The embodiment of the invention also provides a method for calculating the code coverage rate, which is applied to a test terminal and can comprise the following steps: and running the executable program of the target program, and reporting the report information to the server when the report information code is executed. The reporting information code and the corresponding reporting information are respectively inserted into the codes of each logic branch of the target program. Firstly, installing an object program to a test terminal (such as a true machine test), then running an executable program of the object program at the test terminal, and continuously reporting information which is inserted into a logic branch and transferred to a user by the test terminal in the process of running the object program.
Optionally, the executable program for running the object program includes: an executable program of the object program is executed in the execution scene. When the target program runs, the execution scene can be a scene for executing the target program, such as unit test execution, functional test execution, automation scene execution, manual execution of a tester, and the like.
Optionally, running an executable program of the target program, and reporting the report information to the server when executing the report information code, includes: and running the executable program of the target program, and reporting the reporting information inserted in the logic branch to the server side in a network transmission mode when the reporting information code inserted in the logic branch is executed. When the target program runs, when the target program executes the logic branch in which the reporting information code is inserted, the reporting information inserted in the logic branch is uploaded, and then the reporting information is reported to the server side in a network transmission mode, wherein the reporting information of a single logic branch is transmitted by the network every time.
Fig. 5 is a schematic diagram of a main flow of a method of calculating code coverage according to another embodiment of the present invention. As another embodiment of the present invention, as shown in fig. 5, the method for calculating code coverage applied to the server may include:
step 501, receiving the reported information of each logic branch of the target program uploaded by the client, and storing the reported information of each logic branch to a database.
And after receiving the reported information of all the logical branches in the target program uploaded by the client, the server stores the reported information of the logical branches into the database. Specifically, after receiving the report information of all the logical branches, the server needs to create a storage space of all the logical branches in the database, and the database needs to include and not only the following information: the name of the target program, the name of the user, the name of the package where the logical branch is located, the name of the class where the logical branch is located, the name of the method where the logical branch is located, the branch name of the logical branch, and the like, and may further include information such as creation time, coverage status, and the like.
Step 502, in the process of running the executable program of the target program on the test terminal, receiving the report information of the logical branch reported by the test terminal.
And in the running process of the target program, the server receives the reported information of the currently running logic branch reported by the test terminal in real time.
Step 503, determining the code coverage status of each logical branch according to the reporting information of the logical branch reported by the test terminal and the reporting information of each logical branch stored in the database.
Optionally, for any one of the logical branches, the code coverage status of the logical branch is determined by the following method: inquiring the code coverage state of the logic branch in the database according to the reporting information of the logic branch reported by the test terminal; if the code coverage state of the logic branch is uncovered, modifying the coverage state into coverage; and if the code coverage state of the logic branch is coverage, ending the operation. After receiving the reported information of a single logic branch, the server searches the coverage state of the logic branch in the database according to the branch name of the logic branch, if the coverage state of the logic branch is not covered, the coverage state of the logic branch is modified, and if the coverage state is covered, no operation is performed.
According to the various embodiments described above, it can be seen that the technical problem that the code coverage condition cannot be counted in real time in the prior art due to a single usage scenario is solved by the technical means of determining the code coverage state of each logical branch according to the reporting information of the logical branch reported by the test terminal and the reporting information of each logical branch stored in the database in the embodiments of the present invention. The embodiment of the invention can count the code coverage condition of the test case in each execution scene in real time when the program runs, and has simple operation.
Fig. 6 is a schematic diagram of a main flow of a method of calculating code coverage according to one referential embodiment of the present invention. As another embodiment of the present invention, as shown in fig. 6, the method for calculating code coverage applied to the server may include:
step 601, receiving the reported information of each logic branch of the target program uploaded by the client, and storing the reported information of each logic branch to a database.
Step 602, in the process of running the executable program of the target program on the test terminal, receiving the report information of the logical branch reported by the test terminal.
Step 603, determining the code coverage status of each logical branch according to the currently received reporting information of the logical branch and the reporting information of each logical branch stored in the database.
Step 604, receiving a coverage rate query request sent by the client, where the coverage rate query request carries a name of the target program.
Step 605, querying the code coverage status of each logical branch of the target program from the database, thereby calculating the code coverage of the target program.
Step 606, returning the code coverage rate of the target program and the code coverage state of each logic branch to the client.
When a user accesses a front-end page, a client sends a coverage rate query request to a server, after the server receives the coverage rate query request, the code coverage state and the coverage rate of each logic branch of a target program are read from a database and returned to the front-end page, and the front-end page is responsible for displaying the coverage state and the coverage rate of all the logic branches.
According to the embodiment of the invention, the reporting information code and the reporting information are respectively inserted into the codes of all logic branches of the program, development operation is not needed after the executable program is generated, and the code coverage condition of the test case under each execution scene can be counted in real time during the program running.
In addition, in another embodiment of the present invention, the detailed implementation of the method for calculating code coverage is described in detail above, and therefore the repeated description is not repeated here.
Fig. 7 is a schematic diagram of main modules of an apparatus for calculating code coverage according to an embodiment of the present invention, as shown in fig. 7, the apparatus 700 for calculating code coverage is disposed at a client and includes a grammar positioning module 701, an insertion module 702, and an upload module 703; the syntax positioning module 701 is configured to position each logic branch of the target program by analyzing a syntax tree of the target program; the inserting module 702 is configured to insert the reporting information code and the reporting information into the code of each logical branch, respectively, to generate an executable program of the target program; the uploading module 703 is configured to upload the reporting information of each logical branch to the server.
Optionally, the report information includes a name of the target program, a packet name of the logical branch, a class name of the logical branch, a method name of the logical branch, and a branch name of the logical branch.
Optionally, the system further comprises a query module, configured to:
sending a coverage rate query request to the server, wherein the coverage rate query request carries the name of the target program;
and receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server.
It should be noted that, in the implementation of the apparatus for calculating code coverage according to the present invention, the method for calculating code coverage is described in detail above, and therefore, the repeated description is not repeated here.
Fig. 8 is a schematic diagram of main modules of an apparatus for calculating code coverage according to another embodiment of the present invention, and as shown in fig. 8, the apparatus 700 for calculating code coverage is disposed at a server and includes a storage module 801, a receiving module 802 and an updating module 803; the storage module 801 is configured to receive reporting information of each logical branch of a target program uploaded by a client, and store the reporting information of each logical branch in a database; the receiving module 802 is configured to receive reporting information of a logical branch reported by a test terminal in a process of running an executable program of the target program on the test terminal; the updating module 803 is configured to determine a code coverage status of each logical branch according to the currently received reporting information of the logical branch and the reporting information of each logical branch stored in the database.
Optionally, for any one of the logical branches, the code coverage status of the logical branch is determined by the following method:
inquiring the code coverage state of the logic branch in the database according to the currently received report information of the logic branch;
if the code coverage state of the logic branch is uncovered, modifying the coverage state into coverage;
and if the code coverage state of the logic branch is coverage, ending the operation.
Optionally, the system further comprises a computing module, configured to:
receiving a coverage rate query request sent by the client, wherein the coverage rate query request carries the name of the target program;
querying the code coverage state of each logic branch of the target program from the database so as to calculate the code coverage rate of the target program;
and returning the code coverage rate of the target program and the code coverage state of each logic branch to the client.
It should be noted that, in the implementation of the apparatus for calculating code coverage according to the present invention, the method for calculating code coverage is described in detail above, and therefore, the repeated description is not repeated here.
Fig. 9 illustrates an exemplary system architecture 900 of a method of calculating code coverage or an apparatus for calculating code coverage to which embodiments of the present invention may be applied.
As shown in fig. 9, the system architecture 900 may include end devices 901, 902, 903, a network 904, and a server 905. Network 904 is the medium used to provide communication links between terminal devices 901, 902, 903 and server 905. Network 904 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
A user may use the terminal devices 901, 902, 903 to interact with a server 905 over a network 904 to receive or send messages and the like. The terminal devices 901, 902, 903 may have installed thereon various messenger client applications such as, for example only, a shopping-like application, a web browser application, a search-like application, an instant messaging tool, a mailbox client, social platform software, etc.
The terminal devices 901, 902, 903 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 905 may be a server providing various services, such as a background management server (for example only) providing support for shopping websites browsed by users using the terminal devices 901, 902, 903. The background management server may analyze and otherwise process the received data such as the item information query request, and feed back a processing result (for example, target push information, item information — just an example) to the terminal device.
It should be noted that the method for calculating code coverage provided by the embodiment of the present invention is generally performed by the server 905, and accordingly, the apparatus for calculating code coverage is generally disposed in the server 905. The method for calculating the code coverage provided by the embodiment of the present invention may also be executed by the terminal devices 901, 902, 903, and accordingly, the apparatus for calculating the code coverage may be disposed in the terminal devices 901, 902, 903.
It should be understood that the number of terminal devices, networks, and servers in fig. 9 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 10, a block diagram of a computer system 1000 suitable for use with a terminal device implementing an embodiment of the invention is shown. The terminal device shown in fig. 10 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 10, the computer system 1000 includes a Central Processing Unit (CPU)1001 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)1002 or a program loaded from a storage section 1008 into a Random Access Memory (RAM) 1003. In the RAM1003, various programs and data necessary for the operation of the system 1000 are also stored. The CPU 1001, ROM 1002, and RAM1003 are connected to each other via a bus 1004. An input/output (I/O) interface 1005 is also connected to bus 1004.
The following components are connected to the I/O interface 1005: an input section 1006 including a keyboard, a mouse, and the like; an output section 1007 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 1008 including a hard disk and the like; and a communication section 1009 including a network interface card such as a LAN card, a modem, or the like. The communication section 1009 performs communication processing via a network such as the internet. The driver 1010 is also connected to the I/O interface 1005 as necessary. A removable medium 1011 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 1010 as necessary, so that a computer program read out therefrom is mounted into the storage section 1008 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication part 1009 and/or installed from the removable medium 1011. The computer program executes the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 1001.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, 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 disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer programs according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor includes a grammar location module, an insertion module, and an upload module, where the names of the modules do not in some cases constitute a limitation on the module itself.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor includes a storage module, a receiving module, and an updating module, where the names of the modules do not in some cases constitute a limitation on the modules themselves.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, implement the method of: positioning each logic branch of a target program by analyzing a syntax tree of the target program; respectively inserting the reported information code and the reported information into the code of each logic branch to generate an executable program of the target program; and uploading the reported information of each logic branch to a server.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, implement the method of: receiving the reported information of each logic branch of a target program uploaded by a client, and storing the reported information of each logic branch to a database; receiving reporting information of the logic branch reported by the test terminal in the process of running the executable program of the target program on the test terminal; and determining the code coverage state of each logic branch according to the reporting information of the logic branch reported by the test terminal and the reporting information of each logic branch stored in the database.
According to the technical scheme of the embodiment of the invention, the technical means that the reporting information code and the reporting information are respectively inserted into the codes of each logic branch to generate the executable program of the target program and the reporting information of each logic branch is uploaded to the server is adopted, so that the technical problems that the use scene is single and the code coverage condition cannot be counted in real time in the prior art are solved. According to the embodiment of the invention, the reporting information code and the reporting information are respectively inserted into the codes of all logic branches of the program, development operation is not needed after the executable program is generated, and the code coverage condition of the test case under each execution scene can be counted in real time during the program running. The vulnerability testing method can also be displayed through a front-end page for developers and testers to check, and the purpose of reducing the occurrence probability of vulnerabilities is achieved.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (13)

1. A method for calculating code coverage rate, applied to a client, comprises the following steps:
positioning each logic branch of a target program by analyzing a syntax tree of the target program;
respectively inserting the reported information code and the reported information into the code of each logic branch to generate an executable program of the target program;
and uploading the reported information of each logic branch to a server.
2. The method according to claim 1, wherein the report information includes a name of the target program, a packet name of the logical branch, a class name of the logical branch, a method name of the logical branch, and a branch name of the logical branch.
3. The method of claim 1, further comprising:
sending a coverage rate query request to the server, wherein the coverage rate query request carries the name of the target program;
and receiving the code coverage rate of the target program and the code coverage state of each logic branch returned by the server.
4. A method for calculating code coverage rate, applied to a test terminal, includes:
running an executable program of the target program, and reporting the report information to the server when the report information code is executed;
the reporting information code and the corresponding reporting information are respectively inserted into the codes of each logic branch of the target program.
5. The method of claim 4, wherein running the executable program of the target program comprises:
running an executable program of the target program in the execution scene; the execution scene is a unit test execution scene, a function test execution scene, an automation scene execution scene or a manual execution scene of a tester.
6. The method of claim 4, wherein running an executable program of the target program, reporting the report information to the server when executing the report information code, comprises:
and running the executable program of the target program, and reporting the reporting information inserted in the logic branch to the server side in a network transmission mode when the reporting information code inserted in the logic branch is executed.
7. A method for calculating code coverage rate, applied to a server, includes:
receiving the reported information of each logic branch of a target program uploaded by a client, and storing the reported information of each logic branch to a database;
receiving reporting information of the logic branch reported by the test terminal in the process of running the executable program of the target program on the test terminal;
and determining the code coverage state of each logic branch according to the reporting information of the logic branch reported by the test terminal and the reporting information of each logic branch stored in the database.
8. The method of claim 7, wherein for any one of the logical branches, the code coverage status of the logical branch is determined by:
inquiring the code coverage state of the logic branch in the database according to the currently received report information of the logic branch;
if the code coverage state of the logic branch is uncovered, modifying the coverage state into coverage;
and if the code coverage state of the logic branch is coverage, ending the operation.
9. The method of claim 7, further comprising:
receiving a coverage rate query request sent by the client, wherein the coverage rate query request carries the name of the target program;
querying the code coverage state of each logic branch of the target program from the database so as to calculate the code coverage rate of the target program;
and returning the code coverage rate of the target program and the code coverage state of each logic branch to the client.
10. An apparatus for calculating code coverage, provided at a client, comprising:
the grammar positioning module is used for positioning each logic branch of the target program by analyzing a grammar tree of the target program;
the inserting module is used for respectively inserting the reporting information codes and the reporting information into the codes of the logic branches to generate executable programs of the target programs;
and the uploading module is used for uploading the reported information of each logic branch to a server.
11. An apparatus for calculating code coverage, disposed at a server, includes:
the storage module is used for receiving the reported information of each logic branch of the target program uploaded by the client and storing the reported information of each logic branch into a database;
a receiving module, configured to receive reporting information of a logical branch reported by a test terminal in a process of running an executable program of the target program on the test terminal;
and the updating module is used for determining the code coverage state of each logic branch according to the currently received reporting information of the logic branch and the reporting information of each logic branch stored in the database.
12. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
the one or more programs, when executed by the one or more processors, implement the method of any of claims 1-9.
13. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-9.
CN202010603620.XA 2020-06-29 2020-06-29 Method and device for calculating code coverage rate Pending CN113760694A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010603620.XA CN113760694A (en) 2020-06-29 2020-06-29 Method and device for calculating code coverage rate

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010603620.XA CN113760694A (en) 2020-06-29 2020-06-29 Method and device for calculating code coverage rate

Publications (1)

Publication Number Publication Date
CN113760694A true CN113760694A (en) 2021-12-07

Family

ID=78785456

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010603620.XA Pending CN113760694A (en) 2020-06-29 2020-06-29 Method and device for calculating code coverage rate

Country Status (1)

Country Link
CN (1) CN113760694A (en)

Similar Documents

Publication Publication Date Title
US11663110B2 (en) Analysis to check web API code usage and specification
US11550599B2 (en) Method and apparatus for running applet
CN111654495B (en) Method, apparatus, device and storage medium for determining traffic generation source
CN112799940A (en) Regression testing method, device, computer system and computer readable storage medium
CN112631590A (en) Component library generation method and device, electronic equipment and computer readable medium
CN113360377B (en) Test method and device
CN109614327B (en) Method and apparatus for outputting information
US10310962B2 (en) Infrastructure rule generation
CN110888794A (en) Interface test method and device
CN110716956A (en) Data request intercepting method and device
CN112559024A (en) Method and device for generating transaction code change list
CN111221727A (en) Test method, test device, electronic equipment and computer readable medium
CN111414154A (en) Method and device for front-end development, electronic equipment and storage medium
CN110020166B (en) Data analysis method and related equipment
CN110795646A (en) Request processing method and device, electronic equipment and computer-readable storage medium
CN107247661B (en) Method and system for supporting automatic verification of installation package of application
CN110554892A (en) Information acquisition method and device
CN113760694A (en) Method and device for calculating code coverage rate
CN112783903B (en) Method and device for generating update log
CN114238129A (en) Method, device and equipment for generating interface data and storage medium
CN114428815A (en) Data storage method and device, electronic equipment and computer readable medium
CN108628909B (en) Information pushing method and device
CN112579428A (en) Interface testing method and device, electronic equipment and storage medium
CN112612474A (en) Product transplanting method and device, storage medium and electronic equipment
CN113254340A (en) Test case generation method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination