简体   繁体   English

PHPUnit:dataProvider问题

[英]PHPUnit: dataProvider issue

What's wrong with the following test: 以下测试有什么问题:

<?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()),
        );
    }
}

?>

Error message: 错误信息:

$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

Thanks. 谢谢。

It's because your test is also being executed as the constructor: 这是因为你的测试也是作为构造函数执行的:

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

    // ...

}

For PHP4 compatibility, you can use the class name as the method name to declare a constructor. 对于PHP4兼容性,您可以使用类名作为方法名来声明构造函数。 It is also done in a case insensitive manner (ie: testSomething() is considered a constructor to TestSomething ). 它也以不区分大小写的方式完成(即: testSomething()被认为是TestSomething的构造TestSomething )。 Usually, you will append the Test keyword to your class name to prevent that from happening (instead of prepending): 通常,您会将Test关键字附加到类名以防止这种情况发生(而不是预先添加):

class SomethingTest extends PHPUnit_Framework_TestCase
{
    // ...
}

I've also just gotten bit by much the same thing, I was using the __construct() method to setup internal variables. 我也刚刚得到了同样的东西,我使用__construct()方法来设置内部变量。

What I needed to do instead was have a function setUp() {} where that would happen. 我需要做的是有一个function setUp() {} ,这会发生。


I've just fallen over this problem again - but this time the problem was the comment - I had used: 我刚刚再次遇到这个问题 - 但这次问题是评论 - 我用过:

/*
 * @dataProvider ....
 */

But the comment has to start with /** to be recognised. 但评论必须从/**开始被认可。

For future-comers arriving here because phpunit ran the data provider function as a test and showed a "risky test" flag with This test did not perform any assertions , it seems that since phpunit 6 (maybe 6.3?) phpunit no longer ignores the "test" prefix in the data provider functions, eg testAdditionProvider . 由于phpunit将数据提供程序函数作为测试运行并且显示“风险测试”标志,因为This test did not perform any assertions ,似乎因为phpunit 6(可能是6.3?)phpunit不再忽略“测试数据提供程序函数中的前缀,例如testAdditionProvider Renaming it to additionProvider as in the current docs works. 将其重命名为additionProvider就像在当前文档中一样 I'm not 100% sure though. 我不是百分百肯定的。

If you're using PHP pre-5.3.3: 如果您使用的是5.3.3之前的PHP:

Are you giving the variables on establishing the class? 你是否给出了建立班级的变量? Because your class and function name (TestSomething/testSomething) are the same (case-insensitive). 因为您的类和函数名称(TestSomething / testSomething)是相同的(不区分大小写)。 So it sees the function testSomething as as constructor. 所以它将函数testSomething视为构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM