1+ """Shared pytest fixtures for the test suite."""
2+ import os
3+ import tempfile
4+ import shutil
5+ from pathlib import Path
6+ from unittest .mock import Mock , MagicMock
7+ import pytest
8+
9+
10+ @pytest .fixture
11+ def temp_dir ():
12+ """Create a temporary directory for test files."""
13+ temp_path = tempfile .mkdtemp ()
14+ yield Path (temp_path )
15+ # Cleanup after test
16+ shutil .rmtree (temp_path , ignore_errors = True )
17+
18+
19+ @pytest .fixture
20+ def temp_file (temp_dir ):
21+ """Create a temporary file in the temp directory."""
22+ def _make_temp_file (name = "test_file.txt" , content = "" ):
23+ file_path = temp_dir / name
24+ file_path .write_text (content )
25+ return file_path
26+ return _make_temp_file
27+
28+
29+ @pytest .fixture
30+ def mock_config ():
31+ """Create a mock configuration object."""
32+ config = Mock ()
33+ config .debug = False
34+ config .verbose = True
35+ config .timeout = 30
36+ config .max_retries = 3
37+ return config
38+
39+
40+ @pytest .fixture
41+ def mock_network_response ():
42+ """Create a mock network response."""
43+ response = Mock ()
44+ response .status_code = 200
45+ response .text = "Mock response content"
46+ response .json .return_value = {"status" : "success" , "data" : []}
47+ response .headers = {"Content-Type" : "application/json" }
48+ return response
49+
50+
51+ @pytest .fixture
52+ def mock_socket ():
53+ """Create a mock socket object."""
54+ sock = MagicMock ()
55+ sock .connect .return_value = None
56+ sock .send .return_value = 10
57+ sock .recv .return_value = b"Mock data"
58+ sock .close .return_value = None
59+ return sock
60+
61+
62+ @pytest .fixture
63+ def sample_pcap_data ():
64+ """Provide sample PCAP packet data for testing."""
65+ # This would contain mock packet data
66+ # In real tests, you might load actual test PCAP files
67+ return b"\xd4 \xc3 \xb2 \xa1 \x02 \x00 \x04 \x00 " # PCAP magic number
68+
69+
70+ @pytest .fixture
71+ def sample_credentials ():
72+ """Provide sample credentials for testing."""
73+ return {
74+ "username" : "test_user" ,
75+ "password" : "test_pass" ,
76+ "host" : "127.0.0.1" ,
77+ "port" : 22
78+ }
79+
80+
81+ @pytest .fixture
82+ def mock_ssh_client ():
83+ """Create a mock SSH client."""
84+ client = Mock ()
85+ client .connect .return_value = None
86+ client .exec_command .return_value = (
87+ Mock (), # stdin
88+ Mock (read = lambda : b"command output" ), # stdout
89+ Mock (read = lambda : b"" ) # stderr
90+ )
91+ client .close .return_value = None
92+ return client
93+
94+
95+ @pytest .fixture
96+ def sample_pdf_content ():
97+ """Provide sample PDF content for testing."""
98+ # Minimal PDF structure
99+ return b"%PDF-1.4\n 1 0 obj\n << /Type /Catalog >>\n endobj\n %%EOF"
100+
101+
102+ @pytest .fixture
103+ def sample_html_content ():
104+ """Provide sample HTML content for testing."""
105+ return """
106+ <html>
107+ <head><title>Test Page</title></head>
108+ <body>
109+ <h1>Test Content</h1>
110+ <img src="test.jpg" alt="Test Image">
111+ <p>Sample paragraph</p>
112+ </body>
113+ </html>
114+ """
115+
116+
117+ @pytest .fixture (autouse = True )
118+ def reset_environment (monkeypatch ):
119+ """Reset environment variables for each test."""
120+ # Store original environment
121+ original_env = os .environ .copy ()
122+
123+ yield
124+
125+ # Restore original environment
126+ os .environ .clear ()
127+ os .environ .update (original_env )
128+
129+
130+ @pytest .fixture
131+ def capture_stdout (monkeypatch ):
132+ """Capture stdout for testing print statements."""
133+ import io
134+ import sys
135+
136+ captured_output = io .StringIO ()
137+ monkeypatch .setattr (sys , 'stdout' , captured_output )
138+
139+ yield captured_output
140+
141+ # Reset stdout
142+ monkeypatch .undo ()
143+
144+
145+ # Markers for test organization
146+ def pytest_configure (config ):
147+ """Register custom markers."""
148+ config .addinivalue_line (
149+ "markers" , "unit: mark test as a unit test"
150+ )
151+ config .addinivalue_line (
152+ "markers" , "integration: mark test as an integration test"
153+ )
154+ config .addinivalue_line (
155+ "markers" , "slow: mark test as slow running"
156+ )
157+ config .addinivalue_line (
158+ "markers" , "network: mark test as requiring network access"
159+ )
160+ config .addinivalue_line (
161+ "markers" , "skipif_no_bluetooth: skip if bluetooth is not available"
162+ )
0 commit comments