用于运行测试的独立wsgi服务器。
wsgitest的Python项目详细描述
您可以使用wsgitest启动wsgi应用程序的http服务器,然后 控制过程:
@Request.applicationdefapplication(request):returnResponse('Hello World!')classAppServerTestCase(TestCase):deftest_hello_app(self):server=WSGITestServer.create(application)try:response=requests.get(server.application_url)self.assertEqual(response.status_code,200)self.assertEqual(response.content,b"Hello World!")finally:server.terminate()deftest_hello_app_reference(self):server=WSGITestServer.create("tests.test_server.application")try:response=requests.get(server.application_url)self.assertEqual(response.status_code,200)self.assertEqual(response.content,b"Hello World!")finally:server.terminate()
方法wsgitestserver.create()初始化一个multiprocessing.Process 等待服务器启动。
您可以使用服务器作为上下文管理器,以避免测试在 您忘记“终止”服务器:
@Request.applicationdefapplication(request):returnResponse('Hello World!')classAppServerTestCase(TestCase):deftest_hello_app(self):withWSGITestServer(application)asserver:response=requests.get(server.application_url)self.assertEqual(response.status_code,200)self.assertEqual(response.content,b"Hello World!")
您还可以使用特定的主机/IP或TCP端口启动服务器:
@Request.applicationdefapplication(request):returnResponse('Hello World!')classAppServerTestCase(TestCase):deftest_hello_app(self):withWSGITestServer(application,"0.0.0.0",5000)asserver:response=requests.get(server.application_url)self.assertEqual(response.status_code,200)self.assertEqual(response.content,b"Hello World!")