Django 1.8和鼻子:冲突模型?

时间:2015-09-21 16:57:45

标签: django django-models nose

我有一个Django应用程序,我最近升级到Django 1.8.4。我使用机头1.3.7和django-nose 1.4.1为我的测试运行器运行了200多次集成和单元测试。由于升级了Django和鼻子,我发现我的12个测试失败并出现同样的错误:

======================================================================
ERROR: Failure: RuntimeError (Conflicting 'c' models in application 'nose': <class 'account.tests.form_tests.TestAddress'> and <class 'nose.util.C'>.)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/venv/myproj/lib/python2.7/site-packages/nose/loader.py", line 523, in makeTest
    return self._makeTest(obj, parent)
  File "/Users/me/venv/myproj/lib/python2.7/site-packages/nose/loader.py", line 568, in _makeTest
    obj = transplant_class(obj, parent.__name__)
  File "/Users/me/venv/myproj/lib/python2.7/site-packages/nose/util.py", line 644, in transplant_class
    class C(cls):
  File "/Users/me/venv/myproj/lib/python2.7/site-packages/django/db/models/base.py", line 311, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/Users/me/venv/myproj/lib/python2.7/site-packages/django/apps/registry.py", line 223, in register_model
    (model_name, app_label, app_models[model_name], model))
RuntimeError: Conflicting 'c' models in application 'nose': <class 'account.tests.form_tests.TestAddress'> and <class 'nose.util.C'>.

奇怪的是,form_tests.py模块甚至没有引用TestAddress,它实际上是我的&#34; profiles&#34;中的一个类。模型:

# myprof/profile/models.py
class TestAddress(models.Model):
    user = models.OneToOneField(User, primary_key=True, unique=True)
    address_line_1 = models.CharField(max_length=30)
    address_line_2 = models.CharField(max_length=30, null=True, blank=True)
    city = models.CharField(max_length=30)
    region = models.CharField(max_length=30, null=True, blank=True)
    postal_code = models.CharField(max_length=10, null=True, blank=True)
    country = models.ForeignKey('Country')

    class Meta:
        db_table = 'test_address'

    def __unicode__(self):
        return u'%s' % (self.user.username)

当我的测试需要生成TestAddress类的实例时,我使用factory_boy(v.2.5.2)工厂:

# utils/factories.py
from profile.models import TestAddress

class UserFactory(factory.django.DjangoModelFactory):
class Meta:
    model = User
username = 'testuser'

class TestAddressFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = TestAddress
    user = factory.SubFactory('utils.factories.UserFactory')
    address_line_1 = '123 Main St.'
    address_line_2 = 'Apt. A'
    city = 'AnyCity'
    region = 'AnyRegion'
    postal_code = '12345'
    country = factory.SubFactory('utils.factories.CountryFactory')

我在nose loader.py模块中设置了断点,并确认加载器看到&#34; TestAddress&#34; in&#34; profile.models&#34;。但是,有一个&#34; parent .__ name __&#34;变量那里设置为&#34; account.tests.model_tests&#34;。我有几个问题:

1. Why is this occurring? 
2. Is there a way I can fix it?  
3. Is there some way I can get nose to tell me which tests are resulting in these runtime errors so that I can at least disable them if I can't fix the problem?  

我设置&#34; - verbosity = 2&#34;但那 不显示失败测试的名称。我仔细检查了鼻子文档并没有看到任何东西。最糟糕的情况我可以编写一个脚本来单独调用每个测试并在运行之前回显测试名称,但这看起来非常难看且耗时。

感谢。

2 个答案:

答案 0 :(得分:0)

在将我的Django项目从1.6移植到1.8并将django-nose更新为1.4.3时遇到了同样的问题。

似乎在1.7以后使用新的DiscoverRunner,django-nose尝试以Test*作为测试用例运行所有类,这导致它们被改编成django-nose的django应用程序在测试模块中遇到它们之前。

我设法通过将这些问题重命名以避免使用此命名方案来解决此问题,例如AdressTestModel

答案 1 :(得分:0)

我刚遇到这个并用@nottest装饰器解决了它。

从技术上讲,它是根据文档提供的功能,但使用它来装饰类也有效:

from nose.tools import nottest                                                  
@nottest
class TestAddressFactory(...):
   ...

装饰者所做的就是将值__test__的{​​{1}}添加到正在装饰的对象中。

True