【问题标题】:How can I run PHPUnit with CakePHP tests without the Cake console?如何在没有 Cake 控制台的情况下运行带有 CakePHP 测试的 PHPUnit?
【发布时间】:2014-11-03 02:12:28
【问题描述】:

我想用 Grunt 自动化 CakePHP 测试,发现 grunt.loadNpmTasks('grunt-phpunit');,它可以自动化 PHPUnit,但我确定它不能处理 cake test

我对运行 Grunt 的 cake test 的解决方案也很满意,但我真的对运行 PHPUnit 命令的方法很感兴趣,它可以执行 CakePHP 测试。

编辑:我正在使用 CakePHP 的当前稳定版本,现在是 2.5.4。

【问题讨论】:

  • 您需要发布 exact CakePHP 版本。它在 2 和 3 之间有所不同。
  • 你是对的,对不起。我的意思是目前的马厩。我现在编辑我的问题。

标签: cakephp gruntjs phpunit cakephp-2.5


【解决方案1】:

我假设 grunt-phpunit 需要一个默认的标准 phpunit 执行,如果这是真的,你不能那样运行它,你必须熟练使用这个工具。我猜它正在解析测试输出。 CakePHP2 不允许您像现在使用 CakePHP 和其他库那样简单地运行“phpunit”。 CakePHP2 扩展了 phpunit 测试类,所以我怀疑你是否可以在不使用 cakephp 测试外壳的情况下运行它们。

我们使用 Jenkins 和我们自己定制的脚本来自动化测试。

【讨论】:

  • 感谢您的回答。我很失望,因为 FuelPHP 也使用自己的测试用例,但它有一个 bootstrap_phpunit.php 来处理这个问题。我敢肯定 CakePHP 也有类似的东西,但如果我找不到,我会接受你的回答。
【解决方案2】:

我找到了解决办法。

Japanese site 上我发现了一个有趣的想法:

首先我必须将webroot/test.php 复制为webroot/phpunit.php,并替换最后一行:

--- a/webroot/test.php
+++ b/webroot/phpunit-bootstrap.php
@@ -86,4 +86,4 @@ if (Configure::read('debug') < 1) {

 require_once CAKE . 'TestSuite' . DS . 'CakeTestSuiteDispatcher.php';

-CakeTestSuiteDispatcher::run();
+App::load('CakeTestSuiteCommand');

这使得它可以用作 PHPUnit 的引导程序。

然后您可以创建一个phpunit.xml 来简化测试过程。

<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" stopOnFailure="false" bootstrap="webroot/phpunit-bootstrap.php">
    <testsuites>
        <testsuite name="AllTests">
            <directory suffix=".php">Test/Case</directory>
        </testsuite>
    </testsuites>
</phpunit>

现在我只需要将grunt-phpunit 配置添加到我的Gruntfile.js

phpunit: {
    options: {
        bin: 'phpunit',
        configuration: 'phpunit.xml',
        colors: true,
    },
    app: {
        options: {
            testsuite: 'AllTests',
        },
    },
}

现在,如果我运行 grunt phpunit,(或配置 grunt watch 以在更改时运行 phpunit 任务并更改文件)PHPUnit 运行我的测试。

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2023-03-24
    • 2015-04-02
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2023-01-09
    相关资源
    最近更新 更多