2011-01-24 28 views
0

我剛剛在我的Ubuntu上安裝了phpunit。phpunit在mvc結構中

現在我不想實施一些測試到我的(php代碼)mvc結構。但我沒有在那裏我得到測試輸出的任何線索......

我有控制器功能,看起來像這樣:

public function run_test(){ 
     error_log("i was called correctly"); 
     $mytest = new modeltest(); 
     $mytest->run(); 

     // $this->outputvar = testresults ?? 

     $this->set_template("mytestview"); 
    } 

以及我testmodel:

class ModelTest extends PHPUnit_Framework_TestCase{ 

    public function testWorks(){ 
     error_log("i was called correctly as well"); 
     $model = new model(); 
     $this->assertEquals(3, $model->works(2, 1)); 
    } 

} 

作品僅僅是測試phpunit運行的簡單函數,它增加了兩個值。

我如何獲得測試結果?

回答

1

這不是你應該如何運行PHPUnit的測試。請參閱chapter 5 in PHPUnit manual

換句話說,測試過程應與應用程序分開(因此您不需要自己的控制器)。一個命令行phpunit工具爲你完成所有骯髒的工作。 NetBeans IDE(也可能是其他)還允許您在IDE中運行此工具。

+0

AAAH。 ..現在我明白了:-)我上次在2004年或2005年使用它。許多事情在那些日子裏有所不同。 – helle 2011-01-25 10:23:10

1

我打算繼續,假設您使用的是PHPUnit的最新版本,並從命令行運行它。如果你想測試的輸出,測試運行提供了用於存儲輸出幾個選擇:

--log-graphviz <file> Log test execution in GraphViz markup. 
--log-json <file>  Log test execution in JSON format. 
--log-tap <file>  Log test execution in TAP format to file. 
--log-xml <file>  Log test execution in XML format to file. 

這些是PHPUnit的文檔。如果您只是指定一個文件名稱,它會將輸出存儲在您運行測試的目錄中。

1

快速(以防萬一)上Ubunutu安裝

sudo apt-get install php-pear 

sudo pear channel-discover pear.phpunit.de 
sudo pear channel-discover components.ez.no 
sudo pear channel-discover pear.symfony-project.com 
sudo pear install phpunit/PHPUnit 

然後進入文件夾,你的測試是:

phpunit ourTest.php 

和@Mchl鏈接你的文檔