【问题标题】:Nose doesn't find Django tests鼻子没有找到 Django 测试
【发布时间】:2012-10-03 18:57:37
【问题描述】:

我正在尝试在我当前的项目中使用 Django-nose,但我不知道如何让鼻子运行我的测试。于是我开始了一个简单的Django 1.4.1项目来了解nose。但即使是在这个简单的测试项目上,我也无法运行它。

在我继续之前:我知道 Stackoverflow 上有很多类似的问题,例如这个:

How do I tell Django-nose where my tests are?

但是在谷歌搜索、阅读博客文章和 StackOverflow 答案之后,我仍然无法运行它。

我如何设置我的测试项目

  1. 创建虚拟环境。
  2. pip install django django-nose nose
  3. 使用django-admin.py startproject djangonosetest 创建项目。
  4. 创建应用程序manage.py startapp testapp
  5. 编辑 settings.py:

    • ENGINE 设置为django.db.backends.sqlite3
    • 已将django_nosetestapp 添加到INSTALLED_APPS
    • 已添加TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
  6. 运行manage.py test

但我得到的只是这个输出:

nosetests --verbosity 1
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...

但至少应该运行默认测试用例。

当我运行python manage.py test djangonosetest.testapp.tests:SimpleTest 时,它会运行测试。但是,如果我必须为我拥有的每个测试文件都这样做,那似乎有点矫枉过正。但它证明了测试是可以运行的。

当我运行manage.py test -v 3(高详细级别)时,出现了:

nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/settings.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/settings.pyc is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/urls.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/wsgi.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/models.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/models.pyc is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/views.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/tests.py is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/tests.pyc is executable; skipped
nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/manage.py is executable; skipped

鼻子确实找到了我的tests.py,但由于某种原因跳过了它。

文件夹结构

djangonosetest/
    djangonosetest/
        __init__.py
        settings.py
        testapp/
            __init__.py
            models.py
            tests.py
            views.py
        urls.py
        wsgi.py
    manage.py

settings.py

​​>
DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected too.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique and don't share it with anybody.
SECRET_KEY = 'ikh^t)49eincyww4@nq(o)go_129zdr87*im00m^+h@_n!w4ec'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'djangonosetest.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'djangonosetest.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'django_nose',
    'djangonosetest.testapp',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

测试代码

测试代码是 Django 创建的默认 TestCase。

"""
This file demonstrates writing tests using the unit test module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from Django.test import TestCase


class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)

那么我如何告诉 Django-nose/nose 我的测试在哪里?

更新

如果发现有关该主题的有趣的 Google 网上论坛帖子和 Stackoverflow 答案:

两者都归结为鼻子会跳过可执行文件的事实。这就是为什么我得到tests.py is executable; skipped。问题是文件不可执行:

----------+ 1 Jens  staff  383 13 Okt 00:01 tests.py

我也尝试过使用 u-x、g-x 和 o-x,但无论如何都跳过了鼻子。我觉得这是一个错误。该文件在鼻子中找到,不可执行但仍被跳过。

一种解决方法是使用 --exe 开关。运行 manage.py test --exe 运行测试。虽然它比其他解决方法更好,但我对该解决方案并不完全满意。

【问题讨论】:

  • 您的设置看起来不错...只是为了检查我们都犯的愚蠢的小错误...您在设置中拼写了 testapp 吗?或者其他类似的东西?
  • 这是myapp/tests.pymyapp/tests/__init__.py 的问题吗?我不太了解 Nose,也不知道它对 Django 运行测试的方式有何改变,但 Django 会在名为 tests.py 的文件中找到测试,而不是在名为 tests 的模块中。
  • @Colleen 我检查了拼写。没关系。
  • @dokkaebi nose.readthedocs.org/en/latest/finding_tests.html Nose 会寻找几乎任何匹配 *test* 的东西,听起来……Jens 的设置似乎很容易被 Nose 找到。
  • @dokkaebi 我遵循了 django-nose 文档github.com/jbalogh/django-nose 并没有说明更改任何测试。所以我假设你可以运行 manage.py test 并且一切正常。

标签: django nose django-nose


【解决方案1】:

我今天遇到了同样的问题,我已经解决了。

试试:

chmod a-x djangonosetest/djangonosetest/testapp/*.py

【讨论】:

【解决方案2】:

我认为可能是在 Django 1.4 中,如果您不指定项目根目录,则需要在已安装的应用程序中指定项目。不要只是'testapp',试试'djangonosetest.testapp'

https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-INSTALLED_APPS 看看他们的例子。

【讨论】:

  • 我已经认为这是一个潜在的错误来源。所以我将 testapp 从根目录(manage.py 所在的位置)移动到项目文件夹(djangonosetest)并更新了 INSTALLED_APPS。没有帮助。
  • 可以发布您当前的目录结构和设置文件吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 2018-09-02
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
相关资源
最近更新 更多