鼻子没有找到Django测试 [英] Nose doesn't find Django tests

查看:150
本文介绍了鼻子没有找到Django测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



如何告诉Django鼻子我的测试是什么?



但在Googling之后,阅读博文和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_nose testap p INSTALLED_APPS

    • 添加 TEST_RUNNER ='django_nose.NoseTestSuiteRunner' code>。


  6. 运行 manage.py test


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

  nosetests --verbosity 1 
创建别名default的测试数据库...

------------- -------------------------------------------------- -------
Ran 0测试在0.000s

OK
销毁别名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是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/settings.pyc是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/urls.py是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/wsgi.py是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/models.py是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/models.pyc是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/views.py是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/tests.py是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/djangonosetest/testapp/tests.pyc是可执行的;跳过
nose.selector:INFO:/Users/Jens/Projects/Django/djangonosetest/manage.py是可执行的;跳过

鼻子肯定会找到我的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_email@example.com'),


MANAGERS = ADMINS

DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',#添加'postgresql_psycopg2','mysql','sqlite3'或'oracle'。
'NAME':'',#或使用sqlite3的数据库文件路径。
'USER':'',#不与sqlite3一起使用。
'PASSWORD':'',#不与sqlite3一起使用。
'HOST':'',#设置为localhost的空字符串。不适用于sqlite3。
'PORT':'',#设置为空字符串为默认值。不适用于sqlite3。
}
}

#此安装的本地时区。可以在这里找到选择:
#http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
#虽然并不是所有的操作系​​统都可以选择所有选项。
#在Windows环境中,必须将其设置为系统时区。
TIME_ZONE ='美国/芝加哥'

#此安装的语言代码。所有选择都可以在这里找到:
#http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE ='en-us'

SITE_ID = 1

#如果将其设置为False,Django将进行一些优化,以便
#加载国际化机器。
USE_I18N = True

#如果将其设置为False,Django将不会根据当前语言环境格式设置日期,数字和
#日历。
USE_L10N = True

#如果将其设置为False,Django将不会使用时区感知数据时间。
USE_TZ = True

#将保存用户上传文件的目录的绝对文件系统路径。
#示例:/home/media/media.lawrence.com/media/
MEDIA_ROOT =''

#处理从MEDIA_ROOT提供的媒体的URL。确保使用
#尾部斜线。
#示例:http://media.lawrence.com/media/,http://example.com/media/
MEDIA_URL =''

#静态文件的绝对路径也应该被收集。
#不要把任何东西放在这个目录下;在应用程序的static /子目录和STATICFILES_DIRS中存储静态文件
#。
#示例:/home/media/media.lawrence.com/static/
STATIC_ROOT =''

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'

#静态文件的附加位置
STATICFILES_DIRS =(
#将字符串放在这里,像/ home / html / static或C:/ www / django / static
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径


#知道如何在
#各个位置找到静态文件的finder类列表。
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles .finders.DefaultStorageFinder',


#使其独一无二,不与任何人共享。
SECRET_KEY ='ikh ^ t)49eincyww4 @ nq(o)go_129zdr87 * im00m ^ + h @ _n!w4ec'

#知道如何从各种来源导入模板的可调用列表。
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',
#取消注释下一行的简单点击保护:
#'django.middleware.clickjacking.XFrameOptionsMiddleware',


ROOT_URLCONF ='djangonosetest.urls'

#Django的runserver使用的WSGI应用程序的Python虚线路径。
WSGI_APPLICATION ='djangonosetest.wsgi.application'

TEMPLATE_DIRS =(
#将字符串放在这里,如/ home / html / django_templates或C:/ www / django / templates
#即使在Windows上也可以使用正斜杠
#不要忘记使用绝对路径,而不是相对路径


INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites'
'django.contrib.messages',
'django.contrib.staticfiles',
#取消注释下一行以启用admin:
#'django.contrib.admin' ,
#取消注释下一行以启用管理员文档:
#'django.contrib.admindocs',
'django_nose',
'djangonosetest.testapp',


#样本记录配置。通过此配置执行的唯一有形日志
#是在DEBUG = False时向每个HTTP 500错误发送电子邮件至
#的网站管理员。
#有关如何自定义日志配置的详细信息,请参阅http://docs.djangoproject.com/en/dev/topics/logging for
#。
LOGGING = {
'version':1,
'disable_existing_loggers':False,
'filters':{
'require_debug_false':{
' ()':'django.utils.log.RequireDebugFalse'
}
},
'处理程序':{
'mail_admins':{
'level' 'ERROR',
'filters':['require_debug_false'],
'class':'django.utils.log.AdminEmailHandler'
}
},
'logger':{
'django.request':{
'handlers':['mail_admins'],
'level':'ERROR',
'propagate'真的,
},
}
}

TEST_RUNNER ='django_nose.NoseTestSuiteRunner'



测试代码



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

 
此文件演示写入使用单元测试模块进行测试,当您运行manage.py test时,这些测试将通过


将此替换为适用于您的应用程序的更合适的测试。


从Django.test导入TestCase


类SimpleTest(TestCase):
def test_basic_addition(self):

测试1 + 1总是等于2.

self.assertEqual(1 + 1,2)

那么我如何告诉Django鼻子/鼻子我的测试是什么?



更新



如果发现一个有趣的Google网上论坛帖子和Stackoverflow的回答:





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

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

我也尝试过ux,gx和ox,无论如何,鼻子跳过。我觉得这是一个bug。该文件位于鼻子中,不可执行但仍然被跳过。



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

解决方案

我今天有同样的问题,



尝试:

  chmod ax djangonosetest / djangonosetest / testapp /*.py 


I am trying to use Django-nose in my current project, but I can't figure out how to get nose to run my tests. So I started a simple Django 1.4.1 project to get to know nose. But not even on this simple test project I could get it running.

Before I go on: I know there are a ton of similar questions on Stackoverflow, like for instance this one:

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

But after Googling around, reading blog posts and StackOverflow answers I still couldn't get it running.

How I set up my test project

  1. Create virtual environment.
  2. pip install django django-nose nose.
  3. Create project with django-admin.py startproject djangonosetest.
  4. Create an app manage.py startapp testapp
  5. Edit settings.py:

    • Set ENGINE to django.db.backends.sqlite3
    • Added django_nose, testapp to INSTALLED_APPS
    • Added TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'.
  6. Run manage.py test

But all I got was this output:

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

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

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

But at least the default test case should have been run.

When I run python manage.py test djangonosetest.testapp.tests:SimpleTest it'll run the test. But that seems a bit overkill if I had to do it for every test file I have. But it proves that the tests can be run.

When I ran manage.py test -v 3 (high verbosity level) this showed up:

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

The nose does definitely find my tests.py but is skipping it for some reason.

Folder structure

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'

The test code

The test code is the default TestCase Django creates.

"""
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)

So how do I tell Django-nose/nose where my tests are?

UPDATE

If found an interesting Google Groups post and Stackoverflow answer on the subject:

Both boil down to the fact that nose will skip executable files. That's why I get tests.py is executable; skipped. Problem is that the file is not executable:

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

I also tried it with u-x, g-x and o-x but nose skipped anyway. I get the feeling that this is a bug. The file is found in the nose, is not executable but still skipped.

A workaround is to use the --exe switch. Running manage.py test --exe runs the test. While it's better than the other workarounds I am not completely happy with that solution.

解决方案

I had the same issue today and I fixed it.

Try:

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

这篇关于鼻子没有找到Django测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