EcomDev_PHPUnit Tip #6

For years, the test framework EcomDev_PHPUnit is quasi-standard for Magento unit tests. The current version is 0.3.7 and last state of official documentation is version 0.2.0 – since then, much has changed which you have to search yourself in code and GitHub issues. This series shall collect practical usage tips.

Tip #6: Fixture Rollback

If you use fixtures, the fixture data is removed from the database after the corresponding test finishes. But it’s important to note, that there is no transaction rollback, instead all tables affected by the fixture will be truncated after the test.

This has some implications:

  • You shouldn’t ever use table fixtures with tables that contain important core data, like core_config_data or eav_attribute
  • Data in tables, not contained in the fixture, stay. You can trigger deletion of data created during test with an empty fixture, for example for quotes and orders:
      sales/quote: []
      sales/order: []
  • If this behavior is not desired, you should clean up behind yourself, i.e. delete created data in a tearDown method.
  • Don’t rely too much on data present in the original database, as other tests might overwrite and eventually delete it with their fixtures
  • If you use shared fixtures with @loadSharedFixture all data in the shared fixture is only created and removed once. All tables affected by a shared fixture will not be cleaned up in between at all, even if other normal fixtures add data. My advice: Use shared fixtures with caution, or better not at all.