Design Cross-Platform Path and Process Tests
Core idea
Portable code uses Path for local filesystem work and PureWindowsPath or PurePosixPath for lexical reasoning about a specific path flavor. Process tests should avoid shell syntax, use explicit argument lists and sys.executable, and check availability of platform-specific capabilities. A pure path test is not a filesystem test on another OS.
from pathlib import PureWindowsPath, PurePosixPatha = PureWindowsPath('C:/data/box.txt')b = PurePosixPath('/data/box.txt')print(a.drive, b.drive == '')print(a.name == b.name)Windows drives and POSIX roots differ even when basenames match.
Expected output:
C: TrueTrueRun a child without shell syntax
import sys, subprocessr = subprocess.run([sys.executable, '-c', 'import sys; print(sys.argv[1])', 'two words'], capture_output=True, text=True, check=True, timeout=5)print(r.stdout.strip())An argument containing a space remains one argument on this tested system.
Expected output:
two wordsCommon mistake
Do not normalize foreign paths with the local OS path rules or claim Windows execution from a PureWindowsPath test. Subprocess platform restrictions and executable behavior still matter.
Practice
Run the child-process and temporary-file tests on Linux and Windows in separate environments. Record failures and skips, not just a combined success label.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cross-platform testing
Check your understanding of cross-platform testing.
What is printed?
from pathlib import PureWindowsPathprint(PureWindowsPath('D:/box').drive)What is printed?
from pathlib import PurePosixPathprint(PurePosixPath('/a/b').name)What does PureWindowsPath on Linux prove?
What reduces shell-specific quoting dependencies?