Disable Internet connection in unittests
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 | import pytest |
This approach will ensure we do not have any unexpected calls and our unittests can be launched even without Internet connection being active.