Nab*_*eel 2 php phpunit unit-testing
我正在使用 PHPUnit 来测试具有许多功能的类。PHPUnit 框架从上到下运行测试函数。
问题是:如何在不重新排序的情况下按指定的顺序运行测试函数,然后在源代码中。
为了澄清这个问题,假设我们有 5 个测试函数;
框架将运行 testFunc1 然后 testFunc2 直到它到达 testFunc5。
但是,我想运行 testFunc3 然后 testFunc1 然后 testFunc5 然后 testFunc2 然后 testFunc4 而不在源文件中重新排序它们。
PHPUnit 将按照它们在您的*_TestCase类中编写的确切顺序执行测试。
这些测试中的每一个都应该能够独立运行,而不依赖于在它之前执行的其他一些测试。
如果您在对数据库进行测试时遇到问题,我建议您使用以下内容:
class MyTest extends PHPUnit_Framework_TestCase {
public function setUp() {
// REPLACE INTO testDb (ID, NAME, VALUE) VALUES (1001000, 'testing', 'value')
$this->db = $db_connection;
}
public function tearDown() {
// DELETE FROM testDb WHERE ID > 10010000 // or something like this
}
public function testSelect() {
$this->assertSame("value", $this->db->getId(100100));
}
/**
* @depends testSelect
*/
public function testInsert() {
$this->db->insertById(1001111, "mytest", "myvalue");
$this->db->getId(1001111);
}
/**
* @depends testSelect
*/
public function testDelete() {
$this->db->deleteById(1001000);
$this->assertNull($this->db->getId(10010000);
}
// and so on
}
Run Code Online (Sandbox Code Playgroud)
这 setUp()方法将在每个测试用例之前运行,并确保大多数测试用例需要的所有值都在那里,tearDown()并在测试套件之后进行清理。
这 @depends注释将确保当选择测试失败插入测试无法运行。(如果您无法加载值,则插入新值并使其无法正常工作,无需尝试)。
| 归档时间: |
|
| 查看次数: |
1583 次 |
| 最近记录: |