私はAndroidアプリではJUnitを使って開発をしたことがありますが、PHPのユニットテストは初めてです。
PHPUnitをこれから勉強していきます。という超最初の一歩です。
PHPUnitの大変よい点は、日本語のマニュアルがしっかりしてる!ちゃんとある!ってことですね![[smile]](https://i0.wp.com/oc-technote.com/wp-content/themes/oc/assets/images/face/smile.png?w=525&ssl=1)
https://phpunit.de/manual/current/ja/installation.html
ありがたやー ありがたやー
まずは、自分の開発用のWindowsPCに、上記のURLの記述に沿って、PHPUnitをインストールしてみます。
phpunit --version
とかやってみて、バージョンが表示されるのを見て喜んでみます⊂(^-^)⊃
まず、簡単!なテストを作って、テストを走らせてみます。
//テストする元のクラス
//Calculator.php
<?php
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
}
//テスト
<?php
require 'Calculator.php';
class CalculatorTests extends PHPUnit_Framework_TestCase
{
private $calculator;
protected function setUp()
{
$this->calculator = new Calculator();
}
protected function tearDown()
{
$this->calculator = NULL;
}
public function testAdd()
{
$result = $this->calculator->add(1, 2);
$this->assertEquals(3, $result);
}
}
上記2つのファイルを下記のフォルダに入れます。
C:\xampp2\htdocs\test\php_unit
コマンドプロンプトで
phpunit CalculatorTest.php
とやります。
OK
と表示されましたね![[smile]](https://i0.wp.com/oc-technote.com/wp-content/themes/oc/assets/images/face/smile.png?w=525&ssl=1)
