CN114817025A - Code testing method, device, equipment and storage medium - Google Patents

Code testing method, device, equipment and storage medium Download PDF

Info

Publication number
CN114817025A
CN114817025A CN202210424700.8A CN202210424700A CN114817025A CN 114817025 A CN114817025 A CN 114817025A CN 202210424700 A CN202210424700 A CN 202210424700A CN 114817025 A CN114817025 A CN 114817025A
Authority
CN
China
Prior art keywords
code
target
test
network model
script
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210424700.8A
Other languages
Chinese (zh)
Inventor
李宇航
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An International Smart City Technology Co Ltd
Original Assignee
Ping An International Smart City Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ping An International Smart City Technology Co Ltd filed Critical Ping An International Smart City Technology Co Ltd
Priority to CN202210424700.8A priority Critical patent/CN114817025A/en
Publication of CN114817025A publication Critical patent/CN114817025A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention relates to the field of testing, and discloses a code testing method, a device, equipment and a storage medium. The method comprises the following steps: receiving source codes of a plurality of application projects, wherein each source code of the application projects comprises a test mark; respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item; determining target application items with the test coverage rate smaller than a threshold value based on the test coverage rate of each application item; traversing the source code of the target application project to obtain a target code segment which does not contain the test mark; acquiring a target code annotation text corresponding to the target code segment from a preset annotation library; and 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 segment. According to the invention, the test script is generated based on the annotations corresponding to the code segments with the test coverage rate smaller than the threshold value so as to directly test, and the test efficiency is improved.

Description

