-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathconftest.py
More file actions
147 lines (118 loc) · 3.77 KB
/
conftest.py
File metadata and controls
147 lines (118 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sys
import tempfile
import shutil
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
# Add the project root to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
@pytest.fixture
def temp_dir():
"""Create a temporary directory for test files."""
temp_path = tempfile.mkdtemp()
yield Path(temp_path)
shutil.rmtree(temp_path)
@pytest.fixture
def mock_config():
"""Provide a mock configuration object."""
config = Mock()
config.debug = False
config.verbose = False
config.timeout = 30
config.max_retries = 3
return config
@pytest.fixture
def sample_data():
"""Provide sample test data."""
return {
'test_string': 'Hello, World!',
'test_bytes': b'Hello, World!',
'test_list': [1, 2, 3, 4, 5],
'test_dict': {'key1': 'value1', 'key2': 'value2'},
'test_int': 42,
'test_float': 3.14159
}
@pytest.fixture
def mock_file_system(temp_dir):
"""Create a mock file system structure."""
# Create directories
(temp_dir / 'modules').mkdir()
(temp_dir / 'core').mkdir()
(temp_dir / 'utils').mkdir()
# Create some mock files
(temp_dir / 'test_file.txt').write_text('Test content')
(temp_dir / 'modules' / 'module1.py').write_text('# Module 1')
(temp_dir / 'core' / 'core1.py').write_text('# Core 1')
return temp_dir
@pytest.fixture
def mock_network():
"""Mock network-related functions."""
with patch('socket.socket') as mock_socket:
mock_instance = Mock()
mock_socket.return_value = mock_instance
mock_instance.connect.return_value = None
mock_instance.send.return_value = 10
mock_instance.recv.return_value = b'response'
yield mock_instance
@pytest.fixture
def mock_subprocess():
"""Mock subprocess calls."""
with patch('subprocess.run') as mock_run:
mock_run.return_value = Mock(
stdout='Success',
stderr='',
returncode=0
)
yield mock_run
@pytest.fixture(autouse=True)
def reset_environment():
"""Reset environment variables before each test."""
original_env = os.environ.copy()
yield
os.environ.clear()
os.environ.update(original_env)
@pytest.fixture
def capture_logs():
"""Capture log messages during tests."""
import logging
from io import StringIO
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.DEBUG)
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
yield log_capture
logger.removeHandler(handler)
@pytest.fixture
def mock_crypto():
"""Mock cryptographic operations."""
with patch('Crypto.Cipher.AES.new') as mock_aes:
mock_cipher = Mock()
mock_aes.return_value = mock_cipher
mock_cipher.encrypt.return_value = b'encrypted_data'
mock_cipher.decrypt.return_value = b'decrypted_data'
yield mock_cipher
@pytest.fixture(scope="session")
def test_resources():
"""Provide paths to test resource files."""
resources_dir = Path(__file__).parent / 'resources'
return {
'base_dir': resources_dir,
'sample_dll': resources_dir / 'sample.dll',
'sample_exe': resources_dir / 'sample.exe',
'sample_config': resources_dir / 'config.json'
}
# Markers for different test types
def pytest_configure(config):
"""Configure pytest with custom markers."""
config.addinivalue_line(
"markers", "unit: mark test as a unit test"
)
config.addinivalue_line(
"markers", "integration: mark test as an integration test"
)
config.addinivalue_line(
"markers", "slow: mark test as slow running"
)