【发布时间】:2014-05-27 22:07:44
【问题描述】:
所以我有一个页面,预计在 django 中返回内容或返回 403 页面
response = self.client.get(...)
print response.status_code
print type(response.status_code)
assert response.status_code is 200
print "WHAT IS GOING ON!?!?!?!"
response = self.client.get(...)
code = response.status_code
print code
print type(code)
assert code is 403
print "hmm"
返回输出:
200
<type 'int'>
WHAT IS GOING ON!?!?!?!
403
<type 'int'>
很明显,代码在assert code is 403 处失败,但我无法想象为什么。我什至通过将行更改为assert 403 is 403 来检查自己的理智,并且测试通过了。我是 Python 和 Django 的新手,所以我可能会遗漏一些明显的东西。
【问题讨论】:
-
感谢 nija 的编辑,你已经打败了我
-
尝试
==而不是is因为 200 值不一定与您要比较的对象相同 -
200 断言有效,而 403 则无效。和 == 是一个很好的解决方法,但知道为什么会很好。
-
如果
==有效而is无效,那么您无论如何都错误地使用了is。应使用==进行相等性测试。应该 only 用于验证一个对象是 exact same 对象。
标签: python django assertions