Code testing method, device, equipment and storage medium
Technical Field
The present invention relates to the field of testing, and in particular, to a code testing method, apparatus, device, and storage medium.
Background
In order to ensure the stability of software engineering, software engineering is usually developed based on a stable baseline version, and new branches are developed to implement new functions. When a software project creates new branches, the new branches need to be tested to confirm that they can perform the intended function. To gauge whether the test is sufficient for a new branch, the sufficiency of the test may be quantified by counting the number of lines, classes, branches, etc. of code covered by the test. This quantified fullness is the test coverage. In order to facilitate the tester to adjust the test case according to the test coverage of the new branch, the test coverage of the new branch needs to be noted in the test coverage report generated according to the test.
Existing code testing methods are typically based on manually inspecting the code to locate untested code areas and then testing them to improve code coverage, however such methods based on manual location and testing are particularly inefficient.
Disclosure of Invention
The invention mainly aims to solve the problem of low efficiency of the existing code testing method.
The first aspect of the present invention provides a code testing method, including:
receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that a code segment has been tested;
respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item;
determining target application items of which the test coverage rates are smaller than a preset threshold value based on the test coverage rates of the application items;
traversing the source code of the target application project to obtain target code segments which do not contain the test marks in the target application project;
acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
and processing 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 the test script based on annotation information.
Optionally, in a first implementation manner of the first aspect of the present invention, the calculating, according to the source code of each application item and the test flag of each application item, the test coverage of each application item respectively includes:
respectively acquiring the total number of code fragments of each application item and the number of tested code fragments containing the test marks;
and respectively carrying out division operation on the number of the tested code segments containing the test marks in each application project and the total number of the code segments to obtain the test coverage rate of each application project.
Optionally, in a second implementation manner of the first aspect of the present invention, before 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 segment, the method further includes:
constructing an initial code training data set for model training based on a preset public script code containing annotation information;
performing data preprocessing on the initial code training data set to obtain a target code training data set;
initializing network parameters of a preset language network model;
and inputting the target code training data set into the preset language network model to execute script generation training to obtain a first script generation network model.
Optionally, in a third implementation manner of the first aspect of the present invention, the inputting the target code training dataset into the preset language network model to perform script generation training to obtain a first script generation network model includes:
acquiring a target code annotation pair from the target code training dataset, wherein the target code annotation pair comprises a code segment and target annotation information corresponding to the code segment;
predicting a target generation code corresponding to the target annotation information based on the preset language network model;
calculating a loss value between the target generation code and the code segment in the target code annotation pair according to a preset loss function;
and adjusting the network parameters of the preset language network model according to the loss value to obtain a first script generation network model.
Optionally, in a fourth implementation manner of the first aspect of the present invention, the adjusting, according to the loss value, a network parameter of the preset language network model to obtain a first script generation network model includes:
propagating the loss value from an output layer of the preset language network model to a hidden layer in a reverse direction;
when the loss value is transmitted to the hidden layer, according to the loss value, a random gradient descent algorithm is adopted to carry out iterative updating on the network parameters of the preset language network model;
when the network of the preset language network model is converged, determining the current network parameters of the preset language network model as target parameters to obtain the first script generation network model.
Optionally, in a fifth implementation manner of the first aspect of the present invention, 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 segment, the method further includes:
acquiring at least one test case corresponding to the target code segment from a preset logic library;
and generating a network model based on a preset second script, processing at least one test case corresponding to the target code segment, and obtaining and executing a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating the test script based on the test case.
Optionally, in a sixth implementation manner of the first aspect of the present invention, before the obtaining at least one test case corresponding to the target code segment from a preset logic library, the method further includes:
acquiring a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence thereof;
constructing a directed acyclic graph corresponding to each code segment in each application project based on a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence of the logic nodes, wherein the directed acyclic graph is used for representing code logics of the code segments, the directed acyclic graph comprises a plurality of primitive nodes and directed edges connected with the primitive nodes, each primitive node corresponds to each logic node, and the directed edges are used for indicating an execution sequence of the logic nodes;
performing path traversal on each directed acyclic graph, determining each traversed path as a test case, and obtaining at least one test case corresponding to each code segment in each application project;
and storing at least one test case corresponding to each code segment in the logic library according to the application item to which each code segment belongs.
A second aspect of the present invention provides a code testing apparatus, including:
the code receiving module is used for receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that the code segments have been tested;
the coverage calculation module is used for respectively calculating the test coverage of each application item according to the source code of each application item and the test mark of each application item;
the target determining module is used for determining target application items of which the test coverage rates are smaller than a preset threshold value based on the test coverage rates of the application items;
the code traversing module is used for traversing the source code of the target application project to obtain a target code segment which does not contain the test mark in the target application project;
the annotation acquisition module is used for acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
and the script generation module is used for generating a network model based on a preset first script, processing the object code annotation text, and obtaining and executing a first object test script corresponding to the object code fragment, wherein the first script generation network model is a network model for generating a test script based on annotation information.
Optionally, in a first implementation manner of the second aspect of the present invention, the coverage calculating module specifically includes:
an acquiring unit, configured to acquire a total number of code fragments of each application and a number of tested code fragments containing the test flag, respectively;
and the calculating unit is used for respectively carrying out division operation on the number of the tested code segments containing the test marks in each application item and the total number of the code segments to obtain the test coverage rate of each application item.
Optionally, in a second implementation manner of the second aspect of the present invention, the apparatus further includes a model training module, where the model training module specifically includes:
the data set building module is used for building an initial code training data set for model training based on a preset public script code containing annotation information;
the preprocessing module is used for preprocessing the initial code training data set to obtain a target code training data set;
the initialization module is used for initializing the network parameters of the preset language network model;
and the model generation module is used for inputting the target code training data set into the preset language network model to execute script generation training to obtain a first script generation network model.
Optionally, in a third implementation manner of the second aspect of the present invention, the model generation module is specifically configured to:
acquiring a target code annotation pair from the target code training dataset, wherein the target code annotation pair comprises a code segment and target annotation information corresponding to the code segment;
predicting a target generation code corresponding to the target annotation information based on the preset language network model;
calculating a loss value between the target generation code and the code segment in the target code annotation pair according to a preset loss function;
and adjusting the network parameters of the preset language network model according to the loss value to obtain a first script generation network model.
Optionally, in a fourth implementation manner of the second aspect of the present invention, the model generating module is specifically configured to:
acquiring a target code annotation pair from the target code training dataset, wherein the target code annotation pair comprises a code segment and target annotation information corresponding to the code segment;
predicting a target generation code corresponding to the target annotation information based on the preset language network model;
calculating a loss value between the target generation code and the code segment in the target code annotation pair according to a preset loss function;
propagating the loss value from an output layer of the preset language network model to a hidden layer in a reverse direction;
when the loss value is transmitted to the hidden layer, according to the loss value, a random gradient descent algorithm is adopted to carry out iterative updating on the network parameters of the preset language network model;
when the network of the preset language network model is converged, determining the current network parameters of the preset language network model as target parameters to obtain the first script generation network model.
Optionally, in a fifth implementation manner of the second aspect of the present invention, the apparatus further includes an additional testing module, where the additional testing module specifically includes:
the test case acquisition unit is used for acquiring at least one test case corresponding to the target code segment from a preset logic library;
and the generating unit is used for generating a network model based on a preset second script, processing at least one test case corresponding to the target code segment, and obtaining and executing a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating the test script based on the test case.
Optionally, in a sixth implementation manner of the second aspect of the present invention, the additional test module specifically includes:
the logic acquisition unit is used for acquiring a plurality of logic nodes corresponding to each code segment in each application project and the arrangement sequence of the logic nodes;
the acyclic graph constructing unit is used for constructing a directed acyclic graph corresponding to each code segment in each application project based on a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence of the logic nodes, wherein the directed acyclic graph is used for representing code logics of the code segments, the directed acyclic graph comprises a plurality of primitive nodes and directed edges connecting the primitive nodes, each primitive node corresponds to each logic node, and the directed edges are used for indicating an execution sequence of the logic nodes;
the traversal unit is used for performing path traversal on each directed acyclic graph, determining each traversed path as a test case, and obtaining at least one test case corresponding to each code segment in each application project;
the storage unit is used for storing at least one test case corresponding to each code segment in the logic library in a classified manner according to the application item to which each code segment belongs;
the test case acquisition unit is used for acquiring at least one test case corresponding to the target code segment from a preset logic library;
and the generating unit is used for generating a network model based on a preset second script, processing at least one test case corresponding to the target code segment, and obtaining and executing a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating the test script based on the test case.
A third aspect of the present invention provides a code testing apparatus comprising: a memory and at least one processor, the memory having instructions stored therein; the at least one processor invokes the instructions in the memory to cause the code testing apparatus to perform the code testing method described above.
A fourth aspect of the present invention provides a computer-readable storage medium having stored therein instructions, which, when run on a computer, cause the computer to execute the code testing method described above.
According to the technical scheme provided by the invention, the code test coverage rate of each application item is calculated, the target application item smaller than the threshold value and the target code segment without the test mark in the target application item are determined according to the calculated test coverage rate, the annotation information corresponding to the target code segment is further obtained, and finally, the network model is generated based on the script, so that the test script corresponding to the annotation information is directly generated and executed to test the untested code, the test efficiency is improved, and the code test coverage rate is improved.
Drawings
FIG. 1 is a schematic diagram of a first embodiment of a code testing method according to an embodiment of the present invention;
FIG. 2 is a diagram of a second embodiment of a code testing method according to an embodiment of the present invention;
FIG. 3 is a diagram of a third embodiment of a code testing method according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of an embodiment of a code testing apparatus according to the present invention;
FIG. 5 is a schematic diagram of another embodiment of a code testing apparatus according to an embodiment of the present invention;
FIG. 6 is a diagram of an embodiment of a code testing apparatus according to an embodiment of the present invention.
Detailed Description
The terms "first," "second," "third," "fourth," and the like in the description and in the claims, as well as in the drawings, if any, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It will be appreciated that the data so used may be interchanged under appropriate circumstances such that the embodiments described herein may be practiced otherwise than as specifically illustrated or described herein. Furthermore, the terms "comprises," "comprising," or "having," and any variations thereof, are intended to cover non-exclusive inclusions, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
The invention can acquire and process related data based on an artificial intelligence technology. Among them, Artificial Intelligence (AI) is a theory, method, technique and application system that simulates, extends and expands human Intelligence using a digital computer or a machine controlled by a digital computer, senses the environment, acquires knowledge and uses the knowledge to obtain the best result.
The server in the invention can be an independent server, and can also be a cloud server providing basic cloud computing services such as cloud service, a cloud database, cloud computing, cloud functions, cloud storage, Network service, cloud communication, middleware service, domain name service, security service, Content Delivery Network (CDN), big data and artificial intelligence platform and the like.
For understanding, the following describes a specific flow of an embodiment of the present invention, and with reference to fig. 1, an embodiment of a code testing method according to an embodiment of the present invention includes:
101. receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that a code segment has been tested;
it is understood that the plurality of application items may be items of different platform architectures, such as an Android item, a Kotlin item, a Flutter item, a Java item, and the like. In the process of project development, project source codes are packaged and uploaded to a unified code management platform, for example, for a CICD pipeline project, a hook function (hook) is added in the project, and therefore the project source codes are uploaded to the code management platform in a non-invasive mode; for non-pipelined projects, the server may package the projects through a script program and upload the packaged projects to the code management platform, where an upload mode in the script program may be a timed upload or an upload based on a code amount, and is not limited in this embodiment.
Optionally, the server may also upload the specified project code to the code management platform based on the code instrumentation. Specifically, for example, for a Java project, when a Java virtual machine is started, a Java agent and a jacoco plug-in are called to perform instrumentation on a code, and a Client-Server mode is called to directly send project and version information to a code management platform through a timed task; aiming at Kotlin and Android projects, calling jacoco plug-ins to plug the projects, packaging the projects into apk program packages, and sending the apk program packages and version information to a code management platform; and for the Flutterer project, instrumentation is carried out on the code of the Dart file by using an AspectD plug-in, the instrumented data is uniformly collected into variables, and the variables are sent to the code management platform at regular time through a timing task.
Optionally, after the server receives the source codes of the multiple application items, the server further performs validity check and consistency check on the data of each application item, so as to improve data security and integrity.
102. Respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item;
it will be appreciated that each application item includes test marks for identifying the code fragments that have been tested, i.e. each test mark corresponds to a code fragment. Specifically, the test flag may be a pair of identifiers, i.e. a segment header identifier for indicating the beginning of a segment and a segment end identifier for indicating the end of the segment, which are respectively located at the beginning of a code segment and at the end of the code segment.
Specifically, the server firstly obtains the total number of code segments of each application item and the number of tested code segments containing the test mark, and secondly performs division operation on the number of tested code segments containing the test mark in each application item and the total number of code segments, so as to obtain the test coverage of each application item, namely the test coverage is equal to the number of tested code segments divided by the total number of code segments.
103. Determining target application items with the test coverage rates smaller than a preset threshold value based on the test coverage rates of the application items;
it can be understood that the target application item is an application item whose test coverage is smaller than a preset threshold, and the threshold can be adjusted according to the actual service requirement, and preferably the threshold is 100%. Usually, the test coverage of the application should be 100%, that is, each code segment needs to be tested, and in some cases, the coverage of the service code is more than 90%.
104. Traversing the source code of the target application project to obtain a target code segment which does not contain the test mark in the target application project;
it can be understood that the target code segment is a code segment that does not contain a test flag, that is, the code segment is not tested, and the value of this embodiment is not limited to a specific traversal manner.
105. Acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
it will be appreciated that in order to improve the readability of the code, it is often necessary to add annotation information to the written code for annotating the code logic, i.e. translating the machine language in natural language, while the developer is developing the project. And the server scans the annotation information contained in each application item line by line and stores the annotation information in the corresponding annotation library according to the application items.
106. And processing 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 fragment, wherein the first script generation network model is a network model for generating the test script based on the annotation information.
It is 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 billions of lines of public code based on the pretraining of the GPT language model, and supports programming languages including but not limited to Python, JavaScript, TypeScript, Ruby, and the like.
In an optional embodiment, the server may further generate a network model based on a first script obtained after a countermeasure generated network model (GAN) is trained.
In the embodiment, the code test coverage of each application item is calculated, the target application item smaller than the threshold value and the target code segment without the test mark are determined according to the calculated test coverage, the annotation information corresponding to the target code segment is further acquired, 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, so that the test efficiency is improved, and the coverage of the code test is improved.
Referring to fig. 2, a second embodiment of the code testing method according to the embodiment of the present invention includes:
201. receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that a code segment has been tested;
202. respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item;
203. determining target application items with the test coverage rates smaller than a preset threshold value based on the test coverage rates of the application items;
204. traversing the source code of the target application project to obtain a target code segment which does not contain the test mark in the target application project;
205. acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
wherein, the steps 201-205 are similar to the steps of the steps 101-105, and detailed description thereof is omitted here.
206. Constructing an initial code training data set for model training based on a preset public script code containing annotation information;
it should be understood that the public script code may be tb (terabyte) level publicly available code extracted from an open source community such as GitHub, and an example of english language, which is not limited by the embodiment.
207. Carrying out data preprocessing on the initial code training data set to obtain a target code training data set;
it should be understood that the pre-set language network model includes, but is not limited to, the GPT language model.
It is understood that the specific manner of data preprocessing includes, but is not limited to, data deduplication, data denoising, and the like. Optionally, if the annotation information stored in the annotation library is a Chinese annotation, the code data in the initial code training data set is from an open source community such as a GitHub, most open source codes of the initial code training data set adopt English annotations, and in view of differences in Chinese and English grammar and language habits, the server needs manual assistance to convert the English annotations into Chinese annotations, so that the recognition accuracy of the model is improved.
208. Initializing network parameters of a preset language network model, inputting a target code training data set into the preset language network model to execute script generation training, and obtaining a first script generation network model;
it can be understood that the server obtains a target code annotation pair from the target code training dataset, wherein the target code annotation pair includes a code segment and corresponding target annotation information;
predicting a target generation code corresponding to the target annotation information based on a preset language network model; the input layer of the preset language network model encodes data in the target code training data set, and the output layer of the model outputs target annotation information corresponding to the target generation code according to the weight and bias of each neuron learning feature in the hidden layer of the model.
Calculating a loss value between the target generation code and a code segment in the target code annotation pair according to a preset loss function; in this embodiment, the loss function is not particularly limited.
And adjusting the network parameters of the preset language network model according to the loss value to obtain a first script generation network model. In this embodiment, a specific manner 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 of the preset language network model to the hidden layer; when the loss value is transmitted to the hidden layer, according to the loss value, a random gradient descent algorithm is adopted to carry out iterative updating on the network parameters of the preset language network model; when the network of the preset language network model is converged, determining the network parameters of the current preset language network model as target parameters to obtain a first script generation network model, specifically, when the network parameters are continuously reduced in a gradient manner, the calculated loss value fluctuates in a range near a certain minimum value, and when the loss value reaches the minimum value, the network is determined to be converged.
209. And processing 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 fragment, wherein the first script generation network model is a network model for generating the test script based on the annotation information.
Step 209 is similar to the step 106, and is not described herein again.
In this embodiment, a process of constructing the first script generation network model is described in detail, and the preset language network model is trained through a training data set, so that the first script generation network model capable of generating a corresponding script according to the annotation information is obtained, and the script generation efficiency is improved.
Referring to fig. 3, a third embodiment of the code testing method according to the embodiment of the present invention includes:
301. receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that a code segment has been tested;
302. respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item;
303. determining target application items with the test coverage rates smaller than a preset threshold value based on the test coverage rates of the application items;
304. traversing the source code of the target application project to obtain a target code segment which does not contain the test mark in the target application project;
305. acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
306. processing 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 fragment, wherein the first script generation network model is a network model for generating the test script based on the annotation information;
wherein, the steps 301-306 are similar to the steps of the steps 101-106, and detailed description thereof is omitted here.
307. Acquiring at least one test case corresponding to the target code segment from a preset logic library;
it can be understood that before testing the code segments corresponding to the application items, corresponding test cases are generally required to be written in advance, the generated test cases are stored in the logic library in a persistent manner in advance, and then when testing is required, the test cases are directly obtained from the logic library to generate corresponding test scripts.
It is understood that a code segment may represent a certain service module, so at least one test case is usually required to be designed to perform all-around tests on the service module, such as a functional test, a performance test (e.g., response rate, concurrency, etc.).
Optionally, before obtaining the at least one test case corresponding to the target code segment from the preset logic library, the server generates the at least one test case corresponding to the target code segment in advance.
In this embodiment, the specific generation manner of the test case and the specific form of the test case are not limited, for example:
the server acquires a plurality of logic nodes corresponding to each code segment in each application project and the arrangement sequence of the logic nodes;
constructing a directed acyclic graph corresponding to each code segment in each application project based on a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence of the logic nodes, wherein the directed acyclic graph is used for representing code logics of the code segments, the directed acyclic graph comprises a plurality of primitive nodes and directed edges connected with the primitive nodes, each primitive node corresponds to each logic node, and the directed edges are used for indicating an execution sequence of the logic nodes;
traversing paths of each directed acyclic graph, determining each traversed path as a test case, and obtaining at least one test case corresponding to each code segment in each application project;
and storing at least one test case corresponding to each code segment in the logic library according to the application item to which each code segment belongs.
308. And generating a network model based on a preset second script, processing at least one test case corresponding to the target code segment, and obtaining and executing a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating the test script based on the test case.
It can be understood that, for the generation manner of the test script, not only the test script may be generated based on the annotation information including the logical relationship, but also the test script may be generated based on the test case, and both may generate the corresponding script generation network model through a large amount of training data based on the machine learning model, and the difference lies in that the training data, the learning characteristics and the loss function used by the two are different.
In this embodiment, a process of generating a second target test script based on the test case corresponding to the target code segment is described in detail, and the test script corresponding to the test case is generated through the network generated based on the second script, so that the target code segment is subjected to secondary test, and the test accuracy is improved.
With reference to fig. 4, the code testing method in the embodiment of the present invention is described above, and a code testing apparatus in the embodiment of the present invention is described below, where an embodiment of the code testing apparatus in the embodiment of the present invention includes:
a code receiving module 401, configured to receive source code of a plurality of application items, where the source code of each application item includes a test flag for identifying that a code segment has been tested;
a coverage calculation module 402, configured to calculate, according to the source code of each application item and the test flag of each application item, a test coverage of each application item respectively;
a target determining module 403, configured to determine, based on a test coverage of each application item, a target application item for which the test coverage is smaller than a preset threshold;
a code traversing module 404, configured to traverse the source code of the target application project to obtain a target code segment that does not contain the test mark in the target application project;
an annotation acquisition module 405, configured to acquire a target code annotation text corresponding to the target code segment from a preset annotation library;
and the script generating module 406 is 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 segment, where the first script generating network model is a network model that generates a test script based on annotation information.
In the embodiment, the code test coverage of each application item is calculated, the target application item smaller than the threshold value and the target code segment without the test mark are determined according to the calculated test coverage, the annotation information corresponding to the target code segment is further acquired, 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, so that the test efficiency is improved, and the coverage of the code test is improved.
Referring to fig. 5, another embodiment of the code testing apparatus according to the embodiment of the present invention includes:
a code receiving module 501, configured to receive source code of a plurality of application items, where the source code of each application item includes a test flag for identifying that a code segment has been tested;
a coverage calculation module 502, configured to calculate, according to the source code of each application item and the test flag of each application item, a test coverage of each application item respectively;
a target determining module 503, configured to determine, based on a test coverage of each application item, a target application item for which the test coverage is smaller than a preset threshold;
a code traversing module 504, configured to traverse the source code of the target application project to obtain a target code segment that does not contain the test mark in the target application project;
the annotation acquisition module 505 is configured to acquire a target code annotation text corresponding to the target code segment from a preset annotation library;
a script generating module 506, 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 segment, where the first script generating network model is a network model that generates a test script based on annotation information;
an additional testing module 507, configured to perform a secondary test on the target code segment.
The coverage calculation module 502 specifically includes:
an acquiring unit 5021, configured to acquire the total number of code fragments of each application and the number of tested code fragments containing the test flag respectively;
a calculating unit 5022, configured to perform division on the number of tested code segments containing the test tag in each application item and the total number of code segments to obtain the test coverage of each application item.
Wherein, the additional testing module 507 specifically includes:
a test case obtaining unit 5071, configured to obtain at least one test case corresponding to the target code segment from a preset logic library;
the generating unit 5072 is configured to generate a network model based on a preset second script, process at least one test case corresponding to the object code segment, obtain and execute a second object test script corresponding to the object code segment, where the second script generation network model is a network model that generates a test script based on a test case.
In the embodiment of the invention, the modularized design ensures that hardware of each part of the code testing device is concentrated on realizing a certain function, the performance of the hardware is realized to the maximum extent, and meanwhile, the modularized design also reduces the coupling among the modules of the device, thereby being more convenient to maintain.
Fig. 4 and 5 describe the code testing apparatus in the embodiment of the present invention in detail from the perspective of the modular functional entity, and the code testing apparatus in the embodiment of the present invention is described in detail from the perspective of hardware processing.
Fig. 6 is a schematic structural diagram of a code testing apparatus 600 according to an embodiment of the present invention, where the code testing apparatus 600 may have a relatively large difference due to different configurations or performances, and may include one or more processors (CPUs) 610 (e.g., one or more processors) and a memory 620, and one or more storage media 630 (e.g., one or more mass storage devices) for storing applications 633 or data 632. Memory 620 and storage medium 630 may be, among other things, transient or persistent storage. The program stored in the storage medium 630 may include one or more modules (not shown), each of which may include a sequence of instructions operating on the code testing apparatus 600. Still further, the processor 610 may be configured to communicate with the storage medium 630 to execute a series of instruction operations in the storage medium 630 on the code testing apparatus 600.
The code testing apparatus 600 may also 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 so forth. Those skilled in the art will appreciate that the code testing device configuration shown in FIG. 6 does not constitute a limitation of the code testing device, and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The present invention also provides a code testing apparatus, which includes a memory and a processor, wherein the memory stores computer readable instructions, and the computer readable instructions, when executed by the processor, cause the processor to execute the steps of the code testing method in the above embodiments.
The present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium, and which may also be a volatile computer-readable storage medium, having stored therein instructions, which, when run on a computer, cause the computer to perform the steps of the code testing method.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a read-only memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.

