Jon Cram Jon Cram - 2 years ago 140
PHP Question

PHPUnit: Get name of test class and method from setUp()?

PHPUnit runs the

method of a test class prior to running a specific test.

I load up test-specific fixtures for every test in a test class and would prefer not to have to explicitly do so. I would ideally like to handle this automagically in the

If the
method makes available the test class name and test method name this can be done.

Is the name of the test class and method that is about to be run available to me in the

Answer Source

The easiest way to achieve this should be calling $this->getName() in setUp().


class MyTest extends PHPUnit_Framework_TestCase
    public function setUp() {

    public function testMethod()

And running:

phpunit MyTest.php 


PHPUnit 3.7.1 by Sebastian Bergmann.

.string(10) "testMethod"

Time: 0 seconds, Memory: 5.00Mb

OK (1 test, 1 assertion)

In general I'd advice against doing this but there sure are cases where it can be a nice way to do things.

Other options would be to have more than one test class and having all the tests that use the same fixtures together in one class.

Another would be to have private setUp helpers and calling the appropriate one from the test case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download