Python: block network connections for testing purposes?

前端 未结 4 1580
谎友^
谎友^ 2020-12-14 01:26

I\'m trying to test a package that provides interfaces to a few web services. It has a test suite that is supposed to test most functions without connecting to the

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 01:29

    Monkey patching socket ought to do it:

    import socket
    def guard(*args, **kwargs):
        raise Exception("I told you not to use the Internet!")
    socket.socket = guard
    

    Make sure this runs before any other import.

提交回复
热议问题