【发布时间】:2018-09-18 10:06:41
【问题描述】:
我正在测试使用assertContains() 检查csv 文件的内容:
response = client.get('/abc/1/a_b_csv')
print(response.content)
self.assertContains(response.content, 'aakash')
我尝试了不同的选项,例如self.assertContains(response,'aakash')
但没有得到任何结果。
我的 csv 文件看起来像,
Name Age
Aakash 22
有时会出现错误:
bName\r\nVipul\r\n'
E
======================================================================
ERROR: test_csv (timepay.new_test.ReportTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aakash/Projects/test.py", line 28, in test_csv
self.assertNotContains(response.content, 'Vipul')
File "/home/aakash/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 402, in assertNotContains
response, text, status_code, msg_prefix, html)
File "/home/vipul/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 355, in _assert_contains
response.status_code, status_code,
AttributeError: 'bytes' object has no attribute 'status_code'
【问题讨论】:
-
你能添加错误的完整堆栈跟踪吗?
-
@Ralf 更新了查询
-
我为您的部分问题添加了答案。如果您仍有问题,我需要更多信息(但请参阅我的回答了解详细信息)
-
尝试使用
request.json()而不是request.content
标签: python django django-testing django-tests