Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test runner not finding tests

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.


If you're using a yourapp/tests package/style for unittests, make sure there's a __init__.py in your tests folder (since that's what makes it a Python module!).


I can run test for specific apps e.g.

python project/manage.py test app_name

but when I run

python project/manage.py test

0 tests was found

Figure out I need to run this in the same directory as manage.py

so the solution would be, cd to project directory and run

python manage.py test

In my case, the app folder itself was missing an __init__.py. This results in the behaviour that the test will be run with python manage.py test project.app_name but not with python manage.py test.

project/
  app_name/
    __init__.py   # this was missing

This also happens if you have a syntax error in your tests.py.


In my case, I typed def instead of class. Instead of

class TestDisplayWeight(TestCase): # correct!

I had

def TestDisplayWeight(TestCase): # wrong!