Writing good tests for Django applications Writing good tests for Django applications python python

Writing good tests for Django applications


I am not perfect in testing but a few thoughts:

Basically you should test every function, method, class, whatever, that you have written by yourself.

This implies that you don't have to test functions, classes, etc. which the framework provides.

That said, a quick check of your test functions:

  • test_detail_status_code and test_list_status_code:
    Ok to check whether you have configured the routing properly or not. Even more important when you provide your own implementation of get_absolute_url().

  • test_list_numer_of_items:
    Ok if a certain number of items should be returned by the view. Not necessary if the number is not important (i.e. arbitrary).

  • test_detail_template and test_list_template:
    Ok to check whether template variables are correctly set.

  • All the other functions: Not necessary.
    What your are basically testing here is whether the ORM worked properly, whether lists work as expected and whether object properties can be accessed (or not). As long as you don't change e.g. the save() method of a model and/or provide your custom logic, I would not test this. You should trust the framework developers that this works properly.

You only should have to test what you have (over)written.

The model classes are maybe a special case. You basically have to test them, as I said, if you provide custom logic. But you should also test them against your requirements. E.g. it could be that a field is not allowed to be null (or that it has to be a certain datatype, like integer). So you should test that storing an object fails, if it has a null value in this field.
This does not test the ORM for correctly following your specification but test that the specification still fulfills your requirements. It might be that you change the model and change some settings (by chance or because you forgot about the requirements).
But you don't have to test e.g. methods like save() or wether you can access a property.

Of course when you use buggy third party code... well things can be different. But as Django uses the test framework itself to verify that everything is working, I would assume it is working.

To sum up:
Test against your requirements, test your own code.

This is only my point of view. Maybe others have better proposals.


Break your tests into two completely separate kinds.

  • Model tests. Put these in your models.py file with your model. These tests will exercise the methods in your model classes. You can do simple CRUD (Create, Retrieve, Update, Delete) to simply prove that your model works. Don't test every attribute. Do test field defaults and save() rules if you're curious.

    For your example, create a TestNews class that creates, gets, updates and deletes a News item. Be sure to test the default date results. This class should be short and to the point. You can, if your application requires it, test various kinds of filter processing. Your unit test code can (and should) provide examples of the "right" way to filter News.

  • UI Tests. Put these in a separate tests.py file. These tests will test the view functions and templates.

    • Name the TestCase with the "condition" you're creating. "TestNotLoggedIn". "TestLoggedIn". "TestNoValidThis". "TestNotAllowedToDoThat". Your setUp will do the login and any other steps required to establish the required condition.

    • Name each test method with the action and result. "test_get_noquery_should_list", "test_post_should_validate_with_errors", "test_get_query_should_detail".