PHPUnit错误:使用dataProvider缺少参数1

时间:2016-07-11 02:41:43

标签: php unit-testing phpunit

我有以下测试脚本:

class testTest extends PHPUnit_Framework_TestCase
{

    public function provider() {
        return [
            [1,false],
            [2,true]
        ];
    }

   /**
    * @test
    * @provider provider
    */
    public function test_test($num, $expected) {
        $actual = $num%2 ? false : true;
        $this->assertEquals($actual, $expected);
    }
}

每当我运行时,我都会收到错误:

1)  testTest::test_test
Missing argument 1 for testTest::test_test()

我的测试套装中有其他测试没有使用dataProviders并且它们工作正常。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

@provider更改为@dataProvider,例如

/**
* @dataProvider provider
*/
public function test_test($num, $expected) {
    $actual = $num%2 ? false : true;
    $this->assertEquals($actual, $expected);
}

阅读文档: https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.dataProvider

PS:你在assertEquals中得到了错误的参数。它应该是:

$this->assertEquals($expected, $actual);

再次:https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals

相关问题