110

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

geckon
  • 8,316
  • 4
  • 35
  • 59
  • What command are you running to get these to execute? What's your `settings` look like? Is this application in the list of INSTALLED_APPS? – S.Lott Jan 10 '10 at 15:02
  • I've updated the question with my INSTALLED_APPS, is there another part of settings that is relevant? Thanks –  Jan 10 '10 at 15:15
  • What messages are you actually getting? – S.Lott Jan 10 '10 at 15:25
  • I don't get an error message it just says 0 tests run. I'm away from the computer atm but when I try to name the test class directly I get an error along the lines of not a suitable test case. I will post it up exactly when I get back. –  Jan 10 '10 at 16:17
  • @L2Type: You can't easily name the TestCase class directly. You name the app, the Django runner does it's discovery thing by looking in models.py and tests.py – S.Lott Jan 10 '10 at 22:42
  • My tests run fine in my source tree, but in my distribution tree where all files are .pyc, I get `Ran 0 tests in 0.000s` even though the tests.pyc files are there. Is there a way to discover .pyc files? Using `--pattern *.pyc` didn't work. If this is a known limitation, that's fine but I'd like to see that in the docs. – JimB Jul 16 '19 at 19:20
  • you should use `django TestCase` class https://docs.djangoproject.com/en/3.1/topics/testing/overview/#writing-tests – Marek Kamiński Nov 19 '20 at 14:56

26 Answers26

232

I had the same issue but my root cause 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.

Eduardo
  • 22,574
  • 11
  • 76
  • 94
  • 15
    if it still does not work, check if the filename matches this pattern: `test*.py` This is how django picks the test suit files. [django doc](https://docs.djangoproject.com/en/1.7/topics/testing/overview/#running-tests) – linqu Mar 27 '15 at 10:56
  • For other readers, there is also a change to use Django's TestCase instead of unittest.TestCase – carlosayam Sep 07 '18 at 06:41
  • That was the thing that I missed. Thanks! – Razique Jul 04 '21 at 00:04
  • 1
    This tip worked for me . I didn't prefix the functions with "test_" – Ihonosetale Oseghale Oct 14 '21 at 13:06
  • WHAT I am stuck in this since days, reading tons of documentation, am I completely stupid or this is not highlighted in django docs at all? I expected this to be like: TEST.PY 101: let your function start with test_ thanks for the precise and very helpful answer!! – Carlo Mar 29 '22 at 10:51
151

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!).

FlyingZipper
  • 701
  • 6
  • 26
