Django testing, assert CSV content is present

Viewed 484

I am doing a test to check the contents of a csv file using assertContains():

response = client.get('/abc/1/a_b_csv')
print(response.content)
self.assertContains(response.content, 'aakash')

I tried different options such as self.assertContains(response,'aakash') but didn't get any result.

My csv file looks like,

Name Age
Aakash 22

Sometimes there is the error:

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'
1 Answers

Looking at the stacktrace you added to the question, the stacktrace corresponds to the call to self.assertNotContains(response.content, 'Vipul').

Looking at the docs for SimpleTestCase.assertNotContains(), I notice that the method expects the whole response, not just the content as parameter.

So, changing this line:

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

to this

self.assertNotContains(response, 'Vipul')

should clear up the error for which you added the stacktrace.


Now, I notice that you also have this line in your question:

I tried different options such as self.assertContains(response,'aakash'), but didn't get any result.

You will have to be more specific as to what you mean by "didn't get any result" if you want us to be able to help you.

Related