简体   繁体   中英

Django 1.6+ test not Discovering unit tests located in a tests sub-directory

So I have looked at no less than 10 different Stack Overflow questions related to Django not finding unit tests, and everyone one of them still doesn't fix my issue.

I am running Django 1.65 and am trying to get Django to discover my tests.

Here is my directory structure

project
  __init__.py
  my_app
    __init__.py
    tests
      __init__.py
      test_template_tags.py
  my_app2
    __init__.py
    tests
      __init__.py
      test_something_else.py  

The command I am trying at the command line inside the project directory is: python manage.py test my_app

The code inside test_template_tags.py is:

from django.test import TestCase

class MyTestClass(TestCase):

  def setUp(self):
      pass

  def tearDown(self):
      pass

  def test_something(self):
      self.assertTrue(True)

From a tip in another SO this command find my_app -name 'test*.py' was executed and shows all test files as one would expect.

When I open up the django shell inside of the project directory and attempt to import the test class, everything works as expected.

from my_app.tests.test_template_tags import MyTestClass

Lastly I copied and pasted the testing code into a tests.py file inside the app directory, and of course, it worked, but its not the solution I am looking for.

Is there some other obvious small thing that I'm missing here? I have all my __init__.py files in order, all test files have test_ in the front, and my_app is included in the INSTALLED_APPLICATIONS in settings.

In your __init__.py you need to import the other test modules in that directory in order for the test runner to discover them. If your structure looks like this:

project
  __init__.py
  my_app
    __init__.py
    tests
      __init__.py
      test_template_tags.py
  my_app2
    __init__.py
    tests
      __init__.py
      test_something_else.py  

Then my_app.tests.__init__.py would contain:

from test_template_tags import *

And my_app2.tests.__init__.py would contain:

from test_something_else import *

Then your tests will run using python manage.py test

在此处尝试任何答案之前,请确保您的测试文件按照惯例标记为test*.py

Late to the game but... My mistake was typo: " initi .py" instead of init.py in tests directory. I did not have to put any data in init .py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM