2012-02-29 8 views
5

私は(基本的にはPythonから同じたunittest.TestCaseである)私のDjangoアプリケーションのためのテストケースを書くことたunittest.TestCaseを使用しています。テストメソッドが失敗するたびに、以下の形式でその説明を取得します。失敗したテストメソッドの出力にカスタム/デバッグメッセージを追加できる方法はありますか?一般的にpython/django unittest.TestCaseのテストメソッドの失敗の詳細にカスタム/デバッグメッセージを追加する方法はありますか?

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

答えて

7

、あなたはdjango.testからインポートすることによって得ることができますDjangoのunittestのクラスのTestCase、継承したいです。つまり、エラーメッセージを含む、評価しようとしているものにmsg引数を渡すことができます。ここで

は、ヒト化からの例です:

明らか
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)) 

、あなたは上記のように見える必要はありませんが、アクションでのmsg引数を見ることができます。

+3

あなたは 'assertEqual'関数の定義を確認する場合は、あなたがそれを見ることができるが、MSGの引数を受け取り'デフassertEqual(自己、第一、第二、MSG =なし): ' –

関連する問題