【问题标题】:SimpleTest (PHP unit tester) seems to report incorrect test-case countSimpleTest(PHP 单元测试器)似乎报告了不正确的测试用例计数
【发布时间】:2013-03-04 15:46:03
【问题描述】:

这是我的两个测试用例:

<?php
require_once(dirname(__FILE__) . '/../simpletest/unit_tester.php');

class Tests extends UnitTestCase {
    function test_1() {
        $this->assertTrue(true);
    }
    function test_2() {
        $this->assertTrue(true);
    }
}
?>

和我的测试司机:

<?php
require_once(dirname(__FILE__) . '/../simpletest/simpletest.php');

$test = new TestSuite();
$test->addFile(dirname(__FILE__) . '/ex1.php');
$test->run(new TextReporter());
?>

我得到这个输出:

TestSuite
OK
Test cases run: 1/2, Passes: 2, Failures: 0, Exceptions: 0

当我像这样从终端运行驱动程序文件(ex2.php)时:

curl 'http://localhost/~marc/simpletestexample/ex2.php'

现在,为什么它报告“测试用例运行:1/2”而不是“测试用例运行:1/1”?似乎有一个幻影测试用例没有运行。

【问题讨论】:

  • 似乎是一个不同的问题。我将我的课程测试更改为 MyTests,结果完全相同。在我的情况下,问题不在于我的测试没有运行,而是似乎有一个不是我的情况没有运行。
  • @CanGeliş 这与您提供链接的问题不同。无论我们做什么都会发生这种情况,但它仍然显示 1/2。您提供的链接,用户没有使用 test 正确启动函数名称。
  • 我无法重现报告的问题:“测试用例运行:1/1,通过:2,失败:0,异常:0”。如果此错误仍然存​​在,请从 github.com/simpletest/simpletest 获取更高版本的 SimpleTest。如果您遇到问题,请随时在此处打开错误报告。

标签: php unit-testing simpletest


【解决方案1】:
<?php  
  require_once(dirname(__FILE__) . '/simpletest/autorun.php');  
  require_once('/classes/hello.php');     
  class Testhello extends UnitTestCase {  
    function testViewhelloWithEntries()  
    {  
      $hello= new hello();
      $hello->add("Bob", "Hi, I'm Bob.");  
      $hello->add("Tom", "Hi, I'm Tom.");  
      $hello->add("jack", "Hi, I'm Jack.");  
      $entries = $hello->viewAll(); 
      $count_is_greater_than_zero = (count($entries) > 0);  
      $this->assertTrue($count_is_greater_than_zero);  
      $this->assertIsA($entries, 'array');  
      foreach($entries as $entry) {  
          $this->assertIsA($entry, 'array');  
          $this->assertTrue(isset($entry['name']));  
          $this->assertTrue(isset($entry['message']));
      }
    }
    public function hi($name ,$message)
    {
       self::$_entries[] = array('name' => $name, 'message' => $message ); //fixed!  
    return true;  
    }
     function testViewhelloWithNoEntries()  
    {  
        $hello = new hello();  
        $hello->deleteAll(); // Delete all the entries first so we know it's an empty table  
        $hello->jay();
        $entries = $hello->viewAll();  
        $this->assertEqual($entries, array());  
    }  



}
    ?>

在classes文件夹中创建一个hello.php

    <?php  
class hello  
{
 private static $_entries = array(  
        array (  
            'name' => 'Kirk', 
            'message' => 'Hi, I\'m Kirk.'  
        ),  
        array (  
            'name' => 'Ted',  
            'message' => 'Hi, I\'m Ted.'  
        )  
    );  




 public function viewAll() {  
        // Here, we should retrieve all the records from the database.  
        // This is simulated by returning the $_entries array  
        return self::$_entries;  
    }  

    public function add( $name, $message ) {  
        // Here, we simulate insertion into the database by adding a new record into the $_entries array  
        // This is the correct way to do it: self::$_entries[] = array('name' => $name, 'message' => $message );  
        //    print_r(     self::$_entries[] = array('name' => $name, 'message' => $message )); //oops, there's a bug here somewhere  
      echo  " <br> ";
  echo "My name is a {$name}"."&nbsp;"."{$message}";
        return true;  
    }  

    public function deleteAll() {  
        // We just set the $_entries array to simulate   
        self::$_entries = array();  
        return true;  
    }  
     public function jay() {  

        // We just set the $_entries array to simulate   
        //echo "hello world";
        self::$_entries = array();  
        return true;  
    }  

}  
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2018-07-26
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多