为什么我的 PHPUnit Fixtures Stack 测试不能正常工作?
Why does my PHPUnit Fixtures Stack test is not working properly?
我一直在阅读 this 手册,但我不明白为什么 $stack 在每个测试函数后都会丢失值。
这是我的代码:
require_once BASE . 'Transaction.php';
class TransactionTest extends PHPUnit_Framework_TestCase
{
protected $stack;
protected function setUp()
{
Database::getInstance()->connect();
$this->stack = new Transaction(123456789);
}
public function testInsert()
{
$data['name'] = 'Omega';
$this->stack->set($data);
$this->assertTrue($this->stack->save());
}
public function testUpdate()
{
$object = PHPUnitReflectionClass::getInstance($this->stack);
$this->assertEquals(array('name' , 'Omega'), $object->getProperty('name'));
}
}
在"testUpdate"函数中,我没有名字。
如果我只复制所有 PHPUnit 示例,所有测试都将 运行 完美,但我无法在下一个函数中使用 var_dump 查看值。这是我不明白的另一件事。
单元测试应该不依赖于其他测试,这被认为是不好的做法。单元测试的要点是它们 运行 在受控和隔离的环境中。 setUp()
和tearDown()
方法分别在each测试方法前后调用。因此,您的 $stack
属性 在每次测试之前都会被覆盖。您链接到的文档中也指出了这一点(仅在示例 4.1 下方):
setUp()
和 tearDown()
模板方法 运行 测试用例的每个测试方法(以及新实例)一次 class .
(您还假设测试 class 中的方法与它们的编写顺序相同 运行,但情况不一定如此。大多数他们按相同顺序执行 运行 的时间,但你不能依赖它。)
问题是:
- 在每次调用测试方法之前调用设置方法;
- 你们的测试是相互依赖的。
对于第一个问题,您可以使用setUpBeforeClass
方法。来自 doc:
The setUp() and tearDown() template methods are run once for each test
method (and on fresh instances) of the test case class.
In addition, the setUpBeforeClass() and tearDownAfterClass() template
methods are called before the first test of the test case class is run
and after the last test of the test case class is run, respectively.
对于第二个问题,让测试相互依赖是一种不好的做法,但是 PHPUnit 支持声明测试方法之间的显式依赖关系,因此您可以使用 @depends
表示依赖关系的注解:如果一个测试失败,则不执行另一个测试。
所以你的测试class可以是,例如:
require_once BASE . 'Transaction.php';
class TransactionTest extends PHPUnit_Framework_TestCase
{
protected static $stack;
public static function setUpBeforeClass()
{
Database::getInstance()->connect();
$this->stack = new Transaction(123456789);
}
public function testInsert()
{
$data['name'] = 'Omega';
$this->stack->set($data);
$this->assertTrue($this->stack->save());
}
/**
* @depends testInsert
*/
public function testUpdate()
{
$object = PHPUnitReflectionClass::getInstance($this->stack);
$this->assertEquals(array('name' , 'Omega'), $object->getProperty('name'));
}
}
希望对您有所帮助
我一直在阅读 this 手册,但我不明白为什么 $stack 在每个测试函数后都会丢失值。
这是我的代码:
require_once BASE . 'Transaction.php';
class TransactionTest extends PHPUnit_Framework_TestCase
{
protected $stack;
protected function setUp()
{
Database::getInstance()->connect();
$this->stack = new Transaction(123456789);
}
public function testInsert()
{
$data['name'] = 'Omega';
$this->stack->set($data);
$this->assertTrue($this->stack->save());
}
public function testUpdate()
{
$object = PHPUnitReflectionClass::getInstance($this->stack);
$this->assertEquals(array('name' , 'Omega'), $object->getProperty('name'));
}
}
在"testUpdate"函数中,我没有名字。
如果我只复制所有 PHPUnit 示例,所有测试都将 运行 完美,但我无法在下一个函数中使用 var_dump 查看值。这是我不明白的另一件事。
单元测试应该不依赖于其他测试,这被认为是不好的做法。单元测试的要点是它们 运行 在受控和隔离的环境中。 setUp()
和tearDown()
方法分别在each测试方法前后调用。因此,您的 $stack
属性 在每次测试之前都会被覆盖。您链接到的文档中也指出了这一点(仅在示例 4.1 下方):
setUp()
和 tearDown()
模板方法 运行 测试用例的每个测试方法(以及新实例)一次 class .
(您还假设测试 class 中的方法与它们的编写顺序相同 运行,但情况不一定如此。大多数他们按相同顺序执行 运行 的时间,但你不能依赖它。)
问题是:
- 在每次调用测试方法之前调用设置方法;
- 你们的测试是相互依赖的。
对于第一个问题,您可以使用setUpBeforeClass
方法。来自 doc:
The setUp() and tearDown() template methods are run once for each test method (and on fresh instances) of the test case class.
In addition, the setUpBeforeClass() and tearDownAfterClass() template methods are called before the first test of the test case class is run and after the last test of the test case class is run, respectively.
对于第二个问题,让测试相互依赖是一种不好的做法,但是 PHPUnit 支持声明测试方法之间的显式依赖关系,因此您可以使用 @depends
表示依赖关系的注解:如果一个测试失败,则不执行另一个测试。
所以你的测试class可以是,例如:
require_once BASE . 'Transaction.php';
class TransactionTest extends PHPUnit_Framework_TestCase
{
protected static $stack;
public static function setUpBeforeClass()
{
Database::getInstance()->connect();
$this->stack = new Transaction(123456789);
}
public function testInsert()
{
$data['name'] = 'Omega';
$this->stack->set($data);
$this->assertTrue($this->stack->save());
}
/**
* @depends testInsert
*/
public function testUpdate()
{
$object = PHPUnitReflectionClass::getInstance($this->stack);
$this->assertEquals(array('name' , 'Omega'), $object->getProperty('name'));
}
}
希望对您有所帮助