我setup()在PHPUnit中仍然有点困惑.
它是在每个测试用例之前和之后 运行的吗?
对于intance,我想在每次测试之前清理我的文章表,但是我想保留已经注入表中的测试数据.因为我只想清洁它直到下一次测试.
我的测试,
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));
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了我的文章表,没有测试数据,似乎setup()已经清理了它.是它应该如何工作?
怎么样tearDown()- 在每个测试用例之后运行是什么意思?
Sto*_*ler 14
setUp()在每个测试方法之前tearDown()运行,在每个测试方法之后运行.
PHPUnit手册 - 第4章修复:
在运行测试方法之前,将调用名为setUp()的模板方法
...
一旦测试方法运行完毕,无论是成功还是失败,都会调用另一个名为tearDown()的模板方法
请参阅https://phpunit.de/manual/current/en/fixtures.html
| 归档时间: |
|
| 查看次数: |
6977 次 |
| 最近记录: |