【问题标题】:PHPUnit: dataProvider issuePHPUnit:数据提供者问题
【发布时间】:2011-10-21 01:41:10
【问题描述】:

以下测试有什么问题:

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

?>

错误信息:

$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

谢谢。

【问题讨论】:

  • 您确定没有缺少任何括号/花括号?
  • 我看不到错误。谢谢。

标签: php unit-testing testing phpunit


【解决方案1】:

我也有一点点相同的东西,我使用__construct() 方法来设置内部变量。

我需要做的是有一个function setUp() {} 会发生这种情况。


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

/*
 * @dataProvider ....
 */

但评论必须以/** 开头才能被识别。

【讨论】:

  • 我的评论开头是:/**+ 重大错字...浪费了 30 分钟找到问题...
  • 哇,谢谢,我一直在使用 __construct(),直到我添加了 dataProvider,然后一切都崩溃了,故障排除简直是一场噩梦。如果不是你,我可能永远不会想到这一点,谢谢!!
  • /* vs /** 是一个特别可怕的陷阱。
  • Tis 现在可以通过 Rector 立即升级:github.com/rectorphp/rector/issues/2143
【解决方案2】:

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

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

    // ...

}

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

class SomethingTest extends PHPUnit_Framework_TestCase
{
    // ...
}

【讨论】:

  • +1 不错的收获!我寻找了一个构造函数,因为我知道每个数据集都是这样传入的,但完全忘记了 PHP4 类名作为构造函数的事情。
  • 加油!这种构造函数很烂! :( 谢谢。问题已解决!
  • 许多其他语言使用类的名称作为构造方法的名称,例如C++ 和 Java。
【解决方案3】:

对于由于 phpunit 将数据提供程序功能作为测试运行并显示带有This test did not perform any assertions 的“风险测试”标志而到达这里的未来来者,似乎由于 phpunit 6(可能是 6.3?)phpunit 不再忽略“测试" 数据提供者函数中的前缀,例如testAdditionProvider。将其重命名为additionProvider,就像current docs 一样。 不过我不是 100% 确定。

【讨论】:

    【解决方案4】:

    如果您使用的是 PHP 5.3.3 之前的版本:

    您是否提供了建立班级的变量? 因为你的类和函数名(TestSomething/testSomething)是一样的(不区分大小写)。 所以它将函数 testSomething 视为构造函数。

    【讨论】:

    • 其实这种行为在 5.3.3 及更高版本中仍然存在,包括 5.4。
    • 根据示例 #2 中的 PHP 网站 (php.net/manual/en/language.oop5.decon.php),它应该被视为常规函数。但是,我自己没有测试过。
    • 我已经阅读了它,但我使用的是 PHP 5.3... 所以,这个陈述看起来是错误的。谢谢。
    • @thom:其实是__construct
    • @Max 'TheM' Klaversma:你说对了一部分。 “从 PHP 5.3.3 开始,与 namespaced class name 的最后一个元素同名的方法将不再被视为构造函数。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2015-09-07
    • 2014-11-19
    • 1970-01-01
    • 2013-02-14
    • 2017-03-16
    相关资源
    最近更新 更多