2

我正在尝试调试我的测试,并且正在使用自定义测试运行程序。当我不将它与 manage.py 结合使用时,我可以让 pdb 正常工作。

在 mysite/blog/tests/models_tests.py 我上面有以下几行:

import pdb; pdb.set_trace()

症状是当我运行“fab t:i=1”时,测试开始运行。大概 set_trace 正在启动,我只是看不到输出,因为光标只是闪烁,然后在大约 10 秒后停止闪烁。如果我键入 exit,我会收到以下错误:

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

我猜这意味着 pdb 正在加载,我只是看不到输出。此外,如果我键入“c”,pdb 知道继续,并且测试通过。我只是看不到(pdb)提示。这是否意味着“-s”不能正常工作?

在 mysite/blog/tests/models_tests.py 我上面有以下几行:

import pdb; pdb.set_trace()

位于 mysite/mysite/testrunner.py:

from django_coverage.coverage_runner import CoverageRunner
from django_nose import NoseTestSuiteRunner

class NoseCoverageTestRunner(CoverageRunner, NoseTestSuiteRunner):
    pass

位于 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

位于 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')

最后,我从位于 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"
4

1 回答 1

0

好的,我发现了问题所在。

这篇文章告诉了我(在“以编程方式流鼻涕”部分)。

显然,当您以编程方式运行nose 时,就像我对fabric 所做的那样,nose 会捕获sys.stdout,因此打印语句将不起作用(显然pdb 正在使用它输出到控制台)。

当我在没有结构的命令提示符下输入此命令时,它会直接弹出 (pdb) 提示符:

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

如果有人对此问题有解决方法,我希望能够只运行织物,但至少我可以在此期间继续取得进展。

于 2013-10-15T21:42:48.317 回答