PHP为所有测试套件提供不同的bootstrap

Nap*_*pas 18 php phpunit

<phpunit backupGlobals="false" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
    <testsuite name="app1" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

我如何使第一和第二testuite加载不同的bootstraps?

ant*_*nko 23

我做的是拥有一个倾听者.

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>
Run Code Online (Sandbox Code Playgroud)

然后是TestListener.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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,你知道如何在 PHPUnit 8 中使用扩展来实现它吗?`PHPUnit\Runner\BeforeFirstTestHook` (2认同)

Ain*_*ine 15

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

app1.xml

<phpunit bootstrap="app1BootstrapFile.php" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

app2.xml

<phpunit bootstrap="app2BootstrapFile.php" backupGlobals="false" colors="true">
    <testsuite name="app2" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

跑步:

$phpunit --configuration app1.xml app1/
$phpunit --configuration app2.xml app2/
Run Code Online (Sandbox Code Playgroud)

如果你比另一个(比如app1)更多地运行一个测试,请命名xml phpunit.xml并且你可以运行

$phpunit app1/
$phpunit --configuration app2.xml app2/
Run Code Online (Sandbox Code Playgroud)

我用单元/集成测试来做这个.


edo*_*ian 4

你不能。

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

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

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