Question

I'm fairly new to using PHPUnit and I'm trying to use it more effectively by using data providers. I can get data providers to work when writing a normal test case, however I find that I'm rewriting my setup code for across several testcases. So I'm trying to extend PHPUnit_Framework_TestCase with a BaseTestCase class that does all of my common setup. This works if I run simple test in a test case that extends my BaseTestCase class. However I can't seem to use @dataProvider when extending my BaseTestCase class.


So my setup is:

class BaseTestCase extends PHPUnit_Framework_TestCase{
     public static function setUpBeforeClass(){
           //do setup
           //this is getting called and it works
     }
}

class myTest extends BaseTestCase{
     public function myBasicTest(){
           //this works
           $this->assertEquals(2, 1+1);
     }

     public function myProvider(){
           return [
               [1,1,2],
               [1,2,3],
               [1,4,5],
           ]
     }

     /**
      * @dataProvider myProvider
      */
     public function testMyProvider($a, $b, $result){
           //this doesn't work, the provider never gets called
           //this would normally work if I extended PHPUnit_Framework_TestCase
           $this->assertEquals($result, $a+$b);
     }
}

I know the providers get ran before any of the setup does so I'm wondering if PHPUnit doesn't know that the provider exists because of the inheritance. Either way, does anyone know if what I'm trying to do is possible this way or does PHPUnit have another way of accommodating these types of situations?

Thanks for your help,

Jordan

Was it helpful?

Solution 2

This is actually a non issue. I had an incorrect constructor setup in my test file. A very frustrating oversight.

OTHER TIPS

Your test function does not begin with the word 'test'.

public function test_myProviderTest($a, $b, $result){
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top