使用接口测试活动的中奖概率(随机事件测试)

 有一个抽奖活动抽到一等奖的概率是万分之一,您知道怎么测试这个概率吗? 

 抽奖活动的中奖事件是个随机事件,用大量的手动测试来检验中奖概率的正确性显然不可取,除了手工对中奖流程,后续处理的校验外,可以和开发配合,使用接口来测试中奖的概率是否符合预期的设计要求。

 

1.思路:

(1)开发提供中奖的接口,get该接口(此处需要向开发详细了解),每次随机返回以下四个结果:

0--表示未抽中

1--表示抽中1等奖

2--表示抽中2等奖

3--表示抽中3等奖

(2)使用for循环,多次请求该接口,并使用testNG框架中自带的设置多次执行方法和处理多线程的方法,使多个方法并发运行,缩短执行时间,来模拟大数据量下的中奖事件。

(3)对(2)中的中奖事件进行数据处理,获取各类中奖事件的概率。

 

2.代码

用例代码  LotteryTestCase.java 如下:

扫描二维码关注公众号,回复: 6953950 查看本文章
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----表示执行本方法的次数,在此表示每执行本方法10次
    //threadPoolSize-----表示开启的多线程个数,和方法执行次数有关, 此处表示开启5个线程,每2个方法共用同一个线程,多个方法并发同时执行,节省运行的时间成本///不过使用该设置一般用于下面为单个方法执行,此处使用了for循环,所以此处设置不是很有效,可忽略

    public  void testLottery() throws Exception {

        int fail = 0;
        int first = 0;
        int second = 0;
        int third = 0;
        float perfail=0;
        float perfirst=0;
        float persecond=0;
        float perthird=0;
        int m=100;       //设置请求接口get的次数



//        循环使用get方法获取中奖接口的数据,获得中奖的类型数据
        for (int i = 0; i <m; i++) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet get = new HttpGet("http://**********")  //第一次中奖接口,先单个在浏览器中运行看结果是否有异常
           // HttpGet get = new HttpGet("http://**********"); //第二次中奖接口
            CloseableHttpResponse response = httpClient.execute(get);

            try {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                int res=Integer.parseInt(result);  //将中奖的类型为 string类型转化为int类型,

                if(res==0){
                    fail=fail+1;
                 // perfail=(float)fail/m;//获取中奖失败的概率,放在此处会导致每次for循环都执行一次,所以应该放到for循环外

                }else if(res==1){
                    first=first+1;
                   
                }else if(res==2){
                    second=second+1;
                 
                }else{
                    third=third+1;
                    
                }

            } finally {

                response.close();
            }
        }



        perfail=(float)fail/m;//获取中奖失败的概率
        perfirst=(float)first/m;
        persecond=(float)second/m;
        perthird=(float)third/m;
        System.out.println("-------中奖次数--------");
        System.out.println("中奖失败的次数为"+fail);
        System.out.println("中一等奖的次数为"+first);
        System.out.println("中二等奖的次数为"+second);
        System.out.println("中三等奖的次数为"+third);

        System.out.println("-------中奖概率--------");
        System.out.println("中奖失败的概率为"+perfail);
        System.out.println("中一等奖的概率为"+perfirst);
        System.out.println("中二等奖的概率为"+persecond);
        System.out.println("中三等奖的概率为"+perthird);

    }

}

3.结果

在testNG.xml中设置用例的路径,执行即可。综上可看,请求接口1000次,每个方法执行100次请求,执行10个该方法,每个方法的结果如下:

[TestNG] Running: 

D:\krplus-api-test\testcase\testcase\Lottery\tesNG.XML

-------中奖次数--------

中奖失败的次数为16

中一等奖的次数为2

中二等奖的次数为9

中三等奖的次数为73

-------中奖概率--------

中奖失败的概率为0.16

中一等奖的概率为0.02

中二等奖的概率为0.09

中三等奖的概率为0.73

之后和设计中的概率进行对比即可!

在实践过程中的确发现通过这种接口测试可以发现概率和接口方面的问题:

1.第一次中奖时,即使运行1000次,一等奖中奖次数也是0,后来是因为开发重写代码不完整所致

2.运行时抛异常,运行单个接口发现有问题,如下:

其实还是蛮有用的是不撒哈~O(∩_∩)O!

当然具体情况需要具体分析,谁知道后面的 抽奖活动会是甚样设计~

另外附加一个使用tesNG设置多线程的很好例子,参考 http://blog.csdn.net/manchester117/article/details/7867940

猜你喜欢

转载自www.cnblogs.com/lijingxiang/p/11301982.html