pytest-sftpserver
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
- breaking Python Version Support: The last release (1.3.0) officially supports Python 2.7, 3.5-3.7. It has not been updated since 2019, meaning newer Python versions (3.8+) are not officially supported and may lead to compatibility issues or unexpected behavior.
- gotcha Tests Hanging: Tests using the `sftpserver` fixture may hang indefinitely after completion if the SFTP client connections (e.g., `paramiko.Transport` or `paramiko.SFTPClient`) are not explicitly and gracefully closed.
- gotcha In-Memory Content Only: The `sftpserver` fixture serves content directly from Python objects (dictionaries passed to `sftpserver.serve_content`) rather than interacting with actual files on the local filesystem.
Install
-
pip install pytest-sftpserver
Imports
- sftpserver
def test_my_sftp_client(sftpserver): ...
Quickstart
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"