Improved Skeleton Generator in PHPUnit 3

Sebastian Bergmann » 24 September 2006 » in New Features » 2 Comments

PHPUnit can help you write your tests by analyzing the code of an existing class and generating a skeleton test-case class for it. With PHPUnit 3 this feature will be more powerful as it now supports @assert annotations in the class source.

A @assert (1, 2) == 3 annotation for a method with signature add($a, $b) will generate a test method that calls add(1, 2) and expects 3 as the result.
<?php
class Calculator
{
    /**
     * @assert (0, 0) == 0
     * @assert (0, 1) == 1
     * @assert (1, 0) == 1
     * @assert (1, 1) == 2
     */
    public function add($a, $b)
    {
        return $a + $b;
    }
}
?>
We use the skeleton generator to generate a test case class for this class:
sb@wopr-mobile ~ % phpunit --skeleton Calculator
PHPUnit 3.0.0 by Sebastian Bergmann.

Wrote test class skeleton for Calculator to CalculatorTest.php.
Below is the generated test code:
<?php
require_once "PHPUnit/Framework/TestCase.php";
require_once "Calculator.php";
 
class CalculatorTest extends PHPUnit_Framework_TestCase {
    /**
     * Generated from @assert (0, 0) == 0.
     */
    public function testAdd() {
        $o = new Calculator;
        $this->assertEquals(0, $o->add(0, 0));
    }
 
    /**
     * Generated from @assert (0, 1) == 1.
     */
    public function testAdd2() {
        $o = new Calculator;
        $this->assertEquals(1, $o->add(0, 1));
    }
 
    /**
     * Generated from @assert (1, 0) == 1.
     */
    public function testAdd3() {
        $o = new Calculator;
        $this->assertEquals(1, $o->add(1, 0));
    }
 
    /**
     * Generated from @assert (1, 1) == 2.
     */
    public function testAdd4() {
        $o = new Calculator;
        $this->assertEquals(2, $o->add(1, 1));
    }
}
?>
Now we can run these automatically generated tests:
sb@wopr-mobile ~ % phpunit CalculatorTest     
PHPUnit 3.0.0 by Sebastian Bergmann.

....

Time: 00:00


OK (4 tests)
Defined tags for this entry: , , ,

Trackback specific URI for this entry

2 Comments to "Improved Skeleton Generator in PHPUnit 3"

Display comments as (Linear | Threaded)
  1. Toby
    24/09/2006 at 16:32 Permalink
    Cool Stuff! :)

    Reply

  2. Greg Beaver
    25/09/2006 at 05:21 Permalink
    nice work Sebastian

    Reply

1 Trackback to "Improved Skeleton Generator in PHPUnit 3"

  1. Do You PHP ??? 25/09/2006 at 04:27
    ????????????????????????????????????????????????????????????????????????????? PHPUnit can help you write your tests by analyzing the code of an existing class and generating a s

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.