2016-06-14 108 views
8

我的PHPUnit配置文件有兩個測試套件,分別爲unitsystem。當我運行測試跑步者vendor/bin/phpunit時,它將在兩個套件中運行所有測試。我可以使用testsuite標誌:vendor/bin/phpunit --testsuite unit來標記一個套件,但我需要配置測試運行器默認情況下僅運行unit套件,並且只有在使用testsuite標誌專門調用時才運行integration默認情況下在PHPUnit中運行單個測試套件

我的配置:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit colors="true"> 
    <testsuites> 
    <testsuite name="unit"> 
     <directory>tests/Unit</directory> 
    </testsuite> 
    <testsuite name="integration"> 
     <directory>tests/Integration</directory> 
    </testsuite> 
    </testsuites> 
    <filter> 
    <whitelist> 
     <directory suffix=".php">src</directory> 
    </whitelist> 
    </filter> 
    <logging> 
    <log type="coverage-clover" target="build/clover.xml"/> 
    </logging> 
</phpunit> 
+0

建立'phpunit_unit.sh'和'phpunit_integration.sh'文件是不是更好,裏面的配置? –

回答

1

似乎沒有成爲一個方式列出從phpunit.xml文件的多個測試包,但隨後只運行一個。但是,如果您確實可以控制更完整的集成和測試環境,並且可以更精確地配置事物,則可以有多個phpunit配置文件,並設置一個(或多個)涉及更多的環境來設置命令行參數--configuration <file>選項與將做更多的配置。這至少可以確保最簡單的配置以最簡單的方式運行。

如果您專門運行它們,可以調用這兩個文件,但可能需要考慮將快速運行的文件稱爲默認phpunit.xml,以及專門命名和擴展的文件名爲phpunit.xml.dist如果原始純文本.xml不存在,則.dist文件將默認自動運行。另一個選擇是將phpunit.xml.dist文件放在代碼庫中,然後將其複製到phpunit.xml文件中,使用更少的'測試套件,它本身不會檢入版本控制,只保存在本地。 (它可能也被標記爲在.gitignore文件或類似文件中被忽略)。

+1

PHPUnit(自6.1.0開始)現在支持定義默認測試套件,因此不再需要此解決方法。 – GaryJ

+0

@GaryJ:一個鏈接(至少),這是從真正的超文本友好。但是,非常感謝評論和版本號:)/E:Ooops,只是看到你在[下面的答案]中有它(https://stackoverflow.com/a/45446071/367456) – hakre

4

由於PHPUnit 6.1.0,現在支持defaultTestSuite屬性。

https://github.com/sebastianbergmann/phpunit/pull/2533

這可以用來之中,像這樣的其他phpunit屬性:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd" 
     backupGlobals="false" 
     backupStaticAttributes="false" 
     bootstrap="tests/bootstrap.php" 
     colors="true" 
     convertErrorsToExceptions="true" 
     convertNoticesToExceptions="true" 
     convertWarningsToExceptions="true" 
     defaultTestSuite="unit" 
     processIsolation="false" 
     stopOnFailure="false"> 
    <testsuites> 
     <testsuite name="unit"> 
      <directory suffix="Test.php">tests/Unit</directory> 
     </testsuite> 
     <testsuite name="integration"> 
      <directory suffix="Test.php">tests/Integration</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 

您現在可以運行phpunit而不是phpunit --testusite unit

測試套件的名稱可能區分大小寫,請注意。

+0

不錯的增強知之甚少關於。感謝發佈! – hakre