簡體   English   中英

如何跳過PHPUnit中的錯誤測試?

[英]How to skip tests on error in PHPUnit?

當相關的數據集出現錯誤時,如何使PHPUnit跳過測試?


作品

如果我的數據提供者只有導致錯誤的內容,那么它將適當地跳過相關測試。 注意 Skipped: 1

class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
    public function getDataProvider(){
      return [
        ['non-existent_file.txt'],
      ];
    }

    /**
     *  @dataProvider getDataProvider
     */
    public function testCanBeDependedOn($data){
      $actual = file_get_contents($data);
      $this->assertSame('expected',$actual);
    }

    /**
     *  @dataProvider getDataProvider
     *  @depends testCanBeDependedOn
     */
    public function testCanDepend($data){
      $this->assertTrue(false);
    }
}
 PHPUnit 5.5.0 by Sebastian Bergmann and contributors. ES 2 / 2 (100%) Time: 28 ms, Memory: 4.00MB There was 1 error: 1) DataProviderDependsTest::testCanBeDependedOn with data set #0 ('non-existent_file.txt') file_get_contents(non-existent_file.txt): failed to open stream: No such file or directory /home/xenial/phpunittest/test.php:16 ERRORS! Tests: 1, Assertions: 0, Errors: 1, Skipped: 1. 

不起作用

但是,如果我將一個良好的數據添加到提供程序中,那么盡管由其余部分引起了錯誤,PHPUnit仍將繼續執行所有從屬測試(甚至是有錯誤的相應數據集)。 它不會跳過任何內容。 注意添加到數據提供者的['real_file.txt'],

class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
    public function getDataProvider(){
      return [
        ['real_file.txt'],
        ['non-existent_file.txt'],
      ];
    }

    /**
     *  @dataProvider getDataProvider
     */
    public function testCanBeDependedOn($data){
      $actual = file_get_contents($data);
      $this->assertSame('expected',$actual);
    }

    /**
     *  @dataProvider getDataProvider
     *  @depends testCanBeDependedOn
     */
    public function testCanDepend($data){
      $this->assertTrue(false);
    }
}
 PHPUnit 5.5.0 by Sebastian Bergmann and contributors. .EFF 4 / 4 (100%) Time: 19 ms, Memory: 4.00MB There was 1 error: 1) DataProviderDependsTest::testCanBeDependedOn with data set #1 ('non-existent_file.txt') file_get_contents(non-existent_file.txt): failed to open stream: No such file or directory /home/xenial/phpunittest/test.php:16 -- There were 2 failures: 1) DataProviderDependsTest::testCanDepend with data set #0 ('real_file.txt') Failed asserting that false is true. /home/xenial/phpunittest/test.php:25 2) DataProviderDependsTest::testCanDepend with data set #1 ('non-existent_file.txt') Failed asserting that false is true. /home/xenial/phpunittest/test.php:25 ERRORS! Tests: 4, Assertions: 3, Errors: 1, Failures: 2. 

使用@dataProvider時,PHPUnit不會在錯誤時跳過@depends測試

他們的文檔

注意

如果測試依賴於使用數據提供程序的測試,則在至少一個數據集成功依賴測試時,將執行依賴測試

如果從屬測試中提供的數據的任何部分導致錯誤,我想一起跳過所有測試。 有什么辦法可以解決此限制?


如果需要,您可以派生這些文件以進行快速測試,也可以克隆:

git clone https://github.com/admonkey/phpunittest.git

也許這是您期望的行為:

<?php

class DataProviderDependsTest extends PHPUnit_Framework_TestCase
{
    protected static $failed = false;

    public function getDataProvider() {
        return [
            ['real_file.txt'],
            ['non-existent_file.txt'],
        ];
    }

    /**
     * @dataProvider getDataProvider
     */
    public function testCanBeDependedOn($data) {
        try {
            $actual = file_get_contents($data);
            self::assertSame('expected', $actual);
        } catch(Exception $e) {
            self::$failed = true;
            throw $e;
        }
    }

    /**
     * @dataProvider getDataProvider
     * @depends testCanBeDependedOn
     */
    public function testCanDepend($data) {
        if (self::$failed) {
            self::markTestSkipped('testCanBeDependedOn failed');
        }
        self::assertTrue(true);
    }
}   

抱歉,此答案並未真正解決您的問題,因為您需要在數據提供程序中至少有一個通過記錄才能運行基於@depends的測試。 @iRas的答案似乎可以滿足您的要求。

我沒有刪除此答案,因為它可能仍會為其他人提供一些信息。

@depends不執行您期望的功能。 這並不意味着如果另一個失敗則不運行測試。

從@depends手冊中

PHPUnit支持聲明測試方法之間的顯式依賴關系。 這樣的依賴關系沒有定義測試方法的執行順序,但是它們允許生產者返回測試夾具的實例並將其傳遞給依賴的使用者。 示例2.2顯示了如何使用@depends批注來表達測試方法之間的依賴關系。

有關更多詳細信息,請參見“測試依賴項”部分。

這更多地用於在測試功能之間傳遞數據,而不是用於確保如果另一組測試失敗則一組測試不會運行。

文檔中的樣本測試

<?php
use PHPUnit\Framework\TestCase;

class StackTest extends TestCase
{
    public function testEmpty()
    {
        $stack = [];
        $this->assertEmpty($stack);

        return $stack;
    }

    /**
     * @depends testEmpty
     */
    public function testPush(array $stack)
    {
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertNotEmpty($stack);

        return $stack;
    }

    /**
     * @depends testPush
     */
    public function testPop(array $stack)
    {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEmpty($stack);
    }
}
?>

在測試中,返回來自testEmpty()的$ stack,並將其傳遞到testPush中。 這意味着testPush中的$ stack已定義,並且是一個空數組,與undefined相反。

理想情況下,您的測試不應依賴於一次或多次傳遞,並且應該原子性地指出功能是否起作用,以幫助發現代碼中的問題。 如果一個測試以非預期的方式更改數據,則基於測試通過的更多依賴關系會導致很多錯誤,然后所有后續檢查該數據的測試都會失敗,這實際上並不是您應該如何構造這些測試。 測試良好的條件,測試失敗,但測試通過與失敗之間沒有硬性依賴關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM