views:

52

answers:

1

Hey all,

I'm looking to run a bunch of tests with one object with different parameters in the setUp function.

How do I do this? I tried using the @dataProvider, but that doesn't work with setUp I quickly found out..

Here's what I'd like to do (using @dataProvider):

/*
* @dataProvider provider
*/
function setUp($namespace, $args) {
   $this->tag = new Tag($namespace, $args);
}

function provider() {
   return array(
      array('hello', array()),
      array('world', array())
   );
}

function testOne() {

}

function testTwo() {

}

The result is that testOne() and testTwo() are run against an object with the namespace "hello" and an object with the namespace "world"

Any help would be greatly appreciated!

Thanks, Matt

+2  A: 

You don't have to assign the SUT to a member variable of the TestCase instance if it does not suit the test. Simply create new Tag instances in the provider and pass them to the test function instead

/**
 * Provides different test Tag instances
 */
function tagProvider() {
   return array(
      array( new Tag( 'hello', array() ) ),
      array( new Tag( 'world', array() ) )
   );
}

/*
* @dataProvider tagProvider
*/
function testOne( Tag $tag ) {
    $this->assertSomething( $tag );
}

If testOne alters the test in a way that testTwo depends on the changes, you can do:

/*
* @dataProvider tagProvider
*/
function testOne( Tag $tag ) {
    $this->assertSomething( $tag );
    return $tag;
}

/*
* @depends testOne
*/
function testTwo( Tag $tag ) {
    $this->assertSomething( $tag );
}

And then testTwo will use the returned $tag from testOne, along with any state changes made to it in testOne.

Gordon
Perfect. Thanks man!
Matt