记录代码接收错误
Logging Codeception Errors
我是 Codeception 的新手,遇到了一个我无法解决的问题。我的测试套件中有大约 40 个测试,如果测试失败,我需要发送一封电子邮件说明失败的原因。例如,如果 Codeception 在页面上找不到导致测试失败的元素,我需要发送一封仅包含错误的电子邮件,如下所示:
Failed to verify emailing wish list behaves as expected in ThisClass::thisTest (/home/qauser/codeception_tests///acceptance-mobile/Wishlist/EmailWishlistCest.php)
Couldn't see "Success!","//*[@id="wish-list-confirm-popup"]/div/div/div[1]/h4":
我不想发送完整的堆栈跟踪,只想发送实际错误。有谁知道这是否可能?
Codeception 公开了一个有用的事件集合,这些事件将对这个用例派上用场。查看 Codeception 文档的 Customization: Events 部分以获取更多信息。
我建议拦截该页面上描述的两个事件:
test.fail
事件,用于汇总有关每个失败测试的信息。
test.fail.print
事件,用于在 Codeception 完成测试套件并将其自己的故障摘要打印到屏幕时处理聚合数据(例如,通过发送摘要电子邮件)。
为此,您只需构建自定义事件处理程序 class 并将其注册为配置文件中的扩展:
# 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 的新手,遇到了一个我无法解决的问题。我的测试套件中有大约 40 个测试,如果测试失败,我需要发送一封电子邮件说明失败的原因。例如,如果 Codeception 在页面上找不到导致测试失败的元素,我需要发送一封仅包含错误的电子邮件,如下所示:
Failed to verify emailing wish list behaves as expected in ThisClass::thisTest (/home/qauser/codeception_tests///acceptance-mobile/Wishlist/EmailWishlistCest.php) Couldn't see "Success!","//*[@id="wish-list-confirm-popup"]/div/div/div[1]/h4":
我不想发送完整的堆栈跟踪,只想发送实际错误。有谁知道这是否可能?
Codeception 公开了一个有用的事件集合,这些事件将对这个用例派上用场。查看 Codeception 文档的 Customization: Events 部分以获取更多信息。
我建议拦截该页面上描述的两个事件:
test.fail
事件,用于汇总有关每个失败测试的信息。test.fail.print
事件,用于在 Codeception 完成测试套件并将其自己的故障摘要打印到屏幕时处理聚合数据(例如,通过发送摘要电子邮件)。
为此,您只需构建自定义事件处理程序 class 并将其注册为配置文件中的扩展:
# 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!
}
}
希望对您有所帮助!