Test code coverage rate detection method and device, computer equipment and storage medium
By instrumenting the code of the test terminal and processing the coverage data, the problem of being unable to collect and merge coverage reports in the existing technology is solved, and the centralized storage and effectiveness of the coverage reports are realized.
Patent Information
- Application Number
- CN202410729834.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-06
- Publication Date
- 2024-08-27
AI Technical Summary
The existing technology cannot centrally collect and merge the coverage reports of each test client, and the high frequency of front-end test code releases causes the coverage reports to easily become invalid.
By responding to the code test instructions sent by the test terminal, the source code to be tested is obtained for instrumentation processing, the instrumentation code is sent to the test terminal, the coverage data is received and processed, the target test coverage results are determined, the data is merged and processed, and stored in the pre-processing Provide storage space.
Centralized collection and accurate merging of coverage reports from multiple test terminals is achieved to ensure the effectiveness of coverage reports for each version.
Smart Images

Figure CN118550828A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of front-end testing technology, and in particular to a test code coverage detection method, device, computer equipment and storage medium. Background Art
[0002] During the development of software projects (such as mobile phone applications, computer applications, etc.), as the projects are iterated, the complexity of the code continues to increase, and the cost of code maintenance is also getting higher and higher. Moreover, before each software product is officially released, the code of the software product often needs to undergo front-end testing of multiple test versions.
[0003] For example, before software product A is officially released, its code undergoes front-end testing of three test versions, and the three test versions of the code are V1.0, V1.1, and V1.2. Currently, before the server sends the V1.0, V1.1, and V1.2 versions of the code to the client for front-end testing, it will use plug-ins to insert stubs for each version of the code. In this way, after the V1.0, V1.1, and V1.2 versions of the code are front-end tested in the client, the V1.0, V1.1, and V1.2 versions of the code are obtained in the client corresponding to the V1.0 version of the code Coverage report, V1.1 coverage report, and V1.2 coverage report corresponding to the V1.0 version of the code are stored locally on the client, which makes it impossible to centrally view the coverage of all versions of the code in the server.
[0004] Moreover, during specific testing, the test code is released very frequently, that is, it is not limited to the three test versions in the above example. When there are more test versions, there may be multiple releases per day. Releases mean that although it is the same file, the code content inside may be completely different, which will cause the previously collected code coverage to be completely invalid. Summary of the invention
[0005] The embodiments of the present invention provide a test code coverage detection method, apparatus, computer equipment and storage medium, aiming to solve the problem in the prior art that coverage reports in each test client are not obtained and the release frequency of the front-end test code is high, which not only makes it impossible to collect and merge coverage reports in the test client, but also makes the coverage report easily invalid.
[0006] In a first aspect, an embodiment of the present invention provides a test code coverage detection method, which includes:
[0007] In response to a code test instruction sent by the test terminal, obtaining a source code to be tested corresponding to the code test instruction;
[0008] Performing a stub processing on the source code to be tested to obtain a stub code, and sending the stub code to the test terminal;
[0009] Receiving test coverage data corresponding to the plug-in code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result;
[0010] The target data merging processing result is stored in a preset storage space.
[0011] In a second aspect, an embodiment of the present invention further provides a test code coverage detection device, which includes:
[0012] A source code to be tested acquisition unit, used for responding to a code test instruction sent by the test terminal and acquiring a source code to be tested corresponding to the code test instruction;
[0013] A source code plugging unit, used for plugging the source code to be tested to obtain a plugging code, and sending the plugging code to the test terminal;
[0014] A test coverage acquisition unit, configured to receive test coverage data corresponding to the plugged code sent by the test terminal, determine a target test coverage result based on the test coverage data, and acquire a target data merging processing result corresponding to the target test coverage result;
[0015] The merging result storage unit is used to store the target data merging processing result into a preset storage space.
[0016] In a third aspect, an embodiment of the present invention further provides a computer device, which includes a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the method described in the first aspect is implemented.
[0017] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the method described in the first aspect above can be implemented.
[0018] The embodiment of the present invention provides a test code coverage detection method, device, computer equipment and storage medium, the method comprising: in response to a code test instruction sent by a test terminal, obtaining a source code to be tested corresponding to the code test instruction; performing a stub process on the source code to be tested to obtain a stub code, and sending the stub code to the test terminal; receiving test coverage data corresponding to the stub code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result; storing the target data merging processing result in a preset storage space. The embodiment of the present invention can not only centrally collect and accurately merge coverage reports of multiple test terminals, but also ensure the validity of each version of the coverage report. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying any creative work.
[0020] Figure 1 A schematic diagram of an application scenario of the test code coverage detection method provided by an embodiment of the present invention;
[0021] Figure 2 A schematic diagram of a flow chart of a test code coverage detection method provided by an embodiment of the present invention;
[0022] Figure 3 A schematic diagram of a sub-process of a test code coverage detection method provided by an embodiment of the present invention;
[0023] Figure 4 A schematic diagram of another sub-process of the test code coverage detection method provided by an embodiment of the present invention;
[0024] Figure 5 A schematic diagram of another sub-process of the test code coverage detection method provided by an embodiment of the present invention;
[0025] Figure 6 A schematic diagram of another sub-process of the test code coverage detection method provided by an embodiment of the present invention;
[0026] Figure 7 A schematic block diagram of a test code coverage detection device provided by an embodiment of the present invention;
[0027] Figure 8 A schematic block diagram of a computer device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0028] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0029] It should be understood that when used in this specification and the appended claims, the terms "include" and "comprises" indicate the presence of described features, integers, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or combinations thereof.
[0030] It should also be understood that the terms used in this specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.
[0031] It should be further understood that the term "and / or" used in the present description and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
[0032] Please also refer to Figure 1 and Figure 2 ,in Figure 1 The following is a schematic diagram of a scenario for testing a code coverage detection method according to an embodiment of the present invention. Figure 2 FIG. 1 is a flow chart of a test code coverage detection method provided by an embodiment of the present invention. Figure 1 As shown, the test code coverage detection method provided by the embodiment of the present invention is applied to a server 10, and multiple test terminals 20 are connected to the server in communication. Among them, the terminal types corresponding to the multiple test terminals 20 include smart phones, tablet computers, laptops, desktop computers, etc.
[0033] like Figure 2 As shown, the method includes the following steps S110-S140.
[0034] S110. In response to a code test instruction sent by a test terminal, obtain a source code to be tested corresponding to the code test instruction.
[0035] In this embodiment, the technical solution is described with the server as the execution subject, and the interaction scenario between a test terminal and the server is taken as an example. The interaction scenarios between other multiple test terminals and the server can refer to the specific process of the interaction scenario between the aforementioned test terminal and the server. For example, when test terminal A completes the preparation of the software and hardware environment required before the test, and the developer completes the development of the project source code corresponding to the front-end device of test terminal A, test terminal A can send a code test instruction to the server to indicate that test terminal A can start front-end testing. Because the project source code corresponding to test terminal A (i.e., the source code to be tested) has been stored in the server when the development was completed before, after the server receives the code test instruction sent by test terminal A, it can obtain the corresponding source code to be tested.
[0036] S120: Perform a plugging process on the source code to be tested to obtain a plugging code, and send the plugging code to the test terminal.
[0037] In this embodiment, in order to collect the specific execution status of the code, some probes (also called "detectors", which are essentially code segments for information collection, which can be assignment statements or function calls for collecting coverage information) can be inserted into the source code to be tested on the basis of ensuring the original logical integrity of the source code to be tested. The source code to be tested is essentially inserted with some "counters". When the code is executed, the executed code part will also execute the corresponding "counter", so as to obtain the test coverage rate required to be collected.
[0038] In one embodiment, if Figure 3 As shown, step S120 includes:
[0039] S121, obtaining a preset transcoder and parser, and sequentially transcoding and parsing the source code to be tested based on the transcoder and the parser to obtain a current abstract syntax tree;
[0040] S122, dimension-marking the current abstract syntax tree based on a preset dimension type to obtain a marked source code;
[0041] S123: Perform corresponding insertion in the marked source code based on the preset dimension type to obtain the inserted code.
[0042] In this embodiment, when the server performs the plugging process on the source code to be tested, the specific process is as follows:
[0043] 11) The source code to be tested is sequentially transcoded and parsed based on the transcoder and the parser to obtain the current abstract syntax tree; wherein, the Babel transcoder and the ANTLR parser generator can be specifically used; the Babel transcoder can solve the problem of insufficient browser support for ES6 syntax. By using the Babel transcoder, developers can write ES6 code and then convert it into ES5 code, thereby ensuring smooth operation in various environments; the ANTLR parser generator can generate a target parser based on the specific language used by the transcoded code obtained after the source code to be tested is transcoded by the transcoder, and then the transcoded code is passed through the target parser to obtain the current abstract syntax tree; in the current abstract syntax tree, the structure of the source code to be tested is described in a tree-like manner, and each node on the tree represents a structure in the source code to be tested;
[0044] 12) Obtaining a preset dimension type (generally including at least one of the four types of line, function, branch and statement), and then dimensionally marking the current abstract syntax tree by the preset dimension type to obtain a marked source code; wherein the line coverage is used to evaluate whether each line in the source code to be tested has been executed; the function coverage is used to evaluate whether each function in the source code to be tested has been called; the branch coverage is used to evaluate whether each if module in the source code to be tested has been executed; and the statement coverage is used to evaluate whether each statement in the source code to be tested has been executed;
[0045] 13) Probe data corresponding to a preset dimension type is obtained to perform corresponding insertion in the marked source code to obtain the insertion code.
[0046] The above process enables rapid instrumentation of the code to be tested, facilitating subsequent collection and accurate analysis of the test coverage of the code.
[0047] In one embodiment, if Figure 4 As shown, step S123 includes:
[0048] S1231: If it is determined that the front-end type corresponding to the test terminal belongs to the first preset front-end type set, inserting the marking source code during compilation to obtain the inserting code;
[0049] S1232: If it is determined that the front-end type corresponding to the test end belongs to a second preset front-end type set, perform runtime instrumentation on the marked source code to obtain the instrumentation code.
[0050] Among them, the first preset front-end type set includes at least VUE front-end type, REACT front-end type and NG front-end type; the second preset front-end type set includes at least NODE front-end terminal type.
[0051] In this embodiment, because different front-end types correspond to different plugging methods, it is necessary to first determine the front-end type corresponding to the test terminal. For example, still referring to the above example, if it is determined that the corresponding front-end type in the test terminal A is one of the VUE front-end type, the REACT front-end type and the NG front-end type, the marked source code is plugged during compilation to obtain the plugging code; if it is determined that the corresponding front-end type in the test terminal A is the NODE front-end type, the marked source code is plugged during runtime to obtain the plugging code. Among them, VUE is a progressive framework for building user interfaces; REACT is a WEB development framework and a JavaScript library for building user interfaces; the full name of the NG framework is the Angular framework, which is a JavaScript framework for building user interfaces; NODE is a JavaScript runtime environment based on the Chrome V8 engine.
[0052] S130, receiving test coverage data corresponding to the plug-in code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result.
[0053] In this embodiment, still referring to the above example, when the server sends the plug-in code to the test terminal A for front-end testing, the test terminal A will write the plug-in code into the window.__coverage object, and obtain the test coverage data corresponding to the source code to be tested in the test terminal A (of course, it is also the test coverage data corresponding to the plug-in code), and it needs to be processed before it can be reported to the server. For example, when a browser is used for front-end testing in the test terminal A, the test coverage data can be obtained by monitoring the Visibilitychange event and reported to the server; when a small program is used for front-end testing in the test terminal A, the test coverage data can be obtained by monitoring the onShow event and the onHide event and reported to the server; when Node.js is used for front-end testing in the test terminal A, the test coverage data can be obtained through a scheduled task and reported to the server.
[0054] Among them, the server can obtain data such as the full statement execution volume, incremental statement execution volume, full line execution volume, incremental line execution volume, full function execution volume, incremental function execution volume, full branch execution volume and incremental branch execution volume from the test coverage data. At this time, the server can determine the target test coverage result based on the above data.
[0055] In one embodiment, if it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to a full coverage type, Figure 5As shown, step S130 includes:
[0056] S131a, obtaining the total statement execution amount in the test coverage data, determining the total number of key names based on the statement execution amount as a first value, and determining the total number of key names whose key values are not 0 as a second value;
[0057] S132a. Obtain a quotient of the second value and the first value and use it as the target test coverage result.
[0058] In this embodiment, as the first specific embodiment of step S130, it is a scenario of obtaining statement test coverage results, but what is specifically obtained is the full coverage of the statements. After the server receives the test coverage data reported by test terminal A, it can analyze s (statement execution volume) in coverageData in the test coverage data; wherein, the key name in s represents the index of the statement, and the key value in s represents the number of executions of the statement. After analyzing s (statement execution volume) in coverageData, the total number of key names is obtained as the first value, and the total number of key names whose key values are not 0 is obtained as the second value. Afterwards, the second value / first value is used as the target test coverage result, and the target test coverage result represents the full test coverage result of the statements in the code to be tested.
[0059] For example, by analyzing s (statement execution amount) in coverageData in the test coverage data, it can be seen that the first value is the total amount of statements and is expressed as:
[0060] const totalStatement=Object.values(s).length;
[0061] The second value is the amount of statements executed and is expressed as:
[0062] const runningNum=Object.values(s).filter(i=>i).length;
[0063] When the full test coverage result of a statement is expressed by globalCoverRate, the specific calculation formula is const globalCoverRate=runningNum / totalStatement.
[0064] Of course, when it is determined that the preset dimension type includes one or more of statement type, row type, function type or branch type, when calculating the full test coverage results of rows, the full test coverage results of functions and the full test coverage results of branches, the process of calculating the full test coverage results of statements can be referred to. In the above manner, the test coverage results can be centrally viewed on the server.
[0065] In one embodiment, if it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to an incremental coverage type, Figure 6 As shown, step S130 includes:
[0066] S131b, obtaining the incremental statement execution amount in the test coverage data, determining the total number of key names based on the incremental statement execution amount as a third value, and determining the total number of key names whose key values are not 0 as a fourth value;
[0067] S132b. Obtain a quotient of the fourth value and the third value and use it as the target test coverage result.
[0068] In this embodiment, as the second specific embodiment of step S130, it is also a scenario of obtaining statement test coverage results, but specifically obtaining the incremental coverage of statements. After the server receives the test coverage data reported by the test terminal A, it can rely on code diff to analyze the incremental code, and through code diff, it can obtain the statement additions in the instrumented code relative to the previous version of the instrumented code received by the test terminal.
[0069] For example, after determining the newly added statements in the plug-in code relative to the previous version of the plug-in code received by the test terminal, the total amount of the newly added statements is obtained as the third value and expressed as:
[0070] const incrementTotalStatement=Object.values(incrementCoveredS).length;
[0071] The number of statements that have been executed in the new statement is used as the fourth value and is expressed as:
[0072] const runningNum=Object.values(incrementCoveredS).filter(i=>i).length;
[0073] When the incremental test coverage result of a statement is expressed by incrementCoverRate, the specific calculation formula is const incrementCoverRate = runningNum / totalStatement.
[0074] Of course, when it is determined that the preset dimension type includes one or more of statement type, row type, function type or branch type, when calculating the incremental test coverage results of rows, the incremental test coverage results of functions and the incremental test coverage results of branches, the process of calculating the incremental test coverage results of statements can be referred to. In the above manner, the test coverage results can also be centrally viewed on the server.
[0075] However, whether you are obtaining the full test coverage results of a statement or the incremental test coverage results of a statement, the following scenarios exist:
[0076] 21) A new code scenario is added, in which the number of changed lines in the plug-in code is 0 compared to the previous version of the plug-in code received by the test terminal, and the plug-in code is completely retained in the plug-in code;
[0077] 22) Deleting a code scenario, in which the number of lines of the previous version of the plug-in code received by the test terminal is unchanged relative to the plug-in code, and the total number of statements of the plug-in code is less than the total number of statements of the previous version of the plug-in code;
[0078] 23) The code scenario is changed and the number of lines is not increased. In this scenario, the number of new code change lines of the plug-in code is equal to the number of old code change lines of the previous version of the plug-in code received by the test terminal;
[0079] 24) The code scenario is changed and the number of lines is increased. In this scenario, the number of new code change lines of the plug-in code is greater than the number of old code change lines of the previous version of the plug-in code received by the test terminal, and both the number of new code change lines and the number of old code change lines are greater than 0;
[0080] 25) The code scenario is changed and the number of lines is reduced. In this scenario, the number of new code change lines of the plug-in code is less than the number of old code change lines of the previous version of the plug-in code received by the test terminal, and both the number of new code change lines and the number of old code change lines are greater than 0.
[0081] However, in any of the above scenarios, the test coverage data can be determined based on the instrumented code and the previous version of the instrumented code, and the target test coverage result can be finally determined.
[0082] In one embodiment, obtaining the target data merging processing result corresponding to the target test coverage result in step S130 includes:
[0083] Obtain the target test version number corresponding to the target test coverage result;
[0084] If it is determined that the target test version number exists in the local library table, the target test coverage result is saved, the historical test coverage result corresponding to the target test coverage result in the library table is deleted, and the target test coverage result is used as the target data merging processing result;
[0085] If it is determined that the target test version number does not exist in the local library table, the target test coverage result is saved and used as the target data merging processing result.
[0086] In this embodiment, continuing to refer to the above example, after test terminal A executes the instrumented code to complete the front-end test and reports the target test coverage result to the server, it is necessary to determine whether the target test coverage result has been obtained by other test terminals of the same type running the instrumented code. That is, first, it is necessary to obtain the target test version number corresponding to the target test coverage result, such as version V1.1; then query in the library table of the server whether the same test version number has been stored for the instrumented code. If it is determined that the target test version number exists in the local library table, it means that other test terminals have run the instrumented code and have stored the same test version number. At this time, the latest target test coverage result can be used as the target data merge processing result, and the historical test coverage result corresponding to the target test coverage result that has been stored before is deleted. In this way, it can be ensured that for each test version number of the instrumented code in the library table, the test coverage result with a later acquisition time is always saved.
[0087] If it is determined that the target test version number does not exist in the local library table, it means that no other test terminal runs the plug-in code and does not store the same test version number. At this time, the target test coverage result is directly saved and used as the target data merge processing result. It can be seen that based on the above method, at least one test coverage result is saved for each test version number of the plug-in code.
[0088] S140: storing the target data merging processing result into a preset storage space.
[0089] In this embodiment, after the target data merging processing results corresponding to the source code to be tested are obtained in the server, they can be saved in a preset storage space in the server's database to ensure that the test coverage results corresponding to each test version number of the source code to be tested can be saved in time to facilitate subsequent query and traceability.
[0090] In one embodiment, after step S140, the method further includes:
[0091] Obtain a coverage report and coverage report data corresponding to the target data merge processing result, and display the coverage report and coverage report data accordingly.
[0092] In this embodiment, after the target data merging processing result is obtained in the server, the target data merging processing result can be subjected to data analysis to obtain a coverage report including the test coverage result (for example, a pie chart is used to show the percentage of covered code and the percentage of uncovered code in the test coverage result) and coverage report data (for example, obtained by filling the test coverage result into a report template). The obtained coverage report and the obtained coverage report data can be displayed on the local display screen of the server. Of course, the obtained coverage report and the obtained coverage report data can also be pushed to other receiving ends for viewing on the display screens of other receiving ends.
[0093] It can be seen that the embodiment of implementing the method can not only centrally collect and accurately merge the coverage reports of multiple test terminals, but also ensure the validity of each version of the coverage report.
[0094] Figure 7 is a schematic block diagram of a test code coverage detection device provided by an embodiment of the present invention. Figure 7 As shown, corresponding to the above test code coverage detection method, the present invention also provides a test code coverage detection device 100. The test code coverage detection device 100 includes a unit for executing the above test code coverage detection method. Figure 7 The test code coverage detection device 100 includes: a source code acquisition unit 110 to be tested, a source code plugging unit 120, a test coverage acquisition unit 130 and a merge result storage unit 140.
[0095] The source code to be tested acquiring unit 110 is used to acquire the source code to be tested corresponding to the code testing instruction in response to the code testing instruction sent by the test terminal.
[0096] In this embodiment, the technical solution is described with the server as the execution subject, and the interaction scenario between a test terminal and the server is taken as an example. The interaction scenarios between other multiple test terminals and the server can refer to the specific process of the interaction scenario between the aforementioned test terminal and the server. For example, when test terminal A completes the preparation of the software and hardware environment required before the test, and the developer completes the development of the project source code corresponding to the front-end device of test terminal A, test terminal A can send a code test instruction to the server to indicate that test terminal A can start front-end testing. Because the project source code corresponding to test terminal A (i.e., the source code to be tested) has been stored in the server when the development was completed before, after the server receives the code test instruction sent by test terminal A, it can obtain the corresponding source code to be tested.
[0097] The source code plugging unit 120 is used to perform plugging processing on the source code to be tested to obtain plugging code, and send the plugging code to the test terminal.
[0098] In this embodiment, in order to collect the specific execution status of the code, some probes (also called "detectors", which are essentially code segments for information collection, which can be assignment statements or function calls for collecting coverage information) can be inserted into the source code to be tested on the basis of ensuring the original logical integrity of the source code to be tested. The source code to be tested is essentially inserted with some "counters". When the code is executed, the executed code part will also execute the corresponding "counter", so as to obtain the test coverage rate required to be collected.
[0099] In one embodiment, the source code instrumentation unit 120 is used to:
[0100] Obtain a preset transcoder and parser, and sequentially transcode and parse the source code to be tested based on the transcoder and the parser to obtain a current abstract syntax tree;
[0101] Performing dimension marking on the current abstract syntax tree based on a preset dimension type to obtain a marked source code;
[0102] Based on the preset dimension type, corresponding insertion is performed in the marked source code to obtain the inserted code.
[0103] In this embodiment, when the server performs the plugging process on the source code to be tested, the specific process is as follows:
[0104] 31) The source code to be tested is transcoded and parsed in turn based on the transcoder and the parser to obtain the current abstract syntax tree; wherein, the Babel transcoder and the ANTLR parser generator can be specifically used; the Babel transcoder can solve the problem of insufficient browser support for ES6 syntax. By using the Babel transcoder, developers can write ES6 code and then convert it into ES5 code, thereby ensuring smooth operation in various environments; the ANTLR parser generator can generate a target parser based on the specific language used by the transcoded code obtained after the source code to be tested is transcoded by the transcoder, and then the transcoded code is passed through the target parser to obtain the current abstract syntax tree; in the current abstract syntax tree, the structure of the source code to be tested is described in a tree-like manner, and each node on the tree represents a structure in the source code to be tested;
[0105] 32) Obtaining a preset dimension type (generally including at least one of the four types of line, function, branch and statement), and then dimensionally marking the current abstract syntax tree by the preset dimension type to obtain a marked source code; wherein the line coverage is used to evaluate whether each line in the source code to be tested has been executed; the function coverage is used to evaluate whether each function in the source code to be tested has been called; the branch coverage is used to evaluate whether each if module in the source code to be tested has been executed; and the statement coverage is used to evaluate whether each statement in the source code to be tested has been executed;
[0106] 33) Acquire probe data corresponding to a preset dimension type, so as to perform corresponding insertion in the marked source code to obtain the insertion code.
[0107] The above process enables rapid instrumentation of the code to be tested, facilitating subsequent collection and accurate analysis of the test coverage of the code.
[0108] In one embodiment, the step of performing corresponding insertion in the markup source code based on the preset dimension type to obtain the insertion code includes:
[0109] If it is determined that the front-end type corresponding to the test terminal belongs to the first preset front-end type set, inserting the marking source code during compilation to obtain the inserting code;
[0110] If it is determined that the front-end type corresponding to the test end belongs to the second preset front-end type set, the marked source code is instrumented at runtime to obtain the instrumented code.
[0111] Among them, the first preset front-end type set includes at least VUE front-end type, REACT front-end type and NG front-end type; the second preset front-end type set includes at least NODE front-end terminal type.
[0112] In this embodiment, because different front-end types correspond to different plugging methods, it is necessary to first determine the front-end type corresponding to the test terminal. For example, still referring to the above example, if it is determined that the corresponding front-end type in the test terminal A is one of the VUE front-end type, the REACT front-end type and the NG front-end type, the marked source code is plugged during compilation to obtain the plugging code; if it is determined that the corresponding front-end type in the test terminal A is the NODE front-end type, the marked source code is plugged during runtime to obtain the plugging code. Among them, VUE is a progressive framework for building user interfaces; REACT is a WEB development framework and a JavaScript library for building user interfaces; the full name of the NG framework is the Angular framework, which is a JavaScript framework for building user interfaces; NODE is a JavaScript runtime environment based on the Chrome V8 engine.
[0113] The test coverage acquisition unit 130 is used to receive the test coverage data corresponding to the plug-in code sent by the test terminal, determine the target test coverage result based on the test coverage data, and obtain the target data merging processing result corresponding to the target test coverage result.
[0114] In this embodiment, still referring to the above example, when the server sends the plug-in code to the test terminal A for front-end testing, the test terminal A will write the plug-in code into the window.__coverage object, and obtain the test coverage data corresponding to the source code to be tested in the test terminal A (of course, it is also the test coverage data corresponding to the plug-in code), and it needs to be processed before it can be reported to the server. For example, when a browser is used for front-end testing in the test terminal A, the test coverage data can be obtained by monitoring the Visibilitychange event and reported to the server; when a small program is used for front-end testing in the test terminal A, the test coverage data can be obtained by monitoring the onShow event and the onHide event and reported to the server; when Node.js is used for front-end testing in the test terminal A, the test coverage data can be obtained through a scheduled task and reported to the server.
[0115] Among them, the server can obtain data such as the full statement execution volume, incremental statement execution volume, full line execution volume, incremental line execution volume, full function execution volume, incremental function execution volume, full branch execution volume and incremental branch execution volume from the test coverage data. At this time, the server can determine the target test coverage result based on the above data.
[0116] In one embodiment, if it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to a full coverage type, the test coverage acquisition unit 130 is used to:
[0117] Obtaining the total statement execution amount in the test coverage data, determining the total number of key names based on the statement execution amount as a first value, and determining the total number of key names whose key values are not 0 as a second value;
[0118] The quotient of the second value and the first value is obtained and used as the target test coverage result.
[0119] In this embodiment, as the first specific embodiment of the test coverage acquisition unit 130, it is a scenario for obtaining statement test coverage results, but what is specifically obtained is the full coverage of the statements. After the server receives the test coverage data reported by the test terminal A, it can analyze the s (statement execution amount) in the coverageData in the test coverage data; wherein the key name in s represents the index of the statement, and the key value in s represents the number of executions of the statement. After analyzing s (statement execution amount) in coverageData, the total number of key names is obtained as the first value, and the total number of key names whose key values are not 0 is obtained as the second value. Afterwards, the second value / first value is used as the target test coverage result, and the target test coverage result represents the full test coverage result of the statements in the code to be tested.
[0120] For example, by analyzing s (statement execution amount) in coverageData in the test coverage data, it can be seen that the first value is the total amount of statements and is expressed as:
[0121] const totalStatement=Object.values(s).length;
[0122] The second value is the amount of statements executed and is expressed as:
[0123] const runningNum=Object.values(s).filter(i=>i).length;
[0124] When the full test coverage result of a statement is expressed by globalCoverRate, the specific calculation formula is const globalCoverRate=runningNum / totalStatement.
[0125] Of course, when it is determined that the preset dimension type includes one or more of statement type, row type, function type or branch type, when calculating the full test coverage results of rows, the full test coverage results of functions and the full test coverage results of branches, the process of calculating the full test coverage results of statements can be referred to. In the above manner, the test coverage results can be centrally viewed on the server.
[0126] In one embodiment, if it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to an incremental coverage type, the test coverage acquisition unit 130 is used to:
[0127] Obtaining the incremental statement execution amount in the test coverage data, determining the total number of key names based on the incremental statement execution amount as a third value, and determining the total number of key names whose key values are not 0 as a fourth value;
[0128] A quotient of the fourth value and the third value is obtained and used as the target test coverage result.
[0129] In this embodiment, as the second specific embodiment of the test coverage acquisition unit 130, it is also a scenario of acquiring statement test coverage results, but specifically acquiring the incremental coverage of statements. After the server receives the test coverage data reported by the test terminal A, it can rely on code diff to analyze the incremental code, and through code diff, it can acquire the newly added statements in the instrumented code relative to the previous version of the instrumented code received by the test terminal.
[0130] For example, after determining the newly added statements in the plug-in code relative to the previous version of the plug-in code received by the test terminal, the total amount of the newly added statements is obtained as the third value and expressed as:
[0131] const incrementTotalStatement=Object.values(incrementCoveredS).length;
[0132] The number of statements that have been executed in the new statement is used as the fourth value and is expressed as:
[0133] const runningNum=Object.values(incrementCoveredS).filter(i=>i).length;
[0134] When the incremental test coverage result of a statement is expressed by incrementCoverRate, the specific calculation formula is const incrementCoverRate = runningNum / totalStatement.
[0135] Of course, when it is determined that the preset dimension type includes one or more of statement type, row type, function type or branch type, when calculating the incremental test coverage results of rows, the incremental test coverage results of functions and the incremental test coverage results of branches, the process of calculating the incremental test coverage results of statements can be referred to. In the above manner, the test coverage results can also be centrally viewed on the server.
[0136] In one embodiment, the test coverage acquisition unit 130 is further configured to:
[0137] Obtain the target test version number corresponding to the target test coverage result;
[0138] If it is determined that the target test version number exists in the local library table, the target test coverage result is saved, the historical test coverage result corresponding to the target test coverage result in the library table is deleted, and the target test coverage result is used as the target data merging processing result;
[0139] If it is determined that the target test version number does not exist in the local library table, the target test coverage result is saved and used as the target data merging processing result.
[0140] In this embodiment, continuing to refer to the above example, after test terminal A executes the instrumented code to complete the front-end test and reports the target test coverage result to the server, it is necessary to determine whether the target test coverage result has been obtained by other test terminals of the same type running the instrumented code. That is, first, it is necessary to obtain the target test version number corresponding to the target test coverage result, such as version V1.1; then query in the library table of the server whether the same test version number has been stored for the instrumented code. If it is determined that the target test version number exists in the local library table, it means that other test terminals have run the instrumented code and have stored the same test version number. At this time, the latest target test coverage result can be used as the target data merge processing result, and the historical test coverage result corresponding to the target test coverage result that has been stored before is deleted. In this way, it can be ensured that for each test version number of the instrumented code in the library table, the test coverage result with a later acquisition time is always saved.
[0141] If it is determined that the target test version number does not exist in the local library table, it means that no other test terminal runs the plug-in code and does not store the same test version number. At this time, the target test coverage result is directly saved and used as the target data merge processing result. It can be seen that based on the above method, at least one test coverage result is saved for each test version number of the plug-in code.
[0142] The merging result storage unit 140 is used to store the target data merging processing result in a preset storage space.
[0143] In this embodiment, after the target data merging processing results corresponding to the source code to be tested are obtained in the server, they can be saved in a preset storage space in the server's database to ensure that the test coverage results corresponding to each test version number of the source code to be tested can be saved in time to facilitate subsequent query and traceability.
[0144] In one embodiment, the test code coverage detection device 100 further includes:
[0145] The report generation unit is used to obtain a coverage report and coverage report data corresponding to the target data merge processing result, and to display the coverage report and the coverage report data accordingly.
[0146] In this embodiment, after the target data merging processing result is obtained in the server, the target data merging processing result can be subjected to data analysis to obtain a coverage report including the test coverage result (for example, a pie chart is used to show the percentage of covered code and the percentage of uncovered code in the test coverage result) and coverage report data (for example, obtained by filling the test coverage result into a report template). The obtained coverage report and the obtained coverage report data can be displayed on the local display screen of the server. Of course, the obtained coverage report and the obtained coverage report data can also be pushed to other receiving ends for viewing on the display screens of other receiving ends.
[0147] It can be seen that the implementation of the embodiment of the device can not only centrally collect and accurately merge the coverage reports of multiple test terminals, but also ensure the validity of each version of the coverage report.
[0148] The above-mentioned test code coverage detection device can be implemented in the form of a computer program. The computer program can be used in Figure 8 Runs on the computer device shown.
[0149] See also Figure 8 , Figure 8The present invention provides a schematic block diagram of a computer device provided in an embodiment of the present invention. The computer device integrates any test code coverage detection device provided in an embodiment of the present invention.
[0150] See also Figure 8 The computer device 400 includes a processor 402 , a memory and a network interface 405 connected via a system bus 401 , wherein the memory may include a storage medium 403 and an internal memory 404 .
[0151] The storage medium 403 can store an operating system 4031 and a computer program 4032. The computer program 4032 includes program instructions, and when the program instructions are executed, the processor 402 can execute a test code coverage detection method.
[0152] The processor 402 is used to provide computing and control capabilities to support the operation of the entire computer device.
[0153] The internal memory 404 provides an environment for the operation of the computer program 4032 in the storage medium 403. When the computer program 4032 is executed by the processor 402, the processor 402 can execute the above-mentioned test code coverage detection method.
[0154] The network interface 405 is used to communicate with other devices over the network. Figure 8 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present invention, and does not constitute a limitation on the computer device to which the solution of the present invention is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0155] The processor 402 is used to run the computer program 4032 stored in the memory to implement the following steps:
[0156] In response to a code test instruction sent by the test terminal, obtaining a source code to be tested corresponding to the code test instruction;
[0157] Performing a stub processing on the source code to be tested to obtain a stub code, and sending the stub code to the test terminal;
[0158] Receiving test coverage data corresponding to the plug-in code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result;
[0159] The target data merging processing result is stored in a preset storage space.
[0160] It should be understood that in the embodiment of the present invention, the processor 402 may be a central processing unit (CPU), and the processor 402 may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0161] It can be understood by those skilled in the art that all or part of the processes in the method for implementing the above embodiment can be completed by instructing the relevant hardware through a computer program. The computer program includes program instructions, and the computer program can be stored in a storage medium, which is a computer-readable storage medium. The program instructions are executed by at least one processor in the computer system to implement the process steps of the embodiment of the above method.
[0162] Therefore, the present invention also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions. When the program instructions are executed by a processor, the processor executes the following steps:
[0163] In response to a code test instruction sent by the test terminal, obtaining a source code to be tested corresponding to the code test instruction;
[0164] Performing a stub processing on the source code to be tested to obtain a stub code, and sending the stub code to the test terminal;
[0165] Receiving test coverage data corresponding to the plug-in code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result;
[0166] The target data merging processing result is stored in a preset storage space.
[0167] The storage medium may be any computer-readable storage medium that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a magnetic disk, or an optical disk.
[0168] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0169] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of each unit is only a logical function division, and there may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed.
[0170] The steps in the method of the embodiment of the present invention can be adjusted in order, combined and deleted according to actual needs. The units in the device of the embodiment of the present invention can be combined, divided and deleted according to actual needs. In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0171] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, terminal, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention.
[0172] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed by the present invention, and these modifications or replacements should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A test code coverage detection method, It is characterized in that include: In response to a code test instruction sent by the test terminal, obtaining a source code to be tested corresponding to the code test instruction; Performing a stub processing on the source code to be tested to obtain a stub code, and sending the stub code to the test terminal; Receiving test coverage data corresponding to the plug-in code sent by the test terminal, determining a target test coverage result based on the test coverage data, and obtaining a target data merging processing result corresponding to the target test coverage result; The target data merging processing result is stored in a preset storage space.
2. The method according to claim 1, It is characterized in that The step of performing a plugging process on the source code to be tested to obtain a plugging code includes: Obtaining a preset transcoder and parser, and sequentially transcoding and parsing the source code to be tested based on the transcoder and the parser to obtain a current abstract syntax tree; Performing dimension marking on the current abstract syntax tree based on a preset dimension type to obtain a marked source code; Based on the preset dimension type, corresponding insertion is performed in the marked source code to obtain the inserted code.
3. The method according to claim 2, It is characterized in that The step of performing corresponding insertion in the marked source code based on the preset dimension type to obtain the insertion code includes: If it is determined that the front-end type corresponding to the test terminal belongs to a first preset front-end type set, the marked source code is inserted during compilation to obtain the inserted code; wherein the first preset front-end type set includes at least a VUE front-end type, a REACT front-end type and a NG front-end type; If it is determined that the front-end type corresponding to the test end belongs to a second preset front-end type set, the marked source code is instrumented at runtime to obtain the instrumented code; wherein the second preset front-end type set includes at least the NODE front-end terminal type.
4. The method according to claim 2, It is characterized in that If it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to a full coverage type, determining the target test coverage result based on the test coverage data includes: Obtaining the total statement execution amount in the test coverage data, determining the total number of key names based on the statement execution amount as a first value, and determining the total number of key names whose key values are not 0 as a second value; The quotient of the second value and the first value is obtained and used as the target test coverage result.
5. The method according to claim 2, It is characterized in that If it is determined that the preset dimension type includes a statement type, and the target test coverage result corresponds to an incremental coverage type, determining the target test coverage result based on the test coverage data includes: Obtaining the incremental statement execution amount in the test coverage data, determining the total number of key names based on the incremental statement execution amount as a third value, and determining the total number of key names whose key values are not 0 as a fourth value; The quotient of the fourth value and the third value is obtained and used as the target test coverage result.
6. The method according to claim 4 or 5, It is characterized in that The obtaining of the target data merging processing result corresponding to the target test coverage result includes: Obtain the target test version number corresponding to the target test coverage result; If it is determined that the target test version number exists in the local library table, the target test coverage result is saved, the historical test coverage result corresponding to the target test coverage result in the library table is deleted, and the target test coverage result is used as the target data merging processing result; If it is determined that the target test version number does not exist in the local library table, the target test coverage result is saved and used as the target data merging processing result.
7. The method according to claim 1, It is characterized in that After the step of storing the target data merging processing result in a preset storage space, the method further includes: Obtain a coverage report and coverage report data corresponding to the target data merge processing result, and display the coverage report and coverage report data accordingly.
8. A test code coverage detection device, It is characterized in that include: A source code to be tested acquisition unit, used for responding to a code test instruction sent by the test terminal and acquiring a source code to be tested corresponding to the code test instruction; A source code plugging unit, used for plugging the source code to be tested to obtain a plugging code, and sending the plugging code to the test terminal; A test coverage acquisition unit, configured to receive test coverage data corresponding to the plugged code sent by the test terminal, determine a target test coverage result based on the test coverage data, and acquire a target data merging processing result corresponding to the target test coverage result; The merging result storage unit is used to store the target data merging processing result into a preset storage space.
9. A computer device, It is characterized in that The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the test code coverage detection method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the test code coverage detection method according to any one of claims 1 to 7 can be implemented.
