I'm using SimpleTest, a PHP-based unit testing framework. I'm testing new code which will handle storing and locating website comments from the database. I'm baffled based on how to structure the project to check the database access code.

I'm searching for any suggestions regarding guidelines for testing db code inside a PHP application. Good examples are actually great. Sites for more reading through are wonderful.

Thanks kindly. :)

You might like to allow PHP to produce and offer data to some temporary table/database and test on that table/database. Then it's not necessary to totally reset your database by hand. Most frameworks have database manipulation libraries to really make it simpler. It could take amount of time in the front-end but enables you to test considerably faster later whenever you make changes later.

I'd a nearby database devoted to unit testing having a known title and database username/password. The system tests were hard-coded to that particular location but different designers could override individuals variables when they wanted.

Then before each test you TRUNCATE each table. This really is much faster than shedding/creating tables or even the database itself.

Note: Do not truncate following the tests! This way if your test fails you will find the current condition from the database which frequently helps identify the issue.

When testing database code, it is good to continually have a similar database because the beginning point. Particularly if you do unit-testing (that we assume may be the situation here). One way would be to truncate all tables as Jason recommended, however i would rather possess some beginning data inside it. You realize, you usually must have some 'default' data that's contained in each database.

Also, some tests only seem sensible with full database. So, produce a special demonstration of database for individuals tests. I've about three or four different databases which i plug-in (just copy the files in) before you run some tests. Getting exactly the same beginning point every time guarantees repeatability.

So, just make a couple of database states which are good 'starting points' and back them up. Before you run each group of tests, restore appropriate database, after which run it.

I'd encourage you to definitely not attempt to test the database access code using SimpleTest.

Rather, produce a functional test for the application using, for instance, Selenium: record an evaluation situation when you begin from the known condition of the database adding a comment and appearance (using Selenium's claims) the content indeed can there be.

By doing this it's: - simpler to setup and keep - you verify not only the DB layer, however the presentation layer, too

Nevertheless, for those who have saved methods inside your DB, do use SimpleTest - I have tried it myself effectively. Essentially, create SimpleTests that begin with a recognized DB condition, then execute a couple of Card inserts/UPDATES, then run the saved proc and make certain the condition from the DB is exactly what you'd expect.

If you want to check against a database, I would suggest to import data/create tables before each test. This way, your database begins from the known condition on each test. As this is rather performance-costly, you can begin a transaction (so long as your rdms supports it) in setUp and rollback in tearDown. MySql (That is likely the RDBMS you are using), does not support nested transactions, therefore if the code under test uses transactions, you are able to encounter trouble. You will get for this, using savepoints. Setup a savepoint before testing, and rollback to savepoint after test.

I'll still maintain that if you want point about this, you should look at the chance that your results are attempting to let you know something ..

This is comparable to an issue I requested some time back:

How to Test Web Code,

and that i got a bit of good solutions.