phpunit 中的可重用测试
Reusable tests in phpunit
假设我想测试三种状态下的数据库一致性:
插入数据后 - 我想确保一定数量的行已插入数据库 tables.
当数据更新后-我想重复行量测试(量必须与插入后相同)
删除数据后 - 我想确保所有数据都已删除。
此外,当我插入或更新时,一些文件(图像)可以上传到服务器,文件路径将存储在数据库中。所以我还想检查文件数量是否与数据库文件中的行数量相对应 table.
换句话说,我想在插入和更新后重用两种方法:testRowsAmountOnAddUpdate
和 testFilesAmountOnAddUpdate
。
组织代码的最佳方式是什么?我应该使用固定装置吗?
其实我已经走到了这个结构(代码很简单):
class MyTest extends PHPUnit_Framework_TestCase
{
private static $expectedRowsAmountInDB;
private function getRowsAmountFromDB()
{
//using this method to get rows count from DB
}
public function setUp()
{
//import initial data, set up the world
}
public function testRowsAmountOnAdd()
{
//count rows amount after setup
//using getRowsAmountFromDB helper method
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function testRowsAmountOnUpdate()
{
//...here comes update rows code
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function testRowsAmountOnDelete()
{
//...here comes delete rows code
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function tearDown()
{
//delete all rows that were created in this test
//and back DB to the original state
}
}
正如一些评论所说,只要有可能,最好模拟数据库入口层,但有时您只需要测试代码和数据库之间的交互,尤其是对于复杂的查询。我个人将它用于数据库固定装置和断言:(呃,免责声明,我也记下了这个所以我有偏见)
https://github.com/malteriesch/test-db-acle
它允许以竖线分隔的表格格式插入数据,同时将默认值插入任何非空列,然后检查数据库的状态。
$this->setupTables("
[address]
address_id |company
1 |me
3 |you
[user]
user_id |name
10 |John
20 |Mary
");
// excercise the SUT, e.g.
$this->addressService->addEntry("them");
$this->assertTableStateContains("
[address]
address_id |company
1 |me
3 |you
100 |them #this is the new row inserted
[user]
user_id |name
10 |John
20 |Mary
");
}
也许你可以将它们包装在 testRowsAmountOnAddUpdate
和 testFilesAmountOnAddUpdate
中,对于文件上传,我会读取该字段的值,然后断言那里指定的文件存在。
请注意,DBUnit 也是一个不错的选择,我个人喜欢尽可能简单和简洁地编写测试,并且只专注于与测试实际 changes/is 相关的信息。 Otherfixtures 对我来说有点冗长。
假设我想测试三种状态下的数据库一致性:
插入数据后 - 我想确保一定数量的行已插入数据库 tables.
当数据更新后-我想重复行量测试(量必须与插入后相同)
删除数据后 - 我想确保所有数据都已删除。
此外,当我插入或更新时,一些文件(图像)可以上传到服务器,文件路径将存储在数据库中。所以我还想检查文件数量是否与数据库文件中的行数量相对应 table.
换句话说,我想在插入和更新后重用两种方法:testRowsAmountOnAddUpdate
和 testFilesAmountOnAddUpdate
。
组织代码的最佳方式是什么?我应该使用固定装置吗?
其实我已经走到了这个结构(代码很简单):
class MyTest extends PHPUnit_Framework_TestCase
{
private static $expectedRowsAmountInDB;
private function getRowsAmountFromDB()
{
//using this method to get rows count from DB
}
public function setUp()
{
//import initial data, set up the world
}
public function testRowsAmountOnAdd()
{
//count rows amount after setup
//using getRowsAmountFromDB helper method
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function testRowsAmountOnUpdate()
{
//...here comes update rows code
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function testRowsAmountOnDelete()
{
//...here comes delete rows code
$this->assertEquals(self::$expectedRowsAmountInDB, $this->getRowsAmountFromDB());
}
public function tearDown()
{
//delete all rows that were created in this test
//and back DB to the original state
}
}
正如一些评论所说,只要有可能,最好模拟数据库入口层,但有时您只需要测试代码和数据库之间的交互,尤其是对于复杂的查询。我个人将它用于数据库固定装置和断言:(呃,免责声明,我也记下了这个所以我有偏见)
https://github.com/malteriesch/test-db-acle
它允许以竖线分隔的表格格式插入数据,同时将默认值插入任何非空列,然后检查数据库的状态。
$this->setupTables("
[address]
address_id |company
1 |me
3 |you
[user]
user_id |name
10 |John
20 |Mary
");
// excercise the SUT, e.g.
$this->addressService->addEntry("them");
$this->assertTableStateContains("
[address]
address_id |company
1 |me
3 |you
100 |them #this is the new row inserted
[user]
user_id |name
10 |John
20 |Mary
");
}
也许你可以将它们包装在 testRowsAmountOnAddUpdate
和 testFilesAmountOnAddUpdate
中,对于文件上传,我会读取该字段的值,然后断言那里指定的文件存在。
请注意,DBUnit 也是一个不错的选择,我个人喜欢尽可能简单和简洁地编写测试,并且只专注于与测试实际 changes/is 相关的信息。 Otherfixtures 对我来说有点冗长。