单元测试期间的自定义RequestHandler服务器。连接被拒绝

时间:2018-10-02 06:00:12

标签: python python-3.x unit-testing

我在生成新的HTTP服务器以测试对该服务器的请求时遇到了问题。它当前可以很好地创建一个测试数据库,但是请求被卡住,从不响应,必须手动终止该进程。 RequestHandler是一个定制的处理程序,通过Postman在我的Docker容器上对其进行测试时,它可以正常工作,因此我知道这不是问题。

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

我不知道该怎么做。也许我做错了。我将不胜感激任何提示,或者我对API端点的单元测试做错了。这是目前唯一的测试:

class ViewsTestSuite(unittest.TestCase):

    db_string = "sqlite:///unittest.db"
    engine = create_engine(db_string)
    Base = declarative_base()
    port = 8000
    server = HTTPServer(('', port), RequestHandler)

    def setUp(self):

        self.Base.metadata.drop_all(bind=self.engine)
        self.Base.metadata.create_all(self.engine)
        try:
            self.server.serve_forever()
        except KeyboardInterrupt:
            pass
        self.server.server_close()

    def tearDown(self):
        self.Base.metadata.drop_all(bind=self.engine)
        self.server.shutdown()

    def test_get_recipes(self):
        '''
            TEST FOR GET /recipes ENDPOINT. IT SHOULD RETURN 200
            AND A LIST OF RECIPE OBJECTS.
        '''
        # ADD request POST to include element in database
        r = requests.get('http://localhost:8000/recipes')
        recipes = [{
            "id": 1,
            'name': 'First Response',
            'prep_time': 400,
            "difficulty": 1,
            "vegeterian": True,
            "rating": 4.0
        }]
        self.assertEqual(r.json(), recipes)
        self.assertEqual(r.status_code, 200)

谢谢,感谢您的帮助!

0 个答案:

没有答案