assert断言失败python_python – Django断言失败:assertInHTML(‘hello’,’hello’)

在Django

shell中:

from django.test import SimpleTestCase

c = SimpleTestCase()

haystack = 'contribution'

c.assertInHTML('contribution', haystack)

c.assertInHTML('contribution', haystack)

我不明白为什么第一个断言通过,但第二个断言不通过:

AssertionError Traceback (most recent call last)

in ()

5

6 c.assertInHTML('contribution', haystack)

----> 7 c.assertInHTML('contribution', haystack)

c:\...\lib\site-packages\django\test\testcases.py in assertInHTML(self, needle, haystack, count, msg_prefix)

680 else:

681 self.assertTrue(real_count != 0,

--> 682 msg_prefix + "Couldn't find '%s' in response" % needle)

683

684 def assertJSONEqual(self, raw, expected_data, msg=None):

C:\...\Programs\Python\Python35-32\lib\unittest\case.py in assertTrue(self, expr, msg)

675 if not expr:

676 msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))

--> 677 raise self.failureException(msg)

678

679 def _formatMessage(self, msg, standardMsg):

AssertionError: False is not true : Couldn't find 'contribution' in response

Django docs只是说“传入的参数必须是有效的HTML”.我不认为这是问题,因为第一行上对assert_and_parse_html的调用不会引发:

def assertInHTML(self, needle, haystack, count=None, msg_prefix=''):

needle = assert_and_parse_html(self, needle, None,

'First argument is not valid HTML:')

haystack = assert_and_parse_html(self, haystack, None,

'Second argument is not valid HTML:')

real_count = haystack.count(needle)

if count is not None:

self.assertEqual(real_count, count,

msg_prefix + "Found %d instances of '%s' in response"

" (expected %d)" % (real_count, needle, count))

else:

self.assertTrue(real_count != 0,

msg_prefix + "Couldn't find '%s' in response" % needle)

我正在使用Python 3.5.1和Django 1.8.8.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值