Why does django not see my tests?
Asked Answered
M

7

12

I've created test.py module, filled with

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from forum.models import *

class SimpleTest(TestCase):


    def setUp(self):
        u = User.objects.create_user("ak", "[email protected]", "pwd")
        Forum.objects.create(title="forum")
        Site.objects.create(domain="test.org", name="test.org")

    def content_test(self, url, values):
        """Get content of url and test that each of items in `values` list is present."""
        r = self.c.get(url)
        self.assertEquals(r.status_code, 200)
        for v in values:
            self.assertTrue(v in r.content)

    def test(self):
        self.c = Client()
        self.c.login(username="ak", password="pwd")

        self.content_test("/forum/", ['<a href="/forum/forum/1/">forum</a>'])
        ....

and placed it in folder with my application. When i run tests by

python manage.py test forum

after creating the test database i get an answer "Ran 0 tests in 0.000s"

What am i doing wrong ?

P.S. Here is my project hierarchy:

MyProj:
    forum (it's my app):
        manage.py
        models.py
        views.py
        tests.py
        ...

I renamed test.py to tests.py. Eclipse got that this module is with tests, but answer is still "Ran 0 tests in 0.000s"

Marx answered 7/9, 2010 at 14:10 Comment(0)
P
3

There is something not quite right if you are getting the same result after renaming the file to tests.py. How are you running the tests? Are you doing so from the command line or have you set up a custom run target using Eclipse? Please try it from the command line if you haven't already.

Also fire up Django shell (python manage.py shell) and import your tests module.

from MyProj.forum.tests import SimpleTest

Does the import work correctly?

Pyrazole answered 7/9, 2010 at 16:40 Comment(5)
I tryed "python manage.py shell" and "from MyProj.forum.tests import Test_forum" (i renamed test class), and it didn't work. But "import MyProj.forum.tests" and "from MyProj.forum.tests import *" passed without errors.Marx
Looks like something is amiss. Try this: from MyProj.forum import tests followed by dir(tests). See if your class name is present in the output of the dir command.Pyrazole
As it was expected: <code> >>> dir(tests) ['builtins', 'doc', 'file', 'name', 'package', 'path'] </code> So, there is no my test class in tests.py. What did i missed ?Marx
This is interesting: there is no Test_forum in that list. It seems to me that you have a directory named tests in there. This would explain why from MyProj.forum.tests import Test_forum fails but import MyProj.forum.tests succeeds. Can you check if this is the case?Pyrazole
Yes, you were right. I tryed to test my app with nose and urllib/urllib2 first, and for this purpose i've created package tests. I removed this package and django test tool got my test class in tests.py. Thank you, Manoj Govindan. You saved me !Marx
L
41

You´ll need to use the prefix test_ for each test method.

Leftward answered 7/10, 2011 at 18:29 Comment(1)
Me too, on Django 1.4. Thx!Rhoades
K
21

Summary

  1. Try running for your app only:

    python manage.py test YOUR_APP
    
  2. Check in your settings.py file if YOUR_APP is in INSTALLED_APPS config.

  3. Test method should start with test_, e.g.:

    def test_something(self):
        self.assertEquals(1, 2)
    
  4. If you are using a directory called tests instead of the tests.py file, check if it has a __init__.py file inside it.

  5. If you are using a tests directory, remove tests.pyc and tests.pyo files. (__pycache__ dir for Python3)

Koenraad answered 23/10, 2014 at 16:49 Comment(3)
In addition to 4, don't forget to remove the __pycache__ folderOzellaozen
My Problem was 2. Thanks.Avaunt
FYI: Updated formatting. The underscores in filenames were rendered as bold in Markdown without backticks (codeblocks).Forbes
D
7

Try renaming your method test to something like test_content.

I believe the test runner will run all methods named test_* (see the python docs for organising test code. Django's TestCase is a subclass of unittest.TestCase, so the same rules should apply.

Dorothi answered 7/9, 2010 at 20:16 Comment(2)
I have renamed my test class to Test_forum. But it didn't helpMarx
Don't rename the class, rename the method. It might be case sensitive so try test_forum.Dorothi
D
5

You have to name it tests.py .

Dimorph answered 7/9, 2010 at 14:14 Comment(1)
I renamed test.py to tests.py. Eclipse got that this module is with tests, but answer is still "Ran 0 tests in 0.000s"Marx
S
4

After some time spent in searching I did not find anyone suggesting this, so I will share it as a late answer. In my case I have my manage.py in the root directory eg

.
...
├── dir
│   ├── project_name
│   ├── manage.py
│   ├── media
│   ├── app1
│   ├── static
│   ├── staticfiles
│   ├── templates
│   └── app2
...

so I found that the test command has the option to provide project which tests' to run. In my case I had to do this

python project_name/manage.py test ./project_name/

This successfully ran my tests.

Somnambulism answered 10/10, 2020 at 23:3 Comment(0)
P
3

There is something not quite right if you are getting the same result after renaming the file to tests.py. How are you running the tests? Are you doing so from the command line or have you set up a custom run target using Eclipse? Please try it from the command line if you haven't already.

Also fire up Django shell (python manage.py shell) and import your tests module.

from MyProj.forum.tests import SimpleTest

Does the import work correctly?

Pyrazole answered 7/9, 2010 at 16:40 Comment(5)
I tryed "python manage.py shell" and "from MyProj.forum.tests import Test_forum" (i renamed test class), and it didn't work. But "import MyProj.forum.tests" and "from MyProj.forum.tests import *" passed without errors.Marx
Looks like something is amiss. Try this: from MyProj.forum import tests followed by dir(tests). See if your class name is present in the output of the dir command.Pyrazole
As it was expected: <code> >>> dir(tests) ['builtins', 'doc', 'file', 'name', 'package', 'path'] </code> So, there is no my test class in tests.py. What did i missed ?Marx
This is interesting: there is no Test_forum in that list. It seems to me that you have a directory named tests in there. This would explain why from MyProj.forum.tests import Test_forum fails but import MyProj.forum.tests succeeds. Can you check if this is the case?Pyrazole
Yes, you were right. I tryed to test my app with nose and urllib/urllib2 first, and for this purpose i've created package tests. I removed this package and django test tool got my test class in tests.py. Thank you, Manoj Govindan. You saved me !Marx
C
3

I had tried all these things but I neglected to add the __init__.py file in the tests directory that I created to hold all my tests and Django couldn't find it.

Cleptomania answered 28/11, 2016 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.