2

Well, as in the title, I can think of three ways to manage testing the database output (I'm using ORM in my application, and PDO in unit tests). Which is the best one? How do you handle this?:

  1. Create data set with the data I want specifically for testing, and change the code so that it reads xml instead of the ORM arrays (in tests classes).

  2. Create setUp() method, set the attribute containing the ORM array, and work on that.

  3. Same as the second point, but with another database, created specifically for testing
1
  • 2
    Have you tried using database transaction begin / rollback in setUp / tearDown? I would simply set up a test db (that could be a sqlite database file too) with the same schema as production. Commented Sep 3, 2012 at 19:03

2 Answers 2

8

You may want to read PHPUnit's chapter on database testing.

I use PDO via my own thin wrapper that supports nested transactions via save points. In the bootstrap, I create a test database with the entire structure of production along with very basic seed data. During each setUp() and tearDown() I begin a transaction and then roll back.

Each test imports the subset of data it needs from raw SQL files. From there, the ORM is tested using real inserts, etc. But this only works because my DB driver supports nested transactions. If the tests begin/commit and check for success/failure, everything still works.

If you don't have nested transaction support, you could set up and tear down the entire database on each test, but that will be slower. And note that you don't always have to test against a real database... it depends on what sort of things you are testing.

Sign up to request clarification or add additional context in comments.

Comments

2

In my tests, I use a test database.

MySQL has a few test databases on their site. I find the Sakila rather difficult, so I use the World database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.