-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_tests.py
More file actions
378 lines (296 loc) · 11.8 KB
/
run_tests.py
File metadata and controls
378 lines (296 loc) · 11.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
"""
Test runner for Socket SDK Python client.
This script helps run different test suites with proper configuration.
Usage:
cd tests && python run_tests.py --unit # Run unit tests only
cd tests && python run_tests.py --integration # Run integration tests only
cd tests && python run_tests.py --all # Run all tests
cd tests && python run_tests.py --new-endpoints # Run tests for new endpoints
cd tests && python run_tests.py --help # Show help
"""
import argparse
import os
import sys
import subprocess
from pathlib import Path
def load_env_file():
"""Load environment variables from .env file in project root."""
env_path = Path(__file__).parent.parent / ".env"
if env_path.exists():
print(f"📁 Loading environment from {env_path}")
with open(env_path) as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key.strip()] = value.strip()
return True
return False
def check_environment():
"""Check if the environment is properly configured."""
print("🔍 Checking environment...")
# Check if we're in a virtual environment
in_venv = hasattr(sys, 'real_prefix') or (
hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix
)
if in_venv:
print("✅ Running in virtual environment")
else:
print("⚠️ Not running in virtual environment")
print(" Consider activating .venv: source .venv/bin/activate")
# Check for pytest
try:
import pytest
print(f"✅ pytest available: {pytest.__version__}")
except ImportError:
print("❌ pytest not found. Install with: pip install pytest")
return False
# Check for socketdev
try:
import socketdev
print(f"✅ socketdev available: {socketdev.__version__}")
except ImportError:
print("❌ socketdev not found. Install with: pip install -e .")
return False
return True
def check_credentials():
"""Check if API credentials are configured."""
print("\n🔐 Checking credentials...")
api_key = os.getenv("SOCKET_SECURITY_API_KEY", "")
org_slug = os.getenv("SOCKET_ORG_SLUG", "")
repo_slug = os.getenv("SOCKET_REPO_SLUG", "")
if api_key and org_slug:
print("✅ Basic credentials available (API key and org slug)")
if repo_slug:
print("✅ Repository slug available")
else:
print("⚠️ Repository slug not set (some tests will be skipped)")
else:
print("⚠️ Missing credentials:")
if not api_key:
print(" - SOCKET_SECURITY_API_KEY not set")
if not org_slug:
print(" - SOCKET_ORG_SLUG not set")
print(" Integration tests will be skipped")
return bool(api_key and org_slug)
def run_unit_tests():
"""Run unit tests with mocked API calls."""
print("Running unit tests...")
# Set test environment
os.environ["SOCKET_TEST_MODE"] = "unit"
cmd = [
sys.executable, "-m", "pytest",
"tests/unit/test_socket_sdk_unit.py",
"tests/unit/test_working_endpoints_unit.py",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_comprehensive_unit_tests():
"""Run comprehensive unit tests for all endpoints (includes failing tests)."""
print("Running comprehensive unit tests for all endpoints (includes failing tests)...")
# Set test environment
os.environ["SOCKET_TEST_MODE"] = "unit"
cmd = [
sys.executable, "-m", "pytest",
"tests/unit/test_all_endpoints_unit.py",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_all_unit_tests():
"""Run all unit tests including problematic endpoints."""
print("Running all unit tests (including problematic endpoints)...")
# Set test environment
os.environ["SOCKET_TEST_MODE"] = "unit"
cmd = [
sys.executable, "-m", "pytest",
"tests/unit/",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_integration_tests():
"""Run integration tests with real API calls."""
if not validate_environment():
return False
print("Running integration tests...")
# Set test environment
os.environ["SOCKET_TEST_MODE"] = "integration"
cmd = [
sys.executable, "-m", "pytest",
"tests/integration/",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_comprehensive_integration_tests():
"""Run comprehensive integration tests for all endpoints."""
if not validate_environment():
return False
print("Running comprehensive integration tests for all endpoints...")
# Set test environment
os.environ["SOCKET_TEST_MODE"] = "integration"
cmd = [
sys.executable, "-m", "pytest",
"tests/integration/test_all_endpoints.py",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_new_endpoints_tests():
"""Run tests for new endpoints."""
if not validate_environment():
return False
print("Running new endpoints tests...")
cmd = [
sys.executable, "-m", "pytest",
"tests/integration/test_new_endpoints.py",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_legacy_tests():
"""Run legacy integration tests."""
if not validate_environment():
return False
print("Running legacy integration tests...")
cmd = [
sys.executable, "-m", "pytest",
"tests/integration/test_diffscans_integration.py",
"-v", "--tb=short"
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
def run_tests(test_type, verbose=False):
"""
Run the specified test suite.
Args:
test_type: One of 'unit', 'integration', 'all', 'new-endpoints', 'legacy', 'comprehensive-unit', 'all-unit'
verbose: Whether to enable verbose output
"""
success = True
if test_type == "unit":
success = run_unit_tests()
elif test_type == "comprehensive-unit":
success = run_comprehensive_unit_tests()
elif test_type == "all-unit":
success = run_all_unit_tests()
elif test_type == "integration":
success = run_integration_tests() and run_comprehensive_integration_tests()
elif test_type == "new-endpoints":
success = run_new_endpoints_tests()
elif test_type == "legacy":
success = run_legacy_tests()
elif test_type == "all":
print("Running all test suites...")
success = (
run_unit_tests() and
run_integration_tests() and
run_comprehensive_integration_tests() and
run_new_endpoints_tests() and
run_legacy_tests()
)
else:
print(f"Unknown test type: {test_type}")
print("Available options: unit, integration, all, new-endpoints, legacy, comprehensive-unit, all-unit")
success = False
return success
def validate_environment():
"""Validate test environment for integration tests."""
# For integration tests, we need both dependencies and credentials
print("\n🔍 Validating environment...")
# Check dependencies (always needed)
deps_ok = True
try:
import socketdev
print(f"✅ socketdev available: {socketdev.__version__}")
except ImportError:
print("❌ socketdev not found. Install with: pip install -e .")
deps_ok = False
# Check credentials (needed for integration tests)
creds_ok = check_credentials()
return deps_ok and creds_ok
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Test runner for Socket SDK Python client",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
cd tests && python run_tests.py --unit # Run working unit tests only
cd tests && python run_tests.py --comprehensive-unit # Run all unit tests (includes failing)
cd tests && python run_tests.py --all-unit # Run all unit tests including problematic
cd tests && python run_tests.py --integration # Run integration tests only
cd tests && python run_tests.py --all # Run all tests
cd tests && python run_tests.py --new-endpoints # Run tests for new endpoints
cd tests && python run_tests.py --legacy # Run legacy integration tests
Environment Variables:
Can be set in .env file in project root or as environment variables:
SOCKET_SECURITY_API_KEY Your Socket.dev API key (required for integration tests)
SOCKET_ORG_SLUG Your organization slug (required for integration tests)
SOCKET_REPO_SLUG Your repository slug (optional, some tests will be skipped)
"""
)
parser.add_argument("--unit", action="store_true",
help="Run working unit tests only (no API key required)")
parser.add_argument("--comprehensive-unit", action="store_true",
help="Run comprehensive unit tests (includes failing tests)")
parser.add_argument("--all-unit", action="store_true",
help="Run all unit tests including problematic endpoints")
parser.add_argument("--integration", action="store_true",
help="Run comprehensive integration tests")
parser.add_argument("--new-endpoints", action="store_true",
help="Run tests for new endpoints")
parser.add_argument("--legacy", action="store_true",
help="Run legacy integration tests")
parser.add_argument("--all", action="store_true",
help="Run all tests")
parser.add_argument("--quiet", "-q", action="store_true",
help="Run tests in quiet mode")
args = parser.parse_args()
# Determine test type
test_type = "unit" # Default to working unit tests
if args.unit:
test_type = "unit"
elif args.comprehensive_unit:
test_type = "comprehensive-unit"
elif args.all_unit:
test_type = "all-unit"
elif args.integration:
test_type = "integration"
elif args.new_endpoints:
test_type = "new-endpoints"
elif args.legacy:
test_type = "legacy"
elif args.all:
test_type = "all"
verbose = not args.quiet
print("🚀 Socket SDK Python Client Test Runner")
print("=" * 40)
success = run_tests(test_type, verbose)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()