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