Registry / testing / pytest-console-scripts

pytest-console-scripts

JSON →
library1.4.1pypypiunverified

pytest-console-scripts is a pytest plugin designed for comprehensive testing of Python console scripts. It offers two execution modes: an 'in-process' mode for fast development iteration by running scripts within the same interpreter as pytest, and a 'subprocess' mode to simulate real-world execution environments. The library is actively maintained, with its latest version being 1.4.1, and typically follows a minor release cadence as needed for bug fixes and Python version support.

pip install pytest-console-scripts
INSTALL
IMPORT
SIG · PYTEST-CONSOLE-SCR
P
pytest-console-scripts
testingpythonv1.4.1
Install
2.7s avg
Import
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibc
py 3.103.940 runs
installs and imports cleanly · install 2.7s · import 0.000s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

script_runner
def test_my_script(script_runner): ...
The `script_runner` is provided as a pytest fixture and does not require an explicit import in test files, though it can be type-hinted if desired: `from pytest_console_scripts import ScriptRunner`

To use `pytest-console-scripts`, define your console scripts in your project's `setup.py` (or `pyproject.toml`) and install your package in editable mode (`pip install -e .`). Then, in your pytest test files, use the `script_runner` fixture to execute your scripts and assert their output and exit codes. This example demonstrates creating a minimal project structure, installing it, and testing a console script using both default (in-process) and subprocess launch modes.

import pytest import subprocess import sys # Create a dummy console script and setup.py for demonstration # In a real project, these would exist in your project structure # --- my_package/setup.py --- # from setuptools import setup, find_packages # setup( # name='my_package', # version='0.1.0', # packages=find_packages(), # entry_points={ # 'console_scripts': [ # 'my-script=my_package.main:main' # ] # }, # ) # --- my_package/main.py --- # import sys # def main(): # if len(sys.argv) > 1 and sys.argv[1] == '--hello': # print('Hello from my-script') # else: # print('Running my-script') # Simplified setup for a runnable quickstart within one file: # Create dummy files for demonstration purposes # A real setup would involve 'pip install -e .' in a virtual environment. # Create a temporary directory and files import tempfile import os def create_dummy_project(tmp_path): pkg_dir = tmp_path / "my_package" pkg_dir.mkdir() (pkg_dir / "__init__.py").touch() (pkg_dir / "main.py").write_text( """ import sys def main(): if len(sys.argv) > 1 and sys.argv[1] == '--hello': print('Hello from my-script') else: print('Running my-script') sys.exit(0) """ ) (tmp_path / "setup.py").write_text( """ from setuptools import setup, find_packages setup( name='my_package', version='0.1.0', packages=find_packages(), entry_points={ 'console_scripts': [ 'my-script=my_package.main:main' ] }, ) """ ) return tmp_path # Example Test File (e.g., test_scripts.py) def test_my_console_script(script_runner, tmp_path): # Set up the dummy project and install it in editable mode project_root = create_dummy_project(tmp_path) subprocess.run([sys.executable, '-m', 'pip', 'install', '-e', str(project_root)], check=True) # Run the script without arguments result = script_runner.run(['my-script']) assert result.returncode == 0 assert 'Running my-script' in result.stdout assert result.stderr == '' # Run the script with an argument result_hello = script_runner.run(['my-script', '--hello']) assert result_hello.returncode == 0 assert 'Hello from my-script' in result_hello.stdout assert result_hello.stderr == '' # Test behavior in subprocess mode (optional) result_subprocess = script_runner.run(['my-script'], launch_mode='subprocess') assert result_subprocess.returncode == 0 assert 'Running my-script' in result_subprocess.stdout
Debug
Known issues
breakingSupport for Python 3.7 has been dropped in version 1.4.1.
fix
Upgrade your Python environment to 3.8 or newer. If you must use Python 3.7, pin your `pytest-console-scripts` version to `<1.4.1`.
affects: >=1.4.1
breakingSupport for Python 3.6 was dropped in version 1.4.0.
fix
Upgrade your Python environment to 3.7 or newer. If you must use Python 3.6, pin your `pytest-console-scripts` version to `<1.4.0`.
affects: >=1.4.0
gotchaWhen testing scripts that require user input (e.g., via `input()`), mocking might not work as expected in `subprocess` mode, as the script runs in a separate Python interpreter. Mocking is generally more reliable in `inprocess` mode.
fix
For scripts requiring input, consider using `inprocess` mode and `pytest-mock` to patch `builtins.input`. Example: `mocker.patch('builtins.input', return_value='<your_expected_input>')`.
affects: All
gotchaDuring development, if you add new console scripts to your `setup.py` or `pyproject.toml`, they might not be immediately discoverable by `pytest-console-scripts` without a reinstallation.
fix
Ensure your package is installed in editable mode (`pip install -e .`) and re-run this command after adding new entry points to ensure they are registered in the environment.
affects: All
Upgrade
Version history
1.4.1latest on PyPI · released May 31, 2023
Audit
Dependencies
pytestrequiredCore testing framework that this plugin extends.
PythonrequiredMinimum required Python version for 1.4.1 is 3.8.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
pytest-console-scripts — pip install pytest-console-scripts · libregistry