Claims (10)

1. A code testing method, characterized in that the code testing method comprises:
receiving source codes of a plurality of application projects, wherein the source codes of each application project comprise test marks used for identifying that a code segment has been tested;
respectively calculating the test coverage rate of each application item according to the source code of each application item and the test mark of each application item;
determining target application items with the test coverage rates smaller than a preset threshold value based on the test coverage rates of the application items;
traversing the source code of the target application project to obtain target code segments which do not contain the test marks in the target application project;
acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
and processing 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 the test script based on annotation information.
2. The code testing method of claim 1, wherein the calculating the test coverage of each application item according to the source code of each application item and the test mark of each application item respectively comprises:
respectively acquiring the total number of code fragments of each application item and the number of tested code fragments containing the test marks;
and dividing the number of tested code segments containing the test marks in each application item by the total number of the code segments to obtain the test coverage rate of each application item.
3. The code testing method according to claim 1, wherein before generating a network model based on the preset first script, processing the object code annotation text to obtain and execute the first object test script corresponding to the object code segment, the method further comprises:
constructing an initial code training data set for model training based on a preset public script code containing annotation information;
performing data preprocessing on the initial code training data set to obtain a target code training data set;
initializing network parameters of a preset language network model;
and inputting the target code training data set into the preset language network model to execute script generation training to obtain a first script generation network model.
4. The code testing method of claim 3, wherein the inputting the target code training dataset into the preset language network model to perform script generation training, resulting in a first script generation network model comprises:
acquiring a target code annotation pair from the target code training dataset, wherein the target code annotation pair comprises a code segment and target annotation information corresponding to the code segment;
predicting a target generation code corresponding to the target annotation information based on the preset language network model;
calculating a loss value between the target generation code and the code segment in the target code annotation pair according to a preset loss function;
and adjusting the network parameters of the preset language network model according to the loss value to obtain a first script generation network model.
5. The code testing method according to claim 4, wherein the adjusting the network parameters of the predetermined language network model according to the loss value to obtain a first script generation network model comprises:
propagating the loss value from an output layer of the preset language network model to a hidden layer in a reverse direction;
when the loss value is transmitted to the hidden layer, according to the loss value, a random gradient descent algorithm is adopted to carry out iterative updating on the network parameters of the preset language network model;
when the network of the preset language network model is converged, determining the current network parameters of the preset language network model as target parameters to obtain the first script generation network model.
6. The code testing method according to any one of claims 1 to 5, wherein after generating a network model based on a preset first script, processing the object code annotation text, and obtaining and executing a first object test script corresponding to the object code segment, the method further comprises:
acquiring at least one test case corresponding to the target code segment from a preset logic library;
and generating a network model based on a preset second script, processing at least one test case corresponding to the target code segment, and obtaining and executing a second target test script corresponding to the target code segment, wherein the second script generation network model is a network model for generating the test script based on the test case.
7. The code testing method according to claim 6, wherein before the obtaining of the at least one test case corresponding to the target code segment from the preset logic library, the method further comprises:
acquiring a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence thereof;
constructing a directed acyclic graph corresponding to each code segment in each application project based on a plurality of logic nodes corresponding to each code segment in each application project and an arrangement sequence of the logic nodes, wherein the directed acyclic graph is used for representing code logics of the code segments, the directed acyclic graph comprises a plurality of primitive nodes and directed edges connected with the primitive nodes, each primitive node corresponds to each logic node, and the directed edges are used for indicating an execution sequence of the logic nodes;
performing path traversal on each directed acyclic graph, determining each traversed path as a test case, and obtaining at least one test case corresponding to each code segment in each application project;
and storing at least one test case corresponding to each code segment in the logic library according to the application item to which each code segment belongs.
8. A code testing apparatus, characterized in that the code testing apparatus comprises:
the code receiving module is used for receiving source codes of a plurality of application items, wherein the source codes of each application item comprise a test mark for identifying that a code fragment has been tested;
the coverage calculation module is used for respectively calculating the test coverage of each application item according to the source code of each application item and the test mark of each application item;
the target determining module is used for determining target application items of which the test coverage rates are smaller than a preset threshold value based on the test coverage rate of each application item;
the code traversing module is used for traversing the source code of the target application project to obtain a target code segment which does not contain the test mark in the target application project;
the annotation acquisition module is used for acquiring a target code annotation text corresponding to the target code segment from a preset annotation library;
and the script generation module is used for generating a network model based on a preset first script, processing the object code annotation text, and obtaining and executing a first object test script corresponding to the object code fragment, wherein the first script generation network model is a network model for generating a test script based on annotation information.
9. A code testing apparatus, characterized in that the code testing apparatus comprises: a memory and at least one processor, the memory having instructions stored therein;
the at least one processor invoking the instructions in the memory to cause the code testing apparatus to perform the code testing method of any of claims 1-7.
10. A computer-readable storage medium having instructions stored thereon, wherein the instructions, when executed by a processor, implement a code testing method as recited in any of claims 1-7.
CN202210424700.8A 2022-04-22 2022-04-22 Code testing method, device, equipment and storage medium Pending CN114817025A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210424700.8A CN114817025A (en) 2022-04-22 2022-04-22 Code testing method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210424700.8A CN114817025A (en) 2022-04-22 2022-04-22 Code testing method, device, equipment and storage medium

