[英]phpunit with dbunit: how can i keep data in my db across tests?
我有一个关于dbunit的phpunit问题,以及如何保留一项测试在数据库中创建的数据以供下次使用。 我是phpunit的新手(我们多年来一直在使用内部测试仪,但最终还是试图与现代时代接轨),因此,如果这是一个小问题,我深表歉意。
想要的效果,我有一个mysql表,其中包含一个唯一键列。 如果尝试插入此列的重复项,则会发生一些我想测试的特殊事件。 我编写了一个将值插入此列的测试(并测试其是否成功),然后立即编写了另一个测试,以测试该类在尝试重复值时如何失败。 我希望能够捕获该异常并对其进行测试。 我正在使用dbunit将我需要的所有预填充内容预填充到db中。
在每次测试开始时出现问题 ,就好像调用了getDataSet()一样,结果,我在第一个测试中插入的唯一密钥数据不再在那里可以进行测试。 因此,我无法测试插入重复的唯一键的预期失败。
我正在寻找的东西 ,显然是某种在测试之间持久保存数据库数据的方法; 避免在第二个测试开始时调用getDataSet()。
我当然希望这是可能的。 我无法想象为什么不会。 似乎人们应该测试重复的插入! 如果他们完成任务,我愿意接受其他解决方案。
提前致谢!
这是我的测试,简化为相关内容。
<?php
class UserPOSTTest extends \PHPUnit_Extensions_Database_TestCase
{
static private $pdo = null;
private $conn = null;
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection()
{
if($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new \PDO('mysql:host=localhost;dbname=thedatabase', 'user', '*********');
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, "db");
}
return $this->conn;
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
// this is returned at the beginning of every test
return $this->createFlatXmlDataSet(dirname(__FILE__) . '/some_data_set.xml');
}
/**
* test the insertion of the value "unique key value" into a column set as UNIQUE KEY in mysql
* since getDataSet() has cleared this table, it passes.
*/
public function uniqueKeyTest_passes()
{
$inserter = new Inserter("unique key value");
$this->assertEquals($inserter->one,1); // just some bogus assertion
} // uniqueKeyTest_passes
/**
* run the exact same insert as in uniqueKeyTest_passes() above. the purpose of this test is to
* confirm how the Inserter class fails on the attempt to insert duplicate data into a UNIQUE KEY column.
* however, the data inserted in uniqueKeyTest_passes() has been scrubbed out by getDataSet()
* this is the crux of my question
*/
public function uniqueKeyTest_should_fail()
{
try {
// exact same insert as above, should fail as duplicate
$inserter = new Inserter("unique key value");
}
catch(Exception $e) {
// if an exception is thrown, that's a pass
return;
}
// the insert succeeds when it should not
$this->fail("there should be an exception for attempting insert of unique key value here");
} // uniqueKeyTest_should_fail
}
每个测试都独立运行的事实是单元测试的功能和设计目标。
您可以使用以下方法:
/**
* Confirm how the Inserter class fails on the attempt to
* insert duplicate data into a UNIQUE KEY column.
*
* @expectedException Exception
*/
public function uniqueKeyTest_should_fail()
{
$inserter = new Inserter("unique key value");
// exact same insert as above, should fail as duplicate
$inserter = new Inserter("unique key value");
}
请注意@expectedException
的用法,它大大简化了测试代码。 但是,我将以抛出DuplicateKeyException
的方式编写代码,这将使测试更加具体。 在当前形式中,例如,您将不再能够检测数据库连接错误(并且测试将成功!)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.