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.
pip install pytest-shutilVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade Python environment to 3.6 or newer. Review code for Python 2/early 3.x specific constructs.
Migrate `path.py` usage to `pathlib.Path` objects. Replace `mock` imports and usage with `unittest.mock`.
If shell features are needed, modify `workspace.run()` calls to `workspace.run('your_command', shell=True, ...)`.Ensure pytest-shutil is installed in your testing environment. If it is, verify that pytest is correctly discovering your test files.
Use the 'workspace' object directly as a path without attempting to call it. (e.g., `workspace / 'my_file.txt'` instead of `workspace() / 'my_file.txt'`).
Remove the direct import statement. Instead, include 'workspace' as an argument in your test function signature to access the fixture.
Access the `stdout` or `stderr` attributes of the `CommandResult` object to work with the command's output as strings.