Reputation: 2091
So I began writing functional and unit tests in symfony 2 for my application. What should I actually test?
I separate functional from unit tests. Functional tests are for controllers only, unit tests are for the rest. With functional tests I test the HTML output for all scenarios users can go through in my application. But is it enough? I only test the response/request, not the code itself in the controllers, no database queries, etc.
And unit tests...should I test the controllers with it? Also, how about testing repositories? Should I test the output of every query? What if the database would lack the records?
Also, how about the mess that test create in the database? Is there a way to recover the database after the test to its previous state?
Sorry for having multiple questions, but this whole unittest subject is really a mess for me right now.
Thanks a lot!
Upvotes: 4
Views: 118
Reputation: 10483
Also, how about testing repositories? Should I test the output of every query? What if the database would lack the records?
I don't know if it's useful in your case, but you can test Doctrine repositories.
What if the database would lack the records?
Also, how about the mess that test create in the database? Is there a way to recover the database after the test to its previous state?
You can configure a different database for testing:
app/config/config_test.yml
doctrine:
# ...
dbal:
host: localhost
dbname: testdb
user: testdb
password: testdb
Symfony2 also has fixtures, which are a way to populate the database automatically.
Note: all the links lead to the Symfony2 official documentation.
Upvotes: 0
Reputation: 692
Let's separate the two: unit tests and functional tests.
Unit tests
These tests should test the smallest units in your program: functions (which are in classes in SF2). Unit tests should be fast so they test your business logic without using database or rendering anything.
You can achieve this by mocking services in dependency injection classes.
Functional tests
Most web applications are built from frontend instead of backend. I mean it has big twig templates and a few lines of controller and db queries. You cannot test it using unit tests but you want to be sure that certain elements appear in the browser.
Additionally, you want to test a set of features. Using functional testing you can simulate a few steps of manual testing. E.g. Load the login page, then fill the inputs, submit the form, then you can check the response message and the status of authentication.
But functional testing is really expensive. Loading a single page can take seconds which means that a whole set of functional tests in a medium sized project can take hours! For this reason you cannot use TDD (Test Driven Development) and functional testing together.
Conclusion
Upvotes: 2