2

I'm trying to debug my tests and I am using a custom testrunner. I am able to get pdb to function just fine when I am not using it in conjunction with manage.py.

in mysite/blog/tests/models_tests.py I have the following lines on top:

import pdb; pdb.set_trace()

The symptoms are that when i run "fab t:i=1", the test begins to run. Presumably set_trace is kicking in and I just cannot see the output because the cursor just blinks and then stops blinking after about 10 seconds. If I type exit, I get the following error:

Traceback (most recent call last):
  File "/home/jdp/Documents/www/env/local/lib/python2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
    addr.filename, addr.module)
  File "/home/jdp/Documents/www/env/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/home/jdp/Documents/www/env/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/jdp/Documents/www/mysite/blog/tests/models_tests.py", line 2, in <module>
    from blog.tests.factories import BlogFactory, CommentFactory
  File "/home/jdp/Documents/www/mysite/blog/tests/models_tests.py", line 2, in <module>
    from blog.tests.factories import BlogFactory, CommentFactory
  File "/usr/lib/python2.7/bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python2.7/bdb.py", line 67, in dispatch_line
    if self.quitting: raise BdbQuit
BdbQuit

I'm guessing this means that pdb is loading, and I just can't see the output. Also, if I type "c" pdb knows to continue, and the test runs through. I just can't see the (pdb) prompt. Does this mean that "-s" is not working properly?

in mysite/blog/tests/models_tests.py I have the following lines on top:

import pdb; pdb.set_trace()

Located in mysite/mysite/testrunner.py:

from django_coverage.coverage_runner import CoverageRunner
from django_nose import NoseTestSuiteRunner

class NoseCoverageTestRunner(CoverageRunner, NoseTestSuiteRunner):
    pass

located in mysite/mysite/settings.py:

EXTERNAL_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'whoosh',
    'haystack',
    'django.contrib.markup',
    'django.contrib.gis',
    'world',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
]

INTERNAL_APPS = [
    'blog',
]

INSTALLED_APPS = EXTERNAL_APPS + INTERNAL_APPS

located in mysite/mysite/test_settings.py:

# flake8: noqa
"""Settings to be used for running tests."""
import os

from mysite.settings import *


INSTALLED_APPS.append('django_nose')
INSTALLED_APPS.append('django_jasmine')

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'website_db',                     
        'USER': 'xxx',
        'PASSWORD': 'xxx',
        'HOST': '',                      
        'PORT': '',                    
    }
}


EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
SOUTH_TESTS_MIGRATE = False

TEST_RUNNER = 'mysite.testrunner.NoseCoverageTestRunner'
COVERAGE_MODULE_EXCLUDES = [
    'tests$', 'settings$', 'urls$', 'locale$',
    'migrations', 'fixtures', 'admin$', 'django_extensions',
]
COVERAGE_MODULE_EXCLUDES += EXTERNAL_APPS
COVERAGE_REPORT_HTML_OUTPUT_DIR = os.path.join(__file__, '../../coverage')

MEDIA_ROOT = os.path.join(__file__, '../../test_files/media')

and finally, I'm running these tests from fabric, located in mysite/fabfile.py:

from fabric.api import local, lcd, settings

def t(i=0):
    """
    Your central command for running tests. Call it like so:

        fab t
        fab t:i=0
    """
    command = 'python manage.py test -v 2 -s --settings=mysite.test_settings'
    if int(i) == 0:
        command += " --exclude='integration_tests' --exclude='jasmine_tests'"
    with settings(warn_only=True):
        result = local(command, capture=True)
        if result.failed:
            print result.stderr
            with lcd('test_files/media'):
                local('rm -rf image')
                print "Removed Test Folder Regardless of Failure"
        else:       
            with lcd('test_files/media'):
                local('rm -rf image')
                print "Tests OK. Deleting Images Folder"
Jon Poler
  • 183
  • 12

1 Answers1

0

Ok, I figured out the problem.

This article tipped me off (under the section "Running nose programatically").

Apparently when you run nose programatically, as I was doing with fabric, nose captures sys.stdout so print statements will not work (which apparently pdb is using to output to the console).

When I enter this command in command prompt without fabric, it pops right into the (pdb) prompt:

./manage.py test -s --settings=mysite.test_settings

If anyone has a workaround for this issue, I would prefer to be able to just run fabric, but at least I can keep making progress in the meantime.

Jon Poler
  • 183
  • 12