【问题标题】:How to integrate PHPUnit to a custom framework如何将 PHPUnit 集成到自定义框架
【发布时间】:2010-10-25 02:34:31
【问题描述】:

我想将 PHPUnit 集成到我的框架中。 我的意思是,在运行测试之前,我必须在开始时进行一些初始化,比如设置自动加载。

我想使用cli测试运行器,如果我理解正确的话,我必须创建一个类,它有一个静态函数suite(),它返回一个PHPUnit_Framework_TestSuite的实例,并向这个套件添加测试,如http://www.phpunit.de/manual/current/en/textui.html 所述。

到目前为止,我想出了:

class MyTestFW {
    public static function suite() {
        // Do framework initialization here

        $suite = new PHPUnit_Framework_TestSuite();
        $suite->addTest(new SimpleTest());

        // Add more tests

        return $suite;
    }
}

SimpleTest 是一个非常基本的测试类,它扩展了 PHPUnit_Framework_TestCase。 当我运行“phpunit MyTestFW”时,我总是得到:

PHPUnit 3.3.16 by Sebastian Bergmann.

E

Time: 0 seconds

There was 1 error:

1) (SimpleTest)
RuntimeException: PHPUnit_Framework_TestCase::$name must not be NULL.

有人能帮帮我吗?

【问题讨论】:

    标签: php frameworks phpunit


    【解决方案1】:

    PHPUnit_Framework_TestCase::$name 在 TestCase 构造函数中设置,所以你可以试试这个:

    $suite->addTest(new SimpleTest('simpletest'));
    

    edit1:

    我不知道你的代码,所以我不知道这是否有帮助。
    我通常看到的是这个(作为上面的替换,而不是添加):

    $suite->addTestSuite('SimpleTest');
    

    edit2:

    phpunit documentation: Chapter 7 - Organizing Tests

    【讨论】:

    • 不,它看起来正在尝试将其作为方法调用:有 1 次失败:1)SimpleTest(SimpleTest) 方法 SimpleTest 不存在
    • 将它添加到 addTestSuite() 也没有帮助:(
    • 你能提供更多代码吗?很高兴看到 SimpleTest 类本身
    • 当然:类 SimpleTest 扩展 PHPUnit_Framework_TestCase { public function testSimple() { $this->assertEquals(1, 1); } }
    • 所以,如果你替换 $suite->addTest(new SimpleTest());用 $suite->addTestSuite('SimpleTest'); 还是不行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2023-03-14
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多