2010-10-13 81 views
0

我正在運行CodeIgniter 2.0,並且我在索引函數中使用了此代碼的測試控制器設置。看起來無論我把什麼放在「set_test_items」變量中,報告都不會改變。它總是顯示關於測試的所有可能的信息。我覺得我必須錯過這裏明顯的東西。我錯過了什麼?在CodeIgniter中設置測試項單元測試不影響報告輸出

$this->unit->set_test_items(array('test_name', 'result')); 

    $this->_test_user_lib(); 
    $this->_test_user_model(); 

    echo $this->unit->report(); 

而且,我只是想在生成報告和陣列只包含了兩件事情我通過中的var_dump()上可見的項目,所以它被設置正確。

+0

認爲您需要發佈更多信息......評論,測試功能。嘗試整個控制器 – Ross 2010-10-14 16:59:49

回答

2

set_test_items()隻影響run()方法,而不是report()。下面的代碼將只顯示你在set_test_items()規定的項目:

echo $this->unit->run(1 + 1, 2, 'One plus one'); 

但下面會顯示所有項目:

echo $this->unit->report(); 

希望這有助於。

1

您可以擴展Unit_class庫以修復運行方法。

這是一個使用數組助手「元素」來僅保留> _test_items_visible元素的示例。

注:這樣,你必須設置明顯的項目運行測試。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Unit_test extends CI_Unit_test 
{ 
    /** 
    * Llamamos al constructor del padre 
    * 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 


    /** 
    * Reemplazamos la función RUN 
    */ 
    function run($test, $expected = TRUE, $test_name = 'undefined', $notes = '') 
    { 
    // Sacamos la versión 
    $CI =& get_instance(); 
    $CI->load->helper('array'); 


    if ($this->active == FALSE) 
    { 
     return FALSE; 
    } 

    if (in_array($expected, array('is_object', 'is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE)) 
    { 
     $expected = str_replace('is_float', 'is_double', $expected); 
     $result = ($expected($test)) ? TRUE : FALSE; 
     $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected)); 
    } 
    else 
    { 
     if ($this->strict == TRUE) 
     $result = ($test === $expected) ? TRUE : FALSE; 
     else 
     $result = ($test == $expected) ? TRUE : FALSE; 

     $extype = gettype($expected); 
    } 

    $back = $this->_backtrace(); 


    // Only visible elements 
    $report[] = elements 
    (
     $this->_test_items_visible, array 
     (
     'test_name'  => $test_name, 
     'test_datatype' => gettype($test), 
     'res_datatype' => $extype, 
     'result'  => ($result === TRUE) ? 'passed' : 'failed', 
     'file'   => $back['file'], 
     'line'   => $back['line'], 
     'notes'   => $notes 
    ) 
    ) ; 

    $this->results[] = $report; 

    return($this->report($this->result($report))); 
    } 
} 
0

,而不是運行報告()方法

echo $this->unit->report(); 

可以運行的結果()方法:

echo $this->unit->result(); 

這會給你只是你選擇,但在項目一個原始數據格式(即一個關聯數組),這可能更好,因爲報告的格式不是很好。然後,您可以將它們加載到視圖中,並將它們設置爲您想要的格式:

$data['test_results'] = $this->unit->result(); 
$data['title'] = 'Pricing Test'; 
$this->load->view('header'); 
$this->load->view('tests/index', $data);