pytest-sftpserver

1.3.0 · maintenance · verified Sun Apr 12

pytest-sftpserver is a plugin for pytest that provides a local SFTP-Server fixture. The SFTP-Server provided by this fixture serves content directly from Python objects instead of files, simplifying local testing of SFTP client connections. The current version is 1.3.0, released in 2019, suggesting a moderate release cadence based on maintenance needs.

Warnings

Install

Imports

Quickstart

To use `pytest-sftpserver`, define a test function that accepts the `sftpserver` fixture. Inside the test, use `sftpserver.serve_content()` as a context manager to specify the in-memory file structure to be served. Client code (e.g., using `paramiko`) can then connect to the SFTP server using `sftpserver.host` and `sftpserver.port` to interact with this content. Note that `paramiko` is a common dependency for client code interacting with the SFTP server, but not a direct dependency of the `pytest-sftpserver` plugin itself.

from contextlib import closing
import paramiko
import pytest
import os

# Helper function that would typically be part of your application code
def get_sftp_file(host, port, username, password, path):
    with closing(paramiko.Transport((host, port))) as transport:
        transport.connect(username=username, password=password)
        with closing(paramiko.SFTPClient.from_transport(transport)) as sftpclient:
            with sftpclient.open(path, "r") as sftp_file:
                return sftp_file.read().decode('utf-8')

# A test function using the sftpserver fixture
def test_sftp_fetch(sftpserver):
    # The sftpserver fixture is automatically discovered and injected
    # It serves content from Python objects (dictionaries)
    with sftpserver.serve_content({'a_dir': {'somefile.txt': b"File content"}}):
        # Use environment variables or dummy values for actual connection
        # In a real scenario, these might come from test configuration
        assert get_sftp_file(sftpserver.host, sftpserver.port, "user", "pw", "/a_dir/somefile.txt") == "File content"

view raw JSON →