开始使用 TDD 时,我最终问了您在此处提出的相同问题。经过一些研究,以及几周的单元测试等工作,我想出了两个术语;“流程测试”和“模块测试”。
模块测试:作为工程师,我们应该努力遵循 DRY(不要重复自己)原则,因此,我们最终会得到抽象的代码片段,这些代码会被推送到应用程序的最低层,以便可以使用它们任何地方。这些代码片段,无论是类的方法还是独立函数,都应该是原子可测试的,这意味着对任何其他模块、函数等的依赖性最低。显然,当您处理包含几个的方法/函数时,这是可以避免的模块,但这就是流程测试发挥作用的地方。
流测试:我们所有的基本模块都处于可测试状态,我们还需要能够在与现实世界需求相称的场景中测试它们。为了正确地进行流程测试,我们需要建立我所说的“已知商品”。这意味着我们构建了反映流测试中模块返回值的数据,因此我们可以将它们与从 API 生成的值进行比较。
为了帮助更好地展示这些想法,这是我为测试我的缓存 api 所做的流测试(添加了一些额外的注释以更好地解释):
<?php
class HobisTest_Api_Flow_CacheTest extends PHPUnit_Framework_TestCase
{
// Setting some constants so it's easier to construct known goods
const TEST_EXPIRY = 30;
const TEST_KEY_PREFIX = 'test';
const TEST_VALUE = 'brown chicken, brown cow';
//-----
// Support methods
//-----
protected $object;
protected $randomNumber;
// Here we generate a known good key, this allows us to test that the api internal workings generate what we expect
protected function getKnownGoodKey()
{
return self::TEST_KEY_PREFIX . Hobis_Api_Cache_Key::SEPARATOR . $this->getRandomNumber() . Hobis_Api_Cache_Key::SEPARATOR . '1';
}
protected function getObject()
{
return $this->object;
}
protected function getRandomNumber()
{
return $this->randomNumber;
}
//-----
//-----
// Setup and teardown
//-----
// You will want to add setup and teardown functions to your test classes
// These allow you to reference items for EVERY test within the current class
// While ensuring they are not carried over from one test to the other
// Basically a clean slate for every test
public function setUp()
{
$this->object = $this->getMock('Hobis_PhpUnit_DefaultTestObject');
$this->randomNumber = mt_rand();
}
public function tearDown()
{
unset(
$this->object,
$this->randomNumber
);
}
//-----
//-----
// Test methods
//-----
// The actual test method
public function testCache()
{
// Configure object
// Setting up so any references to $this->getId() will return 1
// If you look in the getKnownGoodKey() it is constructed with 1 as well
$this->object->expects($this->any())->method('getId')->will($this->returnValue(1));
// So now I am calling on my API to generate a cache key based on
// values used here, and when I constructed my "known good" key
$key = Hobis_Api_Cache_Key_Package::factory(
array(
'dynamicSuffixes' => array($this->getRandomNumber(), $this->getObject()->getId()),
'expiry' => self::TEST_EXPIRY,
'staticPrefix' => self::TEST_KEY_PREFIX,
'value' => self::TEST_VALUE
)
);
// Calling set via api
$setStatus = Hobis_Api_Cache_Package::set($key);
// Check that call was what we expect
$this->assertTrue($setStatus);
// Now let's retrieve the cached value so we can test if it's available
$cachedValue = Hobis_Api_Cache_Package::get($key);
// Test the attributes against "known good" values
$this->assertSame($key->getKey(), $this->getKnownGoodKey());
$this->assertSame($cachedValue, self::TEST_VALUE);
}
//-----
}