PHPunit为所有测试套件提供不同的引导程序


PHPunit different bootstrap for all testsuites

<phpunit backupGlobals="false" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
    <testsuite name="app1" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>

如何使第一个和第二个测试套件加载不同的引导程序?

我所做的就是有一个监听器。

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./phpunit_bootstrap.php"
     backupGlobals="false"
     backupStaticAttributes="false"
     verbose="true"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="true">
    <testsuites>
        <testsuite name="unit">
            <directory>./unit/</directory>
        </testsuite>
        <testsuite name="integration">
            <directory>./integration/</directory>
        </testsuite>
    </testsuites>
    <listeners>
        <listener class="tests'base'TestListener" file="./base/TestListener.php"></listener>
    </listeners>
</phpunit>

然后测试侦听器.php

class TestListener extends 'PHPUnit_Framework_BaseTestListener
{
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        if (strpos($suite->getName(),"integration") !== false ) {
            // Bootstrap integration tests
        } else {
            // Bootstrap unit tests
        }
    }
}

您可以创建两个不同的引导程序文件和两个不同的配置 xml 文件

应用1.xml

<phpunit bootstrap="app1BootstrapFile.php" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
</phpunit>

应用2.xml

<phpunit bootstrap="app2BootstrapFile.php" backupGlobals="false" colors="true">
    <testsuite name="app2" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>

要运行:

$phpunit --configuration app1.xml app1/
$phpunit --configuration app2.xml app2/

如果你运行一个测试比另一个多(比如 app1(,命名 xml phpunit.xml你可以运行

$phpunit app1/
$phpunit --configuration app2.xml app2/

我通过单元/集成测试来做到这一点。

你不能。

PHPUnit

只允许您指定一个引导程序文件,并且您需要设置所有内容,以便每个测试套件的每个测试用例都可能被执行,并且 PHPUnit 无法从引导 xml 文件为每个测试套件运行"设置"代码。

当使用 phpunit 3.6 不鼓励使用时TestSuite,您可以在这些类中执行此操作,但我的建议是在引导程序中运行所有通用引导代码.php并且如果您需要对 app1 和 app2 中的测试进行特殊设置,以便您继承App1_TestCase

如果App1真的是一个完整的应用程序,我建议有两个独立的项目,它们有自己的测试和设置代码,而不是试图在一个phpunit运行中运行它们。