如何防止PHPUnit在Tests目录中运行PHP文件?

时间:2015-07-14 06:49:59

标签: phpunit mockery

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

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>

我该怎么做?

1 个答案:

答案 0 :(得分: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>