Zum Inhalt springen

PHPUnit – Testklassen automatisch generieren

PHPUnit ist ja ein größeres Thema. Also gibt es jetzt nur einen kleinen Vorgeschmack. Falls sich jemand die Doku angeschaut hat, wird er/sie die Beispiele und die Generierung wiedererkennen und langweilig finden. Ich habe heute diese Funktion aber mal selbst ausprobiert und war ziemlich begeistert, wie toll dies funktioniert.

Eine nervige Arbeit ist immer einen Unittest zu definieren, der eine Methode testet, die mit primitiven Typen umgeht. Man schreibt eigentlich relativ simplen Code und braucht dafür viel Zeit. Das muss aber nicht so sein. PHPUnit kann mir diese Arbeit abnehmen. Ich muss nur ein paar Annotations nutzen, die ich an die entsprechenden Methoden schreibe. Wie passend, dass ich diese im letzten Beitrag bereits erklärt hatte.

Also definiere ich mir erst eine einfache Klasse (Calculations), die eine Methode hat, um 2 Zahlen zu addieren.

class Calculations {

    public function add($a, $b) {
        return $a+$b;
    }
}

Nun möchte ich testen, ob diese Methoden auch richtig funktionieren, hierfür ergänze ich die zu prüfende Methode nur um ein paar Assert Infomationen.

class Calculations {

    /**
     * @assert (0, 0) == 0
     * @assert (0, 1) == 1
     * @assert (1, 2) == 3
     */
    public function add($a, $b) {
        return $a+$b;
    }
}

Dann lass ich PHPUnit auf meine Klasse los und erhalte  eine Klasse, die sich CalculationsTest nennt. In dieser Klasse finde ich die einzelnen Tests.

$ phpunit --skeleton-test Calculations
require_once 'PHPUnit/Framework.php';
require_once 'Calculations.php';

/**
 * Test class for Calculations.
 * Generated by PHPUnit on 2010-01-14 at 21:09:11.
 */
class CalculationsTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var Calculations
     */
    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 Calculations;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * Generated from @assert (0, 0) == 0.
     */
    public function testAdd()
    {
        $this->assertEquals(
          0,
          $this->object->add(0, 0)
        );
    }

    /**
     * Generated from @assert (0, 1) == 1.
     */
    public function testAdd2()
    {
        $this->assertEquals(
          1,
          $this->object->add(0, 1)
        );
    }

    /**
     * Generated from @assert (1, 2) == 3.
     */
    public function testAdd3()
    {
        $this->assertEquals(
          3,
          $this->object->add(1, 2)
        );
    }
}

Diesen Code kann ich direkt mit PHPUnit ausführen und erhalte ein entsprechendes Testergebnis.

$ phpunit CalculationsTest.php 
PHPUnit 3.4.1 by Sebastian Bergmann.
...
Time: 0 seconds
OK (3 tests, 3 assertions)

Ich finde diese Lösung sehr schön und muss mich entschuldigen, dass dieser Beitrag so codelastig ist.

Published inAllgemein

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close