'PyTest: Assert a a call of a object inside a function

I trying to test if an object method is called inside a function. The object is instantiated in the function itselfs. So I mocked the class and tried to assert the call using the class.

I debugged the function and the method its called for sure but the assert keep failing with:

    raise AssertionError(msg)
    AssertionError: Expected 'populate' to have been called.

This is the test m running

from pytest_mock import MockerFixture
def test_populate_model(mocker: MockerFixture):
    mocker.patch('pyspark.sql.functions.col')
    mocker.patch('pyspark.sql.functions.concat')
    mocker.patch('pyspark.sql.functions.lit')
    mocker.patch('pyspark.sql.functions.trim')
    mocker.patch('pyspark.sql.functions.regexp_replace')
    with mocker.patch('src.core.model.my_model.MyModel') as amm:
        from src.core.tasks import populate_model
        from src.core.model.my_model import MyModel
        MyModel.check_state.return_value = 1
        populate_model("", "", "", "", "", "", "")
        MyModel.populate.assert_called() # Fails here
        MyModel.store.assert_called()

Code of the function tested:

def populate_model(spark_context, dbutils, user, password, url, driver, model_path):
    model = MyModel(
        user=user,
        password=password,
        url=url,
        driver=driver,
        spark_context=spark_context,
        dbutils=dbutils,
        model_path=model_path
    )
    if model.check_state():
        model.populate()
        model.store()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source