#19833 closed Bug (fixed)
Django's test framework fails when using unicode strings in settings.py
Reported by: | Danilo Bargen | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 1.4 |
Severity: | Release blocker | Keywords: | unicode, test runner, py3k |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
When specifying a non-standard test runner using an unicode string in settings.py (TEST_RUNNER = u'disccover_runner.DiscoverRunner'
) or when using from __future__ import unicode_literals
, the test
management command breaks.
This is the "faulty" code snippet (test/utils.py
):
def get_runner(settings, test_runner_class=None): if not test_runner_class: test_runner_class = settings.TEST_RUNNER test_path = test_runner_class.split('.') # Allow for Python 2.5 relative paths if len(test_path) > 1: test_module_name = '.'.join(test_path[:-1]) else: test_module_name = '.' test_module = __import__(test_module_name, {}, {}, test_path[-1]) test_runner = getattr(test_module, test_path[-1]) return test_runner
The problem is that a unicode string gets passed to __import__
s fromlist
argument. Apparently that won't work.
I'm not sure what the right solution is. Is it enough to just convert test_path[-1]
to a str
type? Are decoding errors possible in that case? Are there any helper functions in Django to do a conversion that works both in py2 and py3 (str() vs bytes())?
Bug tested on 1.4 and 1.5.
Change History (7)
comment:1 by , 12 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 12 years ago
So django.utils.encoding.force_str(test_path[-1])
would be an acceptable solution? If yes, I'll provide a pull request on Github.
comment:3 by , 12 years ago
Sorry for forgetting about the traceback, here it is:
$ cat config/settings.py | grep TEST_ TEST_RUNNER = 'discover_runner.DiscoverRunner' TEST_DISCOVER_TOP_LEVEL = PROJECT_ROOT TEST_DISCOVER_ROOT = PROJECT_ROOT TEST_DISCOVER_PATTERN = 'test*.py' $ ./manage.py test Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line utility.execute() File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/commands/test.py", line 49, in run_from_argv super(Command, self).run_from_argv(argv) File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/base.py", line 218, in run_from_argv parser = self.create_parser(argv[0], argv[1]) File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/core/management/commands/test.py", line 52, in create_parser test_runner_class = get_runner(settings, self.test_runner) File "/home/danilo/.virtualenvs/pcf/lib/python2.7/site-packages/django/test/utils.py", line 129, in get_runner test_module = __import__(test_module_name, {}, {}, test_path[-1]) TypeError: Item in ``from list'' not a string
comment:4 by , 12 years ago
I wonder if your test runner path is correct. Try to use force_str(test_path[-1])
and see if you are not getting an AttributeError
. In any case, I admit the AttributeError
is more friendly than the TypeError
, so let's go for this change (waiting for your tests, still).
comment:6 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Yes, see
django.utils.encoding.force_str