【问题标题】:PHPUnit Selenium Server - Better/Custom Error Handling?PHPUnit Selenium 服务器 - 更好/自定义错误处理?
【发布时间】:2011-12-08 01:32:53
【问题描述】:

有没有办法让 PHPUnit 在出错后继续?例如,我有一个大型测试套件(400 多个步骤),我希望如果没有找到某个元素,它不会阻止我的脚本的其余部分继续。

【问题讨论】:

    标签: php testing selenium phpunit


    【解决方案1】:

    我们在 Selenium 测试中做同样的事情。您需要捕获因断言失败而引发的异常,唯一的方法是创建一个覆盖断言方法的自定义测试用例基类。您可以使用测试侦听器存储失败消息并在最后使测试失败。

    我面前没有代码,但它非常简单。例如,

    abstract class DelayedFailureSeleniumTestCase extends PHPUnit_Extension_SeleniumTestCase
    {
        public function assertElementText($element, $text) {
            try {
                parent::assertElementText($element, $text);
            }
            catch (PHPUnit_Framework_AssertionFailedException $e) {
                FailureTrackerListener::addAssertionFailure($e->getMessage());
            }
        }
    
        ... other assertion functions ...
    }
    
    class FailureTrackerListener implements PHPUnit_Framework_TestListener
    {
        private static $messages;
    
        public function startTest() {
            self::$messages = array();
        }
    
        public static function addAssertionFailure($message) {
            self::$messages[] = $message;
        }
    
        public function endTest() {
            if (self::$messages) {
                throw new PHPUnit_Framework_AssertionFailedException(
                        implode("\n", self::$messages));
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      有更好的方法来做到这一点。您可以只重载一个方法:runTest(),而不是重载每个 assert*() 方法。它是针对每个断言的,并且可以捕获异常:

      abstract class AMyTestCase extends PHPUnit_Framework_TestCase
      {
          public function runTest()
          {
              try {
                  parent::runTest();
              }
              catch ( MyCustomException $Exc ) {
                  // will continue tests
              }
              catch ( Exception $Exc ) {
                  if ( false === strpos($Exc->getMessage(), 'element not found') ) {
                      // rethrow:
                      throw $Exc;
                  }
                  // will also continue
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-04
        • 2013-02-12
        • 2011-01-29
        相关资源
        最近更新 更多