2017-06-21 140 views
0

對於集成測試,我想重新使用測試結果。依賴性通過註釋來定義。對於要執行的依賴性測試,以前測試的結果需要可用。因此,測試需要按照固定順序執行。否則,跳過依賴於其他測試的測試。爲了確保測試按照固定順序執行,測試套件已經定義。仍然會跳過依賴關係的測試。這是爲什麼?phpunit跳過不同測試用例之間的依賴性測試

ATest.php:

<?php 

use PHPUnit\Framework\TestCase; 

class ATest extends TestCase 
{ 
    public function testA() 
    { 
     self::assertTrue(true); 
     return $this; 
    } 
} 

BTest.php:

<?php 

use PHPUnit\Framework\TestCase; 

class BTest extends TestCase 
{ 

    /** 
    * @depends ATest::testA() 
    */ 
    public function testB($a) 
    { 
     self::assertTrue(true); 
    } 
} 

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit 
     verbose="true" 
> 
    <testsuites> 
     <testsuite name="dependency"> 
      <file>ATest.php</file> 
      <file>BTest.php</file> 
     </testsuite> 
    </testsuites> 
</phpunit> 

phpunit --testsuite dependency

PHPUnit 5.5.7作者Sebastian Bergmann和 貢獻者。

運行時:與Xdebug的2.5.4 PHP 7.1.5配置: /phpunit.xml

.S 2 /2(100%)

時間:49毫秒,內存:4.00MB

有1跳過測試:

1)BTEST :: TESTB該測試依賴於 「ATEST ::種皮()」 通過。

好的,但不完整,跳過或有風險的測試!測試:1,斷言:1, 跳過:1.

回答

2

你不能有一個測試取決於不同TestCase中的測試。測試需要包含在同一個測試用例中。由於該測試不在測試用例中,因此將其視爲測試失敗,並在運行測試時跳過測試。

您的測試需要合併到一個測試中,以使依賴性起作用。對於這種情況的原因

https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies

部分原因是,你的每個測試應隔離,並能夠以任意順序運行。測試依賴於單獨測試用例中的測試意味着測試文件需要按特定順序運行。由於具有循環測試依賴關係,這可能變得非常簡單。

此外,您現在可以影響未包含你的測試用例中測試的東西。這可能會導致維護測試的噩夢。