Unit test method, device, equipment and storage medium
By automatically generating unit test cases using neural network models, the problem of not being able to exhaustively list the scope of influence when manually writing test cases is solved, improving unit test coverage and code quality, and ensuring the integrity and reliability of the code.
Patent Information
- Application Number
- CN202311370474.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-20
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2043-10-20
AI Technical Summary
The existing method of manually writing unit test cases cannot exhaustively cover all the impact points of the corresponding code block, resulting in insufficient unit test coverage, which cannot further improve code quality and poses operational risks.
Unit test cases are generated through a neural network model. The neural network model is pre-built based on the factors that affect unit test coverage, the coverage is calculated, and the model parameters are adjusted when the preset expected value is not met until the coverage meets the requirements.
It enables comprehensive unit test case generation, improves unit test coverage, thereby enhancing code quality and reliability and reducing the probability of failures.
Smart Images

Figure CN118796658B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a unit testing method, device and equipment and storage medium. BACKGROUND
[0002] At present, after the development engineer completes the coding of product and demand, generally, the unit test cases are written and executed for the overall newly added and modified codes, so as to check the defects and vulnerabilities of the codes. Similarly, the unit test cases are also written by the test development engineer, so that the existing problems in the codes can be found simply and quickly. The existing method for writing unit test cases is mainly manual writing. However, the unit test cases written by manual writing cannot exhaust all the influence range points of the corresponding code blocks, and thus the coverage rate of the unit test cannot be improved, there are large design vulnerabilities, the quality of the codes cannot be further improved, and there are operation risks. SUMMARY
[0003] The main purpose of the present application is to provide a unit testing method, device, equipment and storage medium, which aims to solve the technical problem that the existing unit test cases written by manual writing cannot exhaust all the influence range points of the corresponding code blocks, and thus the coverage rate of the unit test cannot be improved.
[0004] To achieve the above purpose, the present application provides a unit testing method, which comprises:
[0005] generating a unit test case corresponding to the to-be-tested code through a neural network model, wherein the neural network model is constructed in advance based on influence factors of unit test coverage rate;
[0006] calculating the unit test coverage rate of the unit test case;
[0007] when the unit test coverage rate does not meet a preset expected value, adjusting the parameters of the neural network model, and returning to the step of generating the unit test case corresponding to the to-be-tested code through the neural network model until the unit test coverage rate meets the preset expected value.
[0008] Optionally, before the step of generating the unit test case corresponding to the to-be-tested code through the neural network model, the method further comprises:
[0009] performing feature analysis and feature extraction on the to-be-tested code to obtain influence factors of the unit test coverage rate, and taking the influence factors as input layer parameters;
[0010] determining the number of input layer nodes by modeling the test data of the influence factors;
[0011] Determine the number of hidden layer nodes according to the mapping relationship from the input layer vector to the output layer vector;
[0012] Construct a neural network model according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes.
[0013] Optionally, the feature analysis and feature extraction of the to-be-tested code are performed to obtain the influencing factors of the unit test coverage, and the influencing factors are taken as the input layer parameters, including:
[0014] The to-be-tested code is decomposed to obtain a code block set;
[0015] The variance contribution of each dimension reduction technique is calculated through a dimension reduction technique, and modeling data is selected according to the variance contribution;
[0016] The feature values of the modeling data are obtained through a dummy coding technique to obtain the influencing factors of the unit test coverage, and the influencing factors are taken as the input layer parameters, including the number of code blocks, the number of code lines of the code blocks, the number of classes of the code blocks, the number of methods of the code blocks, the number of branches of the code blocks, and the number of judgment conditions of the code blocks.
[0017] Optionally, the parameters of the neural network model are adjusted when the unit test coverage does not meet the preset expected value, including:
[0018] When the unit test coverage does not meet the preset expected value, the code type and the code quantity of the to-be-tested code are obtained, and the number of characteristic values of the neural network model is obtained;
[0019] The adjustment strategy value of the neural network model is calculated according to the unit test coverage, the code type, the number of characteristic values, and the code quantity;
[0020] When the adjustment strategy value is greater than or equal to a preset threshold value, the parameters of the neural network model are adjusted according to the gradient descent method, the additional momentum method, and the neural network training function;
[0021] When the adjustment strategy value is less than the preset threshold value, the parameters of the neural network model are adjusted according to a preset self-defined adjustment strategy.
[0022] Optionally, when the adjustment strategy value is less than the preset threshold value, the parameters of the neural network model are adjusted according to the preset self-defined adjustment strategy, including:
[0023] When the adjustment strategy value is less than the preset threshold value, a vector is generated according to the weight value and the bias value of the neural network model, and the vector is taken as an individual to generate a population;
[0024] After the population is generated, the population is initialized;
[0025] After initialization, the population is iteratively optimized to obtain target weight values and target bias values;
[0026] The parameters of the neural network model are adjusted according to the target weight values and the target bias values.
[0027] Optionally, after initialization, the population is iteratively optimized to obtain target weight values and target bias values, comprising:
[0028] After initialization, the individual positions of the population are iteratively updated within a preset population individual boundary;
[0029] When the individual positions of the population are iteratively updated, the neighbor individuals of the updated individual are synchronously updated through chaotic mapping based on a chaotic map;
[0030] The target weight values and the target bias values are determined according to the updated population.
[0031] Optionally, the unit test coverage of the unit test case is calculated, comprising:
[0032] The unit test case code is compiled through a code coverage test tool;
[0033] The compilation information of the unit test case code is compared with the code information of the code to be tested, and the unit test coverage of the unit test case is calculated according to the comparison result.
[0034] In addition, to achieve the above-mentioned purpose, the application further provides a unit test device, which comprises:
[0035] A generation module is configured to generate a unit test case corresponding to the code to be tested through a neural network model, wherein the neural network model is pre-constructed based on influencing factors of unit test coverage;
[0036] A calculation module is configured to calculate the unit test coverage of the unit test case;
[0037] An adjustment module is configured to adjust the parameters of the neural network model when the unit test coverage does not meet a preset expected value, and return to the step of generating a unit test case corresponding to the code to be tested through the neural network model until the unit test coverage meets the preset expected value.
[0038] In addition, to achieve the above object, the application further provides a unit test device, which comprises a memory, a processor and a unit test program stored in the memory and executable on the processor, and the unit test program is configured to implement the unit test method as described above.
[0039] In addition, to achieve the above object, the application further provides a storage medium, which stores a unit test program, and the unit test program is executed by a processor to implement the unit test method as described above.
[0040] In the application, the unit test case corresponding to the to-be-tested code is generated by a neural network model, the neural network model is constructed in advance based on the influencing factors of the unit test coverage, the unit test coverage of the unit test case is calculated, when the unit test coverage does not meet a preset expected value, the parameters of the neural network model are adjusted, and the step of generating the unit test case corresponding to the to-be-tested code by the neural network model is returned until the unit test coverage meets the preset expected value; since the unit test case corresponding to the to-be-tested code is automatically generated by the neural network model, and the error is minimized by continuously training the model and modifying the weight values of the neurons in each layer, a complete and comprehensive unit test case can be obtained, and thus the coverage of the unit test can be improved, and the quality of the code can be improved. BRIEF DESCRIPTION OF DRAWINGS
[0041] Figure 1 is a structural schematic diagram of a unit test device of a hardware running environment related to an embodiment scheme of the application;
[0042] Figure 2 is a flowchart of a first embodiment of the unit test method of the application;
[0043] Figure 3 is a flowchart of a second embodiment of the unit test method of the application;
[0044] Figure 4 is a flowchart of a third embodiment of the unit test method of the application;
[0045] Figure 5 is a schematic diagram of calculating the unit test coverage of an embodiment of the unit test method of the application;
[0046] Figure 6 is a whole flowchart of an embodiment of the unit test method of the application;
[0047] Figure 7 is a structural block diagram of a first embodiment of the unit test device of the application.
[0048] The implementation of the object, the functional features and the advantages of the application will be further described with reference to the embodiments and the accompanying drawings. DETAILED DESCRIPTION
[0049] It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
[0050] Referring to Figure 1 , Figure 1 The unit test equipment structure diagram of the hardware running environment involved in the embodiment of the present application.
[0051] As Figure 1 shown, the unit test equipment can include a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. The communication bus 1002 is used to realize the connection and communication between the components. The user interface 1003 can include a display screen (Display), and the optional user interface 1003 can also include a standard wired interface, a wireless interface. The wired interface of the user interface 1003 can be a USB interface in the present application. The network interface 1004 can optionally include a standard wired interface, a wireless interface (such as a wireless fidelity (Wireless-Fidelity, Wi-Fi) interface). The memory 1005 can be a high-speed random access memory (RAM), and can also be a stable memory (Non-volatile Memory, NVM), such as a disk memory. The memory 1005 can also be an independent storage device from the aforementioned processor 1001.
[0052] Those skilled in the art can understand that Figure 1 the structure shown in the above description does not constitute a limitation on the unit test equipment, and can include more or fewer components than the diagram, or combine certain components, or different component arrangements.
[0053] As Figure 1 shown, the memory 1005, which is identified as a computer storage medium, can include an operating system, a network communication module, a user interface module, and a unit test program.
[0054] In the unit test equipment shown in Figure 1 , the network interface 1004 is mainly used to connect a background server and communicate data with the background server; the user interface 1003 is mainly used to connect a user device; the unit test equipment calls the unit test program stored in the memory 1005 through the processor 1001, and executes the unit test method provided by the embodiment of the present application.
[0055] Based on the hardware structure, an embodiment of the unit test method is provided.
[0056] Referring to Figure 2 , Figure 2 A flowchart of the first embodiment of the unit test method is provided.
[0057] It should be understood that at present, after the development engineer completes the coding of the product and the requirements, generally, the unit test cases are written and executed for the newly added and modified codes in the whole, so as to check the defects and vulnerabilities of the codes, thereby improving the code quality. The unit test cases are also written by the test development engineer, so that the existing problems in the codes can be easily and quickly found, thereby reducing the test execution time in the functional test process and accelerating the iteration speed of product evolution. In general, the execution of unit test can reduce the maintenance cost of product life cycle, realize test left shift, help the development and test personnel to find the problems in the codes as early as possible, improve the code reliability, and reduce the probability of failure.
[0058] In the prior art, the unit test coverage rate is mainly improved by manually writing test cases for the running logic of the codes and the branches of the codes, and the unit test can also be designed in the ways of statement coverage, decision coverage, condition coverage, decision condition coverage, condition combination test, and path coverage. However, through the manual way, all the code influence ranges that can be exhausted by the unit test cases cannot be obtained, especially some abnormal branches and abnormal data, which will bring certain risks to the reliability and predictability of the codes.
[0059] Therefore, in order to overcome the above defects, in the embodiment, the unit test cases corresponding to the to-be-tested codes are automatically generated through a neural network model, and the error is minimized by continuously training the model and modifying the weights of the neurons in each layer, so that complete and comprehensive unit test cases can be obtained, thereby improving the unit test coverage rate and improving the quality of the codes.
[0060] In the first embodiment, the unit test method comprises:
[0061] Step S10: generating the unit test cases corresponding to the to-be-tested codes through a neural network model, wherein the neural network model is constructed in advance based on the influence factors of the unit test coverage rate.
[0062] It can be understood that the execution subject of the embodiment can be a unit test device with data processing, network communication and program running functions, for example, a computer or other electronic devices capable of realizing the same or similar functions, and the embodiment does not limit this.
[0063] It should be understood that the neural network model can be a back propagation (BP) neural network model, and the neural network model is pre-constructed based on the influencing factors of the unit test coverage, for example, feature analysis and feature extraction are performed on the to-be-tested code, the influencing factors of the unit test coverage are obtained, and the influencing factors are taken as input layer parameters, the number of input layer nodes is determined by testing data modeling on the influencing factors, the number of hidden layer nodes is determined according to a mapping relationship from an input layer vector to an output layer vector, and the neural network model is constructed according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes, which is not limited in the embodiment.
[0064] It can be understood that the unit test case corresponding to the to-be-tested code can be obtained at the output layer of the neural network model.
[0065] Step S20: Calculate the unit test coverage of the unit test case.
[0066] It should be understood that the unit test coverage of the unit test case can be calculated by a code coverage test tool. The code coverage test tool can be pre-set, and in the embodiment, the jacoco program is taken as an example for illustration.
[0067] Step S30: When the unit test coverage does not meet the preset expected value, the parameters of the neural network model are adjusted, and the step of generating the unit test case corresponding to the to-be-tested code by the neural network model is returned until the unit test coverage meets the preset expected value.
[0068] It should be understood that when the unit test coverage does not meet the preset expected value, the error back calculation principle of the BP neural network is used to modify the hidden layer parameters synchronously, the parameters of the hidden layer such as the weight and bias values of the neural network are adjusted in the error back repair process, and finally the goal of improving the unit test coverage is met.
[0069] In the embodiment, the unit test case corresponding to the to-be-tested code is generated through a neural network model, the neural network model is constructed in advance based on factors influencing unit test coverage, the unit test coverage of the unit test case is calculated, when the unit test coverage does not meet a preset expected value, the parameters of the neural network model are adjusted, and the step of generating the unit test case corresponding to the to-be-tested code through the neural network model is returned until the unit test coverage meets the preset expected value; since the unit test case corresponding to the to-be-tested code is automatically generated through the neural network model, and the error is minimized by continuously training the model and modifying the weight values of the neurons in each layer, a complete and comprehensive unit test case can be obtained, and thus the coverage of the unit test can be improved, and the quality of the code can be improved.
[0070] With reference to Figure 3 , Figure 3 The flowchart of the second embodiment of the unit test method of the present application is shown in FIG. 2, which is based on the first embodiment shown in FIG. 1. Figure 2 The second embodiment of the unit test method of the present application is proposed based on the first embodiment shown in FIG. 1.
[0071] In the second embodiment, before the step S10, the following steps are further included.
[0072] Step S01: performing feature analysis and feature extraction on the to-be-tested code to obtain factors influencing unit test coverage, and taking the factors as input layer parameters.
[0073] It should be understood that, in order to facilitate subsequent unit testing and improve the efficiency of unit testing, in the embodiment, feature analysis and feature extraction are performed on the to-be-tested code in advance to obtain factors influencing unit test coverage, and the factors are taken as input layer parameters, the number of input layer nodes is determined through testing data modeling of the factors, the number of hidden layer nodes is determined according to the mapping relationship from the input layer vector to the output layer vector, and the neural network model is constructed according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes.
[0074] It should be noted that the to-be-tested code can be code for which a unit test case needs to be generated.
[0075] It can be understood that the feature analysis and feature extraction on the to-be-tested code to obtain factors influencing unit test coverage can be performed through a preset analysis model on the to-be-tested code to obtain factors influencing unit test coverage. The preset analysis model can be set in advance and used for analyzing code.
[0076] Further, considering that there are a large amount of noise data and redundant impurities in various types of original information of the code, the embodiment adopts a processing mode combining dimension reduction technology and dummy coding to process the code. The step S01 includes: decomposing the code to be tested to obtain a code block set; calculating the variance contribution of each dimension reduction technology by using the dimension reduction technology, and selecting modeling data according to the variance contribution; obtaining the influencing factors of the unit test coverage by using the dummy coding technology to take values of features of the modeling data, and taking the influencing factors as input layer parameters. The influencing factors include the number of code blocks, the number of code lines of the code blocks, the number of classes of the code blocks, the number of methods of the code blocks, the number of branches of the code blocks, and the number of judgment conditions of the code blocks.
[0077] It can be understood that when the features of the code to be tested are analyzed and extracted, the code to be tested is decomposed to obtain code blocks. Considering that there are a large amount of noise data and redundant impurities in various types of original information of the code, the embodiment adopts a processing mode combining PCA dimension reduction technology and dummy coding to process the code blocks. The PCA dimension reduction technology can retain some important features of high-dimensional data in the code blocks, so that the data is more suitable for analyzing the data set to establish a data model. The dummy coding can quickly encode the category data into specific numerical data for use as subsequent neural network parameters. Specifically, the processing process of the embodiment is as follows:
[0078] Step one: the code to be tested is decomposed by a java code decomposer to obtain a code block set, which is divided into the following types: code block, code block line, code block statement, code block method, code block condition judgment, code block branch, code comment, pseudo code, etc.
[0079] Step two: the variance contribution of each code block set is calculated by using the principal component analysis (PCA) dimension reduction technology, the contribution is sorted from high to low, and the noise data and redundant impurities in the code information are removed. In order to make the feature values more complete and sufficient, according to the characteristics of unit testing, the top five are taken as feature values of modeling data, which are the number of code blocks, the number of code lines, the number of code classes, the number of code methods, and the number of code branches.
[0080] Step three: using the dummy coding technology of one-hot encoding, the code block information is one-hot encoded as discrete data, the value of the discrete feature is expanded to the Euclidean space, so that the non-continuous data is more close to the feature value and also more reasonable, after one-hot encoding, the modeling character type data of the feature value is converted to numerical type data to generate the corresponding test data model, that is, the number of code blocks, the number of code lines of the code blocks, the number of classes of the code blocks, the number of methods of the code blocks, and the number of branches of the code blocks are taken as the parameter feature values of the test data model.
[0081] Step S02: determining the number of input layer nodes by testing data modeling of the influence factors.
[0082] It can be understood that by continuously testing data modeling of the influence factors of unit test coverage rate through PCA dimension reduction technology and dummy coding technology, the number of code blocks, the number of code lines of the code blocks, the number of classes of the code blocks, the number of methods of the code blocks, and the number of branches of the code blocks are selected as the number of input layer nodes of the BP neural network model to improve the unit test coverage rate.
[0083] Step S03: determining the number of hidden layer nodes according to the mapping relationship from the input layer vector to the output layer vector.
[0084] It should be understood that the neurons between adjacent layers of the BP neural network model are fully connected, therefore, it is necessary to continuously train a complex nonlinear model to build a multi-model multi-level neural network to achieve the optimal value.
[0085] In the BP neural network model, the input layer is set to include a number of nodes, and the output layer is set to include c number of nodes, so the whole can be regarded as a mapping relationship from an a-dimensional vector to a c-dimensional vector. Therefore, the number of hidden layer nodes b needs to be determined to make the a-dimensional vector obtain a c-dimensional vector through the number of hidden layer nodes b. When determining the number of hidden layer nodes b, a plurality of groups of data are appropriately taken as the feature values of the input layer, and the key factors of the feature values are input into the BP neural network for unit test coverage rate improvement through the parameter normalization principle for repeated training.
[0086] Step S04: constructing a neural network model according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes.
[0087] It can be understood that after determining the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes, a neural network model can be constructed according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes.
[0088] In this embodiment, feature analysis and feature extraction are performed on the code to be tested in advance to obtain the influencing factors of unit test coverage. The influencing factors are used as input layer parameters. The number of input layer nodes is determined by modeling the test data of the influencing factors. The number of hidden layer nodes is determined according to the mapping relationship between the input layer vector and the output layer vector. A neural network model is constructed based on the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes, which facilitates subsequent unit testing and improves unit testing efficiency.
[0089] Reference Figure 4 , Figure 4 This is a flowchart illustrating the third embodiment of the unit testing method of the present invention, based on the above. Figure 2 The first embodiment shown presents a third embodiment of the unit testing method of the present invention.
[0090] In the third embodiment, step S20 includes:
[0091] Step S201: Compile the unit test case code using a code coverage testing tool.
[0092] It should be understood that, in order to more accurately calculate the unit test coverage of unit test cases, in this embodiment, the unit test case code is compiled using a code coverage testing tool, the compilation information of the unit test case code is compared with the code information of the code to be tested, and the unit test coverage of the unit test cases is calculated based on the comparison result.
[0093] It should be noted that the code coverage testing tool can be preset. In this embodiment, the jacoco program is used as an example for illustration.
[0094] Step S202: Compare the compilation information of the unit test case code with the code information of the code to be tested, and calculate the unit test coverage of the unit test case based on the comparison result.
[0095] For ease of understanding, please refer to Figure 5 This description is provided but does not limit the scope of the invention. Figure 5 This diagram illustrates the calculation of unit test coverage in an embodiment of the unit testing method of the present invention. In the diagram, jacoco receives the unit test case information values from the current training. jacoco compares the received unit test case information with the Git address to calculate the unit test case coverage of the current code block or set of code blocks. In fact, jacoco mainly performs the following steps in calculating the unit test coverage of a code block: code execution, compilation, execution of unit tests, statistics, and report generation. The specific process is as follows:
[0096] After the BP neural network model is trained, the unit test case of the output layer is named "test", and the original code block input to the model is named "code". First, jacoco compiles the code block into a.class file, and then internally compiles it into a package. The whole process is to compile the initial code block into a.class file first, and then compile it into an executable file.
[0097] The above process is mainly executed by the Apache Maven built-in jacoco, and the unit test case is executed in the form of a synchronous script. The script operation path of the unit test execution accesses the source code of the business, and at the same time, the execution information of the entire code block and the unit test case is collected by jacoco, and which code is tested by the unit test is also counted.
[0098] Specifically, the unit test coverage calculation formula is as follows:
[0099] Code block coverage=(jacoco collected code block number of execution script / code block total number)*100%;
[0100] Code line coverage=(jacoco collected executed script statements(excluding header files, code comments, empty lines, etc.) / total number of code lines)*100%;
[0101] Code class coverage=(jacoco executed each class unit test code / code class total number)*100%;
[0102] Code method coverage=(jacoco executed each method unit test code / code class total number)*100%;
[0103] Code branch coverage=(jacoco executed each branch unit test code / code class total number)*100%;
[0104] An intermediate file is generated by internal statistical analysis and comparison, and finally a jacoco unit test coverage report is generated.
[0105] In the third embodiment, the step S30 comprises:
[0106] Step S301: When the unit test coverage does not meet the preset expected value, the code type and code quantity of the code to be tested are obtained, and the number of characteristic values of the neural network model is obtained.
[0107] It should be understood that when the unit test coverage does not meet the preset expectation value, the error back calculation principle of the BP neural network is used to modify the hidden layer parameters in synchronization, and the hidden layer adjusts the parameters such as the weight and bias value of the neural network during the error back repair process, so as to finally meet the goal of improving the unit test coverage.
[0108] Step S302: Calculate the adjustment strategy value of the neural network model according to the unit test coverage, the code type, the number of characteristic values, and the number of codes.
[0109] It can be understood that when the weight and bias value of the neural network is modified and adjusted, the adjustment strategy value is calculated first, and the adjustment strategy of the weight and bias value of the neural network is determined according to the adjustment strategy value.
[0110] When calculating the adjustment strategy value P, the following formula is used for calculation:
[0111]
[0112] Wherein, S is the unit test coverage calculated by jacoco, when the unit test coverage is higher, that is, the value of S is higher, the value of P is larger; v is the business importance degree corresponding to the characteristic value of the code, v can be determined according to the business type corresponding to the code, or can be pre-labeled by relevant personnel, when the business importance degree corresponding to the code is larger, the value of v is larger, and correspondingly, the value of P is smaller; w is the number of characteristic values, when the number of characteristic values is larger, the parameters to be adjusted are more, at this time, the value of w is larger, and correspondingly, the value of P is smaller; m is the number of codes to be tested at present, if the current belongs to the test peak, such as the period when the new version is put into operation, the number of codes to be tested is larger, at this time, the value of m is larger, and correspondingly, the value of P is smaller.
[0113] Step S303: When the adjustment strategy value is greater than or equal to the preset threshold value, adjust the parameters of the neural network model according to the gradient descent method, the additional momentum method and the neural network training function.
[0114] It should be noted that the preset threshold value can be pre-set, and the preset threshold value can be an adjustment strategy threshold value.
[0115] It should be understood that after the adjustment strategy value is calculated, the adjustment strategy value is compared with the preset adjustment strategy threshold value, when the adjustment strategy value is not less than the adjustment strategy threshold value, it indicates that the current weight value and the bias value are less different from the ideal weight value and the bias value, or the current weight value and the bias value do not need to be very accurate, or the current needs to meet the time requirement more, therefore, the weight value and the bias value do not need to be adjusted greatly, since the weight value and the bias value optimization according to the self-developed optimization algorithm needs to spend a long time, in order to give the relatively accurate weight value and the bias value improvement result meeting the time requirement as soon as possible, the embodiment will combine the gradient descent method, the additional momentum method and the neural network training function in matlab to quickly train and adjust the neural network weight value and the bias value of the embodiment.
[0116] Step S303': when the adjustment strategy value is less than the preset threshold value, adjusting the parameters of the neural network model according to the preset self-defined adjustment strategy.
[0117] It should be understood that after the adjustment strategy value is calculated, the adjustment strategy value is compared with the preset adjustment strategy threshold value, when the adjustment strategy value is less than the adjustment strategy threshold value, it indicates that the current weight value and the bias value are greatly different from the ideal weight value and the bias value, or the current weight value and the bias value need to be more accurate, therefore, the weight value and the bias value need to be adjusted greatly, at this time, the weight value and the bias value of the neural network can be adjusted according to the self-developed optimization algorithm.
[0118] Further, the step S303' comprises: when the adjustment strategy value is less than the preset threshold value, generating a vector according to the weight value and the bias value of the neural network model, and generating a population with the vector as an individual; after the population is generated, initializing the population; after the initialization, iteratively optimizing the population to obtain a target weight value and a target bias value; and adjusting the parameters of the neural network model according to the target weight value and the target bias value.
[0119] Further, after the initialization, iteratively optimizing the population to obtain a target weight value and a target bias value comprises: after the initialization, iteratively updating the individual position of the population within a preset population individual boundary; when the individual position of the population is iteratively updated, synchronously updating the neighbor individual of the updated individual based on a chaotic map through chaotic mapping; and determining the target weight value and the target bias value according to the updated population.
[0120] For the convenience of understanding, reference Figure 6 is made to the accompanying drawings for the purpose of explanation and is not intended to limit the present application. Figure 6For the overall flowchart of an embodiment of the unit test method of the present application, when adjusting the weight and bias values of the neural network according to the self-developed optimization algorithm, a vector is first generated according to the weight and bias values of the neural network, wherein the vector is represented as:
[0121] V = (W i,j ∪W j,m ∪V n,e ∪V e,r )
[0122] wherein W i,j represents the weight value between the input layer and the hidden layer in the network structure, W j,m represents the weight value between the hidden layer and the output layer; V n,e represents the bias value between the input layer and the hidden layer; and V e,r represents the bias value between the hidden layer and the output layer.
[0123] After obtaining the vector, the vector can be used as an individual to generate a population, and after generating the population, the population is initialized, and after initialization, the population is iteratively optimized.
[0124] In the optimization, the updated position after each iteration is:
[0125]
[0126] wherein, is the current individual position; is the updated individual position; α is a random number between 0 and 1; is the best individual position in the current iteration; and ρ i represents the attraction value of the current individual to other individuals. The attraction value ρ i can be determined according to the following formula:
[0127]
[0128]
[0129] wherein γ is a random number between 0 and 1; the DIS function is the Euclidean distance calculation formula; T is the set maximum number of iterations; and the COUNT function is a counting function. is the other individual in the current population except the current individual and the best individual. The attraction value of the current individual to other individuals is determined according to the distance between the current individual and other individuals. The more the number of other individuals within the distance L i from the current individual, the greater the attraction value of the current individual to other individuals.
[0130] In addition, in order to accelerate the optimization speed, the population individual boundary is set in the embodiment, wherein the population individual boundary is determined by the following formula:
[0131]
[0132]
[0133] wherein p represents the population individual position boundary value, the maximum value of p calculated according to the above formula represents the upper boundary of the population individual position, and the minimum value of p calculated according to the above formula represents the lower boundary of the population individual position. represents the current individual position as the diagonal matrix value of the coverage rate of each code attribute value under the weight value and the deviation value corresponding to the current position; q1 represents the code block coverage rate under the weight value and the deviation value corresponding to the current position; q2 represents the code line coverage rate under the weight value and the deviation value corresponding to the current position; q3 represents the code class coverage rate under the weight value and the deviation value corresponding to the current position; q4 represents the code method coverage rate under the weight value and the deviation value corresponding to the current position; q5 represents the code branch coverage rate under the weight value and the deviation value corresponding to the current position; V represents the position of the code related to the interface test in the code to be tested; M represents the position of the code related to the function test in the code to be tested; E represents the position of the code related to the UI interface test in the code to be tested; G represents the position of the code related to the unit test in the code to be tested; and R represents the total coverage rate value calculated by jacoco under the weight value and the deviation value corresponding to the current position.
[0134] In the embodiment, in order to further improve the iteration efficiency, the neighbor individual of the current individual is also updated when the current individual position is updated each time. When updating, the optimization algorithm adopts a chaotic mapping method based on a chaotic map to update the neighbor individual, so as to improve the global search ability.
[0135] Specifically, the optimization algorithm combines the Chebyshev chaotic map to perform chaotic mapping, and the mapping formula is:
[0136]
[0137] wherein, is the population individual whose distance to is less than the preset distance.
[0138] After the population optimization is completed, the best weight value and the best deviation value can be obtained, and the optimal BP neural network model can be obtained according to the best weight value and the best deviation value.
[0139] When the neural network weight and bias value of the embodiment is quickly trained and adjusted according to the gradient descent method, the momentum method and the neural network training function in matlab, the embodiment is not described in detail because it can be realized based on the existing algorithm.
[0140] After the infinite cycle training, the output unit test case information meets the expected value of the current unit test coverage, the parameters of the hidden layer are recorded, the training is completed, and the current BP neural network model meets the expected value of the unit test coverage.
[0141] In addition, with reference to Figure 7 The embodiment of the present application also provides a unit test device.
[0142] It should be understood that at present, after the development engineer completes the coding of the product and the demand, the unit test case and execution of the overall newly added and modified code are generally written, so as to check the defects and vulnerabilities of the code, thereby improving the code quality. The unit test case is also written by the test development engineer, so that the existing problems in the code can be easily and quickly found, so as to reduce the test execution time in the functional test process and accelerate the iteration speed of product evolution. In general, the execution of the unit test can reduce the cost of product life cycle maintenance, realize test left shift, help the development and test personnel to find the problems in the code as soon as possible, improve the code reliability, and reduce the probability of fault occurrence.
[0143] In the prior art, the unit test coverage is mainly improved by manually writing test cases according to the running logic of the code and the branches of the code, and the unit test can also be designed in the modes of statement coverage, decision coverage, condition coverage, decision condition coverage, condition combination test and path coverage. However, through the manual mode, all the code influence ranges exhausted by the unit test case cannot be obtained, especially some abnormal branches and abnormal data, which will bring certain risks to the reliability and predictability of the code.
[0144] Therefore, in order to overcome the above defects, in the embodiment, the unit test case corresponding to the to-be-tested code is automatically generated through the neural network model, and the error is minimized by continuously training the model and modifying the weight of each layer of neurons, so that complete and comprehensive unit test cases can be obtained, and the unit test coverage and the code quality can be improved.
[0145] In the embodiment, the unit test device comprises:
[0146] The generating module 10 is configured to generate the unit test case corresponding to the to-be-tested code through a neural network model, and the neural network model is constructed in advance based on the influence factors of the unit test coverage.
[0147] It should be understood that the neural network model can be a back propagation (BP) neural network model, and the neural network model is pre-constructed based on the influencing factors of the unit test coverage, for example, feature analysis and feature extraction are performed on the to-be-tested code, the influencing factors of the unit test coverage are obtained, and the influencing factors are taken as input layer parameters, the number of input layer nodes is determined by testing data modeling on the influencing factors, the number of hidden layer nodes is determined according to a mapping relationship from an input layer vector to an output layer vector, and the neural network model is constructed according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes, which is not limited in the embodiment.
[0148] It can be understood that the unit test case corresponding to the to-be-tested code can be obtained at the output layer of the neural network model.
[0149] The computing module 20 is configured to calculate the unit test coverage of the unit test case.
[0150] It should be understood that the unit test coverage of the unit test case can be calculated by a code coverage test tool. The code coverage test tool can be pre-set, and in the embodiment, the jacoco program is taken as an example for illustration.
[0151] The adjusting module 30 is configured to adjust parameters of the neural network model when the unit test coverage does not meet the preset expected value, and return to the step of generating the unit test case corresponding to the to-be-tested code by the neural network model until the unit test coverage meets the preset expected value.
[0152] It should be understood that when the unit test coverage does not meet the preset expected value, the error back calculation principle of the BP neural network is used to modify the hidden layer parameters synchronously, the hidden layer adjusts parameters such as weights and bias values of the neural network in the error back repair process, and finally the goal of improving the unit test coverage is met.
[0153] In the embodiment, a unit test case corresponding to the to-be-tested code is generated by a neural network model, the neural network model is pre-constructed based on influencing factors of unit test coverage, the unit test coverage of the unit test case is calculated, when the unit test coverage does not meet a preset expected value, parameters of the neural network model are adjusted, and the step of generating the unit test case corresponding to the to-be-tested code by the neural network model is returned until the unit test coverage meets the preset expected value. Since the unit test case corresponding to the to-be-tested code is automatically generated by the neural network model in the embodiment, and the error is minimized by continuously training the model and modifying the weight values of the neurons in each layer, a complete and comprehensive unit test case can be obtained, and the coverage of the unit test and the quality of the code can be improved.
[0154] In an embodiment, the unit test device further comprises:
[0155] The construction module is configured to perform feature analysis and feature extraction on the to-be-tested code, obtain influencing factors of unit test coverage, and take the influencing factors as input layer parameters; determine the number of input layer nodes by modeling test data on the influencing factors; determine the number of hidden layer nodes according to the mapping relationship from the input layer vector to the output layer vector; and construct a neural network model according to the input layer parameters, the number of input layer nodes, and the number of hidden layer nodes.
[0156] In an embodiment, the construction module is further configured to decompose the to-be-tested code to obtain a code block set; calculate the variance contribution of each dimension reduction technique by using a dimension reduction technique, and select modeling data according to the variance contribution; perform feature value acquisition on the modeling data by using a dummy coding technique to obtain the influencing factors of unit test coverage, and take the influencing factors as input layer parameters. The influencing factors include the number of code blocks, the number of code lines of the code blocks, the number of classes of the code blocks, the number of methods of the code blocks, the number of branches of the code blocks, and the number of judgment conditions of the code blocks.
[0157] In an embodiment, the adjustment module 30 is further configured to, when the unit test coverage does not meet a preset expected value, obtain the code type and the code quantity of the to-be-tested code, and obtain the number of characteristic values of the neural network model; calculate an adjustment strategy value of the neural network model according to the unit test coverage, the code type, the number of characteristic values, and the code quantity; when the adjustment strategy value is greater than or equal to a preset threshold value, adjust the parameters of the neural network model according to the gradient descent method, the additional momentum method, and the neural network training function; and when the adjustment strategy value is less than the preset threshold value, adjust the parameters of the neural network model according to a preset self-defined adjustment strategy.
[0158] In an embodiment, the adjusting module 30 is further configured to generate a vector according to the weight value and the bias value of the neural network model and generate a population with the vector as an individual when the adjustment strategy value is less than a preset threshold value.
[0159] After the population is generated, the population is initialized.
[0160] After the population is initialized, the population is iteratively optimized to obtain target weight values and target bias values.
[0161] The parameters of the neural network model are adjusted according to the target weight values and the target bias values.
[0162] In an embodiment, the adjusting module 30 is further configured to iteratively update the position of an individual of the population within a preset population individual boundary after the population is initialized; when the position of the individual of the population is iteratively updated, the neighbor individual of the updated individual is synchronously updated through chaotic mapping based on a chaotic map; and the target weight values and the target bias values are determined according to the updated population.
[0163] In an embodiment, the computing module 20 is further configured to compile the unit test case code through a code coverage test tool; compare the compilation information of the unit test case code with the code information of the code to be tested, and calculate the unit test coverage of the unit test case according to the comparison result.
[0164] Other embodiments or specific implementations of the unit test device according to the present application can refer to the above-mentioned method embodiments, and will not be described here.
[0165] In addition, an embodiment of the present application further proposes a storage medium, the storage medium stores a unit test program, and the unit test program is executed by a processor to implement the unit test method as described above.
[0166] It should be noted that in this document, the term "comprising" or "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such a process, method, article or system. Without more limitations, the element defined by the statement "including a" does not exclude the presence of additional identical elements in the process, method, article or system including the element.
[0167] The above-mentioned embodiment numbers of the present application are only for description, and do not represent the advantages and disadvantages of the embodiments.
[0168] Those skilled in the art can clearly understand the above-mentioned embodiment method can be realized by means of software and the necessary general hardware platform, of course, can also be realized by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present application essentially or say the part of the prior art contribution can be embodied in the form of a software product, which is stored in a storage medium (such as a read-only memory image (Read Only Memory image, ROM) / random access memory (Random Access Memory, RAM), a magnetic disk, an optical disk), including a plurality of instructions for making a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) execute the method described in various embodiments of the present application.
[0169] The above is only the preferred embodiment of the present application, and does not limit the patent scope of the present application, and any equivalent structure or equivalent flow transformation made by using the content of the specification and drawings, or directly or indirectly applied to other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A method of unit testing, characterized by, The unit test method comprises: generating a unit test case corresponding to the to-be-tested code through a neural network model, the neural network model being pre-constructed based on influencing factors of unit test coverage; calculating a unit test coverage of the unit test case; when the unit test coverage does not satisfy a preset expected value, adjusting parameters of the neural network model, and returning to the step of generating the unit test case corresponding to the to-be-tested code through the neural network model until the unit test coverage satisfies the preset expected value; wherein the adjusting the parameters of the neural network model when the unit test coverage does not satisfy the preset expected value comprises: when the unit test coverage does not satisfy the preset expected value, obtaining a code type and a code quantity of the to-be-tested code, and obtaining a quantity of characteristic values of the neural network model; calculating an adjustment strategy value of the neural network model according to the unit test coverage, the code type, the quantity of characteristic values, and the code quantity; when the adjustment strategy value is greater than or equal to a preset threshold value, adjusting the parameters of the neural network model according to a gradient descent method, an additional momentum method, and a neural network training function; when the adjustment strategy value is less than the preset threshold value, adjusting the parameters of the neural network model according to a preset self-defined adjustment strategy.
2. The unit testing method of claim 1, wherein, Before the generating the unit test case corresponding to the to-be-tested code through the neural network model, the method further comprises: performing feature analysis and feature extraction on the to-be-tested code to obtain influencing factors of unit test coverage, and taking the influencing factors as input layer parameters; determining a quantity of input layer nodes through modeling of test data of the influencing factors; determining a quantity of hidden layer nodes according to a mapping relationship from an input layer vector to an output layer vector; constructing a neural network model according to the input layer parameters, the quantity of input layer nodes, and the quantity of hidden layer nodes.
3. The method of claim 2, wherein, The performing feature analysis and feature extraction on the to-be-tested code to obtain the influencing factors of unit test coverage, and taking the influencing factors as the input layer parameters comprises: decomposing the to-be-tested code to obtain a code block set; calculating variance contribution degrees of each dimension reduction technique through a dimension reduction technique, and selecting modeling data according to the variance contribution degrees; performing feature value extraction on the modeling data through a dummy encoding technique to obtain the influencing factors of unit test coverage, and taking the influencing factors as the input layer parameters, the influencing factors comprising a code block quantity, a code line quantity of a code block, a class quantity of a code block, a method quantity of a code block, a branch quantity of a code block, and a judgment condition quantity of a code block.
4. The method of claim 1, wherein, The adjusting the parameters of the neural network model according to the preset self-defined adjustment strategy when the adjustment strategy value is less than the preset threshold value comprises: when the adjustment strategy value is less than the preset threshold value, generating a vector according to weight values and bias values of the neural network model, and taking the vector as an individual to generate a population; after the population is generated, initializing the population; after the initialization, iteratively optimizing the population to obtain target weight values and target bias values; Adjust parameters of the neural network model according to the target weight value and the target bias value.
5. The method of claim 4, wherein, The target weight value and the target bias value are obtained by iteratively optimizing the population after initialization. The individual positions of the population are iteratively updated within a preset population individual boundary after initialization. When the individual positions of the population are iteratively updated, the neighbor individuals of the updated individual are synchronously updated by chaotic mapping based on a chaotic map. The target weight value and the target bias value are determined according to the updated population.
6. The unit testing method of any one of claims 1 to 3, wherein, The unit test coverage of the unit test case is calculated, including: Compiling the unit test case code by a code coverage test tool; Comparing the compilation information of the unit test case code with the code information of the code to be tested, and calculating the unit test coverage of the unit test case according to the comparison result.
7. A unit testing apparatus, characterized by, The unit test device comprises: A generation module configured to generate a unit test case corresponding to the code to be tested by a neural network model, wherein the neural network model is pre-constructed based on influencing factors of unit test coverage; A calculation module configured to calculate the unit test coverage of the unit test case; An adjustment module configured to adjust parameters of the neural network model when the unit test coverage does not meet a preset expected value, and return to the step of generating the unit test case corresponding to the code to be tested by the neural network model until the unit test coverage meets the preset expected value. The adjustment module is further configured to, when the unit test coverage does not meet the preset expected value, obtain a code type and a code quantity of the code to be tested, and obtain a number of characteristic values of the neural network model; calculate an adjustment strategy value of the neural network model according to the unit test coverage, the code type, the number of characteristic values, and the code quantity; when the adjustment strategy value is greater than or equal to a preset threshold value, adjust the parameters of the neural network model according to a gradient descent method, an additional momentum method, and a neural network training function; and when the adjustment strategy value is less than the preset threshold value, adjust the parameters of the neural network model according to a preset self-defined adjustment strategy.
8. A unit testing device, characterized by The unit test device comprises a memory, a processor, and a unit test program stored on the memory and executable on the processor, and the unit test program, when executed by the processor, implements the unit test method of any one of claims 1 to 6.
9. A storage medium, characterized by The storage medium stores a unit test program, and the unit test program, when executed by the processor, implements the unit test method of any one of claims 1 to 6.
Citation Information
Patent Citations
Method and system for training yield prediction model and related equipment
CN114169505A
Case generation model training method, test case generation method and storage medium
CN115168218A