Phpunit: PHPUnit_Framework_TestCase 子类中的构造函数导致@dataProvider 中断

创建于 2012-08-09  ·  4评论  ·  资料来源: sebastianbergmann/phpunit

在测试中使用构造函数会导致 dataProvider 注释因缺少参数的错误而中断:

PHPUnit 3.6.12 by Sebastian Bergmann.

...E

Time: 0 seconds, Memory: 3.25Mb

There was 1 error:

1) Test::testTheProvider
Missing argument 1 for Test::testTheProvider()

/home/nick/development/PHP-1.php:43

FAILURES!
Tests: 4, Assertions: 5, Errors: 1.

删除构造函数会导致 testTheProvider 再次工作,并且似乎不会破坏@depends注释。

<?php
class Test extends PHPUnit_Framework_TestCase {

    public function __construct() {
        parent::__construct();
    }

    public function testEmpty() {
        $stack = array();
        $this->assertEmpty($stack);

        return $stack;
    }

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

        return $stack;
    }

    /**
     * <strong i="11">@depends</strong> testPush
     */
    public function testPop(array $stack) {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEmpty($stack);
    }

    public function theProvider() {
        return array(
            array(true, false)
        );
    }

    /**
     * <strong i="12">@dataProvider</strong> theProvider
     */
    public function testTheProvider($var1, $var2) {
        //$var1 = true;
        //$var2 = false;

        $this->assertNotEquals($var1, $var2);
    }

}
?>

最有用的评论

我已经解决了这样的问题:

类测试扩展 PHPUnit_Framework_TestCase {
公共函数 __construct($name = NULL, 数组 $data = array(), $dataName = '') {
父::__construct($name, $data, $dataName);
}
}

所有4条评论

预期的行为。 如果您覆盖PHPUnit_Framework_TestCase::__construct() (您不应该这样做),您的实现必须接受原始实现的所有参数并将它们传递给parent::__construct()

谢谢你的澄清。 但是,PHPUnit_Framework_TestCase 中的构造函数为其参数提供了默认值,并且无论如何它们的定义都是相同的。 除非您特别想更改某些内容,否则调用它应该没有什么区别。

此外,文档(http://www.phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestCase)中没有提到这一点,DBUnit BankAccount 示例甚至这样做(https:/ /github.com/sebastianbergmann/dbunit/blob/master/Samples/BankAccountDB/BankAccountDBTest.php#L60)。

如果某些注释有效,而有些注释不只是因为这个,我不会说这是预期的行为。

我已经解决了这样的问题:

类测试扩展 PHPUnit_Framework_TestCase {
公共函数 __construct($name = NULL, 数组 $data = array(), $dataName = '') {
父::__construct($name, $data, $dataName);
}
}

你好,
我很幸运找到了这个答案,节省了很多时间。 泰, @foued611

我的情况是这样的,它失败了。

# my base test class
use PHPUnit_Framework_TestCase;

class mytest extends PHPUnit_Framework_TestCase {
   public function __construct() {
     # do some common stuff to my tests...
  }
}


class IntegrationTest extends mytest {
  public function __construct() {
     parent::__construct();
  }
   /**
    * <strong i="9">@dataProvider</strong> additionProvider
    */
    public function testAdd($a, $b, $expected)
    {
        $this->assertEquals($expected, $a + $b);
    }

    public function additionProvider()
    {
        return [
            'adding zeros'  => [0, 0, 0],
            'zero plus one' => [0, 1, 1],
            'one plus zero' => [1, 0, 1],
            'one plus one'  => [1, 1, 3]
        ];
    }

}

固定和工作版本看起来像这样

# my base test class
use PHPUnit_Framework_TestCase;

class mytest extends PHPUnit_Framework_TestCase {
   public function __construct() {
     # do some common stuff to my tests...
    parent::__construct(...func_get_args());
  }
}


class IntegrationTest extends mytest {
  public function __construct($name = NULL, array $data = array(), $dataName = '') {
    parent::__construct(...func_get_args());
  }
   /**
    * <strong i="13">@dataProvider</strong> additionProvider
    */
    public function testAdd($a, $b, $expected)
    {
        $this->assertEquals($expected, $a + $b);
    }

    public function additionProvider()
    {
        return [
            'adding zeros'  => [0, 0, 0],
            'zero plus one' => [0, 1, 1],
            'one plus zero' => [1, 0, 1],
            'one plus one'  => [1, 1, 3]
        ];
    }

}

当前的 PHPUnit 文档不会以任何方式发出警告,即不会为使用 dataProvider 的测试类添加自定义 __constructor()。 该信息可以添加到当前的 Data Providers 文档中,至少它会提示您为什么即使您复制粘贴了示例 dataProvider 代码,您的实现也无法正常工作。
如果您需要使用自定义 __constructor,请添加一些指南以使其工作。

Ping 因为关闭了@sebastianbergmann

此页面是否有帮助?
0 / 5 - 0 等级