Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#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 Claude Paroz, 11 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Are there any helper functions in Django to do a conversion that works both in py2 and py3 (str() vs bytes())?

Yes, see django.utils.encoding.force_str

comment:2 by Danilo Bargen, 11 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 Danilo Bargen, 11 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 Claude Paroz, 11 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:5 by Danilo Bargen, 11 years ago

Yes, force_str solves the issue in this case.

comment:6 by Claude Paroz <claude@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 632361611c6386696dc525ad3aecf065e6ed98ee:

Fixed #19833 -- Fixed import parameter encoding in get_runner

Thanks Danilo Bargen for the report.

comment:7 by Claude Paroz <claude@…>, 11 years ago

In 41848b078a2f0402b23043da7cfb517d0a194aa3:

[1.5.x] Fixed #19833 -- Fixed import parameter encoding in get_runner

Thanks Danilo Bargen for the report.
Backport of 63236161 from master.

Note: See TracTickets for help on using tickets.
Back to Top