Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are standard/best practices for creating unit tests for functionality using databases?

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)?

like image 906
ryanzec Avatar asked Aug 24 '10 19:08

ryanzec


People also ask

Can unit tests use databases?

To test an application it is not enough to use unit tests. You must also perform functional testing and regression testing. Database access falls outside the scope of unit testing, so you would not write unit tests that include database access.


1 Answers

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.

like image 110
Gordon Avatar answered Sep 22 '22 12:09

Gordon