0

I use the App Engine for run my application and want to test how it will handle server errors. Is there any possibility to simulate an error 500 via the WebTest ?

starter
  • 169
  • 2
  • 10

2 Answers2

3

I got around it using a try except loop.

try:
    self.testapp.get('/')
    self.assertEqual(1, 2, 'GET request should have resulted in a 405 error') # Purposely fail
except webtest.app.AppError:
    pass

Another way is the following:

self.assertEqual("500 Internal Server Error", self.testapp.post('/', params={}, expect_errors=True).status, 'POST Request should have resulted in a 500 error')

Both methods still will cause traceback to appear but the test passes

1

A 500 error is just what your webapp returns to the client when it gets an uncaught exception. It's not a specific failure - just what it shows to your users when something unexpected goes wrong. Instead, you should unit-test your handlers to ensure they act as expected.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • Yes. For this reason I want to emulate a server error, at the time when a get method calls and to test how to handle the error. I.e. I need an entry point into the get method to pass an exception in it or I need have predefined behavior of a TestApp before test. – starter Jun 24 '11 at 07:58
  • @starter You're going to have to be clearer about _what_ you want to test. A "server error" is just what your app returns when it gets an uncaught exception. Do you want to test how your app handles uncaught exceptions? How it handles a particular exception thrown in a particular place? – Nick Johnson Jun 24 '11 at 13:25
  • Yes. I want test how it handles a particular exception thrown in a particular place and how my app handles uncaught exceptions. – starter Jun 24 '11 at 14:23
  • @starter Then you're not testing 500s at all - you're testing your app's behaviour, and standard testing practices apply. – Nick Johnson Jun 26 '11 at 23:47
  • Maybe I'm not understand something, but how to simulate a certain behavior without emulation server errors? – starter Jun 27 '11 at 14:41
  • @starter Again, a server error is a _symptom_ of an uncaught exception. There's no point in testing server errors (it's not even clear to me what you'd test? that you get one when an uncaught exception occurs?) - instead, you unit test the relevant parts of your code. – Nick Johnson Jun 28 '11 at 01:25
  • Unfortunately English is not my native language, so I don't correctly explain what I want. I was whant do some request and at this time emulate a server error via a webtest or any mock objects and test how my application handles an exception raised due the error of server. – starter Jun 28 '11 at 13:37
  • @starter You're going to have to be more specific. What sort of 'server error' are you trying to simulate? – Nick Johnson Jun 29 '11 at 00:50
  • I understand what you want. I want the same think. I have to test what hapends into my application when an unexpected error is thrown. Because I'm using a middleware that get the exceptions. But I'm not able to reproduce it :( So sad that till 2011 noone finds an answer for your question. – Shil Nevado Nov 06 '18 at 12:34