David Lam
  • 4,689
  • 3
  • 23
  • 34
  • 6
    Yes! If you use a custom folder for the desired app to run tests (e.g: `my_apps/app_name/`", then you should create `__init__.py` file in `my_apps/`. –  May 31 '20 at 11:10
  • For me, when I add the `__init__.py` file in each level of subfolders `my_app/tests/app/models` tests was found. – Sergio Belevskij Dec 30 '22 at 17:52
48

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
codingrhythm
  • 1,456
  • 14
  • 26
  • Same thing happened to me (https://github.com/agconti/cookiecutter-django-rest/blob/master/%7B%7Bcookiecutter.github_repository_name%7D%7D/fabfile.py) – Julien Le Coupanec Oct 07 '17 at 15:34
  • 3
    This helped me, thanks. It appears that test runner discovers current working directory, which must itself be a Python package (https://docs.djangoproject.com/en/dev/topics/testing/overview/#running-tests). Alternatively, you can invoke tests like this `python project/manage.py test project/` – rubick Jan 11 '20 at 14:26
  • 1
    Really simple solution, but I if you now a workaround to user `python src/manage.py test` and run all tests please share and update the aswer. Thanks a lot the solution worked for me. – allexiusw Apr 30 '21 at 16:09
  • 1
    "cd to project directory and run" - thanks ! – Librain Dec 27 '22 at 10:43
27

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
Thijs
  • 647
  • 5
  • 15
  • 1
    Thanks! You helped me find my problem which was that I created a folder 'tests' to hold my test scripts, but forgot to add the file __init__.py – Little Brain Aug 28 '19 at 17:26
  • Same thing here: I had `project/apps/core`, with no `__init__.py` in `project/apps`. – abcdn Mar 20 '20 at 00:37
  • Thanks for posting this - I created a `tests` subfolder but forgot to include an `__init__.py` underneath that direction. Adding it did the trick! – Ezra_Bender Mar 29 '20 at 23:40
  • Thanks. Struggled with this problem for very long. Finally adding `__init__.py` to app worked. Initially I was just checking `__init__.py` in the tests folder. – Pooja Oct 05 '20 at 08:03
18

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

class TestDisplayWeight(TestCase): # correct!

I had

def TestDisplayWeight(TestCase): # wrong!
Flimm
  • 136,138
  • 45
  • 251
  • 267
18

This may also happen when you are using a tests module instead of a tests.py. In this case you need to import all the test classes into the __init__.py of your tests module, e.g.

tests/
    __init__.py
    somemodule.py

In your __init__.py you now need to import the somemodule like this:

from .somemodule import *
devsnd
  • 7,382
  • 3
  • 42
  • 50
  • 1
    this was my problem and fixed it thanks to you. regards! – dhargan Aug 15 '18 at 21:57
  • 2
    This may work but I don't think it's the right fix as I have another python project that does not require this import to run tests. – Matt Nov 02 '18 at 00:58
  • That was also my problem. Thank you very much! – dnsiv Nov 30 '18 at 17:34
  • I am using scripts to run my tests. A few months and a new computer later, tests unexpectedly broke. This fixed it. No idea what the difference was. Maybe a different python version. Maybe a newer Django... – Chris Jul 16 '19 at 12:57
16

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

Daniel Gasull
  • 465
  • 6
  • 13
13

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

8

I know I am late at this point but I also had trouble with

Found 0 test(s).
System check identified no issues (1 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

I have followed all the steps still I was facing the same issue. My fix was I missed __init__.py file in the test directory. Adding the file and re-running the command solved my issue.

HIGHLIGHTING IT A BIT:

Make sure you have __init__.py file

Abdullah Mujahid
  • 888
  • 1
  • 12
  • 34
7

in my case, I miss starting my functions name with test_ and when run my test with :

python manage.py test myapp

result was :

Creating test database for alias 'default'...
System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...

it seems Django cannot recognize my tests!

then i change myproject/myapp/test.py file like this :

from django.test import TestCase
# Create your tests here.
class apitest(TestCase):

    def test_email(self):
        pass
    def test_secend(self):
        pass

after that result is:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 2.048s

OK
Destroying test database for alias 'default'...
Benyamin
  • 400
  • 4
  • 11
6

Here's another one that I've just had: Check your test files are not executable. My virtualbox auto-mounted them as executable so the test discover missed them completely. I had to add them into the relevant __init__.py files before someone told me what the issue was as a work around, but now they are removed, and non-executable and everything _just_works.

David Boshton
  • 2,555
  • 5
  • 30
  • 51
  • When running tests inside a docker image on Docker Desktop, I was having the same problem. The solution came from this answer. I had lots of test_*.py inside /{app}/tests/ folders. When importing this tests in /{app}/tests/__init__.py solved. Thanks a lot for that insight. – Pablo Abdelhay Apr 08 '21 at 22:32
3

I had this happen when I had a test.py file, and a test/ subdirectory, in the same Django app directory. I guess I'm confusing python or the test runner whether I'm looking for a test module (in test.py) or a test package (in test/ subdir).

Nils
  • 5,612
  • 4
  • 34
  • 37
1

If you are trying to run a test in your main app, such as my_app/my_app/ make sure you have the following checked:

  1. App name is listed in INSTALLED_APPS inside settings.py
  2. Make sure your DATABASES['default'] inside settings.py is set properly
  3. The App has a models.py (even if you are not using one, at least an empty one is required to be there)
Nilesh
  • 20,521
  • 16
  • 92
  • 148
1

See https://docs.djangoproject.com/en/1.11/topics/testing/overview/

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 instead of unittest.TestCase. It has the Client bundled in.

https://docs.djangoproject.com/en/1.11/topics/testing/tools/#django.test.TestCase

SGaist
  • 906
  • 8
  • 33
  • 109
S.Lott
  • 384,516
  • 81
  • 508
  • 779
1

Using this syntax

python manage.py test

instead of ./manage.py test solved this problem for me.

1

I had the same problem, turns out I saved the __init__ as a python file but it did not put .py at the end of its name. I added .py at the end of file's name. it was ok afterwards

(in other words, I had created __init__ instead of __init__.py )

0

In the same file, I had two test classes with the SAME NAME, and of course this prevented all tests from running.

Rick Graves
  • 517
  • 5
  • 11
0

I created a method called run in my test class which turned out to be a very bad idea. Python could see that I wanted to run tests, but was unable to. This problem is slightly different, but the result is the same - it made it seem as if the tests couldn't be found.

Note that the following message was displayed: You want to run the existing test: <unittest.runner.TextTestResult run=0 errors=0 failures=0>

Qarj
  • 21
  • 3
0

Run --help and look for verbose. Crank it to max.

I ran manage.py test --verbose and found this debug output right at the top:

>nosetests --with-spec --spec-color --verbose --verbosity=2.

Oh look! I had installed and forgotten about nosetests. And it says --verbosity=2. I figured out that 3 is the max and running it with 3 I found lots of these:

nose.selector: INFO: /media/sf_C_DRIVE/Users/me/git/django/app/tests/test_processors.py is executable; skipped

That gave me the right hint. It indeed has problems with files having the x-bit set. However, I was thrown off the track as it had run SOME of the tests - even though it explicitly said it would skip them. Changing bits is not possible, as I run the tests in a VM, sharing my Windows NTFS-disk. So adding --exe fixed it.

Chris
  • 5,788
  • 4
  • 29
  • 40
0

Had the same issue and it was because my filename had a - char in its name. My filename was route-tests.py and changed it to route_tests.py

O-9
  • 1,626
  • 16
  • 15
0

If you encounter this error after upgrading to Django 3, it might be because the -k parameter changed meaning from:

-k, --keepdb          Preserves the test DB between runs.

to

-k TEST_NAME_PATTERNS   Only run test methods and classes that match the pattern or substring. Can be used multiple times. Same as unittest -k option.

So just replace -k with --keepdb to make it work again.

Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
0

Django engine searches files and folders with test_ prefix (inside of a tests folder). In my case it was simple solution.

So, be sure to checkout file/folder name starts with it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mastermind
  • 454
  • 3
  • 11
0

I had the same problem, it was caused by init.py at the project root - deleted that, all tests ran fine again.

Doha Simon
  • 324
  • 3
  • 9
0

This is late. but you can simply add your app name in front of importing models. like

    from myapp.models import something

This works for Me.

Winston
  • 55
  • 5
0

In Django, methods in test classes must start with "test" keyword. for example test_is_true(). methods name like is_true() will not execute.

0

In my i resolve this, using python manage.py test apps and works, because i'm creating my apps inside folder apps, not at the same level of project.


    .
    ├── HFarm
    ├── .vscode
    ├── apps/
    │   └── accounts/
    │       ├── migrations
    │       ├── templates
    │       ├── tests/
    │       │   ├── __init__.py
    │       │   └── testAccount.py
    │       ├── utils/
    │       │   ├── __init__.py
    │       │   └── mail.py
    │       ├── __init__.py
    │       ├── admin.py
    │       ├── apps.py
    │       ├── forms.py
    │       ├── models.py
    │       ├── urls.py
    │       └── views.py
    ├── backend/
    │   ├── __init__.py
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── manage.py

#tests/init.py


    from .testsAccount import *
Igor Lopes
  • 11
  • 1