【问题标题】:PHPunit different bootstrap for all testsuitesPHPunit所有测试套件的不同引导程序
【发布时间】:2012-03-02 14:39:49
【问题描述】:
<phpunit backupGlobals="false" colors="true">
    <testsuite name="app1" >
        <directory>./app1</directory>
    </testsuite>
    <testsuite name="app1" >
        <directory>./app2</directory>
    </testsuite>
</phpunit>

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

【问题讨论】:

    标签: php phpunit


    【解决方案1】:

    我所做的就是拥有一个 Listener。

    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>
    

    然后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
            }
        }
    }
    

    【讨论】:

    • 我喜欢这个,关于如何在 PHPUnit 8 中使用扩展的任何想法? PHPUnit\Runner\BeforeFirstTestHook
    【解决方案2】:

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

    app1.xml

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

    app2.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/
    

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

    【讨论】:

    • 我认为这不是一个好主意,因为根据您的解决方案,您需要 10 个不同的引导文件来创建 10 个 phpunitblabla.xml 文件
    【解决方案3】:

    你不能。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2013-01-23
      • 2012-10-19
      相关资源
      最近更新 更多