Python · 952 bytes Raw Blame History
1 """Pytest configuration and fixtures."""
2
3 import sys
4 import tempfile
5 from pathlib import Path
6
7 import pytest
8
9 ROOT = Path(__file__).resolve().parents[1]
10 SRC = ROOT / "src"
11
12 if str(SRC) not in sys.path:
13 sys.path.insert(0, str(SRC))
14
15
16 @pytest.fixture
17 def temp_dir():
18 """Create a temporary directory for tests."""
19 with tempfile.TemporaryDirectory() as tmpdir:
20 yield Path(tmpdir)
21
22
23 @pytest.fixture
24 def sample_file(temp_dir):
25 """Create a sample file for testing."""
26 file_path = temp_dir / "sample.txt"
27 file_path.write_text("Line 1\nLine 2\nLine 3\n")
28 return file_path
29
30
31 @pytest.fixture
32 def sample_python_file(temp_dir):
33 """Create a sample Python file for testing."""
34 file_path = temp_dir / "sample.py"
35 file_path.write_text(
36 '''"""Sample module."""
37
38 def hello():
39 """Say hello."""
40 return "Hello, world!"
41
42 def add(a, b):
43 """Add two numbers."""
44 return a + b
45 '''
46 )
47 return file_path