TDD golang实践上的部分个人经验谈

TDD

在我看来,使用TDD的一个好处是避免在程序中加入无关的代码。参考一个TDD的定义和TDD的开发feature的流程如下:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
    https://www.ibm.com/garage/method/practices/code/practice_test_driven_development
    因此也就意味着当我们使用TDD的方式进行开发的时候,我们要首先将功能表述为代码的行为或者测试案例,然后将测试案例落实。此时我们的代码是无法通过测试的(红)。然后我们只需要写必要的代码使得测试通过(绿)。
    从而我们保证了:
  • 所编写的测试案例是最小的
  • 所编写的代码是最小的
  • 所有功能是通过的

测试的角度

我们接下来参考一个计算器实现来说举例一下。

范围 处理方式
接口 unit test
模拟使用 binary test
集成 integration test

Unit Test

对于指定功能如sum(a,b) 我们可以在接口层面进行测试。

test_sum
1,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值