pytest不会引起例外

发布于 2025-02-05 04:05:38 字数 3991 浏览 2 评论 0原文

我找不到进行此单元测试的例外。

def test_something(monkeypatch):
    # Arrange
    os.environ["ENTITY"] = "JURKAT" # should monkeypatch this, but ignore for now.
    opgave_or_sag_id = "S7777777"
    url = "https://testdriven.io"
    auth = ("test", "test")
    tx = 999999999
    patch_called = False
    def mock_get(*args, **kwargs):
        nonlocal patch_called
        patch_called = True
        return MockResponseGet410()
    monkeypatch.setattr(Session, "get", mock_get)
    # Act
    with pytest.raises(Exception) as exc_info:
        dsu.fetch_entity_from_green(opgave_or_sag_id, url, auth, tx)
    # Assert
    assert patch_called
    assert exc_info.value.args[0] == "Expected status code 200 (or 410 with invalid opgaver), but got 410 for entity JURKAT. Error: Invalid opgave - It's gone - https://testdriven.io"

class MockResponseGet410:
    def __init__(self):
        self.status_code = 410
        self.text = "the opgave has status: INVALID, it's gone now."
        self.reason = "It's gone"
        self.url = "https://testdriven.io"
        self.headers = {
            "Content-Type": "application/json",
            "Content-Length": "123",
            "Content-Location": "tx=123",
        }

# From dsu
def fetch_entity_from_green(opgave_or_sag_id, url, auth, tx):
    """Retrieve missing entity from green's api.

    Parameters
    ----------
    opgave_or_sag_id : string, required
    tx : int, required
        A tx number.
    url : str, required
    auth : AWSAuthObject, required

    Returns
    -------
    entity : dict
        A dict representing the entity from green.
    status_code : int

    """
    try:
        ENTITY = os.environ["ENTITY"]
        url_with_id = url + str(opgave_or_sag_id)
        s = fetch_request_session_with_retries()
        r = s.get(url_with_id, auth=auth)
        handle_non_200_response_for_invalid_opgaver(r, opgave_or_sag_id, ENTITY)
        # I deleted the rest, not relevant in this test as the above function should throw an exception.

    except Exception as e:
        print(f"An exception occured on request with id {opgave_or_sag_id}: {e}")

异常应放在hander_non_200_response_for_invalid_opgaver

def handle_non_200_response_for_invalid_opgaver(request, opgave_or_sag_id, ENTITY):
    """
    Handles a non-200 response from the API but allows 410 responses on invalid opgaver.
    """
    # 410 because Team Green returns this for invalid opgaver, which becomes a valid response.
    if request.status_code != 200 and (
        request.status_code == 410 and ENTITY != "OPGAVER"
    ):
        print(f"Status code for request on {opgave_or_sag_id}: {request.status_code}")
        raise Exception(  # TODO be more explicit with exception.
            f"Expected status code 200 (or 410 with invalid opgaver), but got {request.status_code} for entity {ENTITY}. Error: {request.text} - {request.reason} - {request.url}"
        )

中,测试经历了,所以我在正确的道路上:

def test_handle_non_200_response():
    # Arrange
    r = MockResponse()
    # Act
    with pytest.raises(Exception) as exc_info:
        handle_non_200_response(r)
    # Assert
    assert (
        exc_info.value.args[0]
        == "Expected status code 200, but got 504. Error: Gateway Timeout - Exceeded 30 seconds - https://testdriven.io"
    )

class MockResponse:
    def __init__(self):
        self.status_code = 504
        self.text = "Gateway Timeout"
        self.reason = "Exceeded 30 seconds"
        self.url = "https://testdriven.io"

    def json(self):
        return {"id": 1}

def handle_non_200_response(request):
    """
    Handles a non-200 response from the API.
    """
    if request.status_code != 200:
        print(f"Status code for request on {id}: {request.status_code}")
        raise Exception(
            f"Expected status code 200, but got {request.status_code}. Error: {request.text} - {request.reason} - {request.url}"
        )

您能看到我误入歧途吗?

