PHPUnit-setUp()-它是否在每个测试用例之前和之后运行?
我仍然对PHPUnit中的PHPUnit-setUp()-它是否在每个测试用例之前和之后运行?,phpunit,php-5.6,Phpunit,Php 5.6,我仍然对PHPUnit中的setup()有点困惑 它是否在每个测试用例之前和之后运行 对于intance,我想在每次测试之前清理我的文章表,但我想保留我已经注入表中的测试数据。因为我只想在下次测试前清洗它 我的测试 namespace Test\Foo\Article; use Test\SuiteTest; use Foo\Article; class ArticleTest extends SuiteTest { protected static $Article; /
setup()
有点困惑
它是否在每个测试用例之前和之后运行
对于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));
}
}
我检查了我的文章表,这里不再有测试数据了,似乎setup()
已经把它清理干净了。它应该如何工作
关于tearDown()
-它是否意味着在每个测试用例之后运行?setUp()
在每个测试方法之前运行,tearDown()
在每个测试方法之后运行
PHPUnit手册-第4章固定件:
在运行测试方法之前,将调用名为setUp()的模板方法
一旦测试方法完成运行,无论成功与否,都会调用另一个名为tearDown()的模板方法
请参见谢谢,但将其更改为公共后没有任何区别。受保护否,
设置()
可以受保护。