Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#22945 closed Bug (invalid)

Creating a tests directory with __init__ breaks the testrunner

Reported by: gene.dolgin@… Owned by: nobody
Component: Testing framework Version: 1.7-rc-1
Severity: Normal Keywords: testrunner, tests, test
Cc: mmitar@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

In any given app, creating a directory called 'tests' with an init.py file and a number of test_*.py files which contain TestCases, the testrunner errors when trying to load the 'tests' directory

Traceback (most recent call last):
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_manage.py", line 127, in <module>
    utility.execute()
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_manage.py", line 102, in execute
    PycharmTestCommand().run_from_argv(self.argv)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_manage.py", line 89, in handle
    failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast)
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_runner.py", line 228, in run_tests
    extra_tests=extra_tests, **options)
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_runner.py", line 128, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Applications/PyCharm.app/helpers/pycharm/django_test_runner.py", line 92, in build_suite
    suite = super(DjangoTeamcityTestRunner, self).build_suite(*args, **kwargs)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/test/runner.py", line 95, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 204, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 265, in _find_tests
    raise ImportError(msg % (mod_name, module_dir, expected_dir))
ImportError: 'tests' module incorrectly imported from '/Users/gene/Python/dd_django/pacer/tests'. Expected '/Users/gene/Python/dd_django/pacer'. Is this module globally installed?

Change History (13)

comment:1 by gene.dolgin@…, 10 years ago

One more note -- changing the 'tests' directory to anything other than 'tests' resolves the issue.

comment:2 by Aymeric Augustin, 10 years ago

Description: modified (diff)

The problem seems to be that:

  • you have a structure like pacer/tests/tests and both tests directories are on PYTHONPATH, or
  • you have a structure like pacer/tests/tests.py and the the tests directory and the tests.py module are on PYTHONPATH.

Can you check that?

comment:3 by gene.dolgin@…, 10 years ago

The folder structure is the following: ~/dd_django/pacer/tests/ where pacer is an app directory with all of the normal stuff (models, admin, views, etc.)
Within /tests there are: init.py and test_models.py
The former is empty. The latter is a number of TestCase subclasses to test models within /pacer

settingy.py is, other than adding the pacer app and a few niceties, out-of-the-box standard.

I'm running in a virtualenv as well, created using virtualenvwrapper. This is standard as well, with no modifications to activate or activate_this.py.

The test runner is being initialized as:
/Users/gene/Python/venvs/dd_django/bin/python /Applications/PyCharm.app/helpers/pycharm/django_test_manage.py test pacer /Users/gene/Python/dd_django

which makes me think that it may be a pycharm problem...

comment:4 by Aymeric Augustin, 10 years ago

Can you reproduce the issue without PyCharm?

Have you tried reporting it to the PyCharm developers?

comment:5 by gene.dolgin@…, 10 years ago

Reproduced bug without any PyCharm interaction, running 'test' admin task from manage.py. Same error.

$ python manage.py test

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Users/gene/Python/venvs/dd_django/lib/python2.7/site-packages/django/test/runner.py", line 95, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 204, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 285, in _find_tests
    for test in self._find_tests(full_path, pattern):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 265, in _find_tests
    raise ImportError(msg % (mod_name, module_dir, expected_dir))
ImportError: 'tests' module incorrectly imported from '/Users/gene/Python/dd_django/pacer/tests'. Expected '/Users/gene/Python/dd_django/pacer'. Is this module globally installed?
Last edited 10 years ago by Tim Graham (previous) (diff)

comment:6 by Tim Graham, 10 years ago

Please use formatting for tracebacks (Aymeric and I edited your previous posts).

I couldn't reproduce this by modifying the tutorial like this:

mysite/
   polls/
       tests/
           __init__.py
           test_foo.py  (originally polls/tests.py)
$ python manage.py test
Creating test database for alias 'default'...
..........
----------------------------------------------------------------------
Ran 10 tests in 0.030s

comment:7 by Preston Timmons, 10 years ago

Do you have a tests.py or a tests.pyc in the pacer directory by any chance?

I can recreate the error with this structure:

tmp/
    __init__.py
    tests.py
    tests/
        __init__.py
       test_foo.py

And by running python -m unittest discover tmp.

comment:8 by gene.dolgin@…, 10 years ago

Yes! Very embarrassed. Can't believe I missed that in the directory tree!

Ran it again and was able to reproduce, and solve the problem.

So, not sure if this is then a bug or not, but that seems to be the issue.

Sorry to send this to the bug tracker without doing enough digging on my own.

comment:9 by Carl Meyer, 10 years ago

Resolution: invalid
Status: newclosed

Closing this issue; prestontimmons' ability to reproduce using python -m unittest discover tmp reveals that this is a behavior of Python's unittest module discovery, not an issue in Django.

comment:10 by Jimmy Merrild Krag, 9 years ago

Should there perhaps be a ticket for improving the error message this results in?
I just spent a lot of time figuring this out. I won't do that again, but to new developers, this could be a tremendous help the first time.
Perhaps this should be a general check every time Django tries to load models, test, or other. In debug mode only of course.

comment:11 by Tim Graham, 9 years ago

The error message comes from Python, not Django. Without diving into the unittest discovery code, it's not obvious to me if the error message could be improved.

comment:12 by Mitar, 9 years ago

I had the same problem, with the following structure:

missing/
  __init__.py
  test.py
  tests/
    __init__.py
    test_missing.py

comment:13 by Mitar, 9 years ago

Cc: mmitar@… added
Note: See TracTickets for help on using tickets.
Back to Top