How to safely write operations in symfony 2?

I want to create tests for all my CRUDs. But how do I set up a separate database for them? Is this the best way to go?

This is another question, but it is related: Should I run tests on a production server? Sometimes in different environments things can go wrong, so I think. But then I need the specified separate database, right?

Any tips?

+4
source share
3 answers

Running any tests on a production server is usually a bad idea (unless it is hardware that has not yet been started).

A Unit Test does not get into the database (or any other external system). So, to create a Unit Test, you need to remove the database dependency.

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

Two common solutions to your problem:

  • At the beginning of the test, restore the backup copy of the database containing the known data into a separate test database, then run your tests against it.

  • Using a “fixed” known test database at the beginning of each transaction test run, run the test, and then cancel the transaction to leave the database in the same known state.

(No. 1 is often preferable because the database in (2) may become “contaminated”).

+10
source

I agree with Mitch. I would add that you have to decide whether you want to run an integration test or unit test (or both, but not in one test). If you really want to do a unit test, do:

  • Your code has a “dependency” on an external database.
  • When testing modules, you will need to find a way to "fake" the database. You want to test the “unit”, which means one thing, not two or more things (ie your CRUD code and your connection to the database and the database itself).
  • Usually, you need to reorganize your code using something like dependency injection so that during unit testing you can fake things that your code depends on.

Unit testing is not just checking your code. This is actually the easy part. The more complex part makes your code verifiable.

I recommend going to http://artofunittesting.com/ and watch the free videos on the right side under the heading "Unit Testing." Forget it working in .NET, as these are important principles.

Then check out GoogleTechTalks Misko Hevery, where it explains why you want to do dependency injection.

Design Tech Talk Series Presents: OO Design for testing

Clean Code Codes - Testing Devices

(He also has a. Series of six GoogleTechTalks .)

+3
source

Today I had a similar problem, and I think I found a good solution.

  • Create a copy of your database (also create a new empty database).
  • Modify the config_test.yml file to change the database name.

An example of my test configuration (may differ depending on whether you have several db-s, etc.)

doctrine: dbal: dbname: test_db 
  • Update your database to display objects in your application by calling php app/console doctrine:schema:update --force --env=test (required if you just created a new db, and also every time you change the model applications).

Your application should now use the test database during unit tests. NB! Be sure to back up your database before messing with the active database.

However, as mentioned above, these are no longer Unit Tests, but integration tests.

+3
source

All Articles