Registry / testing / testpath

testpath

JSON →
library0.6.0pypypi✓ verified 24d ago

Testpath is a collection of utilities for Python code working with files and commands. It contains functions to check things on the filesystem, and tools for mocking system commands and recording calls to those. The current version is 0.6.0.

pip install testpath
INSTALL
IMPORT
SIG · TESTPATH
T
testpath
testingpythonv0.6.0
Install
1.5s avg
Import
30ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.0 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.032s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.028s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

testpath
import testpath
The library provides functions and context managers directly under the `testpath` module.

Demonstrates asserting the existence of a temporary file and then using `testpath.assert_calls` to mock an external system command during a function call.

import tempfile import os import testpath # 1. Asserting filesystem state with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f: f.write(b"hello world") temp_file_path = f.name try: testpath.assert_isfile(temp_file_path) print(f"Assertion successful: {temp_file_path} is a file.") finally: os.remove(temp_file_path) # 2. Mocking system commands def func_that_calls_git(): # In a real scenario, this would execute a 'git' command # e.g., subprocess.run(['git', 'status']) print("Simulating a call to 'git status'") with testpath.assert_calls('git', ['status']): func_that_calls_git() print("Mocked 'git status' call intercepted.")
Debug
Known issues
gotchaWhen mocking system commands using `testpath.assert_calls` or similar utilities, `testpath` manipulates the system's `PATH` environment variable. This makes the command mock global to the entire process, affecting all threads and coroutines, not just the isolated test scope. This can lead to unexpected interference and flaky tests in concurrent testing scenarios.
fix
Be aware of the global nature of command mocking. For highly concurrent tests, consider alternative mocking strategies that are process-isolated or carefully manage `PATH` manipulation across test setups/teardowns, though this is difficult with `testpath`'s approach. Ensure mocks are properly torn down.
affects: All versions
gotchaThe `testpath.assert_calls` utility is specifically designed to mock *external system commands* by replacing them in the system's `PATH`. It does not directly mock Python functions or modules. If your code calls a Python function that then uses `subprocess.run` to execute an external command, `assert_calls` will intercept the *external command*. However, it will not intercept calls to Python functions themselves or if the 'command' is a Python-internal function call disguised as an external command.
fix
Use `testpath` for mocking external commands only. For mocking Python functions, classes, or methods, use Python's built-in `unittest.mock` library or other Python-specific mocking frameworks.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'testpath'
The testpath package is not installed in the current Python environment.
fix
pip install testpath
AttributeError: module 'testpath' has no attribute 'assert_exists'
The function 'assert_exists' does not exist in the testpath module; the correct function name is 'assert_path_exists'.
fix
Use testpath.assert_path_exists('/path/to/check') instead.
ModuleNotFoundError: No module named 'testpath.commands'
The MockCommand class is directly available under the testpath module, not within a submodule named 'commands'.
fix
Import MockCommand directly from testpath using: from testpath import MockCommand
TypeError: MockCommand.__init__() missing 1 required positional argument: 'name'
The MockCommand class requires the name of the command to be mocked as its first argument during instantiation.
fix
Provide the command name when creating MockCommand, for example: with testpath.MockCommand('ls') as mock_ls:
Upgrade
Version history
0.6.0latest on PyPI · released Feb 23, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Resources
testpath — pip install testpath · libregistry