【问题标题】:Simpletest is running all of my tests twice. Why?Simpletest 正在运行我所有的测试两次。为什么?
【发布时间】:2011-10-28 20:18:54
【问题描述】:

我有一个简单的测试套件,我一直在为我最近用 PHP 编写的一些 API 包装器代码编写代码。但是每次我运行测试时,它都会运行所有测试两次。

我的电话号码:

require_once(dirname(__FILE__) . '/simpletest/autorun.php');  
require_once('CompanyNameAPI.php');


$test = new TestSuite('API test');
$test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
if (TextReporter::inCli()) {
    exit ($test->run(new TextReporter()) ? 0 : 1);
} else {
    $test->run(new HtmlReporter());
}

authentication_test.php 看起来像:

class Test_CallLoop_Authentication extends UnitTestCase {  

    function test_ClassCreate(){
        $class = new CallLoopAPI();
        $this->assertIsA($class, CallLoopAPI);
    }
        //More tests
}

在 authentication_test.php 中也不再包含 autorun.php 或其他对 simpletest 的调用。

想法?

【问题讨论】:

    标签: php tdd simpletest


    【解决方案1】:

    你应该像这样改变你的调用代码:

    require_once(dirname(__FILE__) . '/simpletest/autorun.php');  
    require_once('CompanyNameAPI.php');
    
    $test = new TestSuite('API test');
    $test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
    

    autorun.php 文件会自动执行你的测试,隐式调用 run() 方法,当你调用 run() 方法时,你会再次执行测试。

    【讨论】:

    • 我还不是最简单的专家,但我一直在阅读文档,它说您尝试使用的代码已经嵌入在 simpletest 库中,因此您的测试将自动识别到哪个记者在您使用命令行或浏览器的情况下使用。我建议您的代码实际上在浏览器和命令行中都使用适当的 Reporter 运行。
    【解决方案2】:

    从 simpletests 文档中,您应该使用静态方法 prefer(REPORTER)

    <?php
    require_once('show_passes.php');
    require_once('simpletest/simpletest.php');
    SimpleTest::prefer(new ShowPasses());
    require_once('simpletest/autorun.php');
    
    class AllTests extends TestSuite {
        function __construct() {
            parent::__construct('All tests');
            $this->addFile(dirname(__FILE__).'/log_test.php');
            $this->addFile(dirname(__FILE__).'/clock_test.php');
        }
    }
    ?>
    

    【讨论】:

    • 如果我可以标记多个问题,我会将其标记为后续问题的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2017-04-20
    • 2012-09-12
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多