Created
May 1, 2011 14:28
-
-
Save yuya-matsushima/950537 to your computer and use it in GitHub Desktop.
unit_testをCLIから実行し、テストが失敗した場合には、失敗したテストを表示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class MY_Unit_test extends CI_Unit_test | |
{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function report($result = array()) | |
{ | |
if(count($result) == 0) | |
{ | |
$result = $this->result(); | |
} | |
$CI = get_instance(); | |
$CI->load->language('unit_test'); | |
$this->_parse_template(); | |
$r = ''; | |
if( ! defined('STDIN')) | |
{ | |
foreach($result as $res) | |
{ | |
$table = ''; | |
foreach($res as $key=>$val) | |
{ | |
if($key == $CI->lang->line('ut_result')) | |
{ | |
if($val == $CI->lang->line('ut_passed')) | |
{ | |
$val = '<span style="color: #0C0;">' . $val . '</span>'; | |
} | |
elseif($val == $CI->lang->line('ut_failed')) | |
{ | |
$val = '<span style="color: #C00;">' . $val . '</span>'; | |
} | |
} | |
$tmp = $this->_template_rows; | |
$tmp = str_replace('{item}', $key, $tmp); | |
$tmp = str_replace('{result}', $val, $tmp); | |
$table .= $tmp; | |
} | |
$r .= str_replace('{rows}', $table, $this->_template); | |
} | |
return $r; | |
} | |
else | |
{ | |
$passed = 0; | |
$failed = 0; | |
$failed_test = array(); | |
$count = 0; | |
$r1 = "Execute Unit Test!! \n\n"; | |
foreach($result as $res) | |
{ | |
if($res['Result'] == 'Passed') | |
{ | |
$passed += 1; | |
$count += 1; | |
} | |
elseif($res['Result'] == 'Failed') | |
{ | |
$failed += 1; | |
$failed_test[] = $res['Test Name']; | |
$count += 1; | |
} | |
} | |
$r2 = sprintf("All:%s Passed:%s Failed:%s\n", $count, $passed, $failed); | |
if(count($failed_test) > 0) | |
{ | |
$r = $r1 . "\x1b[31m" . $r2 . "\x1b[0m" . sprintf("Failed Test: %s \n", implode(', ', $failed_test)); | |
} | |
else | |
{ | |
$r = $r1 . "\x1b[32m" . $r2 . "\x1b[0m"; | |
} | |
return $r; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Sample extends MY_Controller | |
{ | |
function __construct() | |
{ | |
parent::__construct(); | |
$this->load->library('unit_test'); | |
$this->unit->use_strict(TRUE); | |
} | |
function index() | |
{ | |
$this->test_a(); | |
$this->test_b(); | |
$this->test_c(); | |
$this->test_d(); | |
echo $this->unit->report(); | |
} | |
function test_a() | |
{ | |
$test = 1 + 1; | |
$expected = 2; | |
$name = 'sample'; | |
$this->unit->run($test, $expected, $name); | |
} | |
function test_b() | |
{ | |
$test = 1 + 1; | |
$expected = 4; | |
$name = 'hogehoge'; | |
$this->unit->run($test, $expected, $name); | |
} | |
function test_c() | |
{ | |
$test = 1 + 1; | |
$expected = 2; | |
$name = 'ok!'; | |
$this->unit->run($test, $expected, $name); | |
} | |
function test_d() | |
{ | |
$test = 1 + 1; | |
$expected = 3; | |
$name = 'Out'; | |
$this->unit->run($test, $expected, $name); | |
$this->unit->run($test, 'is_int', $name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment