0

I am writing unit test for a below function in WebSocketNotification.py

async def notify(self,websocket,path):
    if "notify/download" in path:
        await self.__serve_download_status(websocket)
    else:
        await websocket.send('{"status":Invalid URL')

unit test that i wrote :

def test_notify_with_serve_download_status(self):
    path="notify/download"
    webnotif = WebSocketNotification()
    webnotif.__serve_download_status = MagicMock()
    
    webnotif.notify(websockets,path)
    webnotif.__serve_download_status.assert_called_once(websockets)

I am getting error : AssertionError : Expected 'mock' to have been called once. Called 0 times.

  • Does it help declaring `test_notify_with_serve_download_status` as `async`? – md2perpe Mar 26 '22 at 15:59
  • `await`, but you've tagged this with Python 2? – 9769953 Mar 26 '22 at 16:00
  • @md2perpe yep !!, it passed. Thanks a lot !! :) – Paras Jain Mar 26 '22 at 16:05
  • @9769953 sry i didnt knew await is not in python 2 . I am new to python – Paras Jain Mar 26 '22 at 16:05
  • @md2perpe but i am also getting a warning, TestWebSocketNotification.test_notify_with_serve_download_status was never awaited. any comment over this? – Paras Jain Mar 26 '22 at 16:08
  • Change to `await webnotif.notify(websockets, path)` – md2perpe Mar 26 '22 at 17:38
  • @md2perpe, Thanks for the help , I tried to run the code coverage tool for this, and found that this test case is not covering the method at all, can you suggest what could be the reason. what mistake is made inside testcase ? – Paras Jain Mar 26 '22 at 20:24
  • @md2perpe you can also refer full file here : https://stackoverflow.com/questions/71627626/how-to-write-unittest-for-a-websocket-notification-class-in-python?noredirect=1#comment126597026_71627626 I have raised this here as well on above URL in stackoverflow . – Paras Jain Mar 26 '22 at 20:25
  • @ParasJain. That's not the full file. Definitions or imports for `NotifBase`, `Logger`, `CommonConfig` and `websockets` are missing. – md2perpe Mar 26 '22 at 20:36
  • @md2perpe i can mock the CommonConfig, cannot send here. for websockets i will update the code. NotifBase is also i cannot share. can you help with the rest of code , just an example of how to write UT for this code – Paras Jain Mar 26 '22 at 20:39
  • What test framework are you using? – md2perpe Mar 26 '22 at 20:55
  • @md2perpe i am using pytest – Paras Jain Mar 26 '22 at 21:00
  • This class `AsyncMock` might be useful: https://stackoverflow.com/a/32498408/1412534 – md2perpe Mar 26 '22 at 21:49

1 Answers1

0

Install pytest-asyncio. Then define an AsyncMock class:

class AsyncMock(MagicMock):
    async def __call__(self, *args, **kwargs):
        return super().__call__(*args, **kwargs)

and change your test method to

@pytest.mark.asyncio
async def test_notify_with_serve_download_status(self):
    path="notify/download"
    webnotif = WebSocketNotification()
    webnotif.__serve_download_status = AsyncMock()
    webnotif.notify(websockets,path)
    webnotif.__serve_download_status.assert_called_once(websockets)
md2perpe
  • 3,372
  • 2
  • 18
  • 22