question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Path issues, dependent on global test case run vs specific test module run

See original GitHub issue

The setup for my current project is that the project root directory has a certain name, and there is a directory within the root, of the same name, which in turn contains all the app directories. So:

/path/to/project_name/ /path/to/project_name/project_name/ /path/to/project_name/project_name/app_1/ /path/to/project_name/project_name/app_2/ /path/to/project_name/project_name/app_3/

And so on. When I run:

REUSE_DB=1 ./manage.py test

It passes many tests, but fails whenever the test case calls reverse():

File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 476, in reverse
  return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 363, in _reverse_with_prefix
  possibilities = self.reverse_dict.getlist(lookup_view)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 276, in reverse_dict
  self._populate()
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 242, in _populate
  for pattern in reversed(self.url_patterns):
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 328, in url_patterns
  patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 323, in urlconf_module
  self._urlconf_module = import_module(self.urlconf_name)
File "<virtual_env_path>/local/lib/python2.7/site-packages/django/utils/importlib.py", line 41, in import_module
  __import__(name)

ImportError: No module named urls

Doing some debugging, it looks like it’s failing when trying to import “project_name.urls”. I realized that it was getting confused, because /path/to/project_name/urls.py doesn’t exist, but /path/to/project_name/project_name/urls.py does. If I rename the first “project_name” directory (which my teammates prefer not to do), it fixes it:

/path/to/blah/project_name/app_1 /path/to/blah/project_name/urls.py

And so forth.

Interestingly, the problem ALSO goes away (without renaming anything) when I run a specific test module:

REUSE_DB=1 ./manage.py test project_name.app_1.tests

I tried looking at the offending import line. The name it’s importing, and the sys.path at that point, are identical. Even in the sense of basic Python, I don’t understand how this is possible, but maybe there’s something I don’t understand about the importing process.

Is this a bug in Django Nose? In either case, is there something else I could try with the sys.path that I’m missing?

Thanks.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lingstercommented, Jan 7, 2018

I had a similar problem, when running unittests with nose in django 1.11, some of my tests were failing with: No Module Named xyz.urls my workaround was to add the following in settings.py, ie amended my ROOT_URLCONF from xyz.urls to xyz.xyz.urls

import sys
TESTING = sys.argv[1:2] == ['test']
if TESTING:
    ROOT_URLCONF = 'xyz.xyz.urls'
else:
    ROOT_URLCONF = 'xyz.urls'

hope this might be of use to others who have similar problems

0reactions
jwhitlockcommented, Jul 7, 2015

I believe this goes away with the Django 1.4 project layout. I’ll re-open if someone is still bit by this in 1.4+.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running a specific test case in Django when your app has a ...
Since Django 1.6 you can run a complete test case, or single test, ... TestCase subclass A inside a file tests.py under the...
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
The unittest module can be used from the command line to run tests from modules, classes or even individual test methods: python -m...
Read more >
Testing in Java & JVM projects - Gradle User Manual
All JVM testing revolves around a single task type: Test. This runs a collection of test cases using any supported test library —...
Read more >
Unit Tests, How to Write Testable Code, and Why It Matters
In this article, I will show that unit testing itself is quite easy; the real problems that complicate unit testing, and introduce expensive...
Read more >
Running PHPUnit tests - Drupal
Execute PHPUnit tests from the command line.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found