A method and terminal for automatically generating unit test code
A unit testing, automatic generation technology, applied in software testing/debugging, error detection/correction, instruments, etc., can solve the problem of time-consuming and laborious, and achieve the effect of reducing tedious and repetitive work, improving work efficiency and saving time
- Summary
- Abstract
- Description
- Claims
- Application Information
AI Technical Summary
Problems solved by technology
Method used
Image
Examples
Embodiment 1
[0059] Please refer to figure 1 , a method to automatically generate unit tests, including steps:
[0060] S1. Receive specified input and output for the unit under test;
[0061] Wherein, the input refers to the condition of the if statement in the code, and the output refers to the result when the condition is satisfied or not satisfied. If the source code of the unit under test is changed, such as by changing the three parameters into four parameters, 16 test cases need to be rewritten under the traditional method, but the present invention only needs to modify the input conditions, and the test cases can be automatically regenerated;
[0062] S2. Analyzing the input and output to obtain all possible branches;
[0063] S3. Analyze the input and output of each branch to determine whether piling is required, and if necessary, piling;
[0064] Specifically, by traversing all output result strings, perform semantic analysis on the strings in turn; judge whether the string is...
Embodiment 2
[0070] Apply the method described in Embodiment 1 to a specific example, in the java test:
[0071] First, the system provides the following methods for inputting parameters:
[0072] a). The method of setting conditions (hereinafter referred to as the if method), this method is used to receive the conditional statement provided by the developer. Suppose there is such a statement: return a>0? 1:0, the condition is "a>0" ;
[0073] b). The method of setting the return value when the condition is met (hereinafter referred to as the then method). This method is used to receive the value when the condition given in the if method provided by the developer is met. Taking the above statement as an example, it is "1" ;
[0074] c). The method of setting the return value when the condition is not met (hereinafter referred to as the else method). This method is used to receive the value when the condition given in the if method provided by the developer is not met. Taking the above st...
Embodiment 3
[0097] Apply the method described in Embodiment 2 to a specific example, in the java test:
[0098] Suppose a developer wants to write a unit test case for the following piece of code:
[0099] public class Foo{
[0100] public int getValue(int value1,int value2,int value3){
[0101] if(value1>0||value2>0||value3>0){
[0102] return MathTools. next(value1, value2, value3);
[0103]}
[0104] return 0;
[0105]}
[0106]}
[0107]The core of this code is only three lines, and the result is: when the three input parameters are all natural numbers, return the return value (MathTools.next) of a method of another class, otherwise return 0; Branch coverage will write 8 test cases, and it will take a lot of time to add later maintenance. In the case of using the solution of the present invention, the process is as follows:
[0108] S1. Receive the specified input and output for the unit under test, specifically:
[0109] (1) First, the developer calls the if method and enters t...
PUM
Login to View More Abstract
Description
Claims
Application Information
Login to View More 


