Test coverage optimization method and device and electronic equipment
By inserting callback statements at the code change position, the test coverage of the change method is determined, and the problem of low test coverage in the existing technology is solved, and more efficient test coverage optimization is achieved.
Patent Information
- Application Number
- CN202411395629.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-08
- Publication Date
- 2025-01-10
AI Technical Summary
In the prior art, it is difficult to improve the test coverage when code changes, mainly because developers and testers rely on manual summary of test scenarios, which can easily lead to incomplete scenarios and miss tests, resulting in low test coverage.
By obtaining the code change location, positioning the change method and inserting callback statements in it, the test case is executed to determine the test coverage, and the test case is adjusted to improve coverage.
The test coverage of the changed method was clarified, the test case adjustment was guided, the test coverage was improved, the human error was avoided, and the statistical and inspection work efficiency was improved.
Smart Images

Figure CN119292929A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of testing technology, and in particular to a test coverage optimization method, device and electronic equipment. Background Art
[0002] When a function is launched, confirming whether each line of modified code is fully covered by the test is an important part of the launch process. In the current implementation method, developers summarize test scenarios based on the modified code, testers summarize and output test cases, and test according to the test cases. This method based on manual and historical experience often has scenarios where the code is omitted and not covered. On the one hand, the test scenarios summarized by developers may not be fully covered. On the other hand, testers may miss test scenarios and fail to test them. In short, this link is currently constrained by work specifications. As long as it is manually controlled, there will always be omissions. The existing implementation method has the problem of incomplete test scenarios, which makes it difficult to improve test coverage. Summary of the invention
[0003] The purpose of the embodiments of the present invention is to provide a test coverage optimization method, device and electronic device, which are used to determine the test coverage of each change method based on a callback statement, thereby achieving the effect of improving the test coverage.
[0004] In a first aspect, the present invention provides a test coverage optimization method, the method comprising:
[0005] Get the change position of the current version code compared with the previous version code;
[0006] Locating a change method in the current version code based on the change location;
[0007] Inserting a callback statement in the change method;
[0008] The test cases written for the current version code are executed, and the test coverage of the changed method is determined based on the execution status of the callback statement. The test coverage of the changed method is used to guide the adjustment of the test cases.
[0009] In an optional implementation manner, the step of obtaining a change position of the current version code compared to the previous version code includes:
[0010] Compare all corresponding files of the current version code with the previous version code to determine files with differences;
[0011] For each file with differences, the difference lines in the file with differences are determined, and the line numbers of the difference lines are sequentially saved in an array.
[0012] In an optional implementation manner, the step of locating the change method in the current version code based on the change position includes:
[0013] Traverse each file in the current version of the code and determine whether the traversed files have been changed;
[0014] For a file with changes, determining a changed line number in the file based on the changed position;
[0015] Find the method node in the file, and determine the changed method from the method node according to the changed line number.
[0016] In an optional implementation, the step of determining the changed method from the method node according to the changed line number includes:
[0017] For each of the method nodes, determining the starting line number and the ending line number of the method node;
[0018] It is detected whether there is a line number between the start line number and the end line number in the changed line number, and if so, it is determined that the method node is a changed method.
[0019] In an optional implementation, the step of inserting a callback statement in the change method includes:
[0020] A start statement of the change method is determined, and a callback statement is inserted into the start statement.
[0021] In an optional implementation, the step of determining the test coverage of the change method based on the execution status of the callback statement includes:
[0022] If information returned when the callback statement is called and executed is generated, it is determined that the change method in which the callback statement is inserted is covered by the test;
[0023] If the information returned when the callback statement is called and executed is not generated, it is determined that the changed method in which the callback statement is inserted is not covered by the test.
[0024] In an optional embodiment, the method further comprises:
[0025] Collect statistics on all the changed methods and the information of the changed methods covered by the test among all the changed methods;
[0026] Generates coverage report based on information of all changed methods and changed methods covered by tests.
[0027] In an optional implementation, the information of the change method covered by the test includes the number of the change method covered by the test, the number of times the test was covered, the time point when the test was covered, and the IP address of the server where the test was covered.
[0028] In a second aspect, the present invention provides a test coverage optimization device, the device comprising:
[0029] The acquisition module is used to obtain the change position of the current version code compared with the previous version code;
[0030] A positioning module, used to locate the change method in the current version code based on the change position;
[0031] An insertion module, used for inserting a callback statement in the change method;
[0032] An execution module is used to execute the test cases written for the current version code, determine the test coverage of the changed method based on the execution of the callback statement, and the test coverage of the changed method is used to guide the adjustment of the test cases.
[0033] In a third aspect, the present invention provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores program instructions executable by the processor, and when the electronic device is running, the processor communicates with the storage medium via the bus, and the processor executes the program instructions to implement the test coverage optimization method as described in any one of the aforementioned embodiments.
[0034] The beneficial effects of the embodiments of the present invention include, for example:
[0035] The present invention provides a test coverage optimization method, device and electronic device, which obtain the change position of the current version code compared with the previous version code, locate the change method in the current version code based on the change position, and insert a callback statement in the change method. The test case written for the current version code is executed, and the test coverage of the change method is determined based on the execution of the callback statement. The test coverage of the change method is used to guide the adjustment of the test case. In this solution, by determining the change method and inserting the callback statement in the change method, the test coverage of each change method can be clearly obtained, and then the test case is adjusted based on the test coverage of each change method, so as to achieve the effect of improving the test coverage. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments of the present invention are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0037] Figure 1 A flow chart of a test coverage optimization method provided by an embodiment of the present invention;
[0038] Figure 2 for Figure 1 A flowchart of the sub-steps included in S11;
[0039] Figure 3 for Figure 1 A flowchart of the sub-steps included in S12;
[0040] Figure 4 for Figure 3 A flowchart of the sub-steps included in S123;
[0041] Figure 5 A functional module block diagram of a test coverage optimization device provided by an embodiment of the present invention;
[0042] Figure 6 This is a structural block diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0043] The technical solutions in the embodiments of the present invention will be described below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0044] Figure 1 The flowchart of the test coverage optimization method provided by the embodiment of the present invention, the execution subject of the test coverage optimization method can be a computer, server and other equipment, and the program script for implementing the test coverage optimization method of the present solution is deployed in the computer, server and the like. Figure 1 As shown, the method may include:
[0045] S11, obtaining the change position of the current version code compared with the previous version code.
[0046] S12: Locate a change method in the current version code based on the change position.
[0047] S13, inserting a callback statement into the change method.
[0048] S14, executing the test cases written for the current version of the code, determining the test coverage of the changed method based on the execution status of the callback statement, and the test coverage of the changed method is used to guide the adjustment of the test cases.
[0049] In this embodiment, the GIT code management repository can be used to first determine all the changed positions of the current version of the code by comparison based on the previous version of the code. All the changed positions here can be located in units of statements, that is, all the changed statements in the current version of the code compared to the previous version of the code are determined.
[0050] GIT is an open source distributed version control system. Its main function is to handle version control issues in software development. It can efficiently track file changes, record these changes, track every modification of the code, record historical versions, facilitate tracing and restoring old versions, and allow multiple people to collaborate on the same project. Through GIT, you can find the source code files and line locations of all the modified codes between each version.
[0051] In this embodiment, when performing test coverage statistics, the method can be used as a unit for statistics. Each version code includes multiple file nodes, each file node includes multiple method nodes, and each method node includes multiple statement nodes. When performing test coverage statistics, using method nodes as units can, on the one hand, accurately determine the test coverage of changes in each version code, and on the other hand, avoid the problem of too fine granularity leading to excessive workload.
[0052] Based on this, after determining the change position in the current version code, the change method in the current version code is determined in units of method nodes. In order to clarify whether each change method is covered during testing, in this embodiment, bytecode stub technology is used to insert callback statements in the change method.
[0053] Bytecode instrumentation is a technique for dynamically modifying bytecode during compilation. It allows developers to insert new code snippets into bytecode or replace existing code without changing the original source code. This technique is often used to implement functions such as logging, performance monitoring, exception handling, and modifying third-party library code.
[0054] The current version of the code is tested based on the test cases written by the tester. Since callback statements are inserted into each change method in the current version of the code, if a change method is executed, the callback statement in it will be called. Once the callback statement is called, the information of the call will be fed back. Therefore, the test coverage of the change method can be determined based on the execution of the callback statement.
[0055] Specifically, if the information returned when the callback statement is called and executed is generated, it is determined that the modified method with the callback statement inserted is covered by the test. Conversely, if the information returned when the callback statement is called and executed is not generated, it is determined that the modified method with the callback statement inserted is not covered by the test.
[0056] In this way, it is clear which of all the change methods are covered by the test and which are not. The obtained information is provided to the tester, who can clearly know what defects exist in the written test cases, and then provide the tester with reference information for adjusting the test cases, so as to achieve full test coverage of all change methods by adjusting the test cases in a targeted manner, and achieve the technical effect of improving test coverage.
[0057] The testing link targeted in this embodiment mainly refers to the integration testing link, wherein the testing link includes the unit testing link and the integration testing link. The unit testing link refers to the test code written and executed by the developer to verify the correctness and coding quality of the code, and is a test for the smallest testable unit in the software, such as a function, method or class. Its purpose is to ensure that each unit can work normally independently, thereby improving the quality, reliability and maintainability of the entire program. Unit testing focuses on verifying the correctness of a single component or functional module to ensure that each part is correct. Unit testing is performed in the early stages of the development process.
[0058] Integration testing refers to testing of integrated modules composed of multiple unit modules by an independent testing team, focusing on whether the interfaces and interactions between different units work as expected. It verifies whether the overall behavior of multiple components working together is as expected. Integration testing checks the integration between software modules to ensure that the various parts of the system can work together correctly. It covers the entire system or specific modules, detecting integration problems and interface problems to ensure the correctness, consistency and reliability of the entire system. Integration testing occurs when the basic development of the software is completed.
[0059] See also Figure 2 In this embodiment, when obtaining the change position of the current version code compared with the previous version code, the above can be achieved by:
[0060] S111, comparing the current version code with all corresponding files of the previous version code, and determining files with differences.
[0061] S112, for each file with differences, determining the difference lines in the file with differences, and saving the line numbers of the difference lines in order into an array.
[0062] In this embodiment, the process of determining the change location of the current version code compared to its previous version code is implemented during compilation. The client can scan during compilation to determine the change location, and then determine the change method. The change method is inserted into the callback statement by using the plug-in method, and then the test case is executed during the test run period to determine all covered change methods.
[0063] The pseudo code before the instrumentation is as follows:
[0064]
[0065] The pseudo code after instrumentation is as follows:
[0066]
[0067]
[0068] In this embodiment, by obtaining the GIT branch number of the current version code and the GIT branch number of the previous version code, and then randomly generating a unique package number, all changed files and code lines in the current version code compared to the previous version code are obtained through the JGIT tool library based on the local GIT repository.
[0069] In this embodiment, obtaining all difference files and line numbers of the difference lines between the current version code and the previous version code can be implemented by the pseudo code shown below:
[0070]
[0071]
[0072] As shown above, in this embodiment, a Map named modifiedFileMap may be created, which will store the file name as a key and a list of all difference line numbers in the file as a value.
[0073] By calling the getOldBranchName() and getCurrentBranchName() functions, you can get the names of the two branches to be compared, that is, the name of the current version of the code and the name of the previous version of the code.
[0074] Build a local Git repository reference through the buildRepostiory() function. This is usually implemented using the JGit library and is built based on the path to the local repository.
[0075] Use the git.diff() method to get the difference between two branches. Use the setNewTree() and setOldTree() methods to specify the current version code branch and the previous version code branch, and use setMatchPath("*java") to specify that only files with the suffix .java are compared. Call the call() method to perform the comparison operation and return a list of all the difference files.
[0076] Iterates through the returned diffEntrys list, processing each difference file. For each file, it iterates through all the difference lines of the file and stores the line number in a temporary array lineNumberArray.
[0077] Sort lineNumberArray to ensure that the line numbers are in ascending order.
[0078] Add the list of file names and corresponding line numbers as key-value pairs to modifiedFileMap.
[0079] Finally, this code returns a modifiedFileMap containing all the difference files and their line numbers.
[0080] In this embodiment, by using the above method, files with differences between the current version code and the previous version code, as well as difference lines in the files with differences, can be obtained.
[0081] On this basis, see Figure 3 In this embodiment, the above step of locating the change method in the current version code based on the change position can be implemented in the following way:
[0082] S121, traverse each file in the current version code, and determine whether the traversed file has been changed.
[0083] S122, for a file with changes, determining a changed line number in the file based on the change position.
[0084] S123, searching for a method node in the file, and determining a changed method from the method node according to the changed line number.
[0085] In this embodiment, all codes in the current version code are divided into multiple files, and the structure of the code in each file is an AST (Abstract Syntax Tree) syntax tree structure. The abstract syntax tree is an abstract representation of the syntax structure of the source code, which presents the syntax structure of the programming language in a tree form. In the field of programming, AST is widely used in compiler design, static code analysis, code conversion and reconstruction, etc. By constructing AST, each component in the source code, such as declaration statements, assignment statements, operation statements, etc., can be accurately located and operated, thereby achieving various advanced programming tasks.
[0086] For each file traversed, we can first determine whether there are difference lines in the traversed file, which can be achieved through the line numbers of the difference lines of each file saved above. For example, if the line number of the difference line corresponding to a file is empty, it means that there are no changed lines in the file, and the file can be skipped to process the next file. If the line number of the difference number corresponding to the file is not empty, the change method in the method node can be determined based on the specific change line number.
[0087] Specifically, see Figure 4 In this embodiment, the implementation method of determining the changed method from the method node according to the changed line number is as follows:
[0088] S1231: For each of the method nodes, determine the start line number and the end line number of the method node.
[0089] S1232, detecting whether there is a line number between the start line number and the end line number in the changed line number, and if so, determining that the method node is a changed method.
[0090] Each method node in each version code consists of multiple lines of code, so each method node has a corresponding start line number and end line number from the start code to the end code. The changed line number is recorded in the change position. Therefore, for each method node, if the changed line number contains a line number between the start line number and the end line number of the method node, it indicates that there are changed lines of code in all the codes included in the method node. Therefore, it can be determined that the method node is a changed method.
[0091] On this basis, a callback statement may be inserted into the change method. Specifically, a start statement of the change method may be determined, and the callback statement may be inserted into the start statement.
[0092] In this embodiment, the above-mentioned determination of the change method and the insertion of the callback statement in the change method can be implemented by the following pseudo code:
[0093]
[0094]
[0095] In the pseudo-code implementation shown above, the functions are Java compilation units (JCCompilationUnit), which represent the abstract syntax tree (AST) of the Java source file. The purpose of the code is to inject additional code statements into the Java method at a specific line number. The injection here means inserting a callback statement at the beginning of each modified method, which triggers a pre-defined function.
[0096] Specifically, first initialize the variable jcCompilationUnits, which is an array of AST syntax trees of all Java files to be analyzed, obtained from the parameters of the JAVA bytecode instrumentation extension point callback function. modifiedFileMap, which is a Map, where the key is the file name with differences and the value is an array of line numbers with differences in the file. injectedStatment, which is an AST method node of the callback statement that needs to be injected.
[0097] The AST of each Java source code file is traversed based on the following function: for(var jcCompilationUnit injcCompilationUnits).
[0098] For each file, check if there is a line number that has been modified in that file: var lineArray = modifiedFileMap.get(jcCompilationUnit.getFileName()).
[0099] If the file has been modified, iterate over the methods in the file: for(var jcTree injcCompilationUnit.getClassChild().defs).
[0100] Check whether the AST node is a method node: if (jcTree.getKind() == Tree.Kind.METHOD).
[0101] Iterate over each modified line number and find out if they are within the range of the method node: for(varlineNo in lineArray).
[0102] If the line number is within the range of the method node, insert the injected code statement at the beginning of the method node: jcTree.appendFirstStatments(injectedStatment,jcCompilationUnit.getFileName(),jcTree.getMethodName()).
[0103] In this embodiment, all changed methods in the current version of the code can be obtained during the compilation period, and the changed methods can be plugged in during the integration test period, and the method change methods covered by the test can be determined.
[0104] On this basis, information about all change methods and the change methods covered by the test among all change methods can be counted, and a coverage report can be generated based on the information about all change methods and the change methods covered by the test.
[0105] In this embodiment, the generated information of the changed method covered by the test includes the number of the method covered by the test, the number of times the test was covered, the time point when the test was covered, the IP address of the server where the test was covered, etc.
[0106] The test coverage optimization method provided in this embodiment aims at the defect that the integrated test link does not have a test coverage statistics function. It uses technical means from the perspective of source code to replace the original test case-based method coverage, which can avoid human errors and improve the efficiency of statistics and inspection work. For testers, they can refer to the method coverage to check for omissions in the written test cases. For those responsible for the system, they can know the changes in each version and the completion of test coverage. For those responsible for production quality, they can use the audit reference items of the test quality link in the release review.
[0107] Figure 5 The functional module block diagram of the test coverage optimization device provided in the embodiment of the present invention, the functions implemented by the test coverage optimization device correspond to the steps performed by the above method. The device can be understood as the above-mentioned computer device or server, or the processor of the server, or can also be understood as a component independent of the above-mentioned server or processor that implements the functions of the present invention under the control of the server, such as Figure 5 As shown, the device may include: an acquisition module, a positioning module, an insertion module and an execution module.
[0108] The acquisition module is used to obtain the change position of the current version code compared with the previous version code;
[0109] A positioning module, used to locate the change method in the current version code based on the change position;
[0110] An insertion module, used for inserting a callback statement in the change method;
[0111] An execution module is used to execute the test cases written for the current version code, determine the test coverage of the changed method based on the execution of the callback statement, and the test coverage of the changed method is used to guide the adjustment of the test cases.
[0112] As a possible implementation method, the above acquisition module can be specifically used for:
[0113] Compare all corresponding files of the current version code with the previous version code to determine files with differences;
[0114] For each file with differences, the difference lines in the file with differences are determined, and the line numbers of the difference lines are sequentially saved in an array.
[0115] As a possible implementation, the above positioning module can be used for:
[0116] Traverse each file in the current version of the code and determine whether the traversed files have been changed;
[0117] For a file with changes, determining a changed line number in the file based on the changed position;
[0118] Find the method node in the file, and determine the changed method from the method node according to the changed line number.
[0119] As a possible implementation, the positioning module may be specifically used for:
[0120] For each of the method nodes, determining the starting line number and the ending line number of the method node;
[0121] It is detected whether there is a line number between the start line number and the end line number in the changed line number, and if so, it is determined that the method node is a changed method.
[0122] As a possible implementation, the above-mentioned insertion module can be used for:
[0123] A start statement of the change method is determined, and a callback statement is inserted into the start statement.
[0124] As a possible implementation, the above execution module may be used to:
[0125] If information returned when the callback statement is called and executed is generated, it is determined that the change method in which the callback statement is inserted is covered by the test;
[0126] If the information returned when the callback statement is called and executed is not generated, it is determined that the changed method in which the callback statement is inserted is not covered by the test.
[0127] As a possible implementation, the test coverage optimization device may further include a statistical module, which may be used to:
[0128] Collect statistics on all the changed methods and the information of the changed methods covered by the test among all the changed methods;
[0129] Generates coverage report based on information of all changed methods and changed methods covered by tests.
[0130] As a possible implementation manner, the information of the change method covered by the test includes the number of the change method covered by the test, the number of times the test was covered, the time point when the test was covered, and the IP address of the server where the test was covered.
[0131] The above-mentioned device is used to execute the method provided by the aforementioned embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0132] Figure 6 This is a structural block diagram of an electronic device provided in an embodiment of the present invention, and the electronic device may be a computer device with a data processing function. The electronic device includes a processor and a storage medium.
[0133] The storage medium is used to store the program, and the processor calls the program stored in the storage medium to execute the above method embodiment. The specific implementation method and technical effect are similar and will not be repeated here.
[0134] The processor may be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The processor may implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present invention may be directly embodied as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.
[0135] As a non-volatile computer-readable storage medium, the storage medium can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The storage medium may include at least one type of storage medium, such as flash memory, hard disk, multimedia card, card-type storage medium, random access storage medium (Random Access Memory, RAM), static random access storage medium (Static Random Access Memory, SRAM), programmable read-only storage medium (Programmable Read Only Memory, PROM), read-only storage medium (Read Only Memory, ROM), electrically erasable programmable read-only storage medium (Electrically Erasable Programmable Read-Only Memory, EEPROM), magnetic storage medium, disk, optical disk, etc. The storage medium is any other medium that can be used to carry or store the desired program code in the form of an instruction or data structure and can be accessed by a computer, but is not limited thereto. The storage medium in the embodiment of the present invention can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.
[0136] Optionally, the present invention also provides a program product, such as a computer-readable storage medium, comprising a program, which is used to execute the above method embodiment when executed by a processor.
[0137] In summary, the test coverage optimization method, device, and electronic device provided in the embodiments of the present invention obtain the change position of the current version code compared to the previous version code, locate the change method in the current version code based on the change position, and insert a callback statement in the change method. The test case written for the current version code is executed, and the test coverage of the change method is determined based on the execution of the callback statement. The test coverage of the change method is used to guide the adjustment of the test case. In this solution, by determining the change method and inserting the callback statement in the change method, the test coverage of each change method can be clearly obtained, and then the test case is adjusted based on the test coverage of each change method, so as to achieve the effect of improving the test coverage.
[0138] In the embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. 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. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0139] In addition, the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0140] Furthermore, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.
[0141] It should be noted that if the function is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, and other media that can store program codes.
[0142] In this document, relational terms such as first and second, etc. are used merely to distinguish one entity or operation from another entity or operation, but do not necessarily require or imply any such actual relationship or order between these entities or operations.
[0143] The above description is only an embodiment of the present invention and is not intended to limit the protection scope of the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A test coverage optimization method, characterized in that: The method comprises: Get the change position of the current version code compared with the previous version code; Locating a change method in the current version code based on the change location; Inserting a callback statement in the change method; The test cases written for the current version code are executed, and the test coverage of the changed method is determined based on the execution status of the callback statement. The test coverage of the changed method is used to guide the adjustment of the test cases.
2. The test coverage optimization method according to claim 1, characterized in that: The step of obtaining the change position of the current version code compared with the previous version code includes: Compare all corresponding files of the current version code with the previous version code to determine files with differences; For each file with differences, the difference lines in the file with differences are determined, and the line numbers of the difference lines are sequentially saved in an array.
3. The test coverage optimization method according to claim 1, characterized in that: The step of locating the change method in the current version code based on the change position includes: Traverse each file in the current version of the code and determine whether the traversed files have been changed; For a file with changes, determining a changed line number in the file based on the changed position; Find the method node in the file, and determine the changed method from the method node according to the changed line number.
4. The test coverage optimization method according to claim 3, characterized in that: The step of determining the changed method from the method node according to the changed line number comprises: For each of the method nodes, determining the starting line number and the ending line number of the method node; It is detected whether there is a line number between the start line number and the end line number in the changed line number, and if so, it is determined that the method node is a changed method.
5. The test coverage optimization method according to claim 1, characterized in that: The step of inserting a callback statement in the change method comprises: A start statement of the change method is determined, and a callback statement is inserted into the start statement.
6. The test coverage optimization method according to claim 1, characterized in that: The step of determining the test coverage of the change method based on the execution status of the callback statement includes: If information returned when the callback statement is called and executed is generated, it is determined that the change method in which the callback statement is inserted is covered by the test; If the information returned when the callback statement is called and executed is not generated, it is determined that the changed method in which the callback statement is inserted is not covered by the test.
7. The test coverage optimization method according to claim 1, characterized in that: The method further comprises: Collect statistics on all the changed methods and the information of the changed methods covered by the test among all the changed methods; Generates coverage report based on information of all changed methods and changed methods covered by tests.
8. The test coverage optimization method according to claim 7, characterized in that: The information of the changed method covered by the test includes the number of the changed method covered by the test, the number of times the changed method was covered by the test, the time point when the changed method was covered by the test, and the IP address of the server where the changed method was covered by the test.
9. A test coverage optimization device, characterized in that: The device comprises: The acquisition module is used to obtain the change position of the current version code compared with the previous version code; A positioning module, used to locate the change method in the current version code based on the change position; An insertion module, used for inserting a callback statement in the change method; An execution module is used to execute the test cases written for the current version code, determine the test coverage of the changed method based on the execution of the callback statement, and the test coverage of the changed method is used to guide the adjustment of the test cases.
10. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores program instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium via the bus, and the processor executes the program instructions to implement the test coverage optimization method as described in any one of claims 1 to 8.