Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing test cases for django models

Half way through my current project, after suffering the pain of spending uncountable minutes on debugging, I have decided to adopt TDD. To start, I am planning to write a set of unit tests for each existing models. But for models that only have attributes defined (ie. no additional methods/properties) I am not sure what I need to test nor how.

class Product(models.Model):     name = models.CharField(max_length=50)     description = models.TextField(default='', blank=True)     retails = models.ManyToManyField(Retail, verbose_name='Retail stores that carry the product')     manufacturer = models.ForeignKey(Manufacturer, related_name='products')     date_created = models.DateTimeField(auto_now_add=True)     date_modified = models.DateTimeField(auto_now=True) 

Using Product as an example, what are the things about it that unit tests should cover? And how should ForeignKey and ManyToManyField be covered?

like image 671
tamakisquare Avatar asked Mar 05 '12 22:03

tamakisquare


People also ask

How do you write test cases for models in Django?

If your model has custom methods, you should test that, usually with unit tests. Same goes for custom views, forms, template tags, context processors, middleware, management commands, etc. If you implemented the business logic, you should test your aspects of the code.

What test framework does Django use?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.


2 Answers

This was an article I found helpful: A Guide to Testing in Django (archived link). Here is a good summary of what to test:

Another common setback for developers/designers new to testing is the question of 'what should (or shouldn't) I test?' While there are no hard & fast rules here that neatly apply everywhere, there are some general guidelines I can offer on making the decision:

  • If the code in question is a built-in Python function/library, don't test it. Examples like the datetime library.

  • If the code in question is built into Django, don't test it. Examples like the fields on a Model or testing how the built-in template.Node renders included tags.

  • If your model has custom methods, you should test that, usually with unit tests.

  • Same goes for custom views, forms, template tags, context processors, middleware, management commands, etc. If you implemented the business logic, you should test your aspects of the code.

So, for your example, there wouldn't really be anything to test until you write some custom functions.
In my opinion, testing ForeignKey and ManyToManyField links would fall under the second category (code built into Django), so I wouldn't test these, as you are really testing whether or not Django is functioning properly. If you have a method which creates an instance of your product, including foreign relationships and M2Ms, you could verify the data has been created, that would be testing your custom method, not Django functionality.

Using the TDD paradigm, the tests are built to verify business logic, and design requirements.

like image 172
Furbeenator Avatar answered Oct 03 '22 10:10

Furbeenator


My CS350 class TDD stipulated that it's best practice to test all accessors and mutators. So for a model, you would first write tests that call each assessor function and make sure that it returns the proper value.

For each function which changes a data field in the model, you would not only test the result of that data field in particular, but you would also test all of the other fields in the model instance to make sure that none of them were modified erroneously.

To restat:, if a model has fields a, b, and c, you would create an instance using your constructor, then asset that all three are set properly. Say there's another function, set_a(). You would assert that not only the value of 'a' has changed, but that the values of b and c remain unchanged.

like image 44
dahifi Avatar answered Oct 03 '22 10:10

dahifi