I can not get the exception to throw on this unit test.

def test_something(monkeypatch):
    # Arrange
    os.environ["ENTITY"] = "JURKAT" # should monkeypatch this, but ignore for now.
    opgave_or_sag_id = "S7777777"
    url = "https://testdriven.io"
    auth = ("test", "test")
    tx = 999999999
    patch_called = False
    def mock_get(*args, **kwargs):
        nonlocal patch_called
        patch_called = True
        return MockResponseGet410()
    monkeypatch.setattr(Session, "get", mock_get)
    # Act
    with pytest.raises(Exception) as exc_info:
        dsu.fetch_entity_from_green(opgave_or_sag_id, url, auth, tx)
    # Assert
    assert patch_called
    assert exc_info.value.args[0] == "Expected status code 200 (or 410 with invalid opgaver), but got 410 for entity JURKAT. Error: Invalid opgave - It's gone - https://testdriven.io"

class MockResponseGet410:
    def __init__(self):
        self.status_code = 410
        self.text = "the opgave has status: INVALID, it's gone now."
        self.reason = "It's gone"
        self.url = "https://testdriven.io"
        self.headers = {
            "Content-Type": "application/json",
            "Content-Length": "123",
            "Content-Location": "tx=123",
        }

# From dsu
def fetch_entity_from_green(opgave_or_sag_id, url, auth, tx):
    """Retrieve missing entity from green's api.

    Parameters
    ----------
    opgave_or_sag_id : string, required
    tx : int, required
        A tx number.
    url : str, required
    auth : AWSAuthObject, required

    Returns
    -------
    entity : dict
        A dict representing the entity from green.
    status_code : int

    """
    try:
        ENTITY = os.environ["ENTITY"]
        url_with_id = url + str(opgave_or_sag_id)
        s = fetch_request_session_with_retries()
        r = s.get(url_with_id, auth=auth)
        handle_non_200_response_for_invalid_opgaver(r, opgave_or_sag_id, ENTITY)
        # I deleted the rest, not relevant in this test as the above function should throw an exception.

    except Exception as e:
        print(f"An exception occured on request with id {opgave_or_sag_id}: {e}")

The exception should be thrown in handle_non_200_response_for_invalid_opgaver because mock_get returns a 410 status code and ENTITY is set to JURKAT:

def handle_non_200_response_for_invalid_opgaver(request, opgave_or_sag_id, ENTITY):
    """
    Handles a non-200 response from the API but allows 410 responses on invalid opgaver.
    """
    # 410 because Team Green returns this for invalid opgaver, which becomes a valid response.
    if request.status_code != 200 and (
        request.status_code == 410 and ENTITY != "OPGAVER"
    ):
        print(f"Status code for request on {opgave_or_sag_id}: {request.status_code}")
        raise Exception(  # TODO be more explicit with exception.
            f"Expected status code 200 (or 410 with invalid opgaver), but got {request.status_code} for entity {ENTITY}. Error: {request.text} - {request.reason} - {request.url}"
        )

I can get an exception to throw using pytest.raises(Exception) in a different test (see below), and the test passes, so I'm on the right track:

def test_handle_non_200_response():
    # Arrange
    r = MockResponse()
    # Act
    with pytest.raises(Exception) as exc_info:
        handle_non_200_response(r)
    # Assert
    assert (
        exc_info.value.args[0]
        == "Expected status code 200, but got 504. Error: Gateway Timeout - Exceeded 30 seconds - https://testdriven.io"
    )

class MockResponse:
    def __init__(self):
        self.status_code = 504
        self.text = "Gateway Timeout"
        self.reason = "Exceeded 30 seconds"
        self.url = "https://testdriven.io"

    def json(self):
        return {"id": 1}

def handle_non_200_response(request):
    """
    Handles a non-200 response from the API.
    """
    if request.status_code != 200:
        print(f"Status code for request on {id}: {request.status_code}")
        raise Exception(
            f"Expected status code 200, but got {request.status_code}. Error: {request.text} - {request.reason} - {request.url}"
        )

Can you see where I have gone astray?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文