user1615069
user1615069

Reputation: 633

PHPUnit - database testing, how to manage that

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

Upvotes: 2

Views: 6877

Answers (2)

Matthew
Matthew

Reputation: 48284

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.

Upvotes: 8

JvdBerg
JvdBerg

Reputation: 21856

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.

Upvotes: 2

Related Questions