pytest-shutil

1.8.1 · active · verified Mon Apr 13

pytest-shutil is a Python library offering a collection of Unix shell and environment management tools specifically designed for automated tests with `pytest`. It provides fixtures and utilities to streamline tasks such as managing temporary directories (workspaces), executing shell commands, and manipulating environment variables within test contexts. Currently at version 1.8.1, it is actively maintained with recent updates addressing Python compatibility and modern best practices.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `workspace` fixture, which provides a function-scoped temporary directory as a `pathlib.Path` object. It shows how to create an executable script within this workspace and then run it using `workspace.run()` to capture output and check the return code.

import pytest
from pathlib import Path

# To make the workspace fixture available, pytest-shutil must be installed.
# No explicit import is usually needed for fixtures when installed as a plugin.

def test_create_file_and_run_script(workspace):
    """Demonstrates using the workspace fixture to create a file and run a shell command."""
    # 'workspace' is a pathlib.Path object pointing to a temporary directory
    assert isinstance(workspace.workspace, Path)

    # Create a simple shell script in the workspace
    script_content = """#!/bin/bash
    echo "Hello from $1!"
    """
    script_path = workspace.workspace / 'hello.sh'
    script_path.write_text(script_content)
    script_path.chmod(0o755) # Make it executable

    # Run the script relative to the workspace
    result = workspace.run('./hello.sh World', check=True, capture=True)

    assert result.returncode == 0
    assert "Hello from World!" in result.stdout
    print(result.stdout)

view raw JSON →