[Fixed]-Why does django not see my tests?

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?

41👍

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

👤pedro

20👍

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)

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.

5👍

You have to name it tests.py .

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.

👤Zarrie

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.

Leave a comment