Python 为什么django nose要运行两次测试?

Python 为什么django nose要运行两次测试?,python,django-models,nose,django-testing,django-nose,Python,Django Models,Nose,Django Testing,Django Nose,我有以下型号: class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.question INSTALLED_APPS = ( 'django.contrib.auth',

我有以下型号

class Poll(models.Model):

    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
             return self.question
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',
    'polls',
    'django_nose',

)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--with-coverage',
    '--cover-package=polls',
    '--with-progressive',
    '--verbosity=0',
    '--with-fixture-bundling',
] 
以及下面的测试

from polls.models import Poll
from django.test import TestCase
from django.utils import timezone

class PollModelTest(TestCase):

    def test_poll_save(self):
        q = "What is the best OS?"
        pd = timezone.now()
        p = Poll(question=q,
                 pub_date=pd)
        p.save()
        polls = Poll.objects.all()
        self.assertEquals(polls.count(), 1)
        self.assertEquals(polls[0].question, q)
以及以下设置

class Poll(models.Model):

    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
             return self.question
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',
    'polls',
    'django_nose',

)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--with-coverage',
    '--cover-package=polls',
    '--with-progressive',
    '--verbosity=0',
    '--with-fixture-bundling',
] 
当我尝试
python manage.py测试轮询时
测试运行两次。输出结果如下:

Creating test database for alias 'default'...
Name           Stmts   Miss  Cover   Missing
--------------------------------------------
polls              0      0   100%   
polls.models       6      0   100%   
--------------------------------------------
TOTAL              6      0   100%   

OK!  2 tests, 0 failures, 0 errors in 0.0s
Destroying test database for alias 'default'...
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Destroying test database for alias 'default'...
但是,当我尝试不使用
TEST\u RUNNER='django\u nose.nosetsuiterunner'
时,测试只运行一次。输出结果如下:

Creating test database for alias 'default'...
Name           Stmts   Miss  Cover   Missing
--------------------------------------------
polls              0      0   100%   
polls.models       6      0   100%   
--------------------------------------------
TOTAL              6      0   100%   

OK!  2 tests, 0 failures, 0 errors in 0.0s
Destroying test database for alias 'default'...
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Destroying test database for alias 'default'...
请告诉我怎么了为什么django nose要运行两次测试?

OT:django_nose在同一型号下比unittest花费更多的时间。

编辑:

以下是文件夹结构:

├── database.sqlite
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── templates
│   │   ├── 404.html
│   │   ├── 500.html
│   │   └── base.html
│   ├── urls.py
│   └── wsgi.py
└── polls
    ├── __init__.py
    ├── models.py
    ├── tests
    │   ├── __init__.py
    │   └── test_models.py
    ├── urls.py
    └── views.py

您可能导入了两次测试。您没有显示您的文件结构,但可能您将此测试放在单独的文件中,然后在tests.py中导入
或类似的内容。

@user1629366那么您在
polls/tests/\uu init\uuuuuy.py中有什么?你在那里导入测试模型吗?
从测试模型导入*
就是这样。你得到了答案。从
polls/tests/_init__.py和
中删除这一行,您将得到您想要的结果。之后,我将为视图编写测试,并且要运行所有测试,建议您阅读更相似的答案。这不是错误。该死的,那么django测试只运行一次@user1629366:django nose为你做这件事。它会自动为您发现测试。当您不使用django nose时,建议将测试放入
\uuuu init\uuuuuuuuuuy.py
中,因此请确定您遵循的建议。卸载django nose并将所有测试放入
\uuuu init\uuuuu.py
中,或者使用django nose,而不管您将测试放在何处,让它为您找到它们。使用django nose很方便,因为您可以将测试放在合适的位置,而不是在某些文件中显式列出它们。