[Python] how can i write unit test for function that is making network request without...

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    I read that

    Unit tests run fast.
    If they don’t run fast, they aren’t unit tests.

    A test is not a unit test if
    1. It talks to a database.
    2. It communicates across a network.
    3. It touches the file system.
    4. You have to do special things to your environment (such as editing configuration files) to run it.

    in Working Effectively with legacy code (book).


    I have a function that is downloading the zip from the internet and then converting it into a python object for a particular class.

    import typing as t

    def get_book_objects(date: str) -> t.List[Book]:
    # download the zip with the date from the endpoint
    res = requests.get(f"HTTP-URL-{date}")

    # code to read the response content in BytesIO and then use the ZipFile module
    # to extract data.

    # parse the data and return a list of Book object
    return books


    let's say I want to write a unit test for the function get_book_objects. Then how am I supposed to write a unit test without making a network request? I mean I prefer file system read-over a network request because it will be way faster than making a request to the network although it is written that a good unit test also not touches the file system I will be fine with that.

    So even if I want to write a unit test where I can provide a local zip file I have to modify the existing function to open the file from the local file system or I have to add some additional parameter to the function so I can send a zip file path from unit test function.

    What will you do to write a good unit test in this kind of situation?

    Continue reading...

Compartilhe esta Página