代码之家  ›  专栏  ›  技术社区  ›  shilin agre

在Python中测试修饰函数

  •  0
  • shilin agre  · 技术社区  · 2 年前

    我有一个经过修饰的python函数。

        @retry_if_access_token_expired(app_id)
        def add_something(
            self,
            *,
            argument1,
            argument1 = None,
        ):
    
              """adding something"""
    

    我已经为下面给出的问题写了测试。

        @patch("other inside function to mock")
        @patch("other insdie function to mock 2")
        def test_add_something(
            self, mock_1, mock_2
        ):
            """ some logic to test the add something method """
    

    但是我得到了一个错误,说add_something需要1个位置参数。

    TypeError: add_something() takes 1 positional argument but 3 were given

    1 回复  |  直到 2 年前
        1
  •  1
  •   MusicalNinja    2 年前

    看起来至少有一个装饰师没有正确处理参数的获取和传递。你能帮我发密码吗 @patch() @retry_if_access_token_expired() ?

    它们应该具有以下通用形式: https://realpython.com/primer-on-python-decorators/#decorators-with-arguments