Using an interface test activities probability of winning (random events test)

 The probability of a raffle prize is able to get a million, you know how to test the probability of it? 

 Winning sweepstakes event is a random event, with plenty of manual tests to verify the correctness of the probability of winning is obviously not desirable, in addition to the probability of winning check the manual process, subsequent treatment, can cooperate and develop, use the interface to test winning meets the design requirements.

 

1. ideas:

(1) development to provide the winning interface, get this interface ( here we need to develop a detailed understanding ), each random returns the following four results:

0-- means not drawn

1-- 1 represents the prize draw

2-- representation drawn 2 prize

3-- representation drawn 3 prize

(2) for loop, repeatedly requesting the interface and uses testNG carrying frame is provided and a method of performing multiple multi-threaded processing, a plurality of concurrently operating method, to shorten the execution time, the large amount of data to simulate winning the event.

(3) to (2) in winning the event for data processing, acquisition probability of winning all kinds of events.

 

2. Code

Example Code LotteryTestCase.java use as follows:

package com.krplus.api.autotest.testcase;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

/**
 * Created by wyy on 2016/4/18.
 */
public class LotteryTestCase {

    @Test(invocationCount =10, threadPoolSize = 5)
    //invocationCount ---- indicates how many times this method of execution, to express each execution of the method 10
     // threadPoolSize ----- represents the number of multi-threading turned on, and methods related to execution times, represented here to open five threads every two threads share the same method, a plurality of methods concurrently performed simultaneously saving time running cost // /, but this setting is generally used in the following method as a single execution cycle for use herein, therefore not set here very effective, can be neglected 

    public   void testLottery () throws Exception { 

        int Fail = 0 ;
         int First = 0 ;
         int SECOND = 0 ;
         int THIRD = 0 ;
         a float perfail = 0 ;
         a float perfirst = 0 ;
         a float persecond = 0 ;
         a float perthird = 0;
         Int m = 100;        // Set the number of the get request interface 



//         recycling method for acquiring data using the get interface winning, the winning type data obtained 
        for ( int I = 0; I <m; I ++ ) { 
            CloseableHttpClient httpClient = HttpClients. createDefault (); 
            HttpGet GET = new new HttpGet ( "HTTP: // **********")   // first winning interface to a single run in the browser to see if there are abnormal results
            // HttpGet = new new HttpGet GET ( "HTTP: // **********"); // second prize interfaces 
            CloseableHttpResponse Response = httpClient.execute (GET); 

            the try {
                Entity the HttpEntity ) {= Response.getEntity (); 
                String Result = EntityUtils.toString (Entity);
                 int RES = the Integer.parseInt (Result);   // the type of winning a string type is converted to an int, 

                IF (RES == 0 ) { 
                    Fail fail. 1 + = ;
                  // perfail = (a float) fail / m; // Get winning probability of failure, cause here on the loop is executed once for each time, so it should be placed in an outer loop for 

                } the else  IF (RES . 1 == ) { 
                    First = First +. 1 ; 
                   
                } the else  IF (RES == 2 = SECOND +. 1
                    SECOND; 
                 
                } The else { 
                    THIRD = THIRD +. 1 ; 
                    
                } 

            } the finally { 

                response.close (); 
            } 
        } 



        perfail = ( a float ) Fail / m; // Get winning probability of failure 
        perfirst = ( a float ) First / m; 
        persecond = ( a float ) SECOND / m; 
        perthird = ( a float ) THIRD / m; 
        System.out.println ( "-------- ------- winning number" ); 
        System.out.println ( "winning number failed to" + Fail); 
        System.out.println ( "The number of times the first prize for the" + First); 
        System.out.println ( "the second prize the number is "+ SECOND,); 
        System.out.println ( " third prize in the number of "+ THIRD,); 

        System.out.println ( " the probability of winning ------- -------- " ); 
        System.out.println ( " failure probability of winning is "+ perfail); 
        System.out.println ( " probability of the first prize for the "+ perfirst); 
        System.out.println ( " second prize probability of "+ persecond); 
        System.out.println ( " third prize probability is "+ perthird);

    }

}

3. Results

Disposed in the path for testNG.xml the embodiment, can be performed. In summary see, request interface 1000, each method execution request 100, the method 10 performed, the results of each method are as follows:

[TestNG] Running: 

D: \ krplus-API-Test \ TestCase \ TestCase \ Lottery \ tesNG.XML 

------- -------- winning frequency and 

the number of failures to 16 winning 

the first prize the number is 2 

times the second prize of 9 

in third place in the number 73 

------- -------- probability of winning 

probability of winning the failure was 0.16 

probability of the first prize 0.02 

probability was 0.09 second prize 

in the prize probability of 0.73

After the design and probability in comparison to!

In the course of practice did find the probability of problems can be found and interfaces of this interface testing:

1. First prize, even if the 1000 run, the number of prize winners is 0, then because the development is not complete due to rewrite the code

2. throw runtime exceptions, run a single interface found to have problems, as follows:

But it is still quite useful is not the Sahara ~ O (∩_∩) O!

Of course, the specific circumstances of the need for specific analysis, who knows behind the raffle will be even like design ~

 

Further additionally provided a good example tesNG multithreaded use, reference  http://blog.csdn.net/manchester117/article/details/7867940

Guess you like

Origin www.cnblogs.com/lijingxiang/p/11301982.html