Python

Test for aiohttp's ClientErrorOS

Using mock side effect function

One of the issues of opsdroid was to bump the test coverage of the parsers. Aiohttp is used to connect to the different API's and a ClientOSError exception is raised, when the client fails to connect to the parser API.

I've decided to take on the task to find a way to test these two lines of code that everyone seems to be having issues with.

After doing a bunch of reading and trying different ways to raise the exception, I've finally come to this idea that perhaps mocking a failed connection could work.

From the unit test documentation, the side_effect method looked like something that could provide the answer.

side_effect allows you to perform side effects, including raising an exception when a mock is called -- Unit Test Documentation

python
1async def test_parse_witai_raise_ClientOSError(self):
2 with OpsDroid() as opsdroid:
3 opsdroid.config['parsers'] = [
4 {'name': 'witai', 'access-token': 'test', 'min-score': 0.3}
5 ]
6 mock_skill = amock.CoroutineMock()
7 match_witai('get_weather')(mock_skill)
8
9 mock_connector = amock.CoroutineMock()
10 message = Message("how's the weather outside", "user",
11 "default", mock_connector)
12
13 with amock.patch.object(witai, 'call_witai') as mocked_call:
14 mocked_call.side_effect = ClientOSError()
15 await witai.parse_witai(opsdroid, message,
16 opsdroid.config['parsers'][0])
17
18 self.assertFalse(mock_skill.called)
19 self.assertTrue(mocked_call.called)

The call_witai is the only part of the parser that connects to the Wit.AI API, so this bit is mocked. Then we need to try and parse a message so we use await witai.parse_witai(opsdroid, message,opsdroid.config['parsers'][0])

Since the mocked call to Wit.AI API is returning the exception ClientOSError() the skill mock_skill - mock_skill = amock.CoroutineMock() is not called. But, the mocked call to the API is called so both the tests pass.

The test passes and the coverage were bumped up, but it feels that the exception wasn't really tested at all. All my attempts to assert the exception with self.assertRaises() failed so this is what I came up with.

Do you have any other way that this exception could be tested? I would love to hear from you.

Webmentions

0 Like 0 Comment

You might also like these

How I solved the issue of testing a function that should call sys.exit() when a yaml file couldn't be safely loaded.

Read More
Python

Unittest - How to test for sys.exit

Unittest -  How to test for sys.exit

An example from opsdroid on how to test if a logging call was made successfully.

Read More
Python

Test: Was Logging called

Test: Was Logging called

While working on adding tests to Pyscript I came across a use case where I had to check if an example image is always generated the same.

Read More
Python

How to compare two images using NumPy

How to compare two images using NumPy

Learn what additional permissions you need to add to your user to get django to run tests with a postgresql database.

Read More
Python

Fix django postgresql permissions denied on tests

Fix django postgresql permissions denied on tests