I get the idea behind unit testing however am trying to think of a clean simple way do it when it requires database functionality. For instance I have a function that return result based off a database select query. Would the database alway have to remain the same for me to properly see that only the correct results are being returned. What is the best way to perform unit testing (in PHP) when it requires database inactivity (whether it be read, write, update, or delete)?
views:
117answers:
3
+3
A:
There is a whole chapter on that in the PHPUnit manual:
- http://www.phpunit.de/manual/current/en/database.html and also worth reading
- http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/
It's like with everything else when Unit-Testing. Create a known state and test your code against it to see if it returns the expected results.
Gordon
2010-08-24 19:16:29
+1
A:
Personally, I create a dummy testing database and populate it with a known data set for each testing run (I do that right in the setUp
functions). Then the tests run against that data set, and then it's removed on tearDown
...
Now, this is more of a Integration test than an Unit test (And personally I treat it differently from a unit test, run on its own schedule along with other integration tests), but it's still quite useful.
ircmaxell
2010-08-24 19:19:05
+1 for 'more of a Integration test than an Unit test' - I'm currently in the process of determining which of our 'unit' tests have side-effects on a database and re-categorizing them as integration tests so that we can run the proper unit tests more frequently, and save the slow running integration tests for overnight builds.
Alex Humphrey
2010-08-24 19:59:47
Frankly, it's clear that the asker was talking about *functional* tests, which should indeed have something like your database.
Josh Smith
2010-09-13 05:01:23