Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest: MagicMock-ed Method Fails Assertion #100417

Closed
armartirosyan opened this issue Dec 21, 2022 · 1 comment
Closed

unittest: MagicMock-ed Method Fails Assertion #100417

armartirosyan opened this issue Dec 21, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@armartirosyan
Copy link

armartirosyan commented Dec 21, 2022

Bug report

Nested method fails assertion. More details are in the Environment section

Environment

Files

$ tree
.
├── module.py
└── test_azure_blob.py

module.py

from azure.storage.blob import BlobServiceClient

class AzureBlob:
    def __init__(self):
        self.blob_service_client = BlobServiceClient.from_connection_string("AZURE_STORAGE_CONNECTION_STRING")
        self.container = self.blob_service_client.get_container_client("AZURE_CONTAINER_NAME")

test_azure_blob.py

class TestAzureBlob (unittest.TestCase):

    @patch("module.BlobServiceClient")
    def test_AzureBlob(self, mock_Blob):

        azure_blob = AzureBlob()
        print(mock_Blob)
        print(mock_Blob.mock_calls)
        print(mock_Blob.from_connection_string.mock_calls)
        print(mock_Blob.from_connection_string.get_container_client.mock_calls)
        mock_Blob.from_connection_string.get_container_client.assert_called_once()

if __name__ == "__main__":
    unittest.main()

When tested assertion fails. Traceback is below

$ python -m unittest test_azure_blob.py 
<MagicMock name='BlobServiceClient' id='140122935218912'>
[call.from_connection_string('AZURE_STORAGE_CONNECTION_STRING'),
 call.from_connection_string().get_container_client('AZURE_CONTAINER_NAME'),
 call.__str__()]
[call('AZURE_STORAGE_CONNECTION_STRING'),
 call().get_container_client('AZURE_CONTAINER_NAME')]
[]
F
======================================================================
FAIL: test_AzureBlob (test_azure_blob.TestAzureBlob)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/.pyenv/versions/3.9.1/lib/python3.9/unittest/mock.py", line 1337, in patched
    return func(*newargs, **newkeywargs)
  File "/home/user/Projects/test_azure_blob/test_azure_blob.py", line 23, in test_AzureBlob
    mock_Blob.from_connection_string.get_container_client.assert_called_once()
  File "/home/uaser/.pyenv/versions/3.9.1/lib/python3.9/unittest/mock.py", line 886, in assert_called_once
    raise AssertionError(msg)
AssertionError: Expected 'get_container_client' to have been called once. Called 0 times.

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)

As the prints are showing, the MagicMock identified that the get_container_client nested method was called, but the actual call fails the assertion.

  • CPython versions tested on: [3.9.1, 3.10.2]
  • Operating system and architecture:[Ubuntu x86, Windows x86]
@armartirosyan armartirosyan added the type-bug An unexpected behavior, bug, or error label Dec 21, 2022
@carljm
Copy link
Contributor

carljm commented Dec 22, 2022

You need to change the assertion to look for mock_Blob.from_connection_string.return_value.get_container_client.assert_called_once() -- note the added .return_value. get_container_client is an attribute of the mock returned by calling from_connection_string, it's not an attribute of from_connection_string.

Note that your debug prints also confirm this; the calls are recorded on from_connection_string().get_container_client(...) (note the parentheses); when you print mock_Blob.from_connection_string.get_container_client.mock_calls it is (correctly) empty.

@carljm carljm closed this as completed Dec 22, 2022
@carljm carljm reopened this Dec 22, 2022
@carljm carljm closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants