开发者

Customizing setUp in PHPUnit

开发者 https://www.devze.com 2023-01-03 22:30 出处:网络
I\'m looking to run a bunch of tests with one object with different parameters in the setUp function.

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开发者_Python百科 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消