Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely unit-test write operations in Symfony 2?

I want to create tests for all my CRUD's. But how do I set a separate database for them? Is that the best way to go?

This is another question, but it is related: Should I run the tests in the production server too? Sometimes things can go wrong in different enviroments, so I guess I should. But then I need the mentioned separate database, right?

Any advice?

like image 225
HappyDeveloper Avatar asked Dec 04 '22 21:12

HappyDeveloper


1 Answers

Running any kind of tests on a production server is generally a bad idea (unless it's just the production hardware that hasn't been commisioned yet).

A Unit Test does not hit the database (or any other external system). So, in order to create a unit test you need to remove the dependency on the database.

What you are calling a 'unit test' is probably an integration test. Any test that utilises an external system (such as a database, file system etc.) is an integration test.

Two common solutions to your problem are:

  1. At the start of your test, restore a database backup containing known data to a separate test database, then perform your tests against it.

  2. Using a 'fixed' known test database, at the start of each test start a transaction, perform the test and then rollback the transaction to leave the database in the same known state.

(No. 1 is often preferable, as the database in (2) can become 'polluted').

like image 147
Mitch Wheat Avatar answered Dec 09 '22 03:12

Mitch Wheat