Fra*_*ehl 5 php phpunit unit-testing
我有一个PHPUnit引导程序文件,它创建一个用于DB相关单元测试的测试DB,并在测试完成后注册一个shutdown函数来销毁DB.每次运行的新数据库!
问题:当测试失败时,我想保留数据库以进行调试.目前,我必须手动禁用我的register_shutdown_function()呼叫,然后重新运行测试.
如果我可以访问PHPUnit的运行的最终成功/失败状态,我可以基于PHPUnit引导程序文件中的开关动态触发数据库销毁过程.
为了触发正确的结果时,即输出PHPUnit的某处存储这些信息OKVS FAILURES!.但是,根据我发现的情况,此信息不会暴露给用户级引导程序文件.有没有做过这样的事情?
如果你想探索,这是从命令行运行PHPUnit时出现的PHPUnit的堆栈跟踪...
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:139
PHP 5. PHPUnit_TextUI_Command->handleBootstrap() /usr/share/php/PHPUnit/TextUI/Command.php:620
PHP 6. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/TextUI/Command.php:867
PHP 7. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:79
PHP 8. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:95
PHP 9. [YOUR PHPUNIT BOOTSTRAP RUNS HERE]
Run Code Online (Sandbox Code Playgroud)
为了访问PHPUnit测试用例的状态,我通常建议使用:
请参阅docs:PHPUnit_Framework_TestListener以及如何将其添加到xml配置中.
小样本:
<listeners>
<listener class="DbSetupListener" file="/optional/path/to/DbSetupListener.php"/>
</listeners>
Run Code Online (Sandbox Code Playgroud)
<?php
class DbSetupListener implements PHPUnit_Framework_TestListener {
private $setupHappend = false;
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
$this->error = true;
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
$this->error = true;
}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) { }
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { }
public function startTest(PHPUnit_Framework_Test $test) { }
public function endTest(PHPUnit_Framework_Test $test, $time) { }
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
if(!$this->setupHappend) {
$this->setupDatabase();
$this->setupHappend = true;
}
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
// If you have multiple test suites this is the wrong place to do anything
}
public function __destruct() {
if($this->error) {
// Something bad happend. Debug dump or whatever
} else {
// Teardown
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该让你相当容易.如果你只需要"听"到特定的tests或testsuites那么你可以使用的参数startTest和startTestSuite.
两个对象都有一个getName()方法,分别为您提供测试套件和测试用例的名称.
| 归档时间: |
|
| 查看次数: |
2140 次 |
| 最近记录: |