最近在项目中尝试去向TDD的开发方式靠近,因此接触倒了C语言的单元测试工具Check,模拟Java的JUnit编写而成,功能简单实用,Manual手册就短短两页,在SO上的人气也挺高。
不过也有可能是因为刚接触TDD,所以有些概念产生了一些错误的印象,导致使用Check时也出现了问题,今天碰到的典型问题就是 checked fixture 和 CK_NOFORK 模式结合起来使用时产生。
Manual中是这么解释 checked fixture:
We may want multiple tests that all use the same Money. In such cases, rather than setting up and tearing down objects for each unit test, it may be convenient to add some setup that is constant across all the tests in a test case. Each such setup/teardown pair is called a test fixture in test-driven development jargon.
也就是说,当多个单元测试对象建立和销毁的方式是相同的时候,可以通过向测试用例 Test case(一个测试用例由多个单元测试 unit test 组成)添加一个 checked fixture,它需要传递一个 setup() 和一个 teardown() 用于对象的创建和销毁。通常会配合一个全局变量使用。这样每个单元测试在开始之前都会有该全局变量最初通过setup创建的状态,进而多个测试之间不会产生相互的干扰。
这个“全局变量”让我一开始完全误解了它的意思,还以为它一经创建,状态会一直保存倒最后所有的单元测试完成并通过teardown进行对象