SimpleTest(PHP单元测试器)似乎报告了不正确的测试用例计数

时间:2013-03-15 19:33:43

标签: php unit-testing simpletest

以下是我的两个测试用例:

<?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”?似乎某个虚拟测试案例在某个地方没有被运行。

1 个答案:

答案 0 :(得分:0)

<?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;  
    }  

}  
?>