scripttest
raw JSON → 2.0.post1 verified Fri May 01 auth: no python maintenance
A library for testing command-line scripts (CLI applications) in Python. It provides a TestEnvironment that captures stdout, stderr, and simulates stdin. Current version 2.0.post1. Release cadence is low; no new releases for several years.
pip install scripttest Common errors
error AttributeError: module 'scripttest' has no attribute 'testing' ↓
cause Importing from scripttest.testing in scripttest 2.x raises AttributeError because the submodule is removed.
fix
Use 'from scripttest import TestEnvironment'.
error TypeError: cannot unpack non-iterable Result object ↓
cause Unpacking the result of env.run() as a tuple is no longer supported in scripttest 2.x.
fix
Access result.stdout, result.stderr, result.returncode instead of unpacking.
Warnings
breaking TestEnvironment.run() no longer returns a tuple; it returns a Result object with .stdout, .stderr, .returncode attributes. Code using tuple unpacking (stdout, stderr, returncode) in scripttest 1.x will break. ↓
fix Access attributes: result.stdout, result.stderr, result.returncode instead of unpacking.
deprecated scripttest.testing module is deprecated; import TestEnvironment directly from scripttest. ↓
fix Use 'from scripttest import TestEnvironment'.
gotcha By default, TestEnvironment runs commands in a temporary directory. Access to the current working directory or files outside the temp dir may require explicit path setup. ↓
fix Use env.set_cwd('/path') or pass cwd to run().
Imports
- TestEnvironment wrong
from scripttest.testing import TestEnvironmentcorrectfrom scripttest import TestEnvironment
Quickstart
import os
import sys
from scripttest import TestEnvironment
def test_hello_world():
env = TestEnvironment()
# Run a simple echo command
result = env.run('echo', 'Hello World')
assert result.stdout == 'Hello World\n'
assert result.returncode == 0
print("Test passed!")
if __name__ == '__main__':
test_hello_world()