Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Test a decorated function in Python

I have a python function which is decorated.

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

          """adding something"""

I’ve written tests for the given as below.

    @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 """

But I am getting this error that says add_somehthing takes 1 positional argument.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

It looks like at least one of your decorators don’t handle taking and passing on the arguments properly. Can you post the code for @patch() and @retry_if_access_token_expired()?

They should have the generic form from here: https://realpython.com/primer-on-python-decorators/#decorators-with-arguments

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading