2011-05-04 100 views
2

我正在嘗試創建我的第一個單元測試。我使用Zend Studio的,和我已經打算加入PHPUnit的庫到我的項目:Zend Studio和PHPUnit入門問題

Project -> Properties -> Add Library

當我運行它作爲PHP單元測試,我得到以下錯誤:

Unable to run a PHPUnit session. Only PHPUNIT classes can be run as PHPUnit tests. Reason: Not tests found in IndexControllerTest.php

IndexControllerTest.php:

<?php 
require_once 'application\controllers\IndexController.php'; 
require_once 'PHPUnit\Framework\TestCase.php'; 
/** 
* IndexController test case. 
*/ 
class IndexControllerTest extends PHPUnit_Framework_TestCase 
{ 
    /** 
    * @var IndexController 
    */ 
    private $IndexController; 
    /** 
    * Prepares the environment before running a test. 
    */ 
    protected function setUp() 
    { 
     parent::setUp(); 
     // TODO Auto-generated IndexControllerTest::setUp() 
     $this->IndexController = new IndexController(/* parameters */); 
    } 
    /** 
    * Cleans up the environment after running a test. 
    */ 
    protected function tearDown() 
    { 
     // TODO Auto-generated IndexControllerTest::tearDown() 
     $this->IndexController = null; 
     parent::tearDown(); 
    } 
    /** 
    * Constructs the test case. 
    */ 
    public function __construct() 
    { 
     // TODO Auto-generated constructor 
    } 
    /** 
    * Tests IndexController->init() 
    */ 
    public function testInit() 
    { 
     // TODO Auto-generated IndexControllerTest->testInit() 
     $this->markTestIncomplete("init test not implemented"); 
     $this->IndexController->init(/* parameters */); 
    } 
    /** 
    * Tests IndexController->indexAction() 
    */ 
    public function testIndexAction() 
    { 
     // TODO Auto-generated IndexControllerTest->testIndexAction() 
     $this->markTestIncomplete("indexAction test not implemented"); 
     $this->IndexController->indexAction(/* parameters */); 
    } 
} 

我該如何解決呢?

+0

您是如何運行測試的?你做了「右鍵單擊腳本」>「運行方式」>「PHPUnit測試」? – Gordon 2011-05-04 17:22:53

+0

是的,這正是我所做的。 – 2011-05-04 17:27:15

+1

刪除父::調用,然後再試一次,雖然這不應該是錯誤的原因(調用不需要)。我不時有這個錯誤,但當我第二次重新運行測試時,它通常會消失。 – Gordon 2011-05-04 17:29:15

回答

3

您必須刪除測試案例的__construct()方法。 PHPUnit將參數傳遞給構造函數,因此您必須將它們傳遞給parent::__construct()或更可能 - 完全刪除構造函數。另外,如果您正在使用Zend Framework並測試Zend_Controller_Action類,則可能需要考慮使用Zend_Test_PHPUnit_ControllerTestCase,因爲它爲您提供了大量的腳手架。請注意,每個測試將從一條路徑直接進入渲染的內容,而這些內容可能沒有足夠滿足您的需求。這不是我們的,所以我分別爲控制器和視圖創建了基本測試用例類。

+2

您c單元測試類中的leave __construct()方法。這是多餘的,但不會傷害任何東西。 – allnightgrocery 2011-05-05 13:24:24

+0

@Inkspeak - 如果你的測試使用'@ dataProvider',構造函數將會破壞它們。它也不會設置測試的名稱,這似乎會干擾隔離運行測試。 – 2011-05-05 20:19:36

+1

我沒有看到@Teodor使用@dataProvider,根據我對原帖的評論,我建議他通過套件運行他的測試。感謝澄清雖然。 – allnightgrocery 2011-05-06 01:20:05