Tworzę przykładową klase która bedzie testowana:
Kod
Math.php
class Math {
public function add($a, $b) {
return $a + $b;
}
public function sub($a, $b) {
return $a - $b;
}
}
class Math {
public function add($a, $b) {
return $a + $b;
}
public function sub($a, $b) {
return $a - $b;
}
}
Tworzę dla niej test:
Kod
MathTest.php
require_once("Math.php");
require_once("PHPUnit2\Framework\TestCase.php");
class MathTest extends PHPUnit2_Framework_TestCase {
private $Math;
public function __construct($name) {
parent::__construct($name);
}
public function setUp() {
$this->Math = new Math();
}
public function testAdd() {
$result = $this->Math->add(2, 1);
$this->assertEquals($result, 3, 0, 'Math->add(1, 2) is not 3');
}
public function testSub() {
$result = $this->Math->sub(2, 1);
$this->assertEquals($result, 1, 0, 'Math->sub(2, 1) is not 1');
}
}
require_once("Math.php");
require_once("PHPUnit2\Framework\TestCase.php");
class MathTest extends PHPUnit2_Framework_TestCase {
private $Math;
public function __construct($name) {
parent::__construct($name);
}
public function setUp() {
$this->Math = new Math();
}
public function testAdd() {
$result = $this->Math->add(2, 1);
$this->assertEquals($result, 3, 0, 'Math->add(1, 2) is not 3');
}
public function testSub() {
$result = $this->Math->sub(2, 1);
$this->assertEquals($result, 1, 0, 'Math->sub(2, 1) is not 1');
}
}
Jeżeli uruchomię ten test z wiersza polecen (phpunit MathTest test\MathTest.php) to wszystko jest ok (pokazuje wyniki testów).
Natomiast jeżeli wrzucę to w jakis TestSuite, np:
Kod
AllTest.php
require_once("MathTest.php");
require_once("PHPUnit2\Framework\TestSuite.php");
require_once("PHPUnit2\TextUI\TestRunner.php");
$suite = new PHPUnit2_Framework_TestSuite();
$suite->addTest(new MathTest('testAdd'));
PHPUnit2_TextUI_TestRunner::run($test);
require_once("MathTest.php");
require_once("PHPUnit2\Framework\TestSuite.php");
require_once("PHPUnit2\TextUI\TestRunner.php");
$suite = new PHPUnit2_Framework_TestSuite();
$suite->addTest(new MathTest('testAdd'));
PHPUnit2_TextUI_TestRunner::run($test);
Uruchamiam poleceniem phpunit AllTest test\AllTest.php i nic nie ma, żadnych wyników. Nic nie wypisuje na ekranie nawet jesli ustawie wartosci w assertEquals tak zeby wywalalo blad.