1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Odd errors when trying to verify a requests.post call using pytest-mockito

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 18:02.

  1. Stack

    Stack Membro Participativo

    I'm trying out the pytest-mockito library for some unit testing (I'm primarily a kotlin/java engineer and I think the mockito syntax reads a bit nicer). Most of it makes sense, but I'm running into a weird error when trying to verify a post request.

    class TestSuite:
    def test_verify_we_sent_data_somewhere(self):
    api_url = "http://www.somebooksearch.biz/search"
    payload = {"title": "An updated title", "isbn": 12345}

    update_book(payload)

    verify(requests).post(api_url, data=None, json=payload)


    # Implementation:

    def update_book(payload):
    requests.post("http://www.somebooksearch.biz/search", json=payload)


    When I fire my test case I get the error:


    verify(requests).post(api_url, json=payload, **kwargs) E mockito.mockito.ArgumentError: obj '<module 'requests' from '/Users/cschmitz/Engineering/projects/classroom-book-tracker/.venv/lib/python3.11/site-packages/requests/init.py'>' is not registered

    example_test.py:93: ArgumentError

    It's weird though because I have a different test case where I'm mocking a get request and it passes just fine:

    def test_mock_an_external_library_but_without_comments(self):
    api_url = "http://www.somebooksearch.biz/search"

    mock_response = mock({
    "json": lambda: {"books": [{"title": "Some book title", "isbn": "123456"}]}
    })

    when(requests).get(api_url).thenReturn(mock_response)

    actual = get_book_list()

    assert actual == [{"title": "Some book title", "isbn": "123456"}]


    # Implementation
    def get_book_list():
    response = requests.get("http://www.somebooksearch.biz/search")
    data = response.json()
    return data["books"]


    I see it's saying there's an argument error, but I'm not sure exactly what it means. I know the library's walkthrough says that the mock arguments are really picky, but I'm not exactly sure what I'm missing or have wrong. Any help is appreciated!

    Continue reading...

Compartilhe esta Página