4

I write my handler for server errors and define it at root urls.py: handler500 = 'myhandler' And I want to write unittest for testing how it works. For testing I write view with error and define it in test URLs configuration, when I make request to this view in browser I see my handler and receive status code 500, but when I launch test that make request to this view I see stack trace and my test failed. Have you some ideas for testing handler500 by unittests?

Gr1N
  • 1,337
  • 3
  • 14
  • 19

1 Answers1

3

What you want to do here is integration testing; unit-tests won't be the appropriate tools for it.

Django includes support for using famous live browser testing tools such as Selenium.

As you pointed out and for your specific case, it is indeed pretty complicated to retrieve the status code for a page using Selenium, you might therefore want to use a simpler approach:

  1. Use a LiveServerTestCase
  2. Use a python HTTP client such as requests to access the test page
  3. Check that the test page does generate an HTTP 500 error
  4. Check that the test page contains the expected content.

If you want to test some JS on the test page, then use Selenium for that part, in combination of requests to test the status.

Community
  • 1
  • 1
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116