2017-01-20 200 views
3

我对Codeception相当陌生,而且遇到了一个我无法弄清楚的问题。我的测试套件中有大约40次测试,如果测试失败,我需要发送一封电子邮件,告诉它失败的原因。例如,如果Codeception无法找到导致失败的测试页面上的元素,我需要只用错误发送一封电子邮件,就像这样:日志记录错误

无法验证电子邮件的愿望清单的行为与预期在ThisClass: :thisTest(/home/qauser/codeception_tests///acceptance-mobile/Wishlist/EmailWishlistCest.php) 看不到“成功!”,“// * [@ id =”wish-list-confirm-popup“]/div/div/div [1]/h4“:

我不想发送完整的堆栈跟踪,只是实际的错误。有谁知道这是否可能?

回答

4

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! 
    } 
} 

希望这有助于!

+0

顺便说一句,这个建议假设你只想在测试套件的末尾发送摘要邮件,而不是每个失败测试的单个邮件。如果这个假设是无效的,那么解决方案就变得更简单了,因为你不需要拦截'Codeception \ Events :: TEST_FAIL_PRINT'事件 – Nate