Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing unit tests in Django / Python

I've not used Unit Tests before other than a quick introduction in a Uni course. I'm currently writing an application though and would like to teach myself TDD in the process. The problem is, I've no idea what to test or really how.

I'm writing a Django application, and so far have only created the models (and customised the admin application). This is how I've written the skeletons of my tests so far:

class ModelTests(TestCase):     fixtures = ['initial_data.json',]      def setUp(self):         pass      def testSSA(self):         ssa = SSA.objects.create(name="sdfsdf", cost_center=1111, street_num=8,                 street_name="dfsdfsf Street", suburb="sdfsdfsdf",                 post_code=3333)       def testResident(self):         pass      def testSSA_Client(self):         pass 

I planned to write a function to test each model within the ModelTests class. Is this a good way of writing tests? Also, what exactly should I be testing for? That creating a model with all of the fields completed works? That a half complete model fails? That any special cases are tested (like a null and is_required=False)? I've trust in the ORM, which as far as I'm aware is heavily tested, so I shouldn't need to test all of the methods should I?

What do I need to test for a web application written in Django/Python? Some examples would be nice.

like image 723
Josh Smeaton Avatar asked Jan 21 '09 12:01

Josh Smeaton


People also ask

How do I test a Django project?

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.

How do I test my Django site?

Django comes with a small set of its own tools for writing tests, notably a test client and four provided test case classes. These classes rely on Python's unittest module and TestCase base class. The Django test client can be used to act like a dummy web browser and check views.

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.


1 Answers

Is a function to test each model within the ModelTests class a good way of writing tests?

No.

What exactly should I be testing for?

  • That creating a model with all of the fields completed works?

  • That a half complete model fails?

  • That any special cases are tested (like a null and is_required=False)?

  • I've trust in the ORM, which as far as I'm aware is heavily tested, so I shouldn't need to test all of the methods should I?

Not much of that.

You might test validation rules, but that isn't meaningful until you've defined some Form objects. Then you have something to test -- does the form enforce all the rules. You'll need at least one TestCase class for each form. A function will be a scenario -- different combinations of inputs that are allowed or not allowed.

For each Model class, you'll need at least one TestCase class definition. TestCases are cheap, define lots of them.

Your model embodies your "business entity" definitions. Your models will have methods that implement business rules. Your methods will do things like summarize, filter, calculate, aggregate, reduce, all kinds of things. You'll have functions for each of these features of a model class.

You're not testing Django. You're testing how your business rules actually work in Django.

Later, when you have more stuff in your application (forms, views, urls, etc.) you'll want to use the Django unittest client to exercise each method for each url. Again, one TestCase per

like image 173
S.Lott Avatar answered Sep 24 '22 01:09

S.Lott