25

I'm looking for a way how to run a testcase multiple times with different setting.

I'm testing a database access class (dozens of test methods), and want to test it in "normal mode" and then in "debug mode". Both modes must produce the same test results.

Is there any possibility to do that in the testcase setting? Or overriding the run() method? I don't want to write the test twice, of course :)

Thank you

edit: GOT IT!

public function run(PHPUnit_Framework_TestResult $result = NULL)
{
    if ($result === NULL) {
        $result = $this->createResult();
    }

    /**
     * Run the testsuite multiple times with different debug level
     */
    $this->debugLevel = 0;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    $this->debugLevel = 8;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    $this->debugLevel = 16;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    return $result;
}

public function setUp()
{
    parent::setUp();
    $this->myclass->setOptions('debug', $this->debugLevel);
}
13
  • 11
    Have a look at @dataProvider functions that emit test data sets. Commented Aug 15, 2013 at 13:18
  • 1
    I know the usage dataProviders :) I just want to change one simple setting of the class and then simply run ALL the test methods again. Something like: run(); $class->setDebug(true); run(); Commented Aug 15, 2013 at 13:22
  • 4
    You can move all code to abstract test case and create 2 children with different code in setUp(). Commented Aug 15, 2013 at 14:23
  • 1
    excellent! you should run setUp() for every iteration (but the first) to achieve isolation, though. Commented Nov 10, 2014 at 15:44
  • 1
    FYI you should set count() to 3 since you're running the test 3 times. Commented Apr 6, 2016 at 21:45

4 Answers 4

5

I think Sven's is answer is correct. Write what you want to set, and give the test case parameter you need, like:

/**
 * @dataProvider forMyTest
 */
public function testWithDifferentDbType($type, $param1, $param ....)
{
    $this->assertExists(....);
}

public function forMyTest() {
    return [
        [
            true
            [$prodDb, $param1, $param, .....],
        ],
        [   
            false,
            [$debugDb, $param1 $param, ....,

        ],
    ];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Current docs for @dataProvider: phpunit.readthedocs.io/en/latest/…
2

use --repeat option and the amount you need to run test.

1 Comment

Unfortunately, this functionality has been removed as of phpunit v10 and "Bringing back this functionality is not planned". github.com/sebastianbergmann/phpunit/issues/…
1

PHPUnit offers test decorators. The documentation actually has repeating decorator as the example of what to do with a decorator. The decorator would be the perfect way of implementing the behavior in a reusable way without depending on subclassing PHPUnit_Framework_TestCase.

Comments

0

You could create multiple methods in the unit test. All of them will be run, with the setUp method being called after each test method is run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.