0

I am trying to implement a unit test for the wait_until function. However, I don't have api_key. call_api function is auto-generated. Sometimes get_api can have multiple required parameters. I want to test if get_api function can be called using a fake response.id. The test should fail if there is a missing parameter. Is it possible to create a mock test to achieve that?

 def call_api(api_key, wait):
    response = api(api_key)
    if wait:
         try:
            result = wait_until(get_api(response.id), state='Success')
        except Exception:
                raise
    print Success
  • Does this answer your question? [How to get call\_count after using pytest mocker.patch](https://stackoverflow.com/questions/71803679/how-to-get-call-count-after-using-pytest-mocker-patch) – Marco.S Jul 15 '22 at 14:34

1 Answers1

-1

I believe you can achieve that with pytest-mock.

You can write a test like this:

def test_call_api(mocker):
    mocker.patch(
        'my_module.main.api',
        return_value="mocked_value"
    )
    mocker.patch(
        'my_module.main.get_api',
        return_value="mocked_value"
    )

    expected = "expected_value"
    actual = call_api(api_key, wait)
    assert expected == actual

Please check this guide.

Marco.S
  • 383
  • 2
  • 10