10

Situation

We're using PHPUnit in our project and are using a phpunit.xml to ensure things like backupGlobals is turned off.

To further ensure the include path is set and autoloading is active, we also cascade our test bootstraps. That is to say, every test and alltests-suite has a require_once(__DIR__ . '/../bootstrap.php'); at the top, all the way up to the base folder level, where it obviously reads require_once(__DIR__ . '/bootstrap.php');, and the actual bootstrap file resides.

Essentially, our tests are autonomous. You can call any AllTests.php in any folder and any *Test.php by itself and they'll run with the right configuration.

Except no. 'Wait a moment.'

That's only true if we either force our developers to use phpunit --configuration=path/to/phpunit.xml or they're in the folder with the phpunit.xml (so that PHPUnit pulls it out of the current working directory when it is executed).

Occasionally, this makes it incredibly hard to determine why tests on one developer's machine are breaking and why they're running on another. It just takes forgetting that the bootstrap is not the only thing we need to have the same test environment. Keep in mind that since you couldn't forget the bootstrap if you tried, because it's in the tests themselves, forgetting other settings, especially usually-optional ones like that (if you're in the folder with the phpunit.xml, it's automatically pulled), is easy.

In fact - it's happened a few times.

Question

Is there a way I can supply which phpunit.xml to use in the test file being run, such as in our conveniently ubiquitous bootstrap file, rather than supplying it to PHPUnit beforehand, be that by command-line switch or by being in its directory ?

A cursory glance at the code suggests the answer is no - configuration well and truly seems to be loaded before test files are even pulled:

[PHPUnit/TextUI/Command.php]
...
if (isset($this->arguments['configuration'])) {
    $configuration = PHPUnit_Util_Configuration::getInstance(
        $this->arguments['configuration']
    );
    $phpunit = $configuration->getPHPUnitConfiguration();
    ...

That does make some sense, given that the configuration can contain test white- or blacklists.

Really, it wouldn't make sense to load test filters in the test bootstrap itself, so that's half the potential configuration out the window with, but the actual behavioural flags of PHPUnit...

[sample of part of our phpunit.xml]
<phpunit
    backupGlobals="false" 
    backupStaticAttributes="false" 
    convertErrorsToExceptions="true"  
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    syntaxCheck="false"
    processIsolation="false"
    colors="true">

...perhaps with the exception of 'colors' strikes me as something that the test itself should be able to decide on some level.

Consolation prize for...

Admittedly, right now I'd be happy just knowing if I can teach PHPUnit backupGlobals="false" from the bootstrap file, if someone knows of a way.

(If fruitless, the practical answer I'll pursue will probably be to copy the phpunit.xml into all subfolders. I'd like to avoid that solution since it creates redundant copies, and if we ever choose to change a setting... yeah, ouch!)

4

3 回答 3

10

直接回答:不,你不能那样做。

长篇大论——这类问题最好通过改变开发者的习惯来解决。

我们这样做:

  • 所有开发人员总是从测试根目录运行测试,该目录具有唯一的 phpunit.xml 和所有必要的配置 - 包括引导程序,它设置了一个类自动加载器。
  • 我们没有这样的测试套件,测试使用目录分组,任何地方都没有 AllTests.php,因为它不是必需的。PHPUnit 可以取目录名并在其中运行所有测试。
  • 它仍然可以通过提供路径或整个测试套件(通过提供目录的路径)来运行任何单个测试。它只需要一直从测试根目录完成,否则它将无法工作。

这样做意味着放弃从任何目录启动 PHPUnit 的自由,但老实说 - 我一点也不觉得这是一种损失。

收益要大得多:管理代码量减少,开发人员不会忘记任何事情,因此结果是一致的。

于 2010-10-20T21:34:10.340 回答
4

我的解决方案是添加一个 bash 函数

function phpu ()
{
  phpunit --colors --bootstrap ~/path/to/bootstrap.php "$@";
}

然后将它添加到所有 dev .bashrc 文件中,他们可以切换到使用它。

我们喜欢打电话给他们,vim所以我不得不将其添加到.vimrcset shellcmdflag=-ic

然后添加nmap ;t :! phpu %运行当前所在的测试文件。

于 2012-10-30T14:33:35.750 回答
0

您可以更新启动脚本(Windows bat 文件或 *nix 上的 shell)并在其中设置逻辑以配置到 phpunit.xml 的位置。如果它在当前目录中,则使用它,否则指向主目录。

不过,我也同意 Anti 的观点,即所有测试都应始终运行,因为您要确保更改,即使是在目录分支中,也不会影响其他代码。因此,始终从树的顶部运行。这也要求测试快速执行,但我对 PHPUnit 并没有真正的问题。

在每个目录中维护 PHPUnit.xml 将是一场维护噩梦,除非它从其他路径符号链接以确保只有一个实际文件。

于 2012-10-31T20:57:38.373 回答