mvc结构中的phpunit

发布于 2024-10-14 12:17:11 字数 730 浏览 2 评论 0原文

我刚刚在我的 ubuntu 上安装了 phpunit。

现在我不想在我的(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));
    }

}

Works 只是一个用于测试 phpunit 运行的简单函数,它添加了两个值。

我如何获得测试结果?

i have just installed phpunit on my ubuntu.

now i wan't to implement some tests into my (php code) mvc structur. but i don't have any clue where i get the output of the test ...

i have controller function which looks like this:

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

        // $this->outputvar = testresults ??

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

as well as my 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));
    }

}

works is just a simple function for testing phpunit running, which adds two values.

how do i get the results of the test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就此别过 2024-10-21 12:17:11

这不是您运行 PHPUnit 测试的方式。参见PHPUnit手册第5章

换句话说,测试过程应该分开来自您的应用程序(因此您不需要自己的控制器)。命令行 phpunit 工具会为您完成所有脏活。 NetBeans IDE(也许还有其他)还允许您从 IDE 中运行此工具。

THat's not how you're supposed to run PHPUnit's tests. See chapter 5 in PHPUnit manual

In other words, testing process should be separated from your application (so you do not need your own controller for that). A command line phpunit tool does all the dirty job for you. NetBeans IDE (and maybe others) also allow you to run this tool from within the IDE.

画离情绘悲伤 2024-10-21 12:17:11

我将继续假设您正在使用最新版本的 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 文档。如果您只指定一个文件名,它会将输出存储在您运行测试的目录中。

I'm going to go ahead and assume you're using the latest version of PHPUnit and are running it from the command line. If you want the output of the test, the test runner offers a few choices for storing output:

--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.

These are from the PHPUnit docs. If you just specify a file name it will store the output within the directory you're running the tests from.

衣神在巴黎 2024-10-21 12:17:11

在 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 将您链接到文档

Quick install on Ubunutu (just in case)

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

then go to the folder where your test is:

phpunit ourTest.php

and @Mchl linked you to the documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文