Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start TDD in Django?

I'm new both to Django and unit testing but since I'm starting a project after a fair amount of time playing with the framework I decided to give unit testing a shot.

My enthusiasm aside, I am pretty lost as to how to plan the developing process. I did a little search in github for Django projects that use unit testing and I stumbled upon this. I see there are only tests on the "tagging" app, and it looks good but I have no idea how to break it down. Why the "models.py" file in the apps/tagging/tests/? The model classes there have nothing to do with the ones in apps/tagging/models.py ...

Any ideas on how to figure this out or find a good example on how to implement unit testing into a django environment ?

Also, I read in the docs that Django introduced unittest2 in 1.3 so would "Django 1.1 Testing And Debugging - by Karen M. Tracey" be a helpful reading or do you think it could cause some confusion?

Anyways, I appreciate any input in advance.

like image 501
la_f0ka Avatar asked Oct 11 '11 16:10

la_f0ka


People also ask

What is TDD in Django?

Test Driven Development (TDD) is an iterative development cycle that emphasizes writing automated tests before writing the actual code. The process is simple: Write your tests first. Watch them fail. Write just enough code to make those tests pass.


2 Answers

If I may plug my own tutorial, it covers how to do TDD in Django from the ground up. I cover the same steps as the "official" Django tutorial, except using TDD every step of the way. That includes full browser-automation testing with Selenium (which allows you to test behaviour from the end-user's point of view, including the possibility of including javascript later). I also show how to use the Django TestClient for unit testing...

http://tdd-django-tutorial.com

like image 86
hwjp Avatar answered Sep 25 '22 05:09

hwjp


Django does a great job of getting you started. They outline what should and shouldn't be tested, and how to use some of their built in test classes. https://docs.djangoproject.com/en/dev/topics/testing/

In addition i'd recommend django-nose test runner. It has a lot of great plugins.

I think general convention is to have a tests.py file in each app.

I personally will write at least 1 test for each function that I create. More depending on how complicated logic is. As the app develops these tests develop into regression tests for my project.

like image 41
dm03514 Avatar answered Sep 23 '22 05:09

dm03514