What should I check?

So, I started writing functional and unit tests in symfony 2 for my application. What should I check?

I am decoupling functionality from unit tests. Functional tests are for controllers only, the rest are unit tests. With functional tests, I test the HTML output for all scripts that users can use in my application. But is that enough? I'm only testing the response / request, not the code itself in the controllers, no database requests, etc.

And unit tests ... do I need to test controllers? Also, what about testing repositories? Should I test the output of each request? What if there are no records in the database?

Also, what about the mess the test creates in the database? Is there a way to restore the database after a test to its previous state?

Sorry for having a few questions, but this whole user item is really a mess for me right now.

Thank you so much!

+3


source to share


2 answers


Separate two: block 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 are supposed to be fast, so they test your business logic without using a database or doing anything.

You can achieve this by mocking the services in the dependency injection classes.

Functional tests



Most web applications are built from frontend instead of backend. I mean it has large branch templates and multiple lines of controller and db requests. You cannot test it with unit tests, but you want to ensure that certain elements are displayed in the browser.

Also, you want to test a set of features. Using functional testing, you can simulate multiple manual testing steps. For example. Load the login page, then complete the tabs, submit the form, then you can check the response message and the authentication status.

But functional testing is really expensive. It can take a few seconds for a single page to load, which means 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

  • Unit Testing + Business Logic can be used in TDD, but you cannot fully test the entire system and output. But it can only be used to test business logic.
  • Functional testing + storage hardware can be used to test the system so that items appear on rendered pages and check completed processes, but they are awfully slow!
+2


source


Also, what about testing repositories? Should I test the output of each request? What if there are no records in the database?

I don't know if this is helpful in your case, but you can check the Doctrine repositories .

What if there are no records in the database?

Also, what about the mess the test creates in the database? Is there a way to restore the database after a test to its previous state?

You can set up another database for testing :



app/config/config_test.yml

doctrine:
    # ...
    dbal:
        host:     localhost
        dbname:   testdb
        user:     testdb
        password: testdb

      

Symfony2 also has fixtures that allow you to auto-populate the database.

Note: all links lead to the official Symfony2 documentation.

0


source







All Articles