views:

558

answers:

2

I'd like to integrate PHPUnit to my framework. By this, I mean that I have to do some initializing in the beginning, like setting up autoloads, before I'd run the tests.

I'd like to use the cli test runner, and if I understand correctly, I have to make a class, that has a static function suite(), which returns an instance of PHPUnit_Framework_TestSuite, and add tests to this suite, as noted on http://www.phpunit.de/manual/current/en/textui.html.

So far I have come up with:

class MyTestFW {
    public static function suite() {
        // Do framework initialization here

        $suite = new PHPUnit_Framework_TestSuite();
        $suite->addTest(new SimpleTest());

        // Add more tests

        return $suite;
    }
}

SimpleTest is a very basic test class, that extends PHPUnit_Framework_TestCase. When I run "phpunit MyTestFW", I always get:

PHPUnit 3.3.16 by Sebastian Bergmann.

E

Time: 0 seconds

There was 1 error:

1) (SimpleTest)
RuntimeException: PHPUnit_Framework_TestCase::$name must not be NULL.

Could someone help me out a little please?

+1  A: 

PHPUnit_Framework_TestCase::$name gets set in the TestCase constructor, so you could try this:

$suite->addTest(new SimpleTest('simpletest'));

edit1:

I don't know your code, so i don't know if this helps.
What I usually see is this (as a replacement of the above, not addition):

$suite->addTestSuite('SimpleTest');

edit2:

phpunit documentation: Chapter 7 - Organizing Tests

Karsten
Nope, it looks it's trying to call it as a method:There was 1 failure:1) SimpleTest(SimpleTest)Method SimpleTest does not exist
WishCow
Adding it to addTestSuite() didn't help either :(
WishCow
can you provide more code? the SimpleTest class itself would be nice to see
Karsten
Sure:class SimpleTest extends PHPUnit_Framework_TestCase { public function testSimple() { $this->assertEquals(1, 1); }}
WishCow
so, if you replace $suite->addTest(new SimpleTest()); with $suite->addTestSuite('SimpleTest');, it still does not work?
Karsten
My mistake, it works. I put the 'SimpleTest' string in the testcase's constructor. Now I just need some best practices, thank you!
WishCow
A: 

Thanks men. Im my case I replaces $suite->addTest(new IndexTestCase()) with $suite->addTestSuite('IndexTestCase') and Works fine.

Thank you!

Giovanni Cândido da Silva