PHPUnit: use different dataset on every test in a testcase

PHPUnit offers the possibility to perform tests based in database fixtures. The common way is like described in the PHPUnit documentation.

But what if you just want to have a small and different dataset on every test?

My way to achieve that is the following:

  1. create a minimal dataset with a table and a column that exist in your database, because getDataSet() has to return an object that implements the PHPUnit_Extensions_Database_DataSet_IDataSet interface.

    The minimal dataset might look like the following:
  2. implement the getTearDownOperation function which is called after each test
    • resets the database to be empty

  3. create your own setup function
    • copied from the original SetUp function in PHPUnit_Extensions_Database_TestCase
    • added a parameter to mySetup function to carry the dataset
    • call PHPUnit_Framework_TestCase::SetUp() instead of parent::SetUp()

  4. use it on every test function

Enjoy.

  • Lulu

    thanks for sharing, it works very well 🙂