2013-06-27 2 views
4

나는 unittests 무작위로 실패 의이 wierd 문제로 실행됩니다. 나는 기본적으로 신디케이션 여기장고 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

관련 문제