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

如果我对数据提供程序函数进行任何更改(即更改要返回的数据、提供程序函数名称或类型等)并重新运行,它会通过一次并在所有连续测试运行中失败并出现上述错误。

不确定,但 PHPUnit 是否使用任何缓存机制来缓存提供者数据? 如果是,那么有什么方法可以清理它(可能使用setUptearDown )?


以下代码工作一次(通过一次):

/**
 * <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 等级