【问题标题】:SimpleTester on CodeIgniter fails with "Class 'GroupTest' not found"CodeIgniter 上的 SimpleTester 失败,并显示“找不到类‘GroupTest’”
【发布时间】:2012-02-09 18:11:36
【问题描述】:

我正在尝试在新的 CodeIgniter 应用程序上进行全新安装 SimpleTester,按照此处的说明进行操作:http://codeigniter.com/wiki/SimpleTester_-_Unit_testing_library

一切都很好,直到第 6 步,当我将“simplester”添加到自动加载的库列表中时。一旦我这样做,访问任何页面都会导致:

致命错误:在中找不到类“GroupTest” /path/to/app/application/libraries/simplester.php 在第 84 行

浏览 GroupTest 的代码,我只看到它在 cmets 和一个自述文件中被引用,其中说明了以下内容:

GroupTest 已重命名为 TestSuite(见下文)。 它在 1.1 中被完全删除,有利于这一点 名字。

我尝试修改第 84 行以将 GroupTest 替换为 TestSuite,但随后出现以下错误:

致命错误:调用未定义的方法 TestSuite::addTestFile() in /home/path/to/app/application/libraries/simplester.php 在第 96 行

这是他们的错误吗?有人见过这个吗?

【问题讨论】:

    标签: unit-testing codeigniter simpletest


    【解决方案1】:

    我遇到了同样的问题。 GroupTest 类可以在 SimpleTest 1.0.1 版本的 test_case.php 中找到: http://sourceforge.net/projects/simpletest/files/simpletest/simpletest_1.0.1/

    不幸的是,简单地将 v1.0.1 插入到库文件夹中并不能解决世界上所有的问题。我不再收到“致命错误:找不到类‘GroupTest’...”错误,但我确实遇到了分段错误并且我的网站不再工作。

    我曾短暂尝试追查问题,但无济于事。

    注意:我也回复了CodeIgniter Wiki page containing the same question

    【讨论】:

      【解决方案2】:

      我在当前项目中遇到了同样的问题,发现问题是 GroupTest 被替换为 TestSuite,它的工作方式略有不同。

      这是我使用的库代码:

      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
      
      $libraryDir = APPPATH . 'libraries/simpletest';
      
      if(!is_dir($libraryDir))
          exit("Simpletest must be located in \"$libraryDir\"");
      
      require_once $libraryDir . '/unit_tester.php';
      require_once $libraryDir . '/mock_objects.php';
      require_once $libraryDir . '/collector.php';
      
      class SimpleTester
      {
          /**
          * What reporter should be used for display.
          * Could be either HtmlReporter, SmallReporter, MinimalReporter or ShowPasses.
          */
          public $Reporter = 'MinimalReporter';
      
          private $testDir;
          private $testTitle;
          private $fileExtension;
      
          public function __construct($params = false)
          {
              $ci =& get_instance();
      
              $ci->config->load('simpletester');
      
              if($params == false) {
                  $params['runFromIPs'] = $ci->config->item('runFromIPs');
                  $params['testDir'] = $ci->config->item('testDir');
                  $params['fileExtension'] = $ci->config->item('fileExtension');
                  $params['autorun'] = $ci->config->item('autorun');
                  $params['reporter'] = $ci->config->item('reporter');
                  $params['testTitle'] = $ci->config->item('testTitle');
              }
      
              if(isset($params['runFromIPs']) && strpos($params['runFromIPs'], $ci->input->server('SERVER_ADDR') === FALSE))
              {
                  // Tests won't be run automatically from this IP.
                  $params['autorun'] = FALSE;
              }
      
              // Check if call was an AJAX call. No point in running test
              // if not seen and may break the call.
              $header = 'CONTENT_TYPE';
              if(!empty($_SERVER[$header])) {
                  // @todo Content types could be placed in config.
                  $ajaxContentTypes = array('application/x-www-form-urlencoded', 'multipart/form-data');
                  foreach ($ajaxContentTypes as $ajaxContentType) {
                      if(false !== stripos($_SERVER[$header], $ajaxContentType))
                      {
                          $params['autorun'] = FALSE;
                          break;
                      }
                  }
              }
      
              $this->testDir = $params['testDir'];
              $this->testTitle = $params['testTitle'];
              $this->fileExtension = $params['fileExtension'];
      
              if(isset($params['reporter']))
                  $this->Reporter = $params['reporter'];
      
              if($params['autorun'] == TRUE)
                  echo $this->Run();
          }
      
          /**
          * Run the tests, returning the reporter output.
          */
          public function Run()
          {
              // Save superglobals that might be tested.
              if(isset($_SESSION)) $oldsession = $_SESSION;
              $oldrequest = $_REQUEST;
              $oldpost = $_POST;
              $oldget = $_GET;
              $oldfiles = $_FILES;
              $oldcookie = $_COOKIE;
      
              $test_suite = new TestSuite($this->testTitle);
      
              // Add files in tests_dir
              if(is_dir($this->testDir))
              {
                  if($dh = opendir($this->testDir))
                  {
                      while(($file = readdir($dh)) !== FALSE)
                      {
                          // Test if file ends with php, then include it.
                          if(substr($file, -(strlen($this->fileExtension)+1)) == '.' . $this->fileExtension)
                          {
                              $test_suite->addFile($this->testDir . "/$file");
                          }
                      }
                      closedir($dh);
                  }
              }
      
              // Start the tests
              ob_start();
              $test_suite->run(new $this->Reporter);
              $output_buffer = ob_get_clean();
      
              // Restore superglobals
              if(isset($oldsession)) $_SESSION = $oldsession;
              $_REQUEST = $oldrequest;
              $_POST = $oldpost;
              $_GET = $oldget;
              $_FILES = $oldfiles;
              $_COOKIE = $oldcookie;
      
              return $output_buffer;
          }
      }
      
      // Html output reporter classes //////////////////////////////////////
      
      /**
      * Display passes
      */
      class ShowPasses extends HtmlReporter
      {
          function ShowPasses()
          {
              $this->HtmlReporter();
          }
      
          function paintPass($message)
          {
              parent::paintPass($message);
              print "<span class=\"pass\">Pass</span>: ";
              $breadcrumb = $this->getTestList();
              array_shift($breadcrumb);
              print implode("-&gt;", $breadcrumb);
              print "-&gt;$message<br />\n";
          }
      
          function _getCss()
          {
              return parent::_getCss() . ' .pass {color:green;}';
          }
      }
      
      /**
      * Displays a tiny div in upper right corner when ok
      */
      class SmallReporter extends HtmlReporter
      {
          var $test_name;
      
          function ShowPasses()
          {
              $this->HtmlReporter();
          }
      
          function paintHeader($test_name)
          {
              $this->test_name = $test_name;
          }
      
          function paintFooter($test_name)
          {
              if($this->getFailCount() + $this->getExceptionCount() == 0)
              {
                  $text = $this->getPassCount() . " tests ok";
                  print "<div style=\"background-color:#F5FFA8; text-align:center; right:10px; top:30px; border:2px solid green; z-index:10; position:absolute;\">$text</div>";
              }
              else
              {
                  parent::paintFooter($test_name);
                  print "</div>";
              }
          }
      
          function paintFail($message)
          {
              static $header = FALSE;
              if(!$header)
              {
                  $this->newPaintHeader();
                  $header = TRUE;
              }
              parent::paintFail($message);
          }
      
          function newPaintHeader()
          {
              $this->sendNoCacheHeaders();
              print "<style type=\"text/css\">\n";
              print $this->_getCss() . "\n";
              print "</style>\n";
              print "<h1 style=\"background-color:red; color:white;\">$this->test_name</h1>\n";
              print "<div style=\"background-color:#FBFBF0;\">";
              flush();
          }
      }
      
      /**
      * Minimal only displays on error
      */
      class MinimalReporter extends SmallReporter
      {
          function paintFooter($test_name)
          {
              if($this->getFailCount() + $this->getExceptionCount() != 0)
              {
                  parent::paintFooter($test_name);
                  print "</div>";
              }
          }
      }
      

      对我来说效果很好,但我还没有测试所有不同的记者。但是默认的效果很好。

      这就是我使用它的方式:

      $this->load->library('simpletester');
      echo $this->simpletester->Run();
      

      我的配置文件是:

      $config['testDir'] = APPPATH . 'tests';
      $config['runFromIPs'] = '127.0.0.1';
      $config['reporter'] = 'HtmlReporter';
      $config['autorun'] = false;
      $config['fileExtension'] = 'php';
      $config['testTitle'] = 'My Unit Tests';
      

      【讨论】:

      • 感谢您的深入回答!我回家后会尝试一下,然后在这里发帖。
      猜你喜欢
      • 2013-04-17
      • 2014-11-17
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多