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);
}
Vojtech Kurka
  • 875
  • 9
  • 10
  • 11
    Have a look at `@dataProvider` functions that emit test data sets. – Sven Aug 15 '13 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(); – Vojtech Kurka Aug 15 '13 at 13:22
  • Ok, I assume this is not the usual case then. Have you tried extending the original test class and changing the one thing in `setUp()`. This should inherit all test code under a new label and run the stuff twice. – Sven Aug 15 '13 at 13:45
  • Yes, I did try the exact approach, but PHPUnit runs only the child class and ignores the original one. – Vojtech Kurka Aug 15 '13 at 13:47
  • One class per file rule. And the name of the file has to match the name of the test class. – Sven Aug 15 '13 at 14:11
  • 4
    You can move all code to abstract test case and create 2 children with different code in setUp(). – Dmytro Zavalkin Aug 15 '13 at 14:23
  • I've found some solution, it's under the original post. Thank you guys for help, I appreciate that! – Vojtech Kurka Aug 15 '13 at 14:33
  • 1
    excellent! you should run `setUp()` for every iteration (but the first) to achieve isolation, though. – tacone Nov 10 '14 at 15:44
  • 1
    FYI you should set `count()` to 3 since you're running the test 3 times. – mpen Apr 06 '16 at 21:45
  • @VojtechKurka since you've solved it, you could put your solution in an answer, instead of adding it to the original post. – ImprobabilityCast Dec 14 '17 at 20:37
  • @VojtechKurka I know this question is old, but I was curious if you had a reason for doing `$result->run($this)` instead of `parent::run($result)`? – David DeMar Aug 26 '19 at 15:48
  • Here is the doc link for [`@dataProvider`](https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers). You are welcome – Orkhan Alikhanov Jul 16 '20 at 11:34
  • If you only want to do this periodically to check for an unstable test, PHPUnit has the `--repeat` flag. See also https://stackoverflow.com/a/62911514 – AlbinoDrought Dec 03 '20 at 17:35

4 Answers4

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, ....,

        ],
    ];
}
Zero Huang
  • 71
  • 1
  • 5
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.

Lars Strojny
  • 667
  • 4
  • 11
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.

0

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

soha1410
  • 555
  • 5
  • 14