Phpunit: データプロバイダーの引数カウントエラー

作成日 2016年12月19日  ·  4コメント  ·  ソース: sebastianbergmann/phpunit

最新バージョンのPHPUnit(5.7.4)を使用しています。 問題は、テストを実行すると合格することです。 しかし、もう一度実行すると、次のエラーで失敗します。

ArgumentCountError: Too few arguments to function ValidationTest::testValidateType(), 0 passed and at least 3 expected

データプロバイダー関数に変更を加えて(つまり、返されるデータ、プロバイダー関数の名前やタイプなどを変更して)再実行すると、1回合格し、連続するすべてのテスト実行で上記のエラーで失敗します。

確かではありませんが、PHPUnitはプロバイダーデータをキャッシュするためにキャッシュメカニズムを使用していますか? はいの場合、それをきれいにする方法はありますか(多分setUpまたはtearDown )?


次のコードは1回機能します(1回通過)。

/**
 * <strong i="15">@covers</strong> Validation
 * <strong i="16">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="17">@covers</strong> ::validateType
     * <strong i="18">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type) {
        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

次のコードは常に機能します(毎回渡されます)。

/**
 * <strong i="24">@covers</strong> Validation
 * <strong i="25">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="26">@covers</strong> ::validateType
     */
    public function testValidateType() {
        foreach ($this->validateTypeProdiver() as $args) {
            $result = call_user_func_array([$this->validation, 'validateType'], array_slice($args, 1));

            switch ($args[0]) {
                case 'True':
                    $this->assertTrue($result);
                    break;
            }
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

最も参考になるコメント

誰かがいつかこの問題に遭遇した場合。
私はこのテストを受けました

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

__constructをsetUpに変更しましたが、機能します。

全てのコメント4件

ご提供いただいた情報で問題を再現することはできません。 これが私がしたことです:

$ composer require phpunit/phpunit:5.7.4
$ vendor/bin/phpunit && vendor/bin/phpunit

$ vendor/bin/phpunit && vendor/bin/phpunit 
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 44 ms, Memory: 4.00MB

OK (1 test, 1 assertion)
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 32 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

これは私が使用したテストクラスです:

use PHPUnit\Framework\TestCase;

class TestMeTest extends TestCase
{

    protected $validation;

    protected function setUp()
    {

        $this->validation = new Validation();
    }

    /**
     * <strong i="9">@covers</strong> ::validateType
     * <strong i="10">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type)
    {

        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }

    public function validateTypeProdiver()
    {

        return [
            ['True', 'file.txt', 'str']
        ];
    }
}

そしてValidationクラス:

class Validation
{
    public function validateType() {
        return true;
    }
}

PHPUnitはデータプロバイダーを永続的にキャッシュしません。

私もこれを再現することはできません。

誰かがいつかこの問題に遭遇した場合。
私はこのテストを受けました

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

__constructをsetUpに変更しましたが、機能します。

@dmiroginのソリューションは私の一日を作りました、phpunit 8.5

このページは役に立ちましたか?
0 / 5 - 0 評価