Category Archives: TDD

Django – a very simple unit test

Test Driven Development (TDD) is a methodology for developing programs where you first write the test, and then write just enough code to make your program pass all tests

TDD makes it far less likely that fixing one bug introduces a new one, or that a change reintroduces an old bug. It means that you can continuously test your code

Django has a built-in testing system, with two different approaches: doctests and unit tests. Here is how to create and run a very simple unit test

  1. Create a new app – python manage.py startapp testtest
  2. Edit …/testtest/tests.py, and add the following lines:
    class SimpleTest(TestCase):
    def test_basic_addition(self):
    self.failUnlessEqual(1 + 1, 2)
  3. python manage.py test
  4. This should show “OK”
  5. Change the last line to self.failUnlessEqual(1 + 1, 3)
  6. Re-run the test
  7. You should now get an error: AssertionError: 2 != 3