The content of the invention
In regression test, to choose from substantial amounts of available test case when carrying out white-box testing it is important, take off wrong energy
The strong test case preferential treatment ranging sequence of power is tested, and to improve the ability that defect is found in test process, is at this moment needed
The execution order of test case is planned again, the purpose for improving testing efficiency is reached.Traditional genetic algorithm is being found
There is a problem of during the optimal sequence of test case that convergence rate is slow, stability is poor, therefore need badly and seek one kind and be used for
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 step:
Step one:For a program to be measured, tested using the test use cases for having designed, by test case
The sentence coverage condition for treating ranging sequence is recorded, and obtains the sentence coverage information matrix A that test case treats ranging sequence;It is false
If certain program to be measured there are m bar sentences, tested with n test case, if test use cases are represented with Φ, Φ={ T1,
T2,…,Ti,…,Tn, wherein TiI-th test case that (1≤i≤n) is concentrated for test case, the sentence of construction cover letter
The size of breath matrix A is n × m, and in program to be measured, the scope of statement number is 1 to m, and the scope of test case numbering is 1 to n,
If i-th test case covers j-th sentence, A in performingij=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
A priorities of test cases collating sequence is represented, priorities of test cases collating sequence is exactly to be made up of test case numbering
Ordered sequence, i.e. each individuality is encoded as an orderly test case numbering string, and the length of coding is test case
Number n;
Step 3:Construction fitness function;For arbitrary individuality, TSjCan cover j-th in program to be measured for first
Sentence SjThe order residing in the test case Perform sequence of test case, it is assumed that performed after test quilt in program to be measured
The quantity of the sentence of covering is M, then M such order of M sentence correspondence, and institute's orderliness sum isIt is possible thereby to
Construction fitness function APSC, APSC represent the average percent that sentence is covered,
I.e.Wherein TSj(1≤j≤M) can be carried out with sentence coverage information matrix A
Calculate, for the sentence S being cappedj, first test case for covering the sentence is searched in A in the test case sequence
In residing order, then this order is exactly TSjNumerical value;
Step 4:Random initializtion population;Population scale is set as N, i.e., individuality is included in population, if kind is represented with D
Group, then
D=<r11,r12,…,r1n>,<r21,r22,…,r2n>,…,<ri1,ri2,…,rin>,…,<rN1,rN2,…,rNn
>, wherein<ri1,ri2,…,rin>I-th represented in population is individual;Crossover probability P is setcWith mutation probability PmAnd it is maximum
Iterationses MAX;Iterationses g=1 is made, starts iteration;
Step 5:Perform stochastic sampling selection strategy;
(1) all individual adaptive values in population are calculated, an array being made up of N number of adaptive value is obtained, is expressed as
{Fitness1,Fitness2,…,FitnessN, wherein FitnessindividualDuring (1≤individual≤N) represents population
I-th ndividual individual adaptive value, if the meansigma methodss of Population adaptation value are represented with averageFitness,Real number start (0 < is generated at random
Start < averageFitness);Make index=1, index represent individual numbering, make the Population adaptation value sum sum initial value be
Fitness1;
(2) select is made to be the quantity for currently having been chosen by individuality;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
During sum >=pointer, the select+1 position value of index being stored in individuals arrays, and stop in b
Operation;
(3) select=select+1, repeats the operation in (2), as select=N, stops selection operation;
(4) numeral in individuals arrays is sequentially replaced as subscript with corresponding each individuality of these subscripts
Each changed in current population is individual, obtains and former Population Size identical a new generation population;
Step 6:Perform crossover operation;For any two individuality A in population1And A2If,
The offspring individual produced after assuming crossover operation is A1' and A2';The random real number for generating 0 to 1, works as PcMore than this real number
When, crossover operation is performed, the offspring individual produced after crossover operation replaces parent individuality to enter new population, is not intersected
Individuality is directly copied into new population;The detailed process of crossover operation is as follows:
(1) calculate A1And A2The different number of positions of numerical value in middle same position, it is assumed that for t, then A1And A2Middle same position
Upper numerical value identical number of positions is n-t;
(2) N number of sequence is produced, each sequence is the arrangement that a length is t, and k-th sequence can be represented in N number of sequence
ForWherein
K=1,2 ..., N, 1≤u≤t,<ru×k>Expression takes ruThe fractional part of × k, if fractional part is negative, that
Negative is added 1, makes the negative become a positive number, wherein ru=2cos (2 π u/q), q are the minimum elements for meeting q >=2t+3
Number;
(3) delete A1And 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 order, and the sequence being made up of t numerical value after note sequence is s, s=<s1,s2,…,st
>, wherein s1≤s2≤…≤st;WillIt is arranged as according to ascending order
Remember that the sequence is Pk';RecordIn P after sequencekLocation in ', according to the change of position
Situation is to siIn s, location is readjusted, and is obtained by s1,s2,…,stOne new sequence of composition, is designated as
S ', each numerical value reused in s ' fill A respectively1And A2There is in middle same position t position of different numerical value, must
To a new sequence, S is designated ask, the sequence length is n;When k takes 1,2 ..., N-1, during each numerical value in N, holds
Aforesaid operations in row (3), it is possible to generate the sequence that N number of length is n, be designated as S={ S1,S2,…,SN};
(4) adaptive value of the N number of sequence generated in calculating (3), A1' and A2Numerical value in ' on each position is with adaptive value most
Numerical value correspondence in big sequence on each position is identical;
Step 7:Perform mutation operation;
For in population, any one is individual, it is assumed that for B, the random real number for generating 0 to 1, work as PmMore than this reality
During number, mutation operation is performed to B, the process of mutation operation is:Two positions are randomly choosed from B, by the two positions
Numerical value is swapped, so as to obtain a new individual B ';
Step 8:Maximum adaptation value and its corresponding individuality after record current iteration;
Step 9:Judge stopping criterion for iteration, the stopping criterion for iteration in this method is current iteration number of times more than maximum
Iterationses MAX, if end condition is set up, then now the individual corresponding priorities of test cases of maximum adaptation value sorts sequence
Optimum priorities of test cases collating sequence is classified as, the sequence is exported and is stopped iteration;Otherwise, iterationses g=g+1 is made, is returned
Step 5 is returned, continues iteration.
Beneficial effect of the present invention
Selection operation in invention for priorities of test cases sequence employs the mode of stochastic sampling and selects satisfaction
The several body of Population Size, the individual mode of this selection increase the probability for having higher adaptive value individual in choosing population,
Ensure that, increased convergence rate of the algorithm in searching process;
Crossover operation considers two individualities for being intersected and has on which position on identical numerical value and which position different numbers
Value, reduces the blindness of crossover operation, increased convergence rate and stability of the algorithm in searching process;Experimental result shows
Show (by shown in Fig. 2 and Fig. 3), this method generates the fast convergence rate of optimum priorities of test cases ranking results, good stability,
Testing efficiency can be improved, testing cost is saved, is found software defect early.
Specific embodiment
By taking the priorities of test cases sequence of the source program of JavaScript unit test frame Jasmine as an example, with reference to
Accompanying drawing 1 is carried out to a kind of specific embodiment of the priorities of test cases sort method for white-box testing proposed by the present invention
Explanation.
Step one:For a program to be measured, tested using the test use cases for having designed, by test case
The sentence coverage condition for treating ranging sequence is recorded, and obtains the sentence coverage information matrix A that test case treats ranging sequence;
The source program of Jasmine has 292 sentences, is tested with 24 test cases, if test use cases are represented with Φ, Φ=
{T1,T2,…,Ti,…,Tn, wherein TiI-th test case that (1≤i≤n) is concentrated for test case, the sentence of construction are covered
The size of information matrix A is 24 × 292, and in program to be measured, the scope of statement number is 1 to 292, the scope of test case numbering
It is 1 to 24, if i-th test case covers j-th sentence, A in performingij=1, otherwise Aij=0;
Step 2:Coding;Test Case Prioritization problem is for given program to be measured and test use cases, individual to represent one
Individual priorities of test cases collating sequence, priorities of test cases collating sequence are exactly the orderly sequence constituted by test case numbering
Row, i.e. each individuality are encoded as an orderly test case numbering string, and the length of coding is the number 24 of test case;
Step 3:Construction fitness function;For arbitrary individuality, TSjCan cover j-th in program to be measured for first
Sentence SjThe order residing in the test case Perform sequence of test case, performed the source program of Jasmine after test
In the quantity of sentence that is capped be 292, then 292 sentences, 292 such order of correspondence, institute's orderliness sum isIt is possible thereby to construct fitness function APSC, APSC represents the average percent that sentence is covered,
I.e.Wherein TSj(1≤j≤292) can be covered using sentence
Information matrix A is calculated, for the sentence S being cappedj, first test case for covering the sentence is searched in A exist
Residing order in the test case sequence, then this order is exactly TSjNumerical value;
Step 4:Random initializtion population;Population scale is set as 150, i.e., it is individual comprising 150 in population, if using D tables
Show population, then
D=<r11,r12,…,r124>,<r21,r22,…,r224>,…,<r1501,r1502,…,r15024>, wherein<r11,
r12,…,r124>The 1st represented in population is individual,<r21,r22,…,r224>The 2nd represented in population is individual ...,<
r1501,r1502,…,r15024>The 150th represented in population is individual;Crossover probability P is setc=0.85, mutation probability Pm=
0.55, maximum iteration time MAX=50;Iterationses g=1 is made, starts iteration;
Step 5:Perform stochastic sampling selection strategy;
(1) all individual adaptive values in population are calculated, an array being made up of 150 adaptive values is obtained, is expressed as
{Fitness1,Fitness2,…,Fitness150, wherein Fitnessindividual(1≤individual≤150) represent and plant
I-th ndividual individual adaptive value in group, in initial population, all individual adaptive values are
(0.677226,0.68764263,0.68707186,0.6705194,0.6021689,…,
0.75742006,0.7020548,0.70719177,0.7126141,0.63313353,0.7255993)
If the meansigma methodss of Population adaptation value are represented with averageFitness,
I.e.
AverageFitness=103.31777662038803/150=0.6887851774692535;It is random to generate one
Individual real number start (0 < start < averageFitness), its numerical value are 0.36970036874340495;Make index=1,
Index represents individual numbering, makes Population adaptation value sum sum initial value be Fitness1, sum=Fitness1=0.677226;
(2) select is made to be the quantity for currently having been chosen by individuality;Calculate
Pointer=start+select × averageFitness, i.e.,
Pointer=0.36970036874340495+0 × 0.6887851774692535, pointer are evaluation population
The measurement foundation of middle individual fitness size, result of calculation is 0.36970036874340495, due to sum>Pointer, so
The 1st position numerical value 1 of now index being stored in array individuals, during expression have selected population, numbering is 1
Individuality;
(3) select=select+1, now
Pointer=0.36970036874340495+1 × 0.6887851774692535, result of calculation is
1.058485546212658, due to now sum preserve be select=0 when sum final numerical value 0.677226, to sum and
Pointer is compared,
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 now index is stored in array
The 2nd position in individuals;The aforesaid operations in (3) are repeated, as select=N, stops selection operation;
For example: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),
The individuality that numbering is 8,25,29,30 is can be seen that from data above no selected in the selection process, table
Bright these individual fitnesses are little, thus selected probability is less;Numbering be 22,28,55,122 individuality in the selection process
It is selected repeatedly, show that these individual fitnesses are big, thus selected probability is big;
(4) numeral in individuals arrays is sequentially replaced as subscript with corresponding each individuality of these subscripts
Each changed in current population is individual, obtains and former Population Size identical a new generation population;
Step 6:For any two individuality A in population1And A2If, It is false
If the offspring individual produced after crossover operation is A1' and A2';The real number of one 0 to 1 is generated at random, and the random number for generating here is
0.53896, due to PcMore than this real number, therefore crossover operation is performed, the offspring individual produced after crossover operation replaces parent
Individuality enters new population, and the individuality 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,2 1,8,24);
A2=(19,12,7,20,13,9,2,11,1,22,8,6,3,4,23,18,15,10,17,21,14,1 6,5,24);
Then A1And A2Different number of positions t=22 of numerical value, A in middle same position1And A2In middle same position, numerical value is identical
Number of positions n-t=2;
(2) 150 sequences are produced, each sequence pair answers the arrangement that length is 22, and k-th sequence can in N number of sequence
To be expressed asWherein
K=1,2 ..., 150,1≤u≤22;<ru×k>Expression takes ruThe fractional part of × k, if fractional part is negative
Number, then negative plus 1, makes the negative become a positive number, wherein ru=2cos (2 π u/q), q is to meet q >=2t+3 most
Little prime number, q >=2t+3=47 here, thus q takes 47, for example, works as u=3, and during k=1, r3× 1=
1.9289383501087531, then<r3×1>=0.9289383501087531;Work as u=4, during k=1,
r4× 1=-6.482505539488857,<r4×1>=-0.482505539488857, then will<r4×1>
Numerical value be transformed into<r4×1>=-0.482505539488857+1, i.e., 0.5174944605111431;As k=2, pass through
Calculated 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) delete A1And 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 individual position;By the different numerical value in remaining 22 same positions according to by
Little to be ranked up to big order, the sequence being made up of 22 numerical value after note sequence is represented with s, s=<s1,s2,…,s22>, its
Middle 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>;WillIt is arranged as according to ascending orderRemember the sequence
For Pk', here by P2In all numerical value be ranked up according to ascending order, 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 sequencekLocation in ', according to the situation of change pair of position
siIn s, location is readjusted, and is obtained by s1,s2,…,s22One new sequence of composition, is designated as s ', for example
P2In the numerical value 0.9643 of the 1st position its position become for 22 through sequence, then the numerical value 1 on the 1st position in s is deposited
It is placed on the 22nd position of the new sequence that length is 22, P2In the 2nd position numerical value 0.8579 through sequence its position be changed into
18, then the numerical value 2 on the 2nd position in s is stored in into the 18th position ... of the new sequence that length is 22, P2In
The numerical value 0.0802 of 22 positions becomes for 2 its position through sequence, then be stored in the numerical value 23 on the 22nd position in s
Length is the 2nd position of 22 new sequence, through which to P2In 22 numerical value location carries out weight in the sequence
It is new adjust after obtain that (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
Obtain by s1,s2,…,stOne new sequence of composition, each numerical value reused in the sequence sequentially fill A1And A2In
There are in same position 22 positions of different numerical value, just obtained a new sequence, be designated as S2, the 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 ..., during each numerical value in 149,150, aforesaid operations in (3) are carried out, it is possible to which generating 150 length is
24 sequence, is designated as
S={ S1,S2,…,S150};
(4) adaptive value of 150 sequences generated in calculating (3), A1' and A2Numerical value and adaptive value in ' on each position
Numerical value correspondence in maximal sequence on each position is identical;Here the maximum adaptation value in calculated 150 sequences is
0.74999994, corresponding sequence for (9,12,4,19,8,15,5,6,24,10,13,17,21,1,11,18,7,22,16,3,
2,14,20,23), thus A1' and A2' is
(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:Perform mutation operation;
For in population, any one is individual, it is assumed that for B, the random real number for generating 0 to 1, work as PmMore than this reality
During number, mutation operation is performed to B, the process of mutation operation is:Two positions are randomly choosed from B, by the two positions
Numerical value is swapped, so as to obtain a new individual B ';For example, for certain the individual B in population,
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),
The real number of random 0 to 1 for generating is 0.5114276945945321, meets 0.55 > 0.5114276945945321, holds
Row variation is operated:From on B, randomly selected two positions are 7 and 11, the numerical value on the two positions is swapped and obtains one
Individual 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:Maximum adaptation value and its corresponding individuality after record current iteration, the maximum adaptation after current iteration
It is worth for 0.78224885, the corresponding individuality of the adaptive value is
(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 current iteration number of times more than maximum
Iterationses MAX, MAX=50, if end condition is set up, then the priorities of test cases collating sequence for now obtaining is for most
Excellent priorities of test cases collating sequence, exports the sequence and stops iteration;Otherwise, iterationses g=g+1, return to step are made
Five, continue iteration.For example, after the 50th iteration, in population, maximum adaptation value is 0.79723173, the optimum test case of output
Prioritization sequence is
(13,20,9,15,8,4,1,16,6,10,3,5,24,7,19,2,12,11,22,21,18,17,14,23)。
Can realize sorting for the priorities of test cases of white-box testing by above procedure, for jasmine programs
Sentence coverage information, respectively the method in traditional genetic algorithm and the present invention is respectively performed 20 times.
During Fig. 2 is 20 experiments, the adaptive value meansigma methodss of 50 iteration are with iterationses 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 is compared traditional genetic algorithm and has faster convergence rate.
Fig. 3 is the casing figure of maximum adaptation value in 20 experiments of traditional genetic algorithm and this method, contrasts two auxiliary box body figures
Understand, the data distribution that this method is obtained more is concentrated, i.e., carried out using this method it is preferential for the test case of white-box testing
The stability that optimal sequencing result is generated during level sequence is more preferable.
Instance analysis shows that the priorities of test cases sort method for white-box testing proposed by the present invention 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, generates the good stability of optimum priorities of test cases collating sequence, is that one kind is effectively used for whitepack survey
The priorities of test cases sort method of examination.