testpath

0.6.0 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →