{"id":3829,"library":"testpath","title":"testpath","description":"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.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/jupyter/testpath","tags":["testing","filesystem","path","mocking","jupyter"],"install":[{"cmd":"pip install testpath","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The library provides functions and context managers directly under the `testpath` module.","symbol":"testpath","correct":"import testpath"}],"quickstart":{"code":"import tempfile\nimport os\nimport testpath\n\n# 1. Asserting filesystem state\nwith tempfile.NamedTemporaryFile(suffix=\".txt\", delete=False) as f:\n    f.write(b\"hello world\")\n    temp_file_path = f.name\n\ntry:\n    testpath.assert_isfile(temp_file_path)\n    print(f\"Assertion successful: {temp_file_path} is a file.\")\nfinally:\n    os.remove(temp_file_path)\n\n# 2. Mocking system commands\ndef func_that_calls_git():\n    # In a real scenario, this would execute a 'git' command\n    # e.g., subprocess.run(['git', 'status'])\n    print(\"Simulating a call to 'git status'\")\n\nwith testpath.assert_calls('git', ['status']):\n    func_that_calls_git()\n    print(\"Mocked 'git status' call intercepted.\")\n","lang":"python","description":"Demonstrates asserting the existence of a temporary file and then using `testpath.assert_calls` to mock an external system command during a function call."},"warnings":[{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}