如何编写用于单元测试的文件下载模拟文件

时间:2020-11-03 09:29:21

标签: python python-requests mocking

我使用requests_mock作为测试我编写的使用端点的代码的库。

这是我的download_file函数

def download_as_file(url: str, auth=(), file_path="", attempts=2):
    """Downloads a URL content into a file (with large file support by streaming)

    :param url: URL to download
    :param auth: tuple containing credentials to access the url
    :param file_path: Local file name to contain the data downloaded
    :param attempts: Number of attempts
    :return: New file path. Empty string if the download failed
    """
    if not file_path:
        file_path = os.path.realpath(os.path.basename(url))
    logger.info(f"Downloading {url} content to {file_path}")
    url_sections = urlparse(url)
    if not url_sections.scheme:
        logger.debug("The given url is missing a scheme. Adding http scheme")
        url = f"https://{url}"
        logger.debug(f"New url: {url}")
    for attempt in range(1, attempts + 1):
        try:
            if attempt > 1:
                time.sleep(10)  # 10 seconds wait time between downloads
            with requests.get(url, auth=auth, stream=True) as response:
                response.raise_for_status()
                with open(file_path, "wb") as out_file:
                    for chunk in response.iter_content(chunk_size=1024 * 1024):  # 1MB chunks
                        out_file.write(chunk)
                logger.info("Download finished successfully")
                return (response, file_path)
        except Exception as ex:
            logger.error(f"Attempt #{attempt} failed with error: {ex}")
    return None

如何使用requests_mock进行测试?

我知道requests_mock的文档在https://requests-mock.readthedocs.io/en/latest/response.html#registering-responses中涵盖了以下内容

要指定响应的正文,有许多选项取决于您希望返回的格式。

  • json:将转换为JSON字符串的python对象。
  • text:Unicode字符串。通常,这就是您要用于常规文本内容的方式。
  • content:一个字节字符串。这应该用于在响应中包括二进制数据。
  • body:类似于对象的文件,其中包含.read()函数。
  • 原始:将返回预填充的urllib3.response.HTTPResponse。
  • exc:将引发而不是不返回响应的异常。

是正文还是内容?那我怎么写呢?

大多数端点都给了我json,但是我有下载.xlsx文件的特殊用例,所以我想编写一个使用它的测试用例。

1 个答案:

答案 0 :(得分:0)

简短答案:选择身体

长答案:

with requests_mock.mock() as m:
    current_folder = os.path.dirname(os.path.abspath(__file__))
    path = os.path.join(
        current_folder, filename
    )
    with open(path, "rb") as the_file:
        m.get(url_to_mock, body=the_file)