2012-04-12 189 views
2

我有一個包含單元測試的php項目。我使用Netbeans進行開發,並希望將phpunit集成到我的IDE中。如果我從命令行運行phpunit,它正在工作。 如果我按Alt + F6運行Netbeans中沒有測試運行測試,我得到的消息:(發生也許是一個錯誤,驗證輸出窗口)Netbeans「沒有執行測試」

測試未執行

的結構(它是一個Zend框架2模塊):BarTest.php

namespace Foo; 

use PHPUnit_Framework_TestCase as TestCase; 

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

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

內容

我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. 專門設置引導文件(不需要我瘦K)
  3. 要專門設置的XML配置文件(不需要我認爲)
  4. 要設置專門運行所有*測試文件(不是必需的,我認爲)

如何確保Netbeans的可以執行我的phpunit測試?

+0

你試過運行'phpunit --bootstrap bootstrap.php Foo/tests'嗎?因爲多數民衆贊成什麼IDE最終做 – 2012-04-12 15:12:09

回答

0

儘管我已將error_reporting設置爲高級別,但PHP CLI並未打開display_errors。這就是它,所以現在我得到了我的報告...

+0

你能解釋這是什麼意思,我該怎麼做?我也正在'也許發生了一個錯誤,在輸出窗口驗證錯誤。我對PHPUnitTest完全陌生。 – Geek 2013-07-17 10:36:00

+0

bootstrap.php文件包含'error_reporting(E_ALL | E_STRICT);'但不包含'ini_set('display_errors','1');'。所以雖然報告了錯誤,但並未顯示。設置這也顯示了我的錯誤。 – 2013-07-17 10:57:41

+0

感謝您的回覆。你能告訴我在哪裏可以找到這個bootstrap.php文件嗎?我在搜索結果中找到了很多bootstrap.php文件,不知道應該更改哪一個。 – Geek 2013-07-17 11:01:28

0

問題是Netbeans期望位於一個根項目文件夾中的所有測試。如果沒有這樣做,Code Coverage和其他功能無法正常工作。 有關更多信息,請參見enhancement

這個問題是可以解決的創建自定義的測試套件:

  • 創建根項目文件夾目錄「測試」或「測試」,如果它尚未創建。
  • 在此「測試」文件夾中創建文件TestSuite.php
  • 將此TestSuite.php添加到您的Netbeans項目設置中。
<?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

你可以簡單地把所有這些放到你的PHPUnit引導程序中。php:

<?php 

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

NetBeans未找到自動裝載機類。然後,在項目設置中,你必須說什麼類的自動加載器,引導程序等。

在我的情況剛剛進入:項目屬性 - >測試 - > PHPUnit。 我將選項設置爲「使用引導程序」,並告訴引導路徑。

我的引導文件具有自動加載器,然後我的所有測試類都被執行而沒有錯誤。