【问题标题】:Django testing, assert CSV content is presentDjango 测试,断言 CSV 内容存在
【发布时间】: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


【解决方案1】:

查看您添加到问题中的堆栈跟踪,堆栈跟踪对应于对self.assertNotContains(response.content, 'Vipul')的调用。

查看SimpleTestCase.assertNotContains() 的文档,我注意到该方法需要整个响应,而不仅仅是作为参数的内容。

所以,改变这一行:

self.assertNotContains(response.content, 'Vipul')

到这里

self.assertNotContains(response, 'Vipul')

应该清除您为其添加堆栈跟踪的错误。


现在,我注意到您的问题中也有这行:

我尝试了不同的选项,例如self.assertContains(response,'aakash'),但没有得到任何结果。

如果您希望我们能够为您提供帮助,您必须更具体地说明 "didn't get any result" 的含义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2014-07-14
    • 2018-04-11
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多