Python: block network connections for testing purposes?

前端 未结 4 1582
谎友^
谎友^ 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:49

    A simple way to put a gag on the requests library:

    from unittest import mock
    
    requests_gag = mock.patch(
        'requests.Session.request',
        mock.Mock(side_effect=RuntimeError(
            'Please use the `responses` library to mock HTTP in your tests.'
        ))
    )
    
    with requests_gag:
        ...  # no Internet here
    
    
    

提交回复
热议问题