简体   繁体   English

有没有将自定义/调试消息添加到python / django unittest.TestCase的失败测试方法细节的方法?

[英]Any ways to add custom/debug message to details of failed test method of python/django unittest.TestCase?

I am using unittest.TestCase to write test cases for my django app (which is essentially the same unittest.TestCase from python). 我正在使用unittest.TestCase为我的django app编写测试用例(这与python中的unittest.TestCase基本相同)。 Whenever a test method fails, I get the explanation of it in the format below. 每当测试方法失败时,我会以下面的格式得到它的解释。 Is there a way that I can add custom/debug messages to the output of failed test method? 有没有办法可以将自定义/调试消息添加到失败的测试方法的输出中?

======================================================================
FAIL: test_bad_votes (polls.tests.views.PollsViewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/colinh/Development/tutorials/guide-to-testing-in-django/polls/tests/views.py", line 66, in test_bad_votes
    self.assertEqual(resp.context['form']['choice'].errors, [u'This field is required.'])
AssertionError: [] != [u'This field is required.']

In general, you want to inherit from django's unittest class TestCase, which you can get by importing from django.test. 一般来说,你想从django的unittest类TestCase继承,你可以从django.test导入它。 That said, you can pass a msg argument to whatever you're trying to evaluate, containing the failure message. 也就是说,您可以将msg参数传递给您正在尝试评估的任何内容,其中包含失败消息。

Here's an example from Humanize: 以下是Humanize的一个例子:

class HumanizeTests(TestCase):

    def humanize_tester(self, test_list, result_list, method):
        # Using max below ensures we go through both lists
        # However, if the lists are not equal length, this raises an exception
        for test_content, result in zip(test_list, result_list):
            t = Template('{%% load humanize %%}{{ test_content|%s }}' % method)
            rendered = t.render(Context(locals())).strip()
            self.assertEqual(rendered, escape(result),
                         msg="%s test failed, produced '%s', should've produced '%s'" %     (method, rendered, result))

Obviously, yours doesn't need to look like the above, but you can see the msg argument in action. 显然,你的不需要像上面这样,但你可以看到msg参数在行动。

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

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