PHPUnit:dataProvider问题

tho*_*hom 4 php testing phpunit unit-testing

以下测试有什么问题:

<?php

class TestSomething extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider
     */
    public function testSomething($array, $expectedResult)
    {
        $this->assertEquals($array, $expectedResult);
    }

    public function provider()
    {
        return array(
            array(array(), array()),
        );
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

错误信息:

$phpunit index.php
PHP Warning:  Missing argument 1 for TestSomething::testSomething() in /var/www/tests/something-test/index.php on line 8
PHP Warning:  Missing argument 2 for TestSomething::testSomething() in /var/www/tests/something-test/index.php on line 8
PHP Notice:  Undefined variable: array in /var/www/tests/something-test/index.php on line 11
PHP Notice:  Undefined variable: expectedResult in /var/www/tests/something-test/index.php on line 11
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_ExpectationFailedException' with message 'Failed asserting that 
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
 is equal to <string:testSomething>.' in /usr/share/php/PHPUnit/Framework/Constraint/IsEqual.php:164
Stack trace:
#0 /usr/share/php/PHPUnit/Framework/Assert.php(2087): PHPUnit_Framework_Constraint_IsEqual->fail(Array, '')
#1 /usr/share/php/PHPUnit/Framework/Assert.php(343): PHPUnit_Framework_Assert::assertThat(Array, Object(PHPUnit_Framework_Constraint_IsEqual), '')
#2 /var/www/tests/something-test/index.php(11): PHPUnit_Framework_Assert::assertEquals('testSomething', Array)
#3 /usr/share/php/PHPUnit/Framework/TestSuite.php(537): TestSomething->testSomething('testSomething', Array, 0)
#4 /usr/share/php/PHPUnit/Framework/TestSuite.php(816): PHPUnit_Framework_TestSuite::createTest(Object(ReflectionClass), 'testSomething')
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(224): PHPUnit_Framework_TestSuite->addTestMethod(Object(ReflectionClass), Object(Reflectio in /usr/share/php/PHPUnit/Framework/Constraint/IsEqual.php on line 164
Run Code Online (Sandbox Code Playgroud)

谢谢.

Ali*_*man 13

我也刚刚得到了同样的东西,我正在使用该__construct()方法来设置内部变量.

我需要做的是有一个function setUp() {}会发生的地方.


我刚刚再次遇到这个问题 - 但这次问题是评论 - 我曾经使用过:

/*
 * @dataProvider ....
 */
Run Code Online (Sandbox Code Playgroud)

但评论必须首先/**得到承认.


net*_*der 11

这是因为你的测试也是作为构造函数执行的:

class TestSomething extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider
     */
    public function testSomething($array, $expectedResult)
    {
        $this->assertEquals($array, $expectedResult);
    }

    // ...

}
Run Code Online (Sandbox Code Playgroud)

对于PHP4兼容性,您可以使用类名作为方法名来声明构造函数.它也以不区分大小写的方式完成(即:testSomething()被视为构造函数TestSomething).通常,您会将Test关键字附加到类名以防止这种情况发生(而不是预先添加):

class SomethingTest extends PHPUnit_Framework_TestCase
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)


sha*_*adi 5

对于未来到达这里的人,因为 phpunit 将数据提供程序功能作为测试运行并显示了“有风险的测试”标志This test did not perform any assertions,似乎是因为 phpunit 6(可能是 6.3?)phpunit 不再忽略数据提供程序中的“测试”前缀功能,例如testAdditionProvider。将其重命名additionProvider当前文档中的那样有效。不过我不是 100% 确定。