Unit Tests machen nicht nur Sinn, sondern auch Spaß und erleichtern das Leben. Für PHP gibts PHPUnit.
Auf die Schnelle geht’s so (bin Verzeichniss in ~ benötigt):
[schmimi2@cepheus ~]$ cd ~/bin [schmimi2@cepheus bin]$ wget http://pear.phpunit.de/get/phpunit.phar [schmimi2@cepheus bin]$ mv phpunit.phar phpunit [schmimi2@cepheus bin]$ chmod +x phpunit [schmimi2@cepheus bin]$ bash |
Zum Testen einfach das File StackTest.php mit folgendem Inhalt erzeugen:
<?php class StackTest extends PHPUnit_Framework_TestCase { public function testPushAndPop() { $stack = array(); $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); } public function testFail(){ $this->assertEquals(false,true); } } |
Test laufen lassen:
[schmimi2@cepheus unittests]$ phpunit StackTest.php PHPUnit 3.7.21 by Sebastian Bergmann. .F Time: 0 seconds, Memory: 2.75Mb There was 1 failure: 1) StackTest::testFail Failed asserting that true matches expected false. /var/www/virtual/schmimi2/api.giftimessen.de/unittests/StackTest.php:19 FAILURES! Tests: 2, Assertions: 6, Failures: 1. |