【发布时间】:2016-01-07 04:35:01
【问题描述】:
我还是对 PHPUnit 中的setup() 有点困惑。
它是否在之前和之后 每个测试用例运行?
例如,我想在每次测试之前清理我的文章表,但我想保留我已经注入表中的测试数据。因为我只想在下次测试之前清理它。
我的测试,
namespace Test\Foo\Article;
use Test\SuiteTest;
use Foo\Article;
class ArticleTest extends SuiteTest
{
protected static $Article;
/**
* Call this template method before each test method is run.
*/
protected function setUp()
{
$this->truncateTables(
[
'article'
]
);
self::$Article = new Article(self::$PDO);
}
public function testFetchRow()
{
self::$Article->createRow(
[
':title' => 'Hello World',
':description' => 'Hello World',
':content' => 'Hello World'
]
);
$result = self::$Article->fetchRow(
[
':article_id' => self::$PDO->fetchLastInsertId()
]
);
$this->assertArrayHasKey('article_id', $result);
$expected = 12; // 12 keys associate with values in the array
$this->assertEquals($expected, count($result));
}
}
我查看了我的文章表,没有没有测试数据了,看来setup()已经清理过了。它应该如何工作?
tearDown() 怎么样 - 是否意味着 在 每个 测试用例之后运行?
【问题讨论】: