クラスファイルからテストコードを作成
概要
簡易テスト用classファイルより雛形を作成し、修正した結果の実行をテスト
準備
classファイル(DataCheck01.php)
<?php
class DataCheck01{
// コンストラクタ
public function __construct(){
//echo __CLASS__ . "->" . __METHOD__ . "\n";
}
// デストラクタ
public function __destruct(){
//echo __CLASS__ . "->" . __METHOD__ . "\n";
}
// テスト対象のメソッド
public function check01($param){
//echo __CLASS__ . "->" . __METHOD__ . ":START\n";
$result = 0;
if($param === 10){
$result = "1";
}else{
$result = "2";
}
//echo __CLASS__ . "->" . __METHOD__ . ":END\n";
return $result;
}
}
?>
雛形作成
コマンド実行
D:\Tools\Works\php\phpunit_01>phpunit-skelgen --test DataCheck01
PHPUnit Skeleton Generator 1.1.0 by Sebastian Bergmann.
Wrote skeleton for "DataCheck01Test" to "D:\Tools\Works\php\phpunit_01\DataCheck01Test.php".
D:\Tools\Works\php\phpunit_01>
※ファイルはクラス名.phpではないと雛形を作成できない
※コンストラクタは作成しないが、デストラクタは作成する
雛形確認(DataCheck01Test.php)
<?php
/**
* Generated by PHPUnit_SkeletonGenerator on 2012-07-11 at 16:09:25.
*/
class DataCheck01Test extends PHPUnit_Framework_TestCase
{
/**
* @var DataCheck01
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new DataCheck01;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers DataCheck01::__destruct
* @todo Implement test__destruct().
*/
public function test__destruct()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers DataCheck01::check01
* @todo Implement testCheck01().
*/
public function testCheck01()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
修正したテストコード(DataCheck01Test.php)
<?php
require_once('./DataCheck01.php');
/**
* Generated by PHPUnit_SkeletonGenerator on 2012-07-11 at 16:09:25.
*/
class DataCheck01Test extends PHPUnit_Framework_TestCase
{
/**
* @var DataCheck01
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new DataCheck01;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers DataCheck01::__construct
* @todo Implement __construct().
*/
public function test__construct()
{
// メソッドチェック
$this->assertTrue(true);
}
/**
* @covers DataCheck01::__destruct
* @todo Implement test__destruct().
*/
public function test__destruct()
{
// メソッドチェック
$this->assertTrue(true);
}
/**
* @covers DataCheck01::check01
* @todo Implement testCheck01().
*/
public function testCheck01()
{
// メソッドチェック
$res1 = $this->object->check01(10);
$this->assertEquals("1", $res1);
// メソッドチェック
$res2 = $this->object->check01(20);
$this->assertEquals("2", $res2);
}
}
実行結果
実行
D:\Tools\Works\php\phpunit_01>phpunit DataCheck01Test.php
PHPUnit 3.6.11 by Sebastian Bergmann.
...
Time: 1 second, Memory: 3.50Mb
OK (3 tests, 4 assertions)
D:\Tools\Works\php\phpunit_01>
最終更新:2012年07月11日 23:43