简体   繁体   English

Django单元测试随机失败

[英]Django Unittests Failing Randomly

I am running into this wierd issue of randomly failing unittests. 我遇到了一个随机测试失败的怪异问题。 I am basically testing Syndication Feeds 我基本上是在测试联合供稿

Here is the relevant portion of my code: 这是我的代码的相关部分:

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()

The factory: 工厂:

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

Each time I run the test, 3 factories are created, and the tests fail randomly. 每次运行测试时,都会创建3个工厂,并且测试会随机失败。

If I have left out any local variable declaration in the code here, please ignore it, as I have condensed the code to keep it as small as possible. 如果我在此处的代码中未包含任何局部变量声明,请忽略它,因为我压缩了代码以使其尽可能小。

I was considering using this , but the results were still the same. 我正在考虑使用此方法 ,但结果仍然相同。

Please let me know if there is any more information I could provide. 请让我知道我是否可以提供更多信息。

Here is the stacktrace of a failure: 这是失败的堆栈跟踪:

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

The next time I run it, 下次我运行它时,

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

Please forgive me for stating the obvious. 请原谅我说的明显。 The most common cause for random failure is that one of your tests did not tear down properly. 随机失败的最常见原因是您的一项测试未正确拆除。 This happens randomly because the order in which the tests are executed is non-deterministic. 这是随机发生的,因为执行测试的顺序是不确定的。 Try running the failed test alone instead of running the whole test suite. 尝试单独运行失败的测试,而不是运行整个测试套件。 If it passes every time, then it is definitely caused by some other tests. 如果每次都通过,则肯定是由其他测试引起的。

Another possibility is that you may be reusing the old test fixture depending on the arguments you provide to run your tests 另一种可能性是,您可能会根据提供的用于运行测试的参数来重用旧的测试夹具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM