“Nonaktifkan Jaringan di Pytest” Kode Jawaban

Nonaktifkan Jaringan di Pytest

# conftest.py
import pytest
import socket

_original_connect = socket.socket.connect

def patched_connect(*args, **kwargs):
    ...
    # It depends on your testing purpose
    # You may want a exception, add here
    # If you test unconnectable situations
    # it can stay like this 
    

@pytest.fixture
def enable_network():
    socket.socket.connect = _original_connect
    yield
    socket.socket.connect = patched_connect

@pytest.fixture
def disable_network():
    socket.socket.connect = patched_connect
    yield
    socket.socket.connect = _original_connect
Puzzled Puffin

Nonaktifkan Jaringan di Pytest

# test_internet.py
def test_your_unconnectable_situation(disable_network):
    response = request.get('http://stackoverflow.com/')
    response.status_code == 400
Puzzled Puffin

Jawaban yang mirip dengan “Nonaktifkan Jaringan di Pytest”

Pertanyaan yang mirip dengan “Nonaktifkan Jaringan di Pytest”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya