1

I want to ensure that my tests don't use third-party services when they are run.

These answers cover the case when no connections are possible at all.

I want to be able to make TCP connections to the local database (one of the cases).

How to patch Python runtime to allow TCP connections to localhost (or a given host, ideally to the specified ports)?

warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • Based on the answers in your linked question, I believe writing a test in the mock of `socket.socket` to check if your address is localhost (and forward to the real `socket.socket`) or raise an exception should be relatively straightforward – GPhilo Jun 28 '21 at 09:59
  • I tried it to do this some time ago. I don't remember exactly why, but it didn't work that easy. – warvariuc Jun 28 '21 at 11:47

1 Answers1

1

pytest-socket allows disabling socket altogether or allow connections only to specified hosts.

warvariuc
  • 57,116
  • 41
  • 173
  • 227