24

PHPUnit's own manual has some as-yet-unwritten sections entitled "Operations" and "Database Testing Best Practices."

What are best practices for testing a database with PHPUnit, particularly in MySQL?

6
  • 1
  • Gordon, I actually wrote this question specifically because the answers there don't seem to go into much depth, nor does the PHPUnit chapter on database testing referenced there. I'd like to see more on this.
    – Josh Smith
    Sep 14, 2010 at 8:03
  • 1
    To make things clear : you have a function performing some query (or queries) on a DB based on a known input. This will return a result and now you want to check whether that result is what you would expect? If so : then starting from a known state (where you also know what the actual result should be) would be a good start (and that's what they all refer to in the duplicate question). Just restore this known state on every test run.
    – wimvds
    Sep 16, 2010 at 7:55
  • Are you wanting to know how to do "functional testing" with PHPUnit?
    – Andrew
    Sep 16, 2010 at 13:29
  • @wimvds, this is essentially what I've done, though no idea if I'm doing it nearly as well as I should be. I've essentially run mysqldump on a testdb, then run mysql manually to restore testdb to the same state on the build server, as well. Is there any way I should be indicating within my tests, though, what the known state is supposed to be? And is there a more automated way to do this than manually running mysqldump every time?
    – Josh Smith
    Sep 16, 2010 at 20:19

2 Answers 2

9

When I'm doing database testing with PHPUnit I load my MySQL dump at the start of the first suite which contains any of the information I'm assuming to be true across all tests. When each test starts I use a setupDatabase method. This method deletes all the rows from the tables I know have changed then it loads a flat XML dataset containing the data I need to hold true. After this is done I run whatever code I'm testing. Finally, I use a collection of simple methods to select rows from the database to assert the changes I made were done correctly.

I wouldn't say this is a best practice but's worked pretty well for me. The only problems I've run into are having to do a find-and-replace on the XML datasets every time the schema changes and the tests run slowly as a result of all the deleting and inserting.

The Zend Framework has an interesting library for PHPUnit that allows tests to compare a database table to a flat XML dataset but I haven't had a chance to use it yet.

1
  • For database slowness: install another mysql server and have it's entire datafolder in memory-backend mountpoint (e.g. tmpfs in linux). That will speed up db creation & operations remarkably. Apr 4, 2014 at 11:47
3

Some unordered thoughts:

It's good to have fixtures (with or without db structure), loaded into db in startUp() of every test. It can came ie. from JSON or XML files, one for each table. If you couple it with functions like getNthFixture($sTable, $nIndex) or countFixtures($sTable), you can easily test your queries. Even further, you can use [LINQ][1] to get expected results from set of fixtures with little or no difference between DB & fixtures query. I find it quite easy to adapt at early prototyping / developing stage, when db structure is changing very often. Adding assertion to compare directly LINQ query result with db query result makes creating tests pure pleasure ;)

Another hints: db should be reinitialised before every testing method, not before test case. Ideally you should drop the base and rebuild it from complete set of fixtures.

And, if you can, try make tests which works with different databases (some things, of course are not portable, but majority is). Use at least sqlite aside of mysql/postgres/other_big_rdbm.

If you are testing framework or other complex system, you should probably mock up database access singleton. If some hardcoded stuff is buried deep in not-so-flexible-orm, it can be pain in the, let say, neck.

Good idea is to log all queries which didn't passed test, and/or to display them in fail messages. Also for db error messages. If you are testing big databases when performance is a concern, try to log slow queries at the same time.

More magical and maybe a little bit harder, is to automate testing whether all columns used in where / having / joins are indexed. Is maybe something which should belong to Jointed Php / Database Code Sniffer (tm) instead of unit tests, and is not very simple to implement, but once used greatly assures quality of code.

Another good advice, going from sad personal experience: always add tests for veryfing charsets, specially if you work with many different languages. ISO-8859-1 world is very small ;)

[1]: http://phplinq.codeplex.com/ LINQ

Not the answer you're looking for? Browse other questions tagged or ask your own question.