2013-06-27 83 views
4

我遇到了随机失败的单元测试这个奇怪的问题。我基本上测试联合供稿Django Unittests随机失败

这里是我的代码的相关部分:

class ObjectFeedTests(PuppyTestCase): 

    def test_object_feeds(self): 
     site = Site.objects.get_or_create(id=site_id)[0] 

     #Some preprocessing and initialization 

     with self.settings(ROOT_URLCONF='myapp.urls.something'): 

      #Some more preprocessing is done here 

      show = ShowFactory.create(slug=show_slug, title=show_name, 
             site=site) 

      for feed_name, entry_points in OBJECT_FEEDS.items(): 

       factory = default_factories[model] #fetches the correct factory 

       if feed_name == "podcast": 
        instance = factory.create(show=show, slug="podcasts") 
       else: 
        instance = factory.create() 

       #Creates the instance with the factory correctly 

       for feed_type in ['rss', 'atom']: 

        with self.settings(SITE_ID=site.id): 
         class_url_pattern = feed_class().get_url_pattern() 

        slug_of_note = '%s/%s' % (show_slug, instance.slug) 
        presumed_url = '/%s/%s/%s' % (FEED_URL_PREFIXES[feed_type], 
              feed_name, slug_of_note) 

        with self.settings(SITE_ID=site.id): 
         response = self.client.get(presumed_url, 
             {'password': '****'}) 

        self.assertContains(response, '<?xml', 
         msg_prefix="%s %s feed raised a 404 \ 
         or wasn't XML (tried %s)" \ 
          % (feed_name, feed_type, presumed_url)) 

       # End of our loop. We'll delete the object 
       # so that we're not hung up by unique_together constraints 
       instance.delete() 

工厂:

class ShowFactory(factory.Factory): 
    FACTORY_FOR = Show 

    title = factory.Sequence(lambda name: 'My Show {0}'.format(name)) 
    slug = factory.Sequence(lambda name: 'my-show-{0}'.format(name)) 
    creator = factory.SubFactory(UserFactory) 
    last_editor = factory.SubFactory(UserFactory) 
    site = factory.SubFactory(SiteFactory) 
    status = STATUS_CHOICES.PUBLISHED 

我每次运行测试,3个工厂的创建,以及测试失败随机。

如果我在代码中遗漏了任何局部变量声明,请忽略它,因为我压缩了代码以使其尽可能小。

我正在考虑using this,但结果仍然相同。

请让我知道是否有任何更多的信息,我可以提供。

这是一个失败的堆栈跟踪:

====================================================================== 
FAIL: test_object_feeds (tests.syndication_tests.ObjectFeedTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "tests/cases.py", line 44, in run 
    testMethod() 
    File "tests/syndication_tests.py", line 370, in test_object_feeds 
    presumed_url)) 
    File "/usr/local/virtualenvs/lib/python2.6/site-packages/django/test/testcases.py", line 622, in assertContains 
    " (expected %d)" % (response.status_code, status_code)) 
AssertionError: 404 != 200 : people rss feed raised a 404        or wasn't XML (tried /feeds/show/test-show-0): Couldn't retrieve content: Response code was 404 (expected 200) 

---------------------------------------------------------------------- 

Ran 5 tests in 10.613s 

FAILED (failures=1) 
Destroying test database for alias 'default'... 

下一次我运行它,

====================================================================== 
FAIL: test_object_feeds (tests.syndication_tests.ObjectFeedTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "tests/cases.py", line 44, in run 
    testMethod() 
    File "tests/syndication_tests.py", line 370, in test_object_feeds 
    presumed_url)) 
    File "/usr/local/virtualenvs/lib/python2.6/site-packages/django/test/testcases.py", line 622, in assertContains 
    " (expected %d)" % (response.status_code, status_code)) 
AssertionError: 404 != 200 : channels rss feed raised a 404        or wasn't XML (tried /feeds/show/test-show-1): Couldn't retrieve content: Response code was 404 (expected 200) 

---------------------------------------------------------------------- 
Ran 5 tests in 3.483s 

FAILED (failures=1) 
Destroying test database for alias 'default'... 
+1

愚蠢的问题的争论旧测试夹具,但到底是什么测试说,在它的输出?为什么失败? –

+0

@limelights刚刚更新了stacktrace的问题 – karthikr

+0

出于某种原因,您所得到的东西不是有效的XML,这非常明显。你的测试看起来不错,我看不出有什么理由不能工作。 –

回答

2

请原谅我说明明显。随机失败的最常见原因是你的一个测试没有正确拆卸。这是随机发生的,因为执行测试的顺序是非确定性的。尝试单独运行失败的测试,而不是运行整个测试套件。如果每次都通过,那肯定是由其他一些测试引起的。

另一种可能性是,你可能会重用这取决于您提供运行测试

+0

感谢您的回复。你能说一说你的意思吗? – karthikr

+0

这将取决于你的测试跑步者。例如,我正在使用django-nose,它允许我通过重复使用上次运行的测试数据库来执行REUSE_DB = 1 ./manage.py测试,并在测试套件的开始和结束处保存几秒钟。 –

+0

谢谢。我猜你在夹具上的方向让我觉得我可能在夹具上有一些数据问题。我会尝试一下 – karthikr