Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Test Web Code?

Does anyone have some good hints for writing test code for database-backend development where there is a heavy dependency on state?

Specifically, I want to write tests for code that retrieve records from the database, but the answers will depend on the data in the database (which may change over time).

Do people usually make a separate development system with a 'frozen' database so that any given function should always return the exact same result set?

I am quite sure this is not a new issue, so I would be very interested to learn from other people's experience.

Are there good articles out there that discuss this issue of web-based development in general?

I usually write PHP code, but I would expect all of these issues are largely language and framework agnostic.

like image 677
kaybenleroll Avatar asked Aug 05 '08 21:08

kaybenleroll


2 Answers

You should look into DBUnit, or try to find a PHP equivalent (there must be one out there). You can use it to prepare the database with a specific set of data which represents your test data, and thus each test will no longer depend on the database and some existing state. This way, each test is self contained and will not break during further database usage.

Update: A quick google search showed a DB unit extension for PHPUnit.

like image 97
Mike Stone Avatar answered Sep 22 '22 00:09

Mike Stone


If you're mostly concerned with data layer testing, you might want to check out this book: xUnit Test Patterns: Refactoring Test Code. I was always unsure about it myself, but this book does a great job to help enumerate the concerns like performance, reproducibility, etc.

like image 23
Chris Farmer Avatar answered Sep 20 '22 00:09

Chris Farmer