Home > OS >  How to mock test wait_until logic in a python3 using pytest
How to mock test wait_until logic in a python3 using pytest

Time:07-16

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

CodePudding user response:

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.

  • Related