开发者

how to test multiple sets of data using php unit?

开发者 https://www.devze.com 2023-01-01 21:38 出处:网络
I have a class and few functions that I n开发者_运维百科eed to test against multiple sets of data.I load these data from flat files.I understand that I can load a file in the setUp() method and run my

I have a class and few functions that I n开发者_运维百科eed to test against multiple sets of data. I load these data from flat files. I understand that I can load a file in the setUp() method and run my tests, but how do I load multiple sets of data and test the same functions against those data?


class MyTestCase extends PHPUnit_Framework_TestCase {

    private $_testObjects = array();

    public function setUp() 
    {
        // load the files, unserialize the objects 
        // and store them in the $_testObjects array
    }

    public function getTestObjects()
    {
        return $this->_testObjects;
    }

    public function testA()
    {             
         foreach ($this->getTestObjects() as $obj) {
             // execute assertion/s
         }
    }

    public function testB()
    {             
         foreach ($this->getTestObjects() as $obj) {
             // execute assertion/s
         }
    }

    // ...
}


You can take advantage of @dataProvider annotation. That way, you don't need to loop on your own over the test data array.

class MyTestCase extends PHPUnit_Framework_TestCase {

    private $_testObjects = array();

    public function setUp() 
    {
        // load the files, unserialize the objects 
        // and store them in the $_testObjects array
    }

    public function getTestObjects()
    {
        return $this->_testObjects;
    }

    /**
     *
     * @dataProvider getTestObjects
     *
     */
    public function testA($obj)
    {             
         // execute assertion/s
    }

    /**
     *
     * @dataProvider getTestObjects
     *
     */
    public function testB($obj)
    {             
         // execute assertion/s
    }

    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消