pytest:如何在单元测试期间强制引发异常? [英] pytest: How to force raising Exceptions during unit-testing?

查看:71
本文介绍了pytest:如何在单元测试期间强制引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 python 代码中,我期望在调用方法 requests.Session.request() 后可能会引发异常,例如:

In my python code, I am expecting exceptions could possibly be raised after calling method requests.Session.request(), for example these:

  • requests.exceptions.ConnectTimeout
  • requests.exceptions.ReadTimeout
  • requests.exceptions.Timeout

当出现任何这些预期的异常时,我会适当地处理它们,例如可能的重试情况.

When any of these expected exceptions are raised, I handle them appropriately, for example possibly a retry situation.

我的问题,我正在使用 py.test 进行单元测试,并且我有意从我的代码的特定部分注入引发异常.例如,调用 requests.Session.request() 的函数,不是返回有效的 requests.Response,而是引发 requests.exception>.

My question, I am using py.test for unit testing, and I purposely want to inject raising exceptions from specific parts of my code. For example, the function that calls requests.Session.request(), instead of returning a valid requests.Response, it raises a requests.exception.

我想确保我的代码成功处理来自其他包的预期和意外异常,其中包括来自 requests 的异常.

What I want to make sure that my code successfully handles expected and unexpected exceptions coming from other packages, which include those exceptions from requests.

也许...是否有一个 @decorator 可以添加到上述函数中,以便在单元测试期间根据请求引发异常?

Maybe... Is there a @decorator that I could add to the aforementioned function to raise exceptions upon request during unit testing?

针对单元测试进行异常注入的建议?(我的问题的正确措辞将不胜感激.)

Suggestions for doing exceptions injections for unit testing? (proper phrasing of my question would be greatly appreciated.)

感谢您的回复!!!

这是创建 requests.Session 并调用 requests.Session.request() 的整个单例类:

Here is the entire singleton class that creates requests.Session and calls requests.Session.request():

class MyRequest(metaclass=Singleton):

    def __init__(self, retry_tries=3, retry_backoff=0.1, retry_codes=None):
        self.session = requests.session()

        if retry_codes is None:
            retry_codes = set(REQUEST_RETRY_HTTP_STATUS_CODES)

        self.session.mount(
            'http',
            HTTPAdapter(
                max_retries=Retry(
                    total=retry_tries,
                    backoff_factor=retry_backoff,
                    status_forcelist=retry_codes,
                ),
            ),
        )

    def request(self, request_method, request_url, **kwargs):
        try:
            return self.session.request(method=request_method, url=request_url, **kwargs)
        except Exception as ex:
            log.warning(
                "Session Request: Failed: {}".format(get_exception_message(ex)),
                extra={
                    'request_method': request_method,
                    'request_url': request_url
                }
            )
            raise

推荐答案

您可以使用 py.test raises,请在此处查看:http://doc.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions

You can make use of py.test raises, check it here: http://doc.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions

考虑到您的代码,您可以执行以下操作:

Taking into account your code you could do something along the lines of the following:

from requests.exceptions import ConnectTimeout, ReadTimeout, Timeout
from unittest.mock import patch
import pytest

class TestRequestService:
   @patch('path_to_module.MyRequest')
   def test_custom_request(self, my_request_mock):
      my_request_mock.request.side_effect = ConnectTimeout

      with pytest.raises(ConnectTimeout):
          my_request_mock.request(Mock(), Mock())

此外,您可以使用 pytest.parametrize(http://doc.pytest.org/en/latest/parametrize.html) 以及:

Moreover, you could make use of pytest.parametrize(http://doc.pytest.org/en/latest/parametrize.html) as well:

from requests.exceptions import ConnectTimeout, ReadTimeout, Timeout
from unittest.mock import patch
import pytest

class TestRequestService:
    @pytest.mark.parametrize("expected_exception", [ConnectTimeout, ReadTimeout, Timeout])
    @patch('path_to_module.MyRequest')
    def test_custom_request(self, my_request_mock, expected_exception):
        my_request_mock.request.side_effect = expected_exception
        with pytest.raises(expected_exception):
            my_request_mock.request(Mock(), Mock())

您可以在此处找到更多有关参数化的示例:http://layer0.authentise.com/pytest-and-parametrization.html

Here you can find some more examples about parametrize: http://layer0.authentise.com/pytest-and-parametrization.html

这篇关于pytest:如何在单元测试期间强制引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