【问题标题】:Why is django-nose running tests twice?为什么 django-nose 运行测试两次?
【发布时间】:2012-09-12 17:16:11
【问题描述】:

我有以下模型

class Poll(models.Model):

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

    def __unicode__(self):
             return self.question

以及下面的测试

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)

以及以下设置

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 test polls 时,测试运行两次。以下是输出:

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'...

但是,当我尝试不使用TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' 时,测试只运行一次。以下是输出:

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

【问题讨论】:

    标签: python django-models nose django-testing django-nose


    【解决方案1】:

    您的测试可能导入了两次。你没有显示你的文件结构,但也许你有这个测试在单独的文件中,而不是你在 test.py 中的import 或类似的东西。

    【讨论】:

    • @user1629366 那么polls/tests/__init__.py 中有什么?你在那里导入 test_models 吗?
    • from test_models import * 就是这样。
    • 所以你得到了你的答案。从polls/tests/__init__.py and 中删除这一行,你会得到你想要的
    • 稍后我还将为视图编写测试并运行所有测试,建议阅读更多类似的答案。这不是的错误。怎么回事,那么 django 测试只运行一次!
    • @user1629366:django-nose 为你做的。它会自动为您发现您的测试。当您不使用 django-nose 时,建议在 __init__.py 中进行测试,因此请决定您遵循哪个建议。卸载 django-nose 并将所有测试放入 __init__.py 或使用 django-nose 并且不在乎您将测试放在哪里并让它为您找到它们。使用 django-nose 很方便,因为您将测试放在适合的地方,而不是在某些文件中明确列出它们。
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2017-04-20
    • 2015-06-28
    • 2015-04-03
    • 1970-01-01
    • 2013-06-18
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多