disable network on pytest

Solutions on MaxInterview for disable network on pytest by the best coders in the world

showing results for - "disable network on pytest"
Paul
28 Mar 2018
1# conftest.py
2import pytest
3import socket
4
5_original_connect = socket.socket.connect
6
7def patched_connect(*args, **kwargs):
8    ...
9    # It depends on your testing purpose
10    # You may want a exception, add here
11    # If you test unconnectable situations
12    # it can stay like this 
13    
14
15@pytest.fixture
16def enable_network():
17    socket.socket.connect = _original_connect
18    yield
19    socket.socket.connect = patched_connect
20
21@pytest.fixture
22def disable_network():
23    socket.socket.connect = patched_connect
24    yield
25    socket.socket.connect = _original_connect
26
Manuel
07 Jan 2018
1# test_internet.py
2def test_your_unconnectable_situation(disable_network):
3    response = request.get('http://stackoverflow.com/')
4    response.status_code == 400
5
similar questions
queries leading to this page
disable network on pytest