Invention content
In regression test, carry out white-box testing when will from it is a large amount of can be used test case in choose it is important, take off wrong energy
The strong test case preferential treatment ranging sequence of power is tested, and to improve the ability for finding defect in test process, is at this moment needed
The execution order of test case is planned again, achievees the purpose that improve testing efficiency.Traditional genetic algorithm is being found
There is a problem of that convergence rate is slow, stability is poor during the best sequence of test case, therefore is used for there is an urgent need for seeking one kind
The new method of priorities of test cases sequence.
The technical scheme is that:A kind of priorities of test cases sort method for white-box testing specifically includes
Following steps:
Step 1:For a program to be measured, tested using designed test use cases, by test case
The sentence coverage condition for treating ranging sequence is recorded, and the sentence coverage information matrix A that test case treats ranging sequence is obtained;It is false
If certain program to be measured has m sentence, tested with n test case, if test use cases are indicated with Φ, Φ={ T1,
T2,…,Ti,…,Tn, wherein Ti(1≤i≤n) is i-th of test case that test case is concentrated, the sentence covering letter of construction
The size of matrix A is ceased for n × m, the range of statement number is 1 to m in program to be measured, and the range of test case number is 1 to n,
If i-th of test case covers j-th of sentence in executing, then Aij=1, otherwise Aij=0;
Step 2:Coding;Priorities of test cases sequencing problem is for given program to be measured and test use cases, individual
Indicate that a priorities of test cases collating sequence, priorities of test cases collating sequence are exactly to be made of test case number
Ordered sequence, i.e. each individual is encoded as an orderly test case number string, and the length of coding is test case
Number n;
Step 3:Construct fitness function;For any individual, TSvIt can be covered in program to be measured to be first
V-th of sentence SvTest case order residing in sequence is executed in the test case, it is assumed that after having executed test
The quantity for the sentence being capped in program to be measured is M, then M sentence corresponds to M such order, the sum of institute's orderliness isIt is possible thereby to constructing fitness function APSC, APSC indicates the average percent of sentence covering,I.e.Its
Middle TSv(1≤v≤M) can be calculated with sentence coverage information matrix A, for capped sentence Sv, first is searched in A
A test case for covering sentence order residing in the test case sequence, then this order is exactly TSvNumber
Value;
Step 4:Random initializtion population;Population scale is set as N, i.e., includes individual in population, if indicating to plant with D
Group, then D=<r11,r12,…,r1n>,<r21,r22,…,r2n>,…,<rw1,rw2,…,rwn>,…,<rN1,rN2,…,rNn>,
Wherein<rw1,rw2,…,rwn>Represent w-th of individual in population;Crossover probability P is setcWith mutation probability PmAnd greatest iteration
Number MAX;Iterations g=1 is enabled, iteration is started;
Step 5:Execute random sampling selection strategy;
(1) adaptive value for calculating all individuals in population, obtains an array being made of N number of adaptive value, is expressed as
{Fitness1,Fitness2,…,FitnessN, wherein Fitnessindividual(1≤individual≤N) is indicated in population
The adaptive value of i-th ndividual individual, if the average value of Population adaptation value is indicated with averageFitness,Real number start (0 < is generated at random
Start < averageFitness);Enable index=1, index indicates individual number, enables the sum of Population adaptation value sum initial values be
Fitness1;
(2) it is the quantity for the individual currently having been selected to enable select;Calculating pointer=start+select ×
AverageFitness, pointer are the foundation for weighing individual fitness size in population, if Fitnessindex< pointer,
So proceed as follows:A.index=index+1, sum=sum+Fitnessindex;B. the operation in a is repeated, when
When sum >=pointer, the value of index is stored in the select+1 position in individuals arrays, and stop in b
Operation;
(3) select=select+1 repeats the operation in (2), as select=N, stops selection operation;
(4) it using the number in individuals arrays as subscript, is sequentially replaced with each corresponding individual of these subscripts
Each individual in current population is changed, population of new generation identical with former Population Size is obtained;
Step 6:Execute crossover operation;For any two individual A in population1And A2If Assuming that the offspring individual generated after crossover operation is A1' and A2';One 0 to 1 is generated at random
Real number works as PcWhen more than this real number, crossover operation is executed, the offspring individual substitution parent individuality generated after crossover operation enters
New population, the individual not intersected are directly copied into new population;The detailed process of crossover operation is as follows:
(1) A is calculated1And A2The different number of positions of numerical value in middle same position, it is assumed that be t, then A1And A2Middle same position
The identical number of positions of upper numerical value is n-t;
(2) N number of sequence is generated, each sequence is the arrangement that a length is t, and k-th of sequence can indicate in N number of sequence
ForWherein K=1,2 ..., N, 1≤u≤t,<ru×k>Expression takes ru× k's
Negative is added 1, the negative is made to become a positive number, wherein r by fractional part if fractional part is negativeu=2cos (2
π u/q), q is the least prime for meeting q >=2t+3;
(3) A is deleted1And A2In n-t same position on identical numerical value, by the difference in remaining t same position
Numerical value is ranked up according to ascending sequence, and the sequence being made of t numerical value after note sequence is s, s=< s1,s2,…,
sh,…,st>, wherein s1≤s2≤…≤sh≤…≤st(1≤h≤t);It willAccording to by
It is small to it is big be ranked sequentially forRemember that the sequence is Pk';RecordIn P after sequencekThe location of in ', according to the situation of change of position to shResiding in s
Position is readjusted, and is obtained by s1,s2,…,stOne new sequence of composition, is denoted as s ', reuses each in s '
Numerical value fills A respectively1And A2The t position with different numerical value, has just obtained a new sequence, has been denoted as in middle same position
Sk, which is n;When k takes 1,2 ..., when each numerical value in N-1, N, it is carried out aforesaid operations in (3), so that it may with
The sequence that N number of length is n is generated, S={ S are denoted as1,S2,…,SN};
(4) adaptive value of the N number of sequence generated in (3), A are calculated1' and A2Numerical value and adaptive value in ' on each position are most
Numerical value in big sequence on each position corresponds to identical;
Step 7:Execute mutation operation;
For any one individual in population, it is assumed that be B, the random real number for generating one 0 to 1 works as PmMore than this reality
When number, mutation operation is executed to B, the process of mutation operation is:Two positions are randomly choosed from B, it will be on the two positions
Numerical value swaps, to obtain a new individual B ';
Step 8:Record the maximum adaptation value after current iteration and its corresponding individual;
Step 9:Judge stopping criterion for iteration, the stopping criterion for iteration in this method is that current iteration number is more than maximum
Iterations MAX, if end condition is set up, the corresponding priorities of test cases of maximum adaptation value individual sorts sequence at this time
It is classified as optimal priorities of test cases collating sequence, export the sequence and stops iteration;Otherwise, iterations g=g+1 is enabled, is returned
Step 5 is returned, iteration is continued.
Advantageous effect of the present invention
The mode that selection operation in invention for priorities of test cases sequence uses random sampling selects satisfaction
The mode of the several body of Population Size, this selection individual increases the probability chosen and have higher adaptive value individual in population,
Ensure that can choose in population preferably individual in the selection process, increase convergence rate of the algorithm in searching process;
Crossover operation, which considers two individuals intersected and has on which position on identical numerical value and which position, different numbers
Value, reduces the blindness of crossover operation, increases convergence rate and stability of the algorithm in searching process;Experimental result is aobvious
To show (shown in Fig. 2 and Fig. 3), it is good that this method generates the fast convergence rate of optimal priorities of test cases ranking results, stability,
Testing efficiency can be improved, testing cost is saved, finds software defect early.
Specific implementation mode
By taking the sequence of the priorities of test cases of the source program of JavaScript unit test frame Jasmine as an example, in conjunction with
Attached drawing 1 carries out a kind of specific implementation mode of priorities of test cases sort method for white-box testing proposed by the present invention
Explanation.
Step 1:For a program to be measured, tested using designed test use cases, by test case
The sentence coverage condition for treating ranging sequence is recorded, and the sentence coverage information matrix A that test case treats ranging sequence is obtained;
The source program of Jasmine has 292 sentences, is tested with 24 test cases, if test use cases are indicated with Φ, Φ=
{T1, T2..., Ti..., Tn, wherein Ti(1≤i≤n) is i-th of test case that test case is concentrated, the sentence covering of construction
The size of information matrix A is 24 × 292, and the range of statement number is 1 to 292 in program to be measured, the range of test case number
It is 1 to 24, if i-th of test case covers j-th of sentence, A in executingij=1, otherwise Aij=0;
Step 2:Coding;Test Case Prioritization problem indicates one for given program to be measured and test use cases, individual
A priorities of test cases collating sequence, priorities of test cases collating sequence are exactly that test case numbers the orderly sequence formed
Row, i.e. each individual are encoded as an orderly test case number string, and the length of coding is the number 24 of test case;
Step 3:Construct fitness function;For any individual, TSvIt can be covered in program to be measured v-th to be first
Sentence SvTest case order residing in sequence is executed in the test case, executed the source program of Jasmine after test
In the quantity of sentence that is capped be 292, then 292 sentences correspond to 292 such order, the sum of institute's orderliness isIt is possible thereby to constructing fitness function APSC, APSC indicates the average percent of sentence covering,I.e.
Wherein TSv(1≤v≤292) can be calculated using sentence coverage information matrix A, for capped sentence Sv, looked into A
The order for looking for first test case for covering the sentence residing in the test case sequence, then this order is exactly
TSvNumerical value;
Step 4:Random initializtion population;Population scale is set as 150, i.e., includes 150 individuals in population, if with D tables
Show population, then D=<r11,r12,…,r124>,<r21,r22,…,r224>,…,<r1501,r1502,…,r15024>, wherein<r11,
r12,…,r124>The 1st individual in population is represented,<r21,r22,…,r224>The 2nd individual ... in population is represented,<
r1501,r1502,…,r15024>Represent the 150th individual in population;Crossover probability P is setc=0.85, mutation probability Pm=
0.55, maximum iteration MAX=50;Iterations g=1 is enabled, iteration is started;
Step 5:Execute random sampling selection strategy;
(1) adaptive value for calculating all individuals in population, obtains an array being made of 150 adaptive values, is expressed as
{Fitness1,Fitness2,…,Fitness150, wherein Fitnessindividual(1≤individual≤150) indicate kind
The adaptive value of i-th ndividual individual in group, in initial population the adaptive value of all individuals for (0.677226,
0.68764263,0.68707186,0.6705194,0.6021689,…,0.75742006,0.7020548,0.70719177,
0.7126141,0.63313353,0.7255993) if the average value of Population adaptation value is indicated with averageFitness,That is averageFitness=
103.31777662038803/150=0.6887851774692535;A real number start (0 < start < is generated at random
AverageFitness), numerical value 0.36970036874340495;It enables index=1, index indicate individual number, enables kind
The sum of group's adaptive value sum initial values are Fitness1, sum=Fitness1=0.677226;
(2) it is the quantity for the individual currently having been selected to enable select;Calculating pointer=start+select ×
AverageFitness, i.e. pointer=0.36970036874340495+0 × 0.6887851774692535, pointer are
Evaluate the measurement foundation of individual fitness size in population, result of calculation 0.36970036874340495, due to sum>
Pointer, so the numerical value 1 of index at this time is stored in the 1st position in array individuals, expression has selected kind
The individual that number is 1 in group;
(3) select=select+1, at this time pointer=0.36970036874340495+1 ×
0.6887851774692535, result of calculation 1.058485546212658, due to sum at this time preserve be select=0 when
Numerical value 0.677226 final sum, is compared sum and pointer, 0.677226<1.058485546212658 so
Index=index+1=2, sum=sum+Fitness2=0.677226+0.68764263=1.36486863;Compare again
Sum and pointer, due to 1.36486863 > 1.058485546212658, so the numerical value 2 of index at this time is stored in number
The 2nd position in group individuals;The aforesaid operations in (3) are repeated, as select=N, stop selection behaviour
Make;Such as:Value in certain iterative process after the completion of selection operation in individuals arrays is:(1,2,3,4,5,6,7,9,
10,11,12,13,14,15,16,17,18,19,20,21,22,22,23,24,26,27,28,28,31,32,33,34,35,
36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,52,53,54,55,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,
86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,
108,109,110,111,112,113,114,115,116,117,119,120,121,122,122,123,124,125,126,
127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,
146,147,148,149,150),
As can be seen that individual in the selection process without selected, the table that number is 8,25,29,30 from data above
These bright individual fitnesses are small, thus selected probability is smaller;The individual that number is 22,28,55,122 is in the selection process
It has been selected repeatedly, has shown that these individual fitnesses are big, thus selected probability is big;
(4) it using the number in individuals arrays as subscript, is sequentially replaced with each corresponding individual of these subscripts
Each individual in current population is changed, population of new generation identical with former Population Size is obtained;
Step 6:For any two individual A in population1And A2If Assuming that the offspring individual generated after crossover operation is A1' and A2';It is random to generate one 0 to 1
Real number, the random number generated here be 0.53896, due to PcMore than this real number, therefore execute crossover operation, crossover operation
The offspring individual substitution parent individuality generated afterwards enters new population, and the individual not intersected is directly copied into new population;
The detailed process of crossover operation is as follows:
(1) two parent individualities selected in this example are:A1=(17,1,5,15,3,14,20,22,10,23,4,16,
2,6,13,18,11,7,9,12,19,21,8,24);A2=(19,12,7,20,13,9,2,11,1,22,8,6,3,4,23,18,
15,10,17,21,14,16,5,24);Then A1And A2Numerical value different number of positions t=22, A in middle same position1And A2Middle phase
The identical number of positions n-t=2 with numerical value on position;
(2) 150 sequences are generated, it is 22 arrangement that each sequence pair, which answers a length, and k-th of sequence can in N number of sequence
To be expressed asWherein K=1,2 ..., 150,1≤u≤22;<ru×k>Expression takes
ruNegative is added 1, the negative is made to become a positive number, wherein r by the fractional part of × k if fractional part is negativeu=
2cos (2 π u/q), q is the least prime for meeting q >=2t+3, here q >=2t+3=47, thus q takes 47, for example, work as u=3, k
When=1, r3× 1=1.9289383501087531, then<r3×1>=0.9289383501087531;Work as u=4, when k=1,
r4× 1=-6.482505539488857,<r4×1>=-0.482505539488857, then will<r4×1>Numerical value turn
Become<r4×1>=-0.482505539488857+1, i.e., 0.5174944605111431;As k=2, it is obtained by calculation
P2For (0.9643,0.8579,0.6826,0.4416,0.1392,0.7808,0.3727,0.9223,0.4376,0.9273,
0.4004,0.8663,0.3347,0.8149,0.3163,0.8477,0.4175,0.0334,0.7022,0.4299,0.2213,
0.0802);
(3) A is deleted1And A2In 2 same positions on identical numerical value, the numerical value deleted here be A1And A2In the 16th
The numerical value 24 on numerical value 18 and the 24th position on a position;By the different numerical value in remaining 22 same positions according to
Ascending sequence is ranked up, and the sequence being made of 22 numerical value after note sequence is indicated with s, s=<s1,s2,…,s22
>, wherein s1≤s2≤…≤s22, s=here<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,19,20,
21,22,23>;It willAccording to it is ascending be ranked sequentially forRemember that the sequence is Pk', here by P2In all numerical value according to ascending
Sequence be ranked up, obtain (0.0334,0.0802,0.1392,0.2213,0.3163,0.3347,0.3727,0.4004,
0.4175,0.4299,0.4376,0.4416,0.6826,0.7022,0.7808,0.8149,0.8477,0.8579,0.8663,
0.9223,0.9273,0.9643);RecordIn P after sequencekThe location of in ', according to position
The situation of change set is to siIt is readjusted, is obtained by s the location of in s1,s2,…,s22One new sequence of composition
Row, are denoted as s ', such as P2In the numerical value 0.9643 of the 1st position become for 22, then by the 1st in s by its position of sorting
Numerical value 1 on position is stored in the 22nd position of the new sequence that length is 22, P2In the numerical value 0.8579 of the 2nd position pass through
The change of its position sort for 18, then the numerical value 2 in s on the 2nd position is stored in the 18th of the new sequence that length is 22
Position ..., P2In the numerical value 0.0802 of the 22nd position become for 2 by its position of sorting, then will be in s on the 22nd position
Numerical value 23 be stored in length be 22 new sequence the 2nd position, by which to P2In 22 numerical value institute in the sequence
The position at place obtained after being readjusted (19,23,5,22,15,13,7,11,17,21,9,4,3,20,6,14,16,2,12,
8,10,1), i.e., this is obtained by s1,s2,…,stOne new sequence of composition, each numerical value reused in the sequence are suitable
Secondary filling A1And A222 positions with different numerical value, have just obtained a new sequence, have been denoted as S in middle same position2, should
Sequence length is 24, S2=(19,23,5,22,15,13,7,11,17,21,9,4,3,20,6,18,14,16,2,12,8,1 0,
1,24);When k takes 1,2 ..., when each numerical value in 149,150, it is carried out aforesaid operations in (3), so that it may to generate 150
The sequence that length is 24, is denoted as S={ S1,S2,…,S150};
(4) adaptive value of 150 sequences generated in (3), A are calculated1' and A2Numerical value and adaptive value in ' on each position
Numerical value in maximal sequence on each position corresponds to identical;Here the maximum adaptation value in 150 sequences being calculated is
0.74999994, corresponding sequence be (9,12,4,19,8,15,5,6,24,10,13,17,21,1,11,18,7,22,16,3,
, thus A 2,14,20,23)1' and A2' be (9,12,4,19,8,15,5,6,24,10,13,17,21,1,11,18,7,22,
16,3,2,14,20,23);
Step 7:Execute mutation operation;
For any one individual in population, it is assumed that be B, the random real number for generating one 0 to 1 works as PmMore than this reality
When number, mutation operation is executed to B, the process of mutation operation is:Two positions are randomly choosed from B, it will be on the two positions
Numerical value swaps, to obtain a new individual B ';For example, in population certain individual B, B=(4,16,19,24,
1,8,20,17,13,5,12,14,18,7,11,10,9,3,22,2,15,6,21,23) one 0 to 1 real number, generated at random
It is 0.5114276945945321, meets 0.55 > 0.5114276945945321, executes mutation operation:It is randomly choosed from B
Two positions be 7 and 11, the numerical value on the two positions is swapped to obtain a new individual B ', B '=(4,16,
19,24,1,8,12,17,13,5,20,14,18,7,11,10,9,3,22,2,15,6,21,23);
Step 8:Record the maximum adaptation value after current iteration and its corresponding individual, the maximum adaptation after current iteration
Value be 0.78224885, the adaptive value it is corresponding individual for (4,19,8,5,15,9,24,12,13,17,3,7,6,16,11,14,
10,22,2,20,18,21,1,23);
Step 9:Judge stopping criterion for iteration, the stopping criterion for iteration in this method is that current iteration number is more than maximum
Iterations MAX, MAX=50, if end condition is set up, the priorities of test cases collating sequence obtained at this time is most
Excellent priorities of test cases collating sequence exports the sequence and stops iteration;Otherwise, iterations g=g+1, return to step are enabled
Five, continue iteration.For example, after the 50th iteration, maximum adaptation value is 0.79723173 in population, the optimal test case of output
Priority ranking sequence be (13,20,9,15,8,4,1,16,6,10,3,5,24,7,19,2,12,11,22,21,18,17,14,
23)。
It may be implemented to sort for the priorities of test cases of white-box testing by above procedure, for jasmine programs
Sentence coverage information, respectively by traditional genetic algorithm and the present invention in method respectively execute 20 times.
Fig. 2 is in 20 experiments, and the adaptive value average value of 50 iteration is with iterations variation diagram.Figure it is seen that
In iterative process, the adaptive value of this method is greater than the corresponding adaptive value of traditional genetic algorithm, so in white-box testing
Priorities of test cases sorts, and this method has faster convergence rate compared to traditional genetic algorithm.
Fig. 3 is the babinet figure of maximum adaptation value in 20 experiments of traditional genetic algorithm and this method, compares two auxiliary box body figures
It is found that the data distribution that this method obtains more is concentrated, i.e., it is preferential in the test case for using this method be directed to white-box testing
The stability that optimal sequencing result is generated when grade sequence is more preferable.
Instance analysis shows that the priorities of test cases sort method proposed by the present invention for white-box testing is lost with tradition
Propagation algorithm does priorities of test cases sequence and compares, provided by the present invention for the priorities of test cases sequence side of white-box testing
Method fast convergence rate, the stability for generating optimal priorities of test cases collating sequence is good, is that one kind being effectively used for whitepack survey
The priorities of test cases sort method of examination.