テストにコンストラクターがあると、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()
をオーバーライドする場合(これはすべきではありません)、実装は元の実装のすべての引数を受け入れ、それらを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)。
一部の注釈が機能し、一部がこれだけのために機能しない場合、それが期待された動作であるとは言えません。
私はこのような問題を解決しました:
クラスTestはPHPUnit_Framework_TestCaseを拡張します{
public function __construct($ name = NULL、array $ data = array()、$ dataName = ''){
親:: __ construct($ name、$ data、$ dataName);
}
}
こんにちは、
私はこの答えを見つけて幸運でした、多くの時間を節約しました。 Ty、 @ 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()を追加しない方法で警告していません。 その情報は、現在のデータプロバイダーのドキュメントに追加できます。少なくとも、サンプルのdataProviderコードをコピーして貼り付けても、実装が機能しない理由がわかります。
また、カスタム__constructorを使用する必要がある場合は、それが機能するように作成する方法についてのガイドを追加してください。
@sebastianbergmannを閉じたのでping
最も参考になるコメント
私はこのような問題を解決しました:
クラスTestはPHPUnit_Framework_TestCaseを拡張します{
public function __construct($ name = NULL、array $ data = array()、$ dataName = ''){
親:: __ construct($ name、$ data、$ dataName);
}
}