Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

python - Django test runner not finding tests

I am new to both Python and Django and I'm learning by creating a diet management site but I've been completely 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 your 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 it down to all tests 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 subclassing the Django TestCase class as well 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same issue but my problem was different.

I was getting Ran 0 tests, as OP.

But it turns out the test methods inside your test class must start with keyword test to run.

Example:

from django.test import TestCase


class FooTest(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def this_wont_run(self):
        print 'Fail'

    def test_this_will(self):
        print 'Win'

Also the files with your TestCases in them have to start with test.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...