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
muslpy 3.10–3.940 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibcpy 3.10–3.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
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.