Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a PHPUnit testcase multiple times

Tags:

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); } 
like image 208
Vojtech Kurka Avatar asked Aug 15 '13 13:08

Vojtech Kurka


People also ask

What is mock PHPUnit?

Likewise, PHPUnit mock object is a simulated object that performs the behavior of a part of the application that is required in the unit test. The developers control the mock object by defining the pre-computed results on the actions.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.

What is assertion in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.


1 Answers

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, ....,          ],     ]; } 
like image 178
Zero Huang Avatar answered Sep 19 '22 17:09

Zero Huang