测试驱动开发(TDD)实战小例子 (转)

我们知道,测试驱动开发(TDD)的基本思想就是在开发功能代码之前,先编写测试代码。也就是说在明确要开发某个功能后,首先思考如何对这个功能进行测试,并完成测试代码的编写,然后编写相关的代码满足这些测试用例。再循环进行添加其他功能,直到完全部功能的开发
最近,在公司一个支付系统的接口开发中,我按照上述方法进行了一次TDD的开发尝试。具体实现的功能是:解析渠道商返回的业务参数。首先,我整理出了TODO列表如下,
TODOs:
  1)参数完整与格式校验
  2)验证签名
  3)参数正确性校验
  4)通知状态校验
  5)正确解析参数与返回
然后,开始迭代式的演进开发,
?循环一(参数完整与格式校验):
1、编写测试用例
  1)参数完全正确用例
[java] view plaincopy
@Test  
public void parseNotifyParamsOk() {  
  
    Map<String, String> notifyMap = new HashMap<String, String>();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
    } catch (PartnerException pe) {  
        //仅当没有任何异常时,用例通过,否则认为用例失败  
        Assert.fail();  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  




    2)部分参数错误用例
[java] view plaincopy
@Test  
public void parseNotifyParamsError() {  
   灵域 www.uy0.net
    Map<String, String> notifyMap = new HashMap<String, String>();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0aaaaaaaaaaaaaaaaaaaaaaaa");//此处参数格式不正确  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
        Assert.fail();  
  
    } catch (PartnerException pe) {  
        //仅当抛出参数错误异常时,用例通过,否则认为用例失败  
        if (!pe.getCode().equals(ErrorCode.PARAMETER_ERROR)) {  
            Assert.fail();  
        }  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  
2、运行测试用例,让测试失败
3、编写代码,让测试通过
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map<String, String> notifyMap, PartnerInfo info) throws Exception {  
  
    String orderId = notifyMap.get("order_id");  
    String result = notifyMap.get("result");  
    String amount = notifyMap.get("amount");  
      
    if(StringUtil.isBlank(orderId) || orderId.length() > 20){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
      
    if(StringUtil.isBlank(result) || result.length() != 1 ){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
      
    if(StringUtil.isBlank(amount) || !StringUtil.isNum(amount)){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
  
}  
4、重构代码,去除重复
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map<String, String> notifyMap, PartnerInfo info) throws Exception {  
  
    ParamVerify pv = new ParamVerify();  
    pv.checkString("order_id",20,-1,true);//校验map中order_id的长度为20~无穷大,且为必填项  
    pv.checkString("result",1,1,true);//校验map中result的长度为1,且为必填项  
    pv.checkNum("amount",true);//校验map中amount为数字且必填  
      
    if( !pv.verify() ){  
        //如果检验不通过,则认为是请求参数不正确  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
  
}  


循环二(验证签名):
1、编写测试用例
1)签名正确的情况
[java] view plaincopy
@Test  
public void parseNotifySignOk() {  
  
    Map<String, String> notifyMap = new HashMap<String, String>();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    //签名正确的情况  
    notifyMap.put("verifystring", MD5Signature.sign(getSignSrc(notifyMap), md5Key, false));  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
    } catch (PartnerException pe) {  
        //仅当没有任何异常时,用例通过,否则认为用例失败  
        Assert.fail();  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  
2)签名错误的情况
[java] view plaincopy
@Test  
public void parseNotifySignError() {  
  
    Map<String, String> notifyMap = new HashMap<String, String>();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
        Assert.fail();  
  
    } catch (PartnerException pe) {  
        //仅当抛出签名错误异常时,用例通过,否则认为用例失败  
        if (!pe.getCode().equals(ErrorCode.PARAMETER_ERROR)) {  
            Assert.fail();  
        }  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  


2、运行测试用例,让测试失败
3、编写代码,让测试通过
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map<String, String> notifyMap, PartnerInfo info) throws Exception {  
  
    ParamVerify pv = new ParamVerify();  
    pv.checkString("order_id",20,-1,true);//校验map中order_id的长度为20~无穷大,且为必填项  
    pv.checkString("result",1,1,true);//校验map中result的长度为1,且为必填项  
    pv.checkNum("amount",true);//校验map中amount为数字且必填  
      
    if( !pv.verify() ){  
        //如果检验不通过,则认为是请求参数不正确  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
    //验证签名  
    if (!MD5Signature.verify(getSignSrc(notifyMap), info.getMd5Key(), notifyMap.get("verifystring"), true)) {  
        LOGGER.error("渠道返回接口通知参数验签失败");  
        throw new PartnerException(ErrorCode.SIGN_ERROR, "渠道返回数据签名错误");  
    }  
  
}  
4、重构(因为结构简单,无需重构,跳过此步)


按上述步骤循环进行,直至完成全部功能,就是一个简单的测试驱动开发的实战例子了。经过这样过程产生的代码,具有大量的测试用例,因此具备相对好的健壮性、可扩展性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
测试驱动的编程是 XP 困扰程序员的一个方面。对于测试驱动的编程意味着什么以及如何去做,大多数人都做出了不正确的假设。这个月,XP 方面的讲师兼 Java 开发人员 Roy Miller 谈论了测试驱动的编程是什么,它为什么可以使程序员的生产力和质量发生巨大变化,以及编写测试的原理。请在与本文相随的 论坛中提出您就本文的想法,以飨笔者和其他读者。(您也可以单击本文顶部或底部的“讨论”来访问该论坛。) 最近 50 年来,测试一直被视为项目结束时要做的事。当然,可以在项目进行之中结合测试测试通常并不是在 所有编码工作结束后才开始,而是一般在稍后阶段进行测试。然而,XP 的提倡者建议完全逆这个模型。作为一名程序员,应该在编写代码 之前编写测试,然后只编写足以让测试通过的代码即可。这样做将有助于使您的系统尽可能的简单。 先编写测试 XP 涉及两种测试: 程序员测试和 客户测试测试驱动的编程(也称为 测试为先编程)最常指第一种测试,至少我使用这个术语时是这样。测试驱动的编程是让 程序员测试(即单元测试 ― 重申一下,只是换用一个术语)决定您所编写的代码。这意味着您必须在编写代码之前进行测试测试指出您 需要编写的代码,从而也 决定了您要编写的代码。您只需编写足够通过测试的代码即可 ― 不用多,也不用少。XP 规则很简单:如果不进行程序员测试,则您不知道要编写什么代码,所以您不会去编写任何代码。 测试驱动开发(TDD)是极限编程的重要特点,它以不断的测试推动代码的开发,既简化了代码,又保证了软件质量。本文从开发人员使用的角度,介绍了 TDD 优势、原理、过程、原则、测试技术、Tips 等方面。 背景 一个高效的软件开发过程对软件开发人员来说是至关重要的,决定着开发是痛苦的挣扎,还是不断进步的喜悦。国人对软件蓝领的不屑,对繁琐冗长的传统开发过程的不耐,使大多数开发人员无所适从。最近兴起的一些软件开发过程相关的技术,提供一些比较高效、实用的软件过程开发方法。其中比较基础、关键的一个技术就是测试驱动开发(Test-Driven Development)。虽然TDD光大于极限编程,但测试驱动开发完全可以单独应用。下面就从开发人员使用的角度进行介绍,使开发人员用最少的代价尽快理解、掌握、应用这种技术。下面分优势,原理,过程,原则,测试技术,Tips等方面进行讨论。 1. 优势 TDD的基本思路就是通过测试来推动整个开发的进行。而测试驱动开发技术并不只是单纯的测试工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值