如何从 xml 配置中的 PHPUnit 测试套件中排除文件? [英] How to exclude file from PHPUnit test suite in xml config?

查看:32
本文介绍了如何从 xml 配置中的 PHPUnit 测试套件中排除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have following, very simple, XML config for PHPUnit:

<phpunit bootstrap="/_tests/TestAutoload.php">
    <testsuites>
        <testsuite name="Unit Tests">
            <directory suffix=".php">_tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

How to exclude certain file in this directory from test suite? I tried <exclude> and <blacklist>, but it doesn't seem to work in this context. Also couldn't find any other documentation than phpunit.de one, which doesn't mention anything about it. Else than that, this config works perfectly.

解决方案

There are a number of ways to not run a particular test - putting it into a blacklist so it's never run may not be the way - as changing it means editing the blacklist, and you'll often endup bouncing it in and out of version control.

There are several other ways that may be more appropriate:

If a test is not yet ready to run:

$this->markTestIncomplete('This test has not been implemented yet.');

If there's an outside reason it should not be run, skip it:

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped('The MySQLi extension is not available.');
}

You can also put that into the setUp() function, so it will skip all the tests in a test-class.

You can make a test dependant on a previous one succeeding:

public function testEmpty()
{
    $stack = array();
    $this->assertTrue(empty($stack));
    return $stack;   // also sends this variable to any following tests - if this worked
}
/**
 * only runs if testEmpty() passed
 *
 * @depends testEmpty
 */
public function testPush(array $stack)
{
}

The @group -name- annotation is one of the best ways to specifically stop, or run one group of tests

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}

testSomething() is now in two groups, and if either is added on the command line (or in the config.xml) --exclude-group parameter. it won't be run. Likewise, you could run only tests that belong to a particular group - say named after a feature, or bug report.

这篇关于如何从 xml 配置中的 PHPUnit 测试套件中排除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