Sometimes you can forget to mock all of the functions with side effect within your unittests and that might cause some issue. First it violates unittests principle to be launched in isolated environment and to not strictrly depend on any component that is not tested at the moment. And it might happen that you will not be able to launch unittests after your Internet connection is gone.

Solution

Obviously you should use pytest for your unittests and it has nice autouse fixtures. We can mock built in socket module before any invocation of test and therefore intercept any call that tries establish some connection. Then some kind of exception should be thrown showing we have to mock function thas has side effect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import pytest


class SocketBlockedException(RuntimeError):
"""
An exception raised in tests when some function tries to establish a connection.
"""


@pytest.yield_fixture(autouse=True)
def disable_socket():
socket_mock = mock.patch('socket.socket', side_effect=SocketBlockedException).start()
yield
socket_mock.stop()

This approach will ensure we do not have any unexpected calls and our unittests can be launched even without Internet connection being active.

Resources