Configuring setUp in PHPUnit

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

How should I do it? I tried using @dataProvider, but this does not work with setUp, which I quickly discovered.

Here is what I would 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() {

}

As a result, testOne () and testTwo () are launched against the object with the namespace "hello" and the object with the namespace "world"

Any help would be greatly appreciated!

Thanks Matt

+3
source share
1 answer

SUT - TestCase, . .

/**
 * 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 );
}

testOne , testTwo , :

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

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

testTwo $tag testOne, , testOne.

+10

Source: https://habr.com/ru/post/1749969/


All Articles