简体   繁体   中英

Python mock assert called fails even if mock called

I'm using mock and pytest to run several unit test on my project but I'm facing a very strange error.

In a specific test in mock a function called in the function I'm testing and I want to check that this function is called with specific parameters. But the mock_function.assert_called() fails.

To debug it, I add the following lines :

    print(f"called: {str( mock_send_ano.called )}")
    print(f"call args: {str( mock_send_ano.call_args )}")
    assert mock_send_ano.assert_called()
    assert mock_send_ano.assert_called_with(expected_parameter)

In the output console I got :

called: True
call args: call({'msn': 'F8231', 'flight': 'V0001'})

with expected_parameter= {'msn': 'F8231', 'flight': 'V0001'}

And I still get this error

AssertionError: assert None
   +  where None = <bound method NonCallableMock.assert_called of <MagicMock name='send_anomaly' id='2120318385680'>>()

Here is the function I want to test :

def retrieve_anomalies(file):
try:
    if file.split(".")[-1] == "csv" and "anomalies" in file.split("/")[3]:

        print(file)
        s3 = boto3.client("s3")
        csvfile = s3.get_object(Bucket=file.split("/")[2],Key=file.split("/")3])

        csvcontent = csvfile["Body"].read().decode("utf-8").splitlines()
        csv_data = csv.DictReader(csvcontent)

        for row in csv_data:

            anomaly = {
                "msn": row["flight_id"][:5],
                "flight": row["flight_id"][5:]
            }

            send_anomaly(anomaly)

and the test function :

@mock.patch("boto3.client", new=mock.MagicMock())
@mock.patch("lambda_out.lambda-out.send_anomaly")
@mock.patch("csv.DictReader")
def test_ok(mock_csv_reader, mock_send_ano):
    csv_data = [
        {
            "flight_id": "F8231V0001"
        }
    ]
    mock_csv_reader.return_value = csv_data
    file = "s3://bucket/anomalies.csv"

    lambda_out.retrieve_anomalies(file)

    anomaly = {
        "msn": "F8231",
        "flight": "V0001"
    }
    print(f"called: {str( mock_send_ano.called )}")
    print(f"call args: {str( mock_send_ano.call_args )}")
    assert mock_send_ano.assert_called()
    assert mock_send_ano.assert_called_with(anomaly)

When using the assert_called methods from unittest.mock you need to omit the assert calls. Your code simply becomes:

@mock.patch("lambda_out.lambda-out.send_anomaly")
@mock.patch("csv.DictReader")
def test_ok(mock_csv_reader, mock_send_ano):
    csv_data = [
        {
            "flight_id": "F8231V0001"
        }
    ]
    mock_csv_reader.return_value = csv_data
    file = "s3://bucket/anomalies.csv"

    lambda_out.retrieve_anomalies(file)

    anomaly = {
        "msn": "F8231",
        "flight": "V0001"
    }
    print(f"called: {str( mock_send_ano.called )}")
    print(f"call args: {str( mock_send_ano.call_args )}")
    mock_send_ano.assert_called()
    mock_send_ano.assert_called_with(anomaly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM