【问题标题】:How can I prevent PHPUnit from running a PHP file in the Tests directory?如何防止 PHPUnit 在 Tests 目录中运行 PHP 文件?
【发布时间】:2015-07-14 17:18:49
【问题描述】:

我有很多测试需要创建一个特定的模拟对象。所以,这就是我所做的:

namespace AppBundle\Tests\Services\Terminal;

use Mockery\Adapter\Phpunit\MockeryTestCase;
use AppBundle\Entity\Tunnel;
class TunnelBasedTestCase extends MockeryTestCase
{

    /**
     *
     * @var Tunnel
     */
    protected $tunnel;

    protected function setup()
    {
        $tunnel = new Tunnel();
        $tunnel->setName('B9')
            ->setIp('192.168.2.52')
            ->setUsername('username')
            ->setPassword('password')
            ->setSign('sign')
            ->setStatus('open');

        $this->tunnel = $tunnel;
    }
}

但是,问题是现在 PHPUnit 尝试运行这个测试,这只是为了防止我复制粘贴 setup() 函数。我不想跳过它,因为我想得到一个完全绿色的测试结果,而不是得到类似的东西:

....S.....

或者这个:

....I.....

这是我的 PHPUnit 配置,但也无法做到这一点:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
        <directory>../src/*Bundle/Tests</directory>
        <exclude>../src/AppBundle/Tests/Services/Terminal/TunnelBasedTestCase.php</exclude>
    </testsuite>
</testsuites>

我该怎么做?

【问题讨论】:

  • 这个类不是testcase类,所以你可以用一个不匹配正则表达式的名字来重命名它来检查它是否是一个testclass。因此,您可以将其重命名为 BaseTunnel 。希望对您有所帮助
  • 我想到了这一点,但问题是我将不得不为此失去可读性。我首先要确定实际上没有办法,而不是我不知道。

标签: phpunit mockery


【解决方案1】:

你可以像这样编辑你的 phpunit.xml:

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

<phpunit>

    <testsuites>
        <testsuite name="foo">
            <directory>./tests/</directory>
            <exclude>./tests/path/to/excluded/test.php</exclude>
                ^-------------
        </testsuite>
    </testsuites>

</phpunit>

您也可以尝试这样创建黑名单:

<filter>
    <blacklist>
          <file>../src/AppBundle/Tests/Services/Terminal/TunnelBasedTestCase.php</file>
    </blacklist>
</filter>

【讨论】:

  • 非常感谢!不过,我进行了您所需的更改,但没有任何改变。我已经更新了我的答案以显示代码,以防我弄错了。
  • 我已经编辑了我的答案,请立即尝试。这两种方法对我来说都很好
  • 这个也失败了。我已经通过将路径复制粘贴到终端来验证该文件是否存在。我正在使用 PHPUnit 4.7.7。
猜你喜欢
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2011-02-06
  • 2019-09-01
  • 1970-01-01
  • 2017-12-28
  • 2012-08-20
  • 1970-01-01
相关资源
最近更新 更多