【问题标题】:PHPUnit test function outside class类外的PHPUnit测试函数
【发布时间】:2012-07-01 10:51:54
【问题描述】:

我对 PHPUnit 和单元测试非常陌生,所以我有一个问题: 我可以在类之外测试一个函数吗:

    function odd_or_even( $num ) {
    return $num%2; // Returns 0 for odd and 1 for even
}

class test extends PHPUnit_Framework_TestCase {
    public function odd_or_even_to_true() {
        $this->assetTrue( odd_or_even( 4 ) == true );
    }
}

现在它只是返回:

No tests found in class "test".

【问题讨论】:

    标签: php unit-testing phpunit


    【解决方案1】:

    您需要在函数名称前加上“test”,以便将它们识别为测试。

    From the documentation:

    1. 测试是名为 test* 的公共方法。

    或者,您可以在方法的文档块中使用@test 注解将其标记为测试方法。

    拨打odd_or_even()应该没有问题。

    例如:

    class test extends PHPUnit_Framework_TestCase {
        public function test_odd_or_even_to_true() {
            $this->assertTrue( odd_or_even( 4 ) == true );
        }
    }
    

    【讨论】:

    • @Theadamlt: 同样在odd_or_even( 4 ) == true 部分== true 是多余的,只要你使用assertTrue
    • +1 用于提及使用 @test 注释。很棒。
    • 将函数导入测试的最佳方法是什么?我尝试了 require('function.php'),但这会导致 1) test::test_me 异常:不允许序列化 'Closure'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2016-05-21
    • 2019-10-22
    • 2013-12-05
    • 2010-12-29
    • 2017-10-19
    相关资源
    最近更新 更多