Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing django: reason for unexpected http status code

We have many unitests in our django application.

But if a tests fails because the status code does not match:

Traceback (most recent call last):
  File "/home/foo_eins_di514/src/foo-time/foo_time/tests/EditTest.py", line 813, in test_web_entry_with_unclassified_activity
    self.assertEqual(200, response.status_code, url)
  File "/usr/lib64/python2.7/unittest/case.py", line 494, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib64/python2.7/unittest/case.py", line 487, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: /foo_eins_di514/modtime/calendar/entry/view/172/

.... it is completly unknown where the wrong exit status gets created.

I big applications it can take some time to find the root of the problem where the exist status gets created, since the exit status can get created in a middleware, too.

Any hint how to make this more testable?

The test looks like this:

url=reverse(view_name, kwargs=dict(id=entry.id))
response=client.get(url)
self.assertEqual(200, response.status_code, url)
like image 408
guettli Avatar asked May 09 '26 20:05

guettli


1 Answers

Sounds like you should split your tests up into proper unit tests and test smaller units of code. Use Django's request factory and test only the view code. Or split your view into smaller functions and test them individually.

Using Django's test client is more of an integration test than a unittest as it tests url routing, database, middleware, templates etc

like image 180
Carl Avatar answered May 12 '26 10:05

Carl