Home >Backend Development >Python Tutorial >Solution to tornado error Exception (\'Did not get expected exception\')
"Exception("Did not get expected exception")" inpython The reason that appears in tornado is: When using tornado's Testframework for unit testing, this error occurs when the expected exception is not thrown. It could be that the program is not behaving as expected, or the exception is being caught and not passed to the testing framework.
There are several ways to solve this problem:
Make sure the program is running as expected. Check whether the code is correct and whether there are loopholes that cause exceptions not to be thrown.
Make sure the exception is not caught. In the program, the exception is caught without being passed to the testing framework. Make sure there are no exception-catching statements in your test code.
Use Tornado's AsyncTestCase.assertRaises() method to assert that an exception was thrown. This method ensures that expected exceptions are thrown in asynchronous code.
If no exception is thrown, or the exception thrown is not the expected exception, it is recommended to recheck the code, debug the program, and try to understand the status and behavior of the program when it is running.
Yes, here is an example of unit testing using Tornado's AsyncTestCase.assertRaises() method:
import tornado.testing import tornado.WEB class MyHandler(tornado.web.RequestHandler): async def get(self): raise ValueError("This is a test exception") class MyTestCase(tornado.testing.AsyncTestCase): async def test_exception(self): app = tornado.web.Application([(r"/", MyHandler)]) self.assertRaises(ValueError, self.fetch, '/')
In this example, we define a MyHandler class that will throw a ValueError exception when the get method is executed. Then we define a test_exception method in MyTestCase. In this method, we use the self.assertRaises(ValueError, self.fetch, '/') method to assert that a ValueError exception is thrown.
If the program runs normally, the unit test will pass. If the expected exception is not thrown, an "Exception("Did not get expected exception")" error is thrown.
The above is the detailed content of Solution to tornado error Exception (\'Did not get expected exception\'). For more information, please follow other related articles on the PHP Chinese website!