【问题标题】:Netbeans "no tests executed"Netbeans“没有执行测试”
【发布时间】:2012-04-24 22:34:20
【问题描述】:

我有一个包含单元测试的 php 项目。我使用 Netbeans 进行开发,并希望在我的 IDE 中集成 phpunit。如果我从命令行运行 phpunit,它就可以工作。 如果我按 Alt+F6 在 Netbeans 中运行测试,则不运行任何测试,我会收到以下消息:

未执行任何测试(可能发生错误,请在“输出”窗口中验证。)

结构(它是一个 Zend Framework 2 模块):

Foo/
  src/
    Foo/
      Bar.php
      Baz.php
  tests/
    Foo/
      BarTest.php
    bootstrap.php
    phpunit.xml
  Module.php
  autoload_register.php

BarTest.php 的内容

namespace Foo;

use PHPUnit_Framework_TestCase as TestCase;

class BarTest extends TestCase
{
    public function testIsWorking ()
    {
        $this->assertTrue(true);
    }
}

我的 phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php">
    <testsuite name="Foo">
        <directory>./</directory>
    </testsuite>
</phpunit>

我的 bootstrap.php:

// Set error reporting pretty high
error_reporting(E_ALL | E_STRICT);

// Get base, application and tests path
define('BASE_PATH',  dirname(__DIR__));

// Load autoloader
require_once BASE_PATH . '/autoload_register.php';

在我试过的 Netbeans 项目属性中:

  1. 将测试目录设置为Foo/tests(我认为这是必需的)
  2. 专门设置引导文件(我认为不需要)
  3. 专门设置XML配置文件(我认为不需要)
  4. 专门设置为运行所有 *Test 文件(我认为不是必需的)

如何确保 Netbeans 可以执行我的 phpunit 测试?

【问题讨论】:

  • 你试过运行phpunit --bootstrap bootstrap.php Foo/tests 吗?因为这就是 IDE 最终要做的事情

标签: php unit-testing netbeans phpunit


【解决方案1】:

您可以简单地将所有这些放入您的 PHPUnit bootstrap.php:

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

【讨论】:

    【解决方案2】:

    问题在于 Netbeans 期望所有测试都位于一个根项目文件夹中。如果不这样做,代码覆盖率和其他功能将无法正常工作。 请参阅enhancement 了解更多信息。

    这个问题可以通过创建自定义testSuite来解决:

    • 如果尚未创建,请在根项目文件夹中创建目录“test”或“tests”。
    • 在这个“test”文件夹中创建文件TestSuite.php
    • 在您的 Netbeans 项目设置中添加这个 TestSuite.php。
    <?php
    
    class TestSuite extends PHPUnit_Framework_TestSuite
    {
        public static function suite()
        {
            $suite = new TestSuite();
    
            $file = '/Foo/tests/Foo/BarTest.php';
    
            echo 'Adding test: ' . $file . "\n";
            $suite->addTestFile($file);
    
            //add here algorithm that will search and add all test files from your /tests directory
    
        return $suite;
    
    }
    

    Netbeans 7.2 对run all test in separate directory 有很好的功能。

    【讨论】:

      【解决方案3】:

      虽然我已将 error_reporting 设置为高级别,但 PHP CLI 并没有打开 display_errors。就是这样,所以现在我得到了我的报告……

      【讨论】:

      • 你能解释一下这是什么意思吗?我该怎么做?我也收到Perhaps an error occurred, verify in Output window. 错误。我对 PHPUnitTest 完全陌生。
      • bootstrap.php 文件包含 error_reporting(E_ALL | E_STRICT); 但不包含 ini_set('display_errors', '1');。因此,尽管报告了错误,但它们并没有显示出来。设置这个也显示了错误。
      • 感谢您的回复。你能告诉我在哪里可以找到这个 bootstrap.php 文件吗?我在搜索结果中得到了这么多 bootstrap.php 文件,不知道应该改哪个。
      【解决方案4】:

      NetBeans 未找到自动加载器类。然后,在项目设置中你必须说什么类 autoloader、bootstrap 等。

      就我而言,刚刚进入:项目属性 -> 测试 -> PHPUnit。 我将选项设置为“使用引导程序”,并告诉引导程序的路径。

      我的引导文件有自动加载器,然后我的所有测试类都执行没有错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 2013-11-04
        • 1970-01-01
        相关资源
        最近更新 更多