views:

283

answers:

3

I am a new to both Python and Django and I'm learning by creating a diet management site but I've been complete defeated by getting my unit tests to run. All the docs and blogs I've found say that as long as it's discoverable from tests.py, tests.py is in the same folder as models.py and you test class subclasses TestCase it should all get picked up automatically. This isn't working for me, when I run manage.py test <myapp> it doesn't find any tests.

I started with all my tests in their own package but have simplified down to just being in my tests.py file. The current tests.py looks like:

import unittest
from pyDietTracker.models import Weight
from pyDietTracker.weight.DisplayDataAdapters import DisplayWeight

class TestDisplayWeight(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testGetWeightInStone_KG_Correctly_Converted(self):
        weight = Weight()
        weight.weight = 99.8

        testAdapter = DisplayWeight(weight)
        self.assertEquals(testAdapter.GetWeightInStone(), '15 st 10 lb')   

I have tried it by sub classing the Django TestCase class aswell but this didn't work either. I'm using Django 1.1.1, Python 2.6 and I'm running Snow Leopard.

I'm sure I am missing something very basic and obvious but I just can't work out what. Any ideas?

Edit: Just a quick update after a comment

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'pyDietTracker',
 )

To get the tests to run I am running manage.py test pyDietTracker

+1  A: 

See http://docs.djangoproject.com/en/1.1/topics/testing/#id1

The most common reason for tests not running is that your settings aren't right, and your module is not in INSTALLED_APPS.

We use django.test.TestCase intead of unittest.TestCase. It has the Client bundled in.

http://docs.djangoproject.com/en/1.1/topics/testing/#testcase

S.Lott
Thanks, I've been over page and my module is in the INSTALLED_APPS
L2Type
+1  A: 

Does it work if you create a new app (manage.py startapp diet) and copy paste your test code to the tests.py created in the new app? (Also, add the new app to INSTALLED_APPS). Does it work?

Emil Stenström
+1  A: 

Worked it out.

It turns out I had done django-admin.py startproject pyDietTracker but not python manage.py startapp myApp. After going back and doing this, it did work as documented. It would appear I have a lot to learn about reading and the difference between a site and an app in Django.

Thank you for your help S.Lott and Emil Stenström. I wish I could accept both your answers because they are both helped alot.

Most important lesson Tests only work at the app level not the site level

L2Type