2011-01-24 54 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链接你的文档