Codeception揭示了一些有用的事件集合,这些事件将在这个用例中派上用场。请参阅Codeception文档的Customization: Events部分以获取更多信息。
我建议拦截两块页描述的事件:
- 的
test.fail
事件,汇总关于每个信息失败的测试。
test.fail.print
事件,用于在Codeception完成测试套件时处理汇总数据(例如,通过发送汇总电子邮件)并将其自身的故障摘要输出到屏幕。
要做到这一点,你只需要建立一个自定义事件处理程序类并将其注册为在配置文件的扩展名:
# codeception.yml
extensions:
enabled: [MyCustomEventHandler]
# MyCustomEventHandler.php
<?php
// Note: this was drafted using Codeception 2.0. Some of the namespaces
// maybe different if you're using a more-recent version of Codeception.
class MyCustomEventHandler extends \Codeception\Platform\Extension
{
/**
* @var \Exception[]
*/
protected $testFailures = [];
/**
* Maps Codeception events to method names in this class.
*
* Defining an event/method pair in this array essentially subscribes
* the method as a listener for its corresponding event.
*
* @var array
*/
public static $events = [
\Codeception\Events::TEST_FAIL => 'singleTestJustFailed',
\Codeception\Events::TEST_FAIL_PRINT => 'allTestFailuresAreBeingDisplayed',
];
/**
* This method will automatically be invoked by Codeception when a test fails.
*
* @param \Codeception\Event\FailEvent $event
*/
public function singleTestJustFailed(\Codeception\Event\FailEvent $event)
{
// Here we build a list of all the failures. They'll be consumed further downstream.
$this->testFailures[] = $event->getFail();
}
/**
* This method will automatically be invoked by Codeception when it displays
* a summary of all the test failures at the end of the test suite.
*/
public function allTestFailuresAreBeingDisplayed()
{
// Build the email.
$emailBody = '';
foreach ($this->testFailures as $failure) {
// Methods in scope include: $failure->getMessage(), $failure->getFile(), etc.
$emailBody .= $failure->getMessage() . "\r\n";
}
// Now send the email!
}
}
希望这有助于!
顺便说一句,这个建议假设你只想在测试套件的末尾发送摘要邮件,而不是每个失败测试的单个邮件。如果这个假设是无效的,那么解决方案就变得更简单了,因为你不需要拦截'Codeception \ Events :: TEST_FAIL_PRINT'事件 – Nate