Code testing method, device, equipment and storage medium
By receiving and analyzing the test tags in the source code, calculating the coverage and generating test scripts, the untested code fragments are automatically tested, which solves the low efficiency problem in the existing technology and achieves more efficient code test coverage.
Patent Information
- Application Number
- CN202210424700.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-22
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2042-04-22
AI Technical Summary
Existing code testing methods are inefficient and difficult to efficiently locate and test untested code areas.
By receiving and analyzing the test tags in the source code, calculating the test coverage, identifying the untested code fragments, and using the script generation network model to generate and execute the test scripts corresponding to the annotation information, the untested code can be automatically tested.
Improves the efficiency and coverage of code testing, ensuring that all code snippets are fully tested.
Smart Images

Figure CN114817025B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of testing, in particular to a code testing method, device, equipment and storage medium. BACKGROUND
[0002] In order to ensure the stability of software engineering, the development of software engineering is usually based on a stable baseline version, and a new branch is developed to realize new functions. When a new branch of a software engineering is generated, the new branch needs to be tested to confirm that the new branch can realize the expected function. In order to measure whether the test of the new branch is sufficient, the number of code lines, classes, branches and the like covered by the test can be counted to quantify the sufficiency of this test. This quantified sufficiency is the test coverage rate. In order to facilitate the test personnel to adjust the test cases according to the test coverage rate of the new branch, it is necessary to mark the test coverage rate of the new branch in the test coverage rate report generated according to the test.
[0003] The existing code testing method usually locates the untested code area based on manual code inspection, and then tests it to improve the code coverage rate. However, this method based on manual positioning and testing is particularly inefficient. SUMMARY
[0004] The main purpose of the present application is to solve the problem of low efficiency of the existing code testing method.
[0005] The first aspect of the present application provides a code testing method, comprising:
[0006] Receiving the source code of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying a code segment that has been tested;
[0007] According to the source code of each application project and the test mark of each application project, the test coverage rate of each application project is calculated respectively;
[0008] Based on the test coverage rate of each application project, a target application project with a test coverage rate less than a preset threshold is determined;
[0009] Traversing the source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark;
[0010] Obtaining a target code annotation text corresponding to the target code segment from a preset annotation library;
[0011] The target code annotation text is processed based on a preset first script generation network model to obtain and execute a first target test script corresponding to the target code segment.
[0012] Optionally, in the first implementation manner of the first aspect, the test coverage of each application project is calculated according to the source code of each application project and the test mark of each application project, and the test coverage of each application project is calculated according to the total number of code segments of each application project and the number of tested code segments containing the test mark.
[0013] The total number of code segments of each application project and the number of tested code segments containing the test mark are obtained respectively.
[0014] The number of tested code segments containing the test mark in each application project is divided by the total number of code segments to obtain the test coverage of each application project.
[0015] Optionally, in the second implementation manner of the first aspect, before the target code annotation text is processed based on the preset first script generation network model to obtain and execute the first target test script corresponding to the target code segment, the method further includes:
[0016] An initial code training data set for model training is constructed based on a preset common script code containing annotation information.
[0017] The initial code training data set is preprocessed to obtain a target code training data set.
[0018] The network parameters of a preset language network model are initialized.
[0019] The target code training data set is input into the preset language network model to perform script generation training to obtain a first script generation network model.
[0020] Optionally, in the third implementation manner of the first aspect, the target code training data set is input into the preset language network model to perform script generation training to obtain a first script generation network model.
[0021] A target code annotation pair is obtained from the target code training data set, and the target code annotation pair includes a code segment and corresponding target annotation information of the code segment.
[0022] The target generated code corresponding to the target annotation information is predicted based on the preset language network model.
[0023] A loss value between the target generated code and the code segment in the target code annotation pair is calculated according to a preset loss function.
[0024] According to the loss value, a network parameter of the preset language network model is adjusted to obtain a first script generation network model.
[0025] Optionally, in a fourth implementation manner of the first aspect, the adjusting the network parameter of the preset language network model according to the loss value to obtain the first script generation network model comprises:
[0026] The loss value is back propagated from an output layer of the preset language network model to a hidden layer;
[0027] When the loss value is propagated to the hidden layer, the network parameter of the preset language network model is iteratively updated according to the loss value and by using a stochastic gradient descent algorithm;
[0028] When the network of the preset language network model converges, a current network parameter of the preset language network model is determined as a target parameter to obtain the first script generation network model.
[0029] Optionally, in a fifth implementation manner of the first aspect, after the first script generation network model based on the preset is used to process the target code comment text to obtain and execute a first target test script corresponding to the target code segment, the method further comprises:
[0030] At least one test case corresponding to the target code segment is obtained from a preset logic library;
[0031] A second script generation network model based on the preset is used to process the at least one test case corresponding to the target code segment to obtain and execute a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating a test script based on a test case.
[0032] Optionally, in a sixth implementation manner of the first aspect, before the at least one test case corresponding to the target code segment is obtained from the preset logic library, the method further comprises:
[0033] A plurality of logic nodes corresponding to each code segment in each application project and an arrangement order thereof are obtained;
[0034] Based on the plurality of logic nodes corresponding to each code segment in each application project and the arrangement order thereof, a directed acyclic graph corresponding to each code segment in each application project is constructed, wherein the directed acyclic graph is used to represent a code logic of a code segment, the directed acyclic graph comprises a plurality of graph element nodes and directed edges connecting the graph element nodes, each graph element node corresponds to each logic node, and the directed edges are used to indicate an execution order of the logic nodes.
[0035] perform path traversal on each of the directed acyclic graphs, and determine each path traversed as a test case, to obtain at least one test case corresponding to each code snippet in each of the application projects;
[0036] According to the application project to which each code snippet belongs, the at least one test case corresponding to each code snippet is stored in the logic library in a classified manner.
[0037] The second aspect of the present application provides a code testing device, comprising:
[0038] a code receiving module configured to receive source codes of a plurality of application projects, wherein the source code of each application project includes a test mark used to identify a code snippet that has been tested;
[0039] a coverage calculating module configured to calculate a test coverage of each of the application projects according to the source code of each application project and the test mark of each application project;
[0040] a target determining module configured to determine a target application project with a test coverage less than a preset threshold based on the test coverage of each of the application projects;
[0041] a code traversal module configured to traverse the source code of the target application project to obtain a target code snippet in the target application project that does not contain the test mark;
[0042] a comment obtaining module configured to obtain a target code comment text corresponding to the target code snippet from a preset comment library;
[0043] a script generating module configured to process the target code comment text based on a preset first script generation network model to obtain and execute a first target test script corresponding to the target code snippet, wherein the first script generation network model is a network model for generating a test script based on comment information.
[0044] Optionally, in the first implementation manner of the second aspect of the present application, the coverage calculating module specifically comprises:
[0045] an obtaining unit configured to obtain a total number of code snippets and a number of tested code snippets containing the test mark of each of the application projects respectively;
[0046] a calculating unit configured to perform division operation on the number of tested code snippets containing the test mark and the total number of code snippets in each of the application projects respectively to obtain the test coverage of each of the application projects.
[0047] Optionally, in the second implementation manner of the second aspect of the present application, the device further comprises a model training module, and the model training module specifically comprises:
[0048] a data set construction module, configured to construct an initial code training data set for model training based on a preset public script code containing annotation information;
[0049] a preprocessing module, configured to perform data preprocessing on the initial code training data set to obtain a target code training data set;
[0050] an initialization module, configured to initialize network parameters of a preset language network model;
[0051] a model generation module, configured to input the target code training data set into the preset language network model to perform script generation training to obtain a first script generation network model.
[0052] Optionally, in a third implementation manner of the second aspect of the present application, the model generation module is specifically configured to:
[0053] obtain a target code annotation pair from the target code training data set, wherein the target code annotation pair includes a code snippet and corresponding target annotation information of the code snippet;
[0054] predict a target generated code corresponding to the target annotation information based on the preset language network model;
[0055] calculate a loss value between the target generated code and the code snippet in the target code annotation pair according to a preset loss function;
[0056] adjust network parameters of the preset language network model according to the loss value to obtain a first script generation network model.
[0057] Optionally, in a fourth implementation manner of the second aspect of the present application, the model generation module is specifically configured to:
[0058] obtain a target code annotation pair from the target code training data set, wherein the target code annotation pair includes a code snippet and corresponding target annotation information of the code snippet;
[0059] predict a target generated code corresponding to the target annotation information based on the preset language network model;
[0060] calculate a loss value between the target generated code and the code snippet in the target code annotation pair according to a preset loss function;
[0061] perform back propagation of the loss value from an output layer to a hidden layer of the preset language network model;
[0062] When the loss value is propagated to the hidden layer, network parameters of the preset language network model are iteratively updated according to the loss value and by using a stochastic gradient descent algorithm.
[0063] When the network of the preset language network model converges, the network parameters of the current preset language network model are determined as target parameters, and the first script generation network model is obtained.
[0064] Optionally, in a fifth implementation manner of the second aspect of the present application, the device further comprises an additional test module, which specifically comprises:
[0065] a test case acquisition unit, configured to acquire at least one test case corresponding to the target code segment from a preset logic library;
[0066] a generation unit, configured to process the at least one test case corresponding to the target code segment based on a preset second script generation network model, to obtain and execute a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating a test script based on a test case.
[0067] Optionally, in a sixth implementation manner of the second aspect of the present application, the additional test module specifically comprises:
[0068] a logic acquisition unit, configured to acquire a plurality of logic nodes and their arrangement orders corresponding to each code segment in each application project;
[0069] a loop-free graph construction unit, configured to construct a directed acyclic graph corresponding to each code segment in each application project based on the plurality of logic nodes and their arrangement orders corresponding to each code segment in each application project, wherein the directed acyclic graph is used to represent the code logic of the code segment, the directed acyclic graph comprises a plurality of graph element nodes and directed edges connecting the graph element nodes, each graph element node corresponds to each logic node, and the directed edges are used to indicate the execution order of the logic nodes;
[0070] a traversal unit, configured to perform path traversal on each directed acyclic graph, and determine each path traversed as a test case, to obtain at least one test case corresponding to each code segment in each application project;
[0071] a storage unit, configured to store the at least one test case corresponding to each code segment in the logic library according to the application project to which each code segment belongs;
[0072] a test case acquisition unit, configured to acquire at least one test case corresponding to the target code segment from a preset logic library;
[0073] The generating unit is configured to generate a network model based on a preset second script, process at least one test case corresponding to the target code segment, and obtain and execute a second target test script corresponding to the target code segment, wherein the second script is a network model for generating a test script based on a test case.
[0074] The third aspect of the present application provides a code testing device, comprising a memory and at least one processor, the memory storing instructions; the at least one processor invokes the instructions in the memory to enable the code testing device to perform the code testing method described above.
[0075] The fourth aspect of the present application provides a computer readable storage medium, the computer readable storage medium storing instructions, when the instructions are run on a computer, enabling the computer to perform the code testing method described above.
[0076] In the technical solution provided by the present application, the code test coverage of each application project is calculated, and the target application project with a test coverage less than a threshold and the target code segment without a test mark in the target application project are determined according to the calculated test coverage, and then the annotation information corresponding to the target code segment is obtained, and finally the test script corresponding to the annotation information is directly generated and executed based on a script generation network model to test the untested code, thereby improving the test efficiency and the code test coverage. BRIEF DESCRIPTION OF DRAWINGS
[0077] Figure 1 The first embodiment of the code testing method in the present application is shown in the figure;
[0078] Figure 2 The second embodiment of the code testing method in the present application is shown in the figure;
[0079] Figure 3 The third embodiment of the code testing method in the present application is shown in the figure;
[0080] Figure 4 The first embodiment of the code testing device in the present application is shown in the figure;
[0081] Figure 5 The second embodiment of the code testing device in the present application is shown in the figure;
[0082] Figure 6 The first embodiment of the code testing device in the present application is shown in the figure. DETAILED DESCRIPTION
[0083] The terms "first", "second", "third", "fourth" and the like in the description and in the claims of the present application, and above-described drawings, if any, are used to distinguish between similar objects and are not necessarily used to describe a particular sequential or chronological order. It is to be understood that the use of the terms so construed herein can be interchanged, under suitable circumstances, to describe the embodiments of the application described herein in other than the order described herein. Furthermore, the term "comprising" or "having" and any variations thereof, is intended to cover a non-exclusive inclusion, for example, a process, method, system, product or apparatus that comprises a list of steps or units not necessarily limited to those explicitly stated, but can include other steps or units not expressly listed or inherent to such process, method, product or apparatus.
[0084] The present application can acquire and process related data based on artificial intelligence technology. Artificial intelligence (AI) is a theory, method, technology and application system for simulating, extending and expanding human intelligence using a digital computer or a machine controlled by a digital computer, perceiving the environment, acquiring knowledge and using the knowledge to obtain the best results.
[0085] The server in the present application can be a stand-alone server, or a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and basic cloud computing services such as big data and artificial intelligence platforms.
[0086] For ease of understanding, the specific flow of the embodiments of the present application is described below. Please refer to Figure 1 An embodiment of the code testing method in the embodiments of the present application includes:
[0087] 101. Receiving source code of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying that a code segment has been tested;
[0088] It can be understood that the plurality of application projects can be projects of different platform architectures, such as Android projects, Kotlin projects, Flutter projects, Java projects, etc. In the project development process, the project source code is packaged and uploaded to a unified code management platform. For example, for CICD pipeline projects, a hook function is added in the project, so that the project source code is uploaded to the code management platform without invasion; for non-pipeline projects, the server can complete the packaging of the project through a script program, and upload it to the code management platform. The uploading method in the script program can be timed uploading or uploading based on code volume, which is not limited in the present embodiment.
[0089] Optionally, the server can also upload the specified project code to the code management platform based on code instrumentation. Specifically, for example, for Java projects, the javaagent and jacoco plug-ins are called to instrument the code when the Java virtual machine is started, and the client-server mode is called to send the project and version information directly to the code management platform through a scheduled task; for Kotlin and Android projects, the jacoco plug-in is called to instrument the project and package it into an apk package, which is then sent to the code management platform along with its version information; for Flutter projects, the AspectD plug-in is used to instrument the Dart file code, and the instrumented data is uniformly collected as variables, which are then sent to the code management platform through a scheduled task.
[0090] Optionally, after the server receives the source codes of multiple application projects, the server also performs a legality check and a consistency check on the data of each application project, thereby improving data security and integrity.
[0091] 102. Calculate the test coverage of each application project based on the source code of each application project and the test tag of each application project;
[0092] It is understood that each application project includes a test tag used to identify a code snippet as having been tested, i.e., each test tag corresponds to a code snippet. Specifically, the test tag can be a pair of identifiers: a paragraph start identifier for indicating the beginning of a paragraph and a paragraph end identifier for indicating the end of a paragraph, located at the beginning and end of the code snippet, respectively.
[0093] Specifically, the server first obtains the total number of code snippets of each application project and the number of tested code snippets containing the test mark, and then divides the number of tested code snippets containing the test mark in each application project by the total number of code snippets, so as to obtain the test coverage of each application project, that is, the test coverage is equal to the number of tested code snippets divided by the total number of code snippets.
[0094] 103. Based on the test coverage of each application project, determine the target application project whose test coverage is less than a preset threshold;
[0095] It is understood that the target application project is an application project whose test coverage is less than a preset threshold. This threshold can be adjusted according to actual business needs, and the preferred threshold is 100%. Generally, the test coverage of an application project should be 100%, that is, every code segment must be tested. In some cases, achieving a coverage of more than 90% for business code is sufficient.
[0096] 104. traversing source code of the target application project to obtain a target code segment in the target application project that does not contain a test mark;
[0097] It can be understood that the target code segment is a code segment that does not contain a test mark, i.e., the code segment is not tested. The embodiment does not limit the specific traversal manner.
[0098] 105. obtaining target code annotation text corresponding to the target code segment from a preset annotation library;
[0099] It can be understood that, in order to improve the readability of the code, the developer usually needs to add annotation information to the written code when developing the project, which is used to annotate the code logic, i.e., to translate the machine language into natural language. The server scans the annotation information contained in each application project line by line, and stores it in the corresponding annotation library according to the application project.
[0100] 106. generating a network model based on a preset first script, processing the target code annotation text, obtaining and executing a first target test script corresponding to the target code segment, wherein the first script generation network model is a network model for generating a test script based on the annotation information.
[0101] It can be understood that the first script generation network model is a network model for generating code based on annotation information, such as the Codex AI model of OpenAI, which is fine-tuned on the basis of the pre-training of the GPT language model, and supports programming languages including but not limited to Python, JavaScript, TypeScript, Ruby, etc.
[0102] In an optional embodiment, the server can also obtain the first script generation network model based on the Generative Adversarial Networks (GAN) after training.
[0103] In the embodiment, the code test coverage of each application project is calculated, and the target application project whose test coverage is less than a threshold and the target code segment in the target application project that does not contain a test mark are determined according to the calculated test coverage, and then the annotation information corresponding to the target code segment is obtained, and finally the test script corresponding to the annotation information is directly generated and executed based on the script generation network model to test the untested code, which improves the test efficiency and the code test coverage.
[0104] Referring to Figure 2 , the second embodiment of the code test method in the embodiment of the application includes:
[0105] 201、receive source code of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying a code segment that has been tested;
[0106] 202、calculate a test coverage of each application project according to the source code of each application project and the test mark of each application project;
[0107] 203、determine a target application project with a test coverage less than a preset threshold based on the test coverage of each application project;
[0108] 204、traverse the source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark;
[0109] 205、obtain a target code annotation text corresponding to the target code segment from a preset annotation library;
[0110] It should be understood that steps 201-205 are similar to the execution steps of steps 101-105 described above, and will not be described here.
[0111] 206、construct an initial code training data set for model training based on a preset public script code containing annotation information;
[0112] It should be understood that the public script code can be TB (terabyte) level publicly available code extracted from open source communities such as GitHub and English language examples, which are not limited by the present embodiment.
[0113] 207、perform data preprocessing on the initial code training data set to obtain a target code training data set;
[0114] It should be understood that the preset language network model includes but is not limited to GPT language model.
[0115] It can be understood that the specific way of data preprocessing includes but is not limited to data deduplication, data noise reduction, etc. Optionally, if the annotation information stored in the annotation library is all Chinese annotations, and the code data in the initial code training data set comes from open source communities such as GitHub, most of the open source codes in the open source communities use English annotations. Due to the differences in Chinese and English grammar and language habits, the server also needs to manually assist in converting English annotations to Chinese annotations, thereby improving the recognition accuracy of the model.
[0116] 208、initialize the network parameters of the preset language network model, input the target code training data set into the preset language network model to perform script generation training, and obtain a first script generation network model;
[0117] It can be understood that the server obtains a target code annotation pair from the target code training data set, wherein the target code annotation pair includes a code snippet and corresponding target annotation information;
[0118] Based on the preset language network model, the target generated code corresponding to the target annotation information is predicted; that is, the data in the target code training data set is encoded by the input layer of the preset language network model, the weights and biases of the features are learned by each neuron in the hidden layer of the model, and the target generated code corresponding to the target annotation information is output by the output layer of the model.
[0119] According to the preset loss function, the loss value between the target generated code and the code snippet in the target code annotation pair is calculated; in this embodiment, the loss function is not specifically limited.
[0120] According to the loss value, the network parameters of the preset language network model are adjusted to obtain a first script generation network model. In this embodiment, the specific way of adjusting the network parameters of the preset language network model according to the loss value is not limited, for example, in an embodiment, the server reversely propagates the loss value from the output layer to the hidden layer of the preset language network model; when the loss value is propagated to the hidden layer, the network parameters of the preset language network model are iteratively updated according to the loss value and using a stochastic gradient descent algorithm; when the network of the preset language network model converges, the network parameters of the current preset language network model are determined as target parameters, and the first script generation network model is obtained. Specifically, while the network parameters are continuously gradient-decreased, the calculated loss value fluctuates around a certain minimum value; when the loss value reaches the minimum value, it is determined that the network converges.
[0121] 209、Based on the preset first script generation network model, the target code annotation text is processed to obtain and execute a first target test script corresponding to the target code snippet.
[0122] Wherein, step 209 is similar to the execution step of step 106 described above, and will not be repeated here.
[0123] In this embodiment, the process of constructing the first script generation network model is described in detail. The preset language network model is trained by the training data set, so that the first script generation network model which can generate corresponding scripts according to annotation information is obtained, and the script generation efficiency is improved.
[0124] Referring to Figure 3 , the third embodiment of the code testing method in the embodiment of the application comprises:
[0125] 301、receive source code of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying a code segment that has been tested;
[0126] 302、calculate a test coverage rate of each application project according to the source code of each application project and the test mark of each application project;
[0127] 303、determine a target application project with a test coverage rate less than a preset threshold based on the test coverage rates of the application projects;
[0128] 304、traverse the source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark;
[0129] 305、obtain a target code annotation text corresponding to the target code segment from a preset annotation library;
[0130] 306、process the target code annotation text based on a preset first script generation network model to obtain and execute a first target test script corresponding to the target code segment, wherein the first script generation network model is a network model for generating a test script based on annotation information;
[0131] The steps 301-306 are similar to the execution steps of the steps 101-106 described above, and will not be described here in detail.
[0132] 307、obtain at least one test case corresponding to the target code segment from a preset logic library;
[0133] It can be understood that before testing the code segment corresponding to the application project, a corresponding test case usually needs to be written in advance, and the generated test case is stored in the logic library in advance, and then the corresponding test script is generated by directly obtaining it from the logic library when testing is needed.
[0134] It can be understood that a code segment may represent a certain business module, so at least one test case usually needs to be designed to test the business module comprehensively, such as function test, performance test (such as response rate, response rate, concurrency, etc.).
[0135] Optionally, before obtaining at least one test case corresponding to the target code segment from the preset logic library, the server pre-generates at least one test case corresponding to the target code segment.
[0136] In this embodiment, the specific generation method of the test case and the specific form of the test case are not limited, for example:
[0137] The server obtains a plurality of logic nodes corresponding to each code segment in each application project and their arrangement order;
[0138] construct a directed acyclic graph corresponding to each code snippet in each application project based on the plurality of logical nodes and the arrangement order of the plurality of logical nodes corresponding to each code snippet in each application project, wherein the directed acyclic graph is used to represent the code logic of the code snippet, the directed acyclic graph includes a plurality of graph element nodes and directed edges connecting the graph element nodes, each graph element node corresponds to each logical node respectively, and the directed edges are used to indicate the execution order of the logical nodes;
[0139] perform path traversal on each directed acyclic graph, and determine each path traversed as a test case to obtain at least one test case corresponding to each code snippet in each application project;
[0140] store the at least one test case corresponding to each code snippet in the logical library according to the application project to which each code snippet belongs.
[0141] 308、based on a preset second script generation network model, process the at least one test case corresponding to the target code snippet, and obtain and execute a second target test script corresponding to the target code snippet, wherein the second script generation network model is a network model for generating a test script based on a test case.
[0142] It can be understood that, for the generation mode of the test script, not only the test script can be generated based on the annotation information containing the logical relationship, but also the test script can be generated based on the test case. Both of them can generate a corresponding script generation network model based on a machine learning model through a large amount of training data, and the difference lies in that the training data, the learned features, and the loss function used by the two are different.
[0143] In the embodiment, the process of generating a second target test script based on the test case corresponding to the target code snippet is described in detail. The test script corresponding to the test case is generated based on the second script generation network, so that the target code snippet is tested again, and the accuracy of the test is improved.
[0144] The code testing method in the embodiment of the application is described above, and the code testing device in the embodiment of the application is described below. Please refer to Figure 4 One embodiment of the code testing device in the embodiment of the application includes:
[0145] The code receiving module 401 is configured to receive source codes of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying a code snippet that has been tested.
[0146] The coverage calculation module 402 is configured to calculate the test coverage of each application project based on the source code of each application project and the test mark of each application project.
[0147] The target determining module 403 is configured to determine a target application project with a test coverage less than a preset threshold based on the test coverage of each application project.
[0148] The code traversing module 404 is configured to traverse source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark.
[0149] The comment obtaining module 405 is configured to obtain target code comment text corresponding to the target code segment from a preset comment library.
[0150] The script generating module 406 is configured to process the target code comment text based on a preset first script generation network model to obtain and execute a first target test script corresponding to the target code segment, wherein the first script generation network model is a network model for generating a test script based on comment information.
[0151] In this embodiment, the code test coverage of each application project is calculated, and a target application project with a test coverage less than a threshold and a target code segment in the target application project that does not contain a test mark are determined according to the calculated test coverage. Then, comment information corresponding to the target code segment is obtained. Finally, a test script corresponding to the comment information is directly generated and executed based on a script generation network model to test the untested code, thereby improving the test efficiency and the code test coverage.
[0152] Referring to Figure 5 Another embodiment of the code test device in the embodiment of the present application includes:
[0153] The code receiving module 501 is configured to receive source code of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying a code segment that has been tested.
[0154] The coverage calculating module 502 is configured to calculate a test coverage of each application project based on the source code of each application project and the test mark of each application project.
[0155] The target determining module 503 is configured to determine a target application project with a test coverage less than a preset threshold based on the test coverage of each application project.
[0156] The code traversing module 504 is configured to traverse source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark.
[0157] The comment obtaining module 505 is configured to obtain target code comment text corresponding to the target code segment from a preset comment library.
[0158] The script generation module 506 is configured to generate a first target test script corresponding to the target code segment based on a preset first script generation network model by processing the target code annotation text, wherein the first script generation network model is a network model for generating a test script based on annotation information.
[0159] The additional test module 507 is configured to perform secondary testing on the target code segment.
[0160] The coverage calculation module 502 specifically includes:
[0161] The obtaining unit 5021 is configured to obtain the total number of code segments of each application project and the number of tested code segments containing the test mark, respectively.
[0162] The calculation unit 5022 is configured to perform division operation on the number of tested code segments containing the test mark and the total number of code segments in each application project, respectively, to obtain the test coverage of each application project.
[0163] The additional test module 507 specifically includes:
[0164] The test case obtaining unit 5071 is configured to obtain at least one test case corresponding to the target code segment from a preset logic library.
[0165] The generation unit 5072 is configured to generate a second target test script corresponding to the target code segment based on a preset second script generation network model by processing at least one test case corresponding to the target code segment, wherein the second script generation network model is a network model for generating a test script based on a test case.
[0166] In the embodiment of the application, the modular design enables the hardware of each part of the code testing device to focus on the implementation of a certain function, thereby maximizing the performance of the hardware. Meanwhile, the modular design also reduces the coupling between the modules of the device, thereby facilitating maintenance.
[0167] The above Figure 4 and Figure 5 The code testing device in the embodiment of the application is described in detail from the perspective of a modular functional entity, and the code testing device in the embodiment of the application is described in detail from the perspective of hardware processing.
[0168] Figure 6is a structural schematic diagram of a code testing device provided by an embodiment of the present application. The code testing device 600 can have great differences due to different configurations or performances, and can include one or more central processing units (CPUs) 610 (for example, one or more processors) and a memory 620, and one or more storage media 630 (for example, one or more mass storage devices) for storing an application program 633 or data 632. The memory 620 and the storage media 630 can be temporary storage or persistent storage. The program stored in the storage media 630 can include one or more modules (not shown in the figure), and each module can include a series of instruction operations in the code testing device 600. Furthermore, the processor 610 can be configured to communicate with the storage media 630 and execute the series of instruction operations in the storage media 630 on the code testing device 600.
[0169] The code testing device 600 can further include one or more power supplies 640, one or more wired or wireless network interfaces 650, one or more input / output interfaces 660, and / or one or more operating systems 631, such as Windows Server, Mac OS X, Unix, Linux, FreeBSD, and the like. Those skilled in the art can understand that the code testing device 600 can further include other components, and the components shown in the figure are not intended to limit the code testing device. Figure 6 The code testing device structure shown in the figure does not constitute a limitation on the code testing device, and can include more or fewer components than shown in the figure, or combine certain components, or arrange different components.
[0170] The present application also provides a code testing device, which includes a memory and a processor, the memory stores computer readable instructions, and the computer readable instructions are executed by the processor to make the processor execute the steps of the code testing method in each of the embodiments.
[0171] The present application also provides a computer readable storage medium, which can be a non-volatile computer readable storage medium or a volatile computer readable storage medium, and the computer readable storage medium stores instructions, and the instructions make a computer execute the steps of the code testing method when the instructions are run on the computer.
[0172] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described system, device and unit can refer to the corresponding processes in the foregoing method embodiments, which will not be described herein.
[0173] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present application or the entire or part of the technical solutions that essentially contribute to the prior art can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in the various embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes.
[0174] The above-described embodiments are merely used to illustrate the technical solutions of the present application, rather than limit the same; even though the present application has been described in detail with reference to the foregoing embodiments, those ordinarily skilled in the art should understand that: they can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacements for some of the technical features; and these modifications or replacements do not cause the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A code testing method, characterized in that: The code testing method includes: receiving source codes of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying that a code segment has been tested; Calculate the test coverage of each application project based on the source code and test tags of each application project; Based on the test coverage of each application project, determine the target application project whose test coverage is less than a preset threshold; Traversing the source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark; Obtaining target code annotation text corresponding to the target code snippet from a preset annotation library; Based on a preset first script generating network model, processing the target code annotation text, obtaining and executing a first target test script corresponding to the target code fragment, wherein the first script generating network model is a network model for generating a test script based on annotation information; After generating a network model based on a preset first script, processing the target code annotation text, and obtaining and executing a first target test script corresponding to the target code fragment, the method further includes: Obtaining at least one test case corresponding to the target code snippet from a preset logic library; Generate a network model based on a preset second script, process at least one test case corresponding to the target code snippet, obtain and execute a second target test script corresponding to the target code snippet, wherein the network model generated by the second script is a network model that generates a test script based on the test case; Before obtaining at least one test case corresponding to the target code snippet from the preset logic library, the method further includes: Obtain multiple logic nodes corresponding to each code snippet in each of the application projects and their arrangement order; Based on the multiple logical nodes corresponding to each code snippet in each of the application projects and their arrangement order, constructing a directed acyclic graph corresponding to each code snippet in each of the application projects, wherein the directed acyclic graph is used to represent the code logic of the code snippet, the directed acyclic graph includes multiple primitive nodes and directed edges connecting the primitive nodes, each primitive node corresponds to each of the logical nodes, and the directed edges are used to indicate the execution order of the logical nodes; Performing path traversal on each of the directed acyclic graphs, and determining each traversed path as a test case, to obtain at least one test case corresponding to each code snippet in each of the application projects; According to the application project to which each code snippet belongs, at least one test case corresponding to each code snippet is classified and stored in the logic library.
2. The code testing method according to claim 1, characterized in that: Calculating the test coverage of each application project according to the source code of each application project and the test tag of each application project includes: respectively obtaining the total number of code snippets of each application project and the number of tested code snippets containing the test mark; The number of tested code snippets containing the test mark in each application project is divided by the total number of code snippets to obtain the test coverage of each application project.
3. The code testing method according to claim 1, wherein: Before generating a network model based on a preset first script, processing the target code annotation text, and obtaining and executing the first target test script corresponding to the target code fragment, the method further includes: Build an initial code training dataset for model training based on the public script code containing pre-set annotation information; Performing data preprocessing on the initial code training data set to obtain a target code training data set; Initialize the network parameters of the preset language network model; The target code training data set is input into the preset language network model to perform script generation training to obtain a first script generation network model.
4. The code testing method according to claim 3, characterized in that: Inputting the target code training data set into the preset language network model to perform script generation training to obtain a first script generation network model includes: Obtaining a target code annotation pair from the target code training dataset, wherein the target code annotation pair includes a code snippet and its corresponding target annotation information; Based on the preset language network model, predicting the target generated code corresponding to the target annotation information; Calculating the loss value between the target generated code and the code snippet in the target code annotation pair according to a preset loss function; According to the loss value, the network parameters of the preset language network model are adjusted to obtain a first script generation network model.
5. The code testing method according to claim 4, characterized in that: The adjusting the network parameters of the preset language network model according to the loss value to obtain the first script generation network model includes: Back-propagating the loss value from the output layer to the hidden layer of the preset language network model; When the loss value is propagated to the hidden layer, the network parameters of the preset language network model are iteratively updated according to the loss value and using a stochastic gradient descent algorithm; When the network of the preset language network model converges, the network parameters of the current preset language network model are determined as target parameters to obtain the first script generation network model.
6. A code testing device, characterized in that: The code testing device comprises: A code receiving module, configured to receive source codes of a plurality of application projects, wherein the source code of each application project includes a test mark for identifying that a code segment has been tested; A coverage calculation module, configured to calculate the test coverage of each application project based on the source code of each application project and the test tag of each application project; A target determination module, configured to determine, based on the test coverage of each of the application projects, a target application project whose test coverage is less than a preset threshold; A code traversal module, traversing the source code of the target application project to obtain a target code segment in the target application project that does not contain the test mark; An annotation acquisition module, used to acquire the target code annotation text corresponding to the target code fragment from a preset annotation library; a script generation module, configured to generate a network model based on a preset first script, process the target code annotation text, obtain and execute a first target test script corresponding to the target code fragment, wherein the first script-generated network model is a network model that generates a test script based on annotation information; The device further includes an additional test module, which specifically includes: A test case acquisition unit, configured to acquire at least one test case corresponding to the target code snippet from a preset logic library; a generating unit, configured to generate a network model based on a preset second script, process at least one test case corresponding to the target code snippet, obtain and execute a second target test script corresponding to the target code snippet, wherein the network model generated by the second script is a network model that generates a test script based on the test case; The additional test module specifically includes: A logic acquisition unit, configured to acquire a plurality of logic nodes and their arrangement order corresponding to each code snippet in each of the application projects; an acyclic graph construction unit, configured to construct a directed acyclic graph corresponding to each code snippet in each application project based on the multiple logical nodes corresponding to each code snippet in each application project and their arrangement order, wherein the directed acyclic graph is used to represent the code logic of the code snippet, the directed acyclic graph includes multiple graph element nodes and directed edges connecting the graph element nodes, each graph element node corresponds to each logical node, and the directed edges are used to indicate the execution order of the logical nodes; A traversal unit, configured to traverse each of the directed acyclic graphs and determine each traversed path as a test case, thereby obtaining at least one test case corresponding to each code snippet in each of the application projects; The storage unit is used to classify and store at least one test case corresponding to each code snippet in the logic library according to the application project to which each code snippet belongs.
7. A code testing device, characterized in that: The code testing device includes: a memory and at least one processor, wherein instructions are stored in the memory; The at least one processor calls the instructions in the memory to enable the code testing device to execute the code testing method according to any one of claims 1 to 5.
8. A computer-readable storage medium having instructions stored thereon, characterized in that: When the instructions are executed by a processor, the code testing method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Method and device for generating code based on annotation and storage medium
CN113050953A
Module test method, device and equipment and computer storage medium
CN113535534A