TDD
在我看来,使用TDD的一个好处是避免在程序中加入无关的代码。参考一个TDD的定义和TDD的开发feature的流程如下:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
因此也就意味着当我们使用TDD的方式进行开发的时候,我们要首先将功能表述为代码的行为或者测试案例,然后将测试案例落实。此时我们的代码是无法通过测试的(红)。然后我们只需要写必要的代码使得测试通过(绿)。
从而我们保证了:
- 所编写的测试案例是最小的
- 所编写的代码是最小的
- 所有功能是通过的
测试的角度
我们接下来参考一个计算器实现来说举例一下。
范围 | 处理方式 |
---|---|
接口 | unit test |
模拟使用 | binary test |
集成 | integration test |
Unit Test
对于指定功能如sum(a,b)
我们可以在接口层面进行测试。
test_sum
1,