Publications (1)

Publication Number Publication Date
CN114817025A true CN114817025A (en) 2022-07-29

Family

ID=82504906

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210424700.8A Pending CN114817025A (en) 2022-04-22 2022-04-22 Code testing method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN114817025A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116225973A (en) * 2023-05-10 2023-06-06 贵州轻工职业技术学院 Chip code testing method and device based on embedded implementation electronic equipment

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116225973A (en) * 2023-05-10 2023-06-06 贵州轻工职业技术学院 Chip code testing method and device based on embedded implementation electronic equipment
CN116225973B (en) * 2023-05-10 2023-06-30 贵州轻工职业技术学院 Chip code testing method and device based on embedded implementation electronic equipment

Similar Documents

Publication Publication Date Title
US10901709B2 (en) Partitioning based migration of systems to container and microservice based platforms
US9280442B1 (en) System and method for generating coverage reports for software unit tests
US10108535B2 (en) Web application test script generation to test software functionality
US10642720B2 (en) Test case generator built into data-integration workflow editor
CN102945347A (en) Method, system and device for detecting Android malicious software
US20100275062A1 (en) Functional Coverage Using Combinatorial Test Design
CN103425584A (en) Large-scale application regression testing information processing method based on Java bytecode
US8117499B2 (en) Generation of a stimuli based on a test template
JP6486574B2 (en) Program code generating apparatus, program code generating method, and program code generating program
CN114817025A (en) Code testing method, device, equipment and storage medium
US8589734B2 (en) Verifying correctness of processor transactions
CN114416481A (en) Log analysis method, device, equipment and storage medium
CN116991750B (en) Pressure test method for realizing large concurrency simulation by utilizing multithreading and distributed agents
CN117632710A (en) Method, device, equipment and storage medium for generating test code
CN112988578A (en) Automatic testing method and device
EP2972880B1 (en) Kernel functionality checker
US20180253287A1 (en) Method for translation of assembler computer language to validated object-oriented programming language
CN111240987A (en) Migration program detection method and device, electronic equipment and computer readable storage medium
US8819645B2 (en) Application analysis device
US8554522B2 (en) Detection of design redundancy
Cassez et al. Wuppaal: Computation of worst-case execution-time for binary programs with uppaal
US11074069B2 (en) Replaying interactions with transactional and database environments with re-arrangement
CN108255802B (en) Universal text parsing architecture and method and device for parsing text based on architecture
US20240176722A1 (en) Automated public certification of specifications and software
US10509659B1 (en) Input processing logic to produce outputs for downstream systems using configurations

Legal Events

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