我仍然对PHPUnit中的setup()
有点困惑。
是否在每个测试用例之前运行,在之后运行 ?
例如,我想在每次测试之前清理我的文章表,但我想保留已经注入表中的测试数据。因为我只想清洁它直到下次测试。
My test,
namespace TestFooArticle;
use TestSuiteTest;
use FooArticle;
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()
-这意味着在 每个测试用例之后运行吗?
setUp()
在每种测试方法之前运行,tearDown()
在每种测试方法之后运行。
PHPUnit手册-第4章fixture:
在运行测试方法之前,调用名为setUp()的模板方法
…
一旦测试方法完成运行,无论成功还是失败,都将调用另一个名为tearDown()的模板方法
见https://phpunit.de/manual/current/en/fixtures.html