Code coverage rate testing method and device, electronic equipment and storage medium
By extracting the feature vectors of the code to be tested and generating test cases using large language models, the problem of low efficiency of manual design test cases in the prior art is solved, and automated testing of code coverage and efficiency improvement is achieved.
Patent Information
- Application Number
- CN202510189960.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-20
- Publication Date
- 2025-05-23
AI Technical Summary
In the prior art, the way of manually designing test cases based on the code to be tested is not intelligent enough and the testing efficiency is very low.
By extracting the feature vectors of the code to be tested, target test cases are generated using large language models or historical traffic data, and these use cases are executed to count code coverage.
It realizes automated testing of test codes, significantly improving testing efficiency.
Smart Images

Figure CN120029920A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code coverage testing method, device, electronic device and storage medium. Background Art
[0002] In the modern software development process, test coverage is one of the important indicators to measure software quality. High coverage testing can significantly reduce the probability of defects in the software, thereby improving the reliability and stability of the software. The traditional test coverage method generally requires testers to analyze the test code, manually design the corresponding test cases, and then conduct the corresponding tests.
[0003] However, this method of manually designing test cases based on the code to be tested is not smart enough and the testing efficiency is very low. Summary of the invention
[0004] The purpose of the embodiments of the present application is to provide a code coverage test method, device, electronic device and storage medium to solve the problem that the method of manually designing test cases according to the code to be tested is not intelligent enough and the test efficiency is very low. The specific technical solution is as follows:
[0005] In a first aspect, the present application provides a code coverage testing method, comprising:
[0006] In response to a code coverage test request, obtaining code to be tested;
[0007] Extracting a first code feature from the code to be tested;
[0008] Acquire a target test case corresponding to the code to be tested according to the first code feature;
[0009] Execute the target test case, and count the code coverage of the code to be tested after the target test case is executed.
[0010] In a possible implementation, obtaining a target test case corresponding to the code to be tested according to the first code feature includes:
[0011] Acquire a prompt word set, wherein the prompt word set includes a plurality of use case prompt words and a first feature vector corresponding to each use case prompt word;
[0012] In the prompt word set, searching for a use case prompt word corresponding to a first feature vector similar to the first code feature as a target prompt word;
[0013] The target prompt word is input into a large language model so that the large language model outputs a corresponding target test case.
[0014] In a possible implementation, obtaining a prompt word set includes:
[0015] Obtain the first flow data collected at historical time;
[0016] Classifying a plurality of first request data in the first traffic data to obtain request data sets of several categories;
[0017] For each category of request data set, determining a category data element corresponding to the category of request data set;
[0018] Constructing a category test case according to the category data element, and executing the category test case to obtain a corresponding first running code;
[0019] Generate a corresponding use case prompt word according to the test case, and extract a corresponding first feature vector according to the first running code;
[0020] The prompt word set is constructed according to all the use case prompt words and the first feature vector corresponding to each use case prompt word.
[0021] In a possible implementation, obtaining a target test case corresponding to the code to be tested according to the first code feature includes:
[0022] Obtain a test case set, wherein the test case set includes a plurality of test cases and a second feature vector corresponding to each test case;
[0023] In the test case set, a test case whose corresponding second feature vector is similar to the first code feature is searched as a target test case.
[0024] In a possible implementation, obtaining a test case set includes:
[0025] Obtain the second flow data collected at historical time;
[0026] For each second request data in the second traffic data, the second request data is used as a test case, and the test case is executed to obtain a corresponding second running code;
[0027] Collecting dot marking data from the second running code, wherein the dot marking data is used to characterize the state and parameters of key execution nodes of the code;
[0028] Determine the covered code covered when executing the test case in the dot marking data, and clear other codes except the covered code to obtain target dot marking data;
[0029] Extracting a second feature vector from the target dot data;
[0030] The test case set is constructed according to all the test cases and the second feature vector corresponding to each test case.
[0031] In a possible implementation, the method further includes:
[0032] At every preset time interval, obtaining scoring data corresponding to each test case in the test case set, wherein the scoring data includes coverage score, operation result score and efficiency score;
[0033] Performing a weighted sum operation on the coverage score, the operation result score, and the efficiency score to obtain a target score;
[0034] The test cases whose corresponding target scores are less than or equal to the preset score threshold are deleted from the test case set.
[0035] In a possible implementation, the method further includes:
[0036] When the code coverage is less than a preset standard value, obtaining the to-be-processed code that is not covered after the target test case is executed from the to-be-tested code;
[0037] Extracting a second code feature according to the code to be processed;
[0038] Acquire a first supplementary test case corresponding to the code to be processed according to the second code feature;
[0039] Execute the first supplementary test case, and count the code coverage of the code to be processed after completing the execution of the first supplementary test case.
[0040] In a second aspect, the present application provides a code coverage testing device, comprising:
[0041] A code acquisition module, used to obtain the code to be tested in response to a code coverage test request;
[0042] A feature extraction module, used to extract a first code feature from the code to be tested;
[0043] A test case acquisition module, used to acquire a target test case corresponding to the code to be tested according to the first code feature;
[0044] The test case execution module is used to execute the target test case and count the code coverage of the code to be tested after the target test case is executed.
[0045] In a possible implementation, the use case acquisition module is specifically used to:
[0046] Acquire a prompt word set, wherein the prompt word set includes a plurality of use case prompt words and a first feature vector corresponding to each use case prompt word;
[0047] In the prompt word set, searching for a use case prompt word corresponding to a first feature vector similar to the first code feature as a target prompt word;
[0048] The target prompt word is input into a large language model so that the large language model outputs a corresponding target test case.
[0049] In a possible implementation, the use case acquisition module is further used to:
[0050] Obtain the first flow data collected at historical time;
[0051] Classifying a plurality of first request data in the first traffic data to obtain request data sets of several categories;
[0052] For each category of request data set, determining a category data element corresponding to the category of request data set;
[0053] Constructing a category test case according to the category data element, and executing the category test case to obtain a corresponding first running code;
[0054] Generate a corresponding use case prompt word according to the test case, and extract a corresponding first feature vector according to the first running code;
[0055] The prompt word set is constructed according to all the use case prompt words and the first feature vector corresponding to each use case prompt word.
[0056] In a possible implementation, the use case acquisition module is further used to:
[0057] Obtain a test case set, wherein the test case set includes a plurality of test cases and a second feature vector corresponding to each test case;
[0058] In the test case set, a test case whose corresponding second feature vector is similar to the first code feature is searched as a target test case.
[0059] In a possible implementation, the use case acquisition module is further used to:
[0060] Obtain the second flow data collected at historical time;
[0061] For each second request data in the second traffic data, the second request data is used as a test case, and the test case is executed to obtain a corresponding second running code;
[0062] Collecting dot marking data from the second running code, wherein the dot marking data is used to characterize the state and parameters of key execution nodes of the code;
[0063] Determine the covered code covered when executing the test case in the dot marking data, and clear other codes except the covered code to obtain target dot marking data;
[0064] Extracting a second feature vector from the target dot data;
[0065] The test case set is constructed according to all the test cases and the second feature vector corresponding to each test case.
[0066] In a possible implementation, the device further includes a scoring module, which is used to:
[0067] At every preset time interval, obtaining scoring data corresponding to each test case in the test case set, wherein the scoring data includes coverage score, operation result score and efficiency score;
[0068] Performing a weighted sum operation on the coverage score, the operation result score, and the efficiency score to obtain a target score;
[0069] The test cases whose corresponding target scores are less than or equal to the preset score threshold are deleted from the test case set.
[0070] In a possible implementation manner, the apparatus further includes a use case supplement module, configured to:
[0071] When the code coverage is less than a preset standard value, obtaining the to-be-processed code that is not covered after the target test case is executed from the to-be-tested code;
[0072] Extracting a second code feature according to the code to be processed;
[0073] Acquire a first supplementary test case corresponding to the code to be processed according to the second code feature;
[0074] Execute the first supplementary test case, and count the code coverage of the code to be processed after completing the execution of the first supplementary test case.
[0075] In a third aspect, an electronic device is provided, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0076] Memory, used to store computer programs;
[0077] The processor is used to implement any method step described in the first aspect when executing a program stored in the memory.
[0078] In a fourth aspect, a computer-readable storage medium is provided, characterized in that a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any method step described in the first aspect is implemented.
[0079] In a fifth aspect, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute any of the above-mentioned code coverage testing methods.
[0080] Beneficial effects of the embodiments of the present application:
[0081] The embodiment of the present application provides a code coverage test method, device, electronic device and storage medium. In the embodiment of the present application, in response to a code coverage test request, first, the code to be tested is obtained, and a first code feature is extracted from the code to be tested, then, a target test case corresponding to the code to be tested is obtained according to the first code feature, and finally, the target test case is executed, and the code coverage of the code to be tested is counted after the target test case is executed. In this way, the code coverage of the code to be tested is automatically tested, thereby improving the test efficiency.
[0082] Of course, implementing any product or method of the present application does not necessarily require achieving all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0083] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0084] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0085] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.
[0086] Figure 1 A flowchart of a code coverage testing method provided in an embodiment of the present application;
[0087] Figure 2An architectural diagram of a test system provided in an embodiment of the present application;
[0088] Figure 3 A schematic diagram of a test case prompt word generation process provided in an embodiment of the present application;
[0089] Figure 4 A schematic diagram of a code feature extraction process provided in an embodiment of the present application;
[0090] Figure 5 A flowchart of another code coverage testing method provided in an embodiment of the present application;
[0091] Figure 6 A schematic diagram of the overall flow of code coverage testing provided in an embodiment of the present application;
[0092] Figure 7 A schematic diagram of the structure of a code coverage testing device provided in an embodiment of the present application;
[0093] Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0094] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0095] The disclosure below provides many different embodiments or examples to implement different structures of the present invention. In order to simplify the disclosure of the present invention, the parts and settings of specific examples are described below. Of course, they are only examples, and the purpose is not to limit the present invention. In addition, the present invention can repeat reference numbers and / or letters in different examples. This repetition is for the purpose of simplification and clarity, and does not itself indicate the relationship between the various embodiments and / or settings discussed.
[0096] Figure 1A schematic flow chart of a code coverage testing method provided in an embodiment of the present application. This method can be applied to one or more electronic devices such as smart phones, laptops, desktop computers, portable computers, servers, etc. In addition, the execution subject of this method can be hardware or software. When the above-mentioned execution subject is hardware, the execution subject can be one or more of the above-mentioned electronic devices. For example, a single electronic device can execute this method, or a plurality of electronic devices can cooperate with each other to execute this method. When the above-mentioned execution subject is software, this method can be implemented as multiple software or software modules, or as a single software or software module. It is not specifically limited here.
[0097] like Figure 1 As shown, the method specifically includes:
[0098] Step 101: In response to a code coverage test request, obtain code to be tested.
[0099] A code coverage testing method provided in an embodiment of the present application can be applied to Figure 2 The test system shown includes a front end for displaying corresponding functions to users, a server for providing corresponding services, and an infrastructure.
[0100] The above-mentioned code to be tested refers to the code that needs to be tested for code coverage.
[0101] In one embodiment, the code input by the user can be used as the code to be tested. In this way, the user can input the code to be tested according to actual needs.
[0102] In another embodiment, the code to be tested can be obtained by the following steps: obtaining the first request time of the previous code coverage test request and the second request time of the current code coverage test request; and taking the incremental code newly added between the first request time and the second request time as the code to be tested.
[0103] Through this solution, each time a request is received, the incremental code generated in the system between the current request and the previous request can be used as the code to be tested, thereby achieving coverage testing of the incremental code.
[0104] Step 102: extract a first code feature from the code to be tested.
[0105] In an embodiment of the present application, feature extraction can be performed by inputting the code to be tested into a model, for example, by using a GCN (Graph Convolutional Networks) model to extract context-related features in the call link, and to fuse the features of the calling and called methods to obtain a more comprehensive feature vector.
[0106] Step 103: Obtain a target test case corresponding to the code to be tested according to the first code feature.
[0107] Step 104: execute the target test case, and count the code coverage of the code to be tested after the target test case is executed.
[0108] For ease of understanding, step 103 and step 104 are described in a unified manner as follows:
[0109] In one embodiment, step 103 may specifically include the following steps:
[0110] Step A1: obtaining a prompt word set, wherein the prompt word set includes a plurality of use case prompt words and a first feature vector corresponding to each use case prompt word;
[0111] Step A2: in the prompt word set, searching for a use case prompt word corresponding to a first feature vector similar to the first code feature as a target prompt word;
[0112] Step A3: input the target prompt word into the large language model so that the large language model outputs the corresponding target test case.
[0113] The above prompt word set includes a number of pre-built use case prompt words for generating test cases, and code features of the corresponding running code when the test case generated by each use case prompt word is executed, that is, the first feature vector.
[0114] Specifically, obtaining a prompt word set may include the following steps:
[0115] Acquire first traffic data collected at historical time; classify multiple first request data in the first traffic data to obtain request data sets of several categories; for each category of request data set, determine the category data element corresponding to the request data set of the category; construct a category test case according to the category data element, and execute the category test case to obtain the corresponding first running code; generate corresponding use case prompt words according to the test case, and extract the corresponding first feature vector according to the first running code; construct the prompt word set according to all use case prompt words and the first feature vector corresponding to each use case prompt word.
[0116] In the application, all requests in the first traffic data can be classified by the service, interface method, interface path, public parameters, and business parameters corresponding to the request. Among them, the service: refers to classifying each service into one category. Request method: mainly divided into POST and GET. Request path: refers to each page has its own interface, and the path is the path of the interface. Correspondingly, the interface path classification includes: first-level classification: / page / page interface, / activity / activity interface; second-level classification: / page / home homepage, / page / topic special page, / activity / banner member marketing activity. Request parameters: include public parameters and business parameters. The parameter values are not fixed. The parameters of each business scenario are different. They can be classified according to the actual business scenarios, such as parameters representing sites, languages, devices, etc.
[0117] The category data element contains the common characteristics of the request data of the corresponding category, that is, the classification result, for example, "get, page service, page interface, home page, English".
[0118] In this scheme, the data requests in the traffic data are classified, the category features (i.e., category data elements) are extracted, and the corresponding test cases are generated according to the category data elements, and the code features (i.e., the first feature vector) of the code corresponding to the test case (i.e., the first running code) are extracted. Finally, the association relationship between the use case prompt (i.e., the use case prompt word) and the code features is established to obtain a prompt word set.
[0119] Figure 3 Generate a flow chart for the test case prompt words, such as Figure 3 As shown, the following steps are included:
[0120] (1) Traffic recording: Collect request traffic data from the online environment to provide raw materials for subsequent processing. This is the basic data source for use case generation. In the application, user traffic can be collected from service access logs through the Elastic Search cluster.
[0121] (2) Traffic cleaning: Clean and format the collected traffic data to remove noise and redundant data and unify the format. In applications, Pandas and other tools can be used for traffic cleaning. This ensures that the data meets the model training requirements and improves data quality.
[0122] (3) Traffic classification: Classify traffic according to certain rules or standards to assist in the accurate screening and combination of data when generating subsequent use cases, and enhance the pertinence and representativeness of use cases. In applications, decision tree algorithms (such as C4.5 and CART) can be used to classify traffic data based on multiple features (such as request method, request URL path, request parameters, etc.).
[0123] (4) Use case generation:
[0124] a. Use case assembly: Based on the traffic classification results, integrate relevant data elements and build complete test cases to ensure coverage of multiple scenarios and functional paths.
[0125] b. Test case prompt generation: Create prompt information for test cases to guide the test execution direction, help accurately verify functional characteristics, and improve test efficiency and accuracy.
[0126] Based on this, the code feature vector similarity search can be performed later to find the approximate code feature vector and the use case prompt (i.e., the target prompt word) corresponding to the code feature vector in the prompt word set. Then, the target prompt word is input into the LLM (Large Language Model) so that the large language model can output the corresponding target test case.
[0127] In another embodiment, step 103 may specifically include the following steps:
[0128] Step B1: obtaining a test case set, wherein the test case set includes a plurality of test cases and a second feature vector corresponding to each test case;
[0129] Step B2: In the test case set, search for a test case whose corresponding second feature vector is similar to the first code feature as a target test case.
[0130] The test case set includes several pre-collected test cases, and code features of the corresponding running code when each test case is executed, that is, the second feature vector.
[0131] Specifically, obtaining a test case set may include the following steps:
[0132] Acquire second traffic data collected at historical time; for each second request data in the second traffic data, take the second request data as a test case, execute the test case to obtain the corresponding second running code; collect dot marking data from the second running code, wherein the dot marking data is used to characterize the state and parameters of key execution nodes of the code; determine the covered code covered when executing the test case in the dot marking data, and clear other codes except the covered code to obtain target dot data; extract a second feature vector from the target dot data; construct the test case set according to all test cases and the second feature vector corresponding to each test case.
[0133] The above dot marking data is used to characterize the key code execution node status and parameters, such as class, method, code line, code branch, instruction. In the application, the code coverage SDK (Software Development Kit) can be used for marking.
[0134] Figure 4 The following is a schematic diagram of the code feature extraction process: Figure 4 As shown, the following steps are included:
[0135] (1) Traffic playback: using Figure 3 The cleaned traffic reproduces the previously recorded traffic scenario. In the application, the recorded traffic scenario is the request URL, and it can be reproduced by re-requesting the URL, so that the system processes the traffic again in a simulated environment, creating real operating conditions for feature extraction.
[0136] (2) Deployment services: Deploy related services in the test environment to ensure smooth traffic playback and use case execution, providing a stable operating foundation for subsequent operations.
[0137] (3) Execution of test cases: Run the generated test cases to drive the system to process traffic data, trigger code execution and function calls, and expose code operation characteristics.
[0138] (4) Downloading running code marking data: Collecting marking data from the running code. These data reflect the status and parameters of the key execution nodes of the code and provide a basis for feature extraction.
[0139] (5) Cleaning of running code and marking data: Purify the collected marking data, that is, only keep the code covered after the test case is run, refine it to specific classes / methods, and exclude classes / methods that are not covered, so as to eliminate errors or irrelevant data interference and ensure that the data truly and accurately reflects the code characteristics.
[0140] (6) Run code feature extraction: Based on the cleaned dotted data, assemble all covered class / method data into text and provide it to the GCN model for feature extraction, thereby constructing a feature vector.
[0141] (7) Feature vector storage: The extracted feature vectors are properly saved for subsequent model training, analysis, or evaluation, laying the foundation for in-depth insights into code behavior.
[0142] (8) Storage of the relationship between code features and test cases: Recording the relationship between code features and corresponding test cases facilitates tracing the source of features and evaluating the effectiveness of test cases, and assists in optimizing use case generation and feature extraction strategies.
[0143] Based on this, the code feature vector similarity retrieval can be performed later to query the approximate code feature vector and the test case corresponding to the code feature vector, that is, the target test case, in the test case set.
[0144] In addition, in another embodiment of the present application, the following steps may also be included: at preset time intervals, obtaining scoring data corresponding to each test case in the test case set, wherein the scoring data includes a coverage score, an operation result score, and an efficiency score; performing a weighted sum operation on the coverage score, the operation result score, and the efficiency score to obtain a target score; and deleting test cases whose corresponding target scores are less than or equal to a preset scoring threshold from the test case set.
[0145] In the embodiment of the present application, a reinforcement learning algorithm (such as Q-learning or deep Q network DQN) can be used to score the coverage, operation results and efficiency of each test case, and then the target score is calculated by the formula, that is, target score = coverage score * coverage ratio + operation result score * operation result ratio + efficiency score * efficiency ratio. Finally, by comparing the target score with the preset score threshold, the test cases with better performance are retained and the test cases with poor performance are eliminated, thereby ensuring the subsequent test effect.
[0146] In the application, automatic service deployment and incremental code statistics can be achieved through Kubernetes clusters, Jenkins clusters and Git services; Jacoco is used to implement running code tracking, coverage statistics and coverage report generation; Byte Code Engineering Library (BCEL) is used to analyze Java bytecode and generate method call links; the Graph Convolutional Network (GCN) model is used to extract context-related features of methods in the call link, and the features of the calling and called methods are integrated to obtain a more comprehensive feature vector; a relational database (MySQL) is used to store the relationship between traffic and features; and a vector database (Milvus) is used to store feature vectors.
[0147] In the embodiment of the present application, in response to a code coverage test request, first, the code to be tested is obtained, and a first code feature is extracted from the code to be tested, then, a target test case corresponding to the code to be tested is obtained according to the first code feature, and finally, the target test case is executed, and after the target test case is executed, the code coverage of the code to be tested is counted. In this way, automated code coverage testing of the code to be tested is achieved, thereby improving test efficiency.
[0148] See also Figure 5, is a flow chart of another embodiment of a code coverage testing method provided by the present application. Figure 5 As shown, the process may include the following steps:
[0149] Step 501: when the code coverage is less than a preset standard value, obtain the to-be-processed code that is not covered after the target test case is executed from the to-be-tested code;
[0150] Step 502: extracting a second code feature according to the code to be processed;
[0151] Step 503: Obtain a first supplementary test case corresponding to the code to be processed according to the second code feature;
[0152] Step 504: execute the first supplementary test case, and count the code coverage of the code to be processed after the first supplementary test case is executed.
[0153] For ease of understanding, steps 501 to 504 are described in a unified manner as follows:
[0154] In the embodiment of the present application, whether the code coverage rate reaches the preset standard value is used as a standard. When the code coverage rate does not reach the preset standard value, feature extraction is performed on the uncovered incremental code and new test cases are supplemented using LLM, and automated testing is performed again; when the code coverage rate reaches the preset standard value, the automated testing is considered to be completed, and finally a coverage report and a test report are generated.
[0155] In the application, during the execution of steps 501 to 504, it is necessary to determine whether the retry limit is reached. If not, the operation of supplementing the test case is repeated; if the retry limit is reached, a prompt for manually supplementing the test case is given.
[0156] In addition, in another embodiment of the present application, the method may also include the following steps: monitoring the execution process corresponding to the target test case, and when the execution process is interrupted, sending an abnormal reminder message to prompt the user to enter a supplementary prompt word, generating a second supplementary test case based on the supplementary prompt word, executing the second supplementary test case, and counting the code coverage of the code to be tested after executing the target test case.
[0157] With this solution, if an exception occurs during the execution of a test case, the user will be prompted to manually intervene and add a prompt, and then the test case will be re-executed, thus ensuring that the test can be completed smoothly.
[0158] Figure 6 This is a schematic diagram of the overall process of code coverage testing, such as Figure 6 As shown, the specific steps are as follows.
[0159] (1) The entire process starts with the requirement submission. First, incremental code statistics are performed, then features are extracted from the incremental code, and then the code feature vector is queried. Using this information, test case prompts are generated with the help of the language model (LLM), thereby generating test cases.
[0160] (2) After the test case is generated, the test process is triggered, first the service is deployed, and then the test case is executed. After the test case is executed, the code coverage is counted.
[0161] (3) If the coverage is greater than the standard value, a coverage report is generated, and then a test report is generated, and the entire process ends.
[0162] (4) If the coverage rate does not reach the standard value, additional test cases are required. This process generates a prompt for additional test cases through the language model (LLM), then extracts the features of the uncovered code, generates test cases again, and executes them. In this process, it is necessary to determine whether the retry limit has been reached. If not, repeat the operation of adding test cases; if the retry limit has been reached, a prompt for additional test cases is manually generated.
[0163] (5) In addition, if an interruption exception occurs during the execution of a test case, a reminder will be given and the test case will be re-executed.
[0164] Thus, the overall process of code coverage testing is realized.
[0165] Based on the same technical concept, the embodiment of the present application also provides a code coverage testing device, such as Figure 7 As shown, the device comprises:
[0166] A code acquisition module 71 is used to acquire the code to be tested in response to a code coverage test request;
[0167] A feature extraction module 72, used to extract a first code feature from the code to be tested;
[0168] A test case acquisition module 73, configured to acquire a target test case corresponding to the code to be tested according to the first code feature;
[0169] The test case execution module 74 is used to execute the target test case and count the code coverage of the code to be tested after the target test case is executed.
[0170] In a possible implementation, the use case acquisition module is specifically used to:
[0171] Acquire a prompt word set, wherein the prompt word set includes a plurality of use case prompt words and a first feature vector corresponding to each use case prompt word;
[0172] In the prompt word set, searching for a use case prompt word corresponding to a first feature vector similar to the first code feature as a target prompt word;
[0173] The target prompt word is input into a large language model so that the large language model outputs a corresponding target test case.
[0174] In a possible implementation, the use case acquisition module is further used to:
[0175] Obtain the first flow data collected at historical time;
[0176] Classifying a plurality of first request data in the first traffic data to obtain request data sets of several categories;
[0177] For each category of request data set, determining a category data element corresponding to the category of request data set;
[0178] Constructing a category test case according to the category data element, and executing the category test case to obtain a corresponding first running code;
[0179] Generate a corresponding use case prompt word according to the test case, and extract a corresponding first feature vector according to the first running code;
[0180] The prompt word set is constructed according to all the use case prompt words and the first feature vector corresponding to each use case prompt word.
[0181] In a possible implementation, the use case acquisition module is further used to:
[0182] Obtain a test case set, wherein the test case set includes a plurality of test cases and a second feature vector corresponding to each test case;
[0183] In the test case set, a test case whose corresponding second feature vector is similar to the first code feature is searched as a target test case.
[0184] In a possible implementation, the use case acquisition module is further used to:
[0185] Obtaining the second flow data collected at historical time;
[0186] For each second request data in the second traffic data, the second request data is used as a test case, and the test case is executed to obtain a corresponding second running code;
[0187] Collecting dot marking data from the second running code, wherein the dot marking data is used to characterize the state and parameters of key execution nodes of the code;
[0188] Determine the covered code covered when executing the test case in the dot marking data, and clear other codes except the covered code to obtain target dot marking data;
[0189] Extracting a second feature vector from the target dot data;
[0190] The test case set is constructed according to all the test cases and the second feature vector corresponding to each test case.
[0191] In a possible implementation, the device further includes a scoring module, which is used to:
[0192] At every preset time interval, obtaining scoring data corresponding to each test case in the test case set, wherein the scoring data includes coverage score, operation result score and efficiency score;
[0193] Performing a weighted sum operation on the coverage score, the operation result score, and the efficiency score to obtain a target score;
[0194] The test cases whose corresponding target scores are less than or equal to the preset score threshold are deleted from the test case set.
[0195] In a possible implementation manner, the apparatus further includes a use case supplement module, configured to:
[0196] When the code coverage is less than a preset standard value, obtaining the to-be-processed code that is not covered after the target test case is executed from the to-be-tested code;
[0197] Extracting a second code feature according to the code to be processed;
[0198] Acquire a first supplementary test case corresponding to the code to be processed according to the second code feature;
[0199] Execute the first supplementary test case, and count the code coverage of the code to be processed after completing the execution of the first supplementary test case.
[0200] Based on the same technical concept, the embodiment of the present application also provides an electronic device, such as Figure 8 As shown, it includes a processor 111, a communication interface 112, a memory 113 and a communication bus 114, wherein the processor 111, the communication interface 112, and the memory 113 communicate with each other through the communication bus 114.
[0201] Memory 113, used for storing computer programs;
[0202] The processor 111 is used to execute the program stored in the memory 113 to implement the following steps:
[0203] In response to a code coverage test request, obtaining code to be tested;
[0204] Extracting a first code feature from the code to be tested;
[0205] Acquire a target test case corresponding to the code to be tested according to the first code feature;
[0206] Execute the target test case, and count the code coverage of the code to be tested after the target test case is executed.
[0207] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0208] The communication interface is used for communication between the above electronic device and other devices.
[0209] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0210] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0211] In another embodiment provided in the present application, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned code coverage testing methods are implemented.
[0212] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute any code coverage testing method in the above embodiments.
[0213] The device embodiments described above are merely illustrative, wherein 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 modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0214] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0215] It should be understood that the terms used herein are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "one", "an" and "said" as used herein may also be meant to include plural forms. The terms "include", "comprise", "contain", and "have" are inclusive, and therefore specify the existence of stated features, steps, operations, elements and / or parts, but do not exclude the existence or addition of one or more other features, steps, operations, elements, parts, and / or combinations thereof. The method steps, processes, and operations described herein are not interpreted as necessarily requiring them to be performed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps may be used.
[0216] The foregoing is merely a specific embodiment of the present invention, which enables those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A code coverage testing method, characterized in that: The method comprises: In response to a code coverage test request, obtaining code to be tested; Extracting a first code feature from the code to be tested; Acquire a target test case corresponding to the code to be tested according to the first code feature; Execute the target test case, and count the code coverage of the code to be tested after the target test case is executed.
2. The method according to claim 1, characterized in that The acquiring a target test case corresponding to the code to be tested according to the first code feature includes: Acquire a prompt word set, wherein the prompt word set includes a plurality of use case prompt words and a first feature vector corresponding to each use case prompt word; In the prompt word set, searching for a use case prompt word corresponding to a first feature vector similar to the first code feature as a target prompt word; The target prompt word is input into a large language model so that the large language model outputs a corresponding target test case.
3. The method according to claim 2, characterized in that The step of obtaining a prompt word set includes: Obtain the first flow data collected at historical time; Classifying a plurality of first request data in the first traffic data to obtain request data sets of several categories; For each category of request data set, determining a category data element corresponding to the category of request data set; Constructing a category test case according to the category data element, and executing the category test case to obtain a corresponding first running code; Generate a corresponding use case prompt word according to the test case, and extract a corresponding first feature vector according to the first running code; The prompt word set is constructed according to all the use case prompt words and the first feature vector corresponding to each use case prompt word.
4. The method according to claim 1, characterized in that: The acquiring a target test case corresponding to the code to be tested according to the first code feature includes: Obtain a test case set, wherein the test case set includes a plurality of test cases and a second feature vector corresponding to each test case; In the test case set, a test case whose corresponding second feature vector is similar to the first code feature is searched as a target test case.
5. The method according to claim 4, characterized in that The obtaining of the test case set includes: Obtaining the second flow data collected at historical time; For each second request data in the second traffic data, the second request data is used as a test case, and the test case is executed to obtain a corresponding second running code; Collecting dot marking data from the second running code, wherein the dot marking data is used to characterize the state and parameters of key execution nodes of the code; Determine the covered code covered when executing the test case in the dot marking data, and clear other codes except the covered code to obtain target dot marking data; Extracting a second feature vector from the target dot data; The test case set is constructed according to all the test cases and the second feature vector corresponding to each test case.
6. The method according to claim 4, characterized in that The method further comprises: At every preset time interval, obtaining scoring data corresponding to each test case in the test case set, wherein the scoring data includes coverage score, operation result score and efficiency score; Performing a weighted sum operation on the coverage score, the operation result score, and the efficiency score to obtain a target score; The test cases whose corresponding target scores are less than or equal to the preset score threshold are deleted from the test case set.
7. The method according to claim 1, characterized in that The method further comprises: When the code coverage is less than a preset standard value, obtaining the to-be-processed code that is not covered after the target test case is executed from the to-be-tested code; Extracting a second code feature according to the code to be processed; Acquire a first supplementary test case corresponding to the code to be processed according to the second code feature; Execute the first supplementary test case, and count the code coverage of the code to be processed after completing the execution of the first supplementary test case.
8. A code coverage testing device, characterized in that: The device comprises: A code acquisition module, used to obtain the code to be tested in response to a code coverage test request; A feature extraction module, used to extract a first code feature from the code to be tested; A test case acquisition module, used to acquire a target test case corresponding to the code to be tested according to the first code feature; The test case execution module is used to execute the target test case and count the code coverage of the code to be tested after the target test case is executed.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the code coverage testing method according to any one of claims 1 to 7 when executing a program stored in a memory.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the code coverage testing method according to any one of claims 1 to 7 is implemented.