继承Python单元测试的子类构造函数中的父类参数

发布于 2025-02-12 05:48:29 字数 2153 浏览 0 评论 0原文

我正在使用Python嘲笑一些外部API调用来编写测试用例。我有一个父类,将外部API客户端作为具有URL信息的参数。然后,我从父级中继承了子类中的一些方法。当我嘲笑子类方法时,我正在传递一个模拟网址,而不是实际的URL。不幸的是,子班级上的URL已在父级中通过。当我只是嘲笑父班时,断言取决于我通过的模拟网址。

父母类

class Parent:
   def __init__(self, id:str, item:str, item_client = ClientSettings(settings.config.api_url)):
      self.id = id
      self.item = item
      self.item_client = item_client

   async def main_method_in_parent(self):
      order_details = self.item_client.get_item_details(self.id)
      get_order_details = order_details["response"]["payload"]["orders"]["details"]

子类

class Child(Parent):
   def __init__(self, id:str, item:str, status:str = None):
       super().__init__(id, item)
       self.status = status

   async def method_in_child(self):
      await self.main_method_in_parent()
      order_status = self.item_client.get_order_status(self.status)
      get_status = order_status["response"]["result"]["orders"]["details"]["status"]

测试用例

mock_json = {
   "response": {
      "payload": {
         "orders": {
            "details": "order_name",
            "status": "shipped"
         }
      }
   }
}
class TestParent(unittest.TestCase):
   def test_parent_child(self):
      loop = asyncio.get_event_loop()
      mock_client_api = ClientSettings("dummy_url")
      mock_client_api.get_item_details = MagicMock(return_value=async_return(mock_json))
      mock_client_api.get_order_status = MagicMock(return_value=async_return(mock_json))

      test_parent = Parent(id="3dcff3", item="some_item", item_client=mock_client_api)
      test_child = Child(test_parent, status="shipped")

      loop.run_until_complete(test_child.method_in_parent())
      loop.run_until_complete(test_child.method_in_child())

      assert get_order_details == "order_name"
      assert get_status == "shipped"

      loop.close()

if __name__ == "__main__":
    unittest.main()

在上述案例中,父母类的第一个主张通过,但是第二个主张(get_status)失败了

attributeError:'clientConnectorError'对象没有属性'get'

,我发现我的父级正在嘲笑时提供我提供的虚拟API URL,但是子类以某种方式从父类中获取实际的API url。我错过了什么吗?

I am writing test cases by mocking some external api calls using python. I have a parent class that takes external api clients as arguments that has the url information. Then I am inheriting some of the methods in Child class from the parent class. When I am mocking the child class methods, I am passing a mock url instead of the actual url. Unfortunately, the child class takes the url that has been passed in the parent class. When I just mock the parent class, the assertion depends on the mock url that I am passing.

Parent class

class Parent:
   def __init__(self, id:str, item:str, item_client = ClientSettings(settings.config.api_url)):
      self.id = id
      self.item = item
      self.item_client = item_client

   async def main_method_in_parent(self):
      order_details = self.item_client.get_item_details(self.id)
      get_order_details = order_details["response"]["payload"]["orders"]["details"]

Child Class

class Child(Parent):
   def __init__(self, id:str, item:str, status:str = None):
       super().__init__(id, item)
       self.status = status

   async def method_in_child(self):
      await self.main_method_in_parent()
      order_status = self.item_client.get_order_status(self.status)
      get_status = order_status["response"]["result"]["orders"]["details"]["status"]

Test Case

mock_json = {
   "response": {
      "payload": {
         "orders": {
            "details": "order_name",
            "status": "shipped"
         }
      }
   }
}
class TestParent(unittest.TestCase):
   def test_parent_child(self):
      loop = asyncio.get_event_loop()
      mock_client_api = ClientSettings("dummy_url")
      mock_client_api.get_item_details = MagicMock(return_value=async_return(mock_json))
      mock_client_api.get_order_status = MagicMock(return_value=async_return(mock_json))

      test_parent = Parent(id="3dcff3", item="some_item", item_client=mock_client_api)
      test_child = Child(test_parent, status="shipped")

      loop.run_until_complete(test_child.method_in_parent())
      loop.run_until_complete(test_child.method_in_child())

      assert get_order_details == "order_name"
      assert get_status == "shipped"

      loop.close()

if __name__ == "__main__":
    unittest.main()

In the above described case, the first assertion from parent class passes, but the second assertion (get_status) fails with an error

AttributeError: 'ClientConnectorError' object has no attribute 'get'

After debugging, I figured out that my parent class is taking the dummy api url that I provided while mocking, but the child class somehow takes the actual api url from the parent class. Am I missing anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文