-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
88 lines (74 loc) · 2.44 KB
/
run_tests.py
File metadata and controls
88 lines (74 loc) · 2.44 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
#!/usr/bin/env python
"""
Run unit tests with memory limits and optimized settings
"""
import os
import sys
import platform
import django
from django.conf import settings
# Conditionally import resource module (not available on Windows)
try:
import resource
HAS_RESOURCE = True
except ImportError:
HAS_RESOURCE = False
# Set memory limit (default 256MB)
def set_memory_limit():
"""Set memory limit for the process"""
if not HAS_RESOURCE:
print("Warning: Resource module not available on Windows. Memory limits will not be applied.")
return
try:
memory_limit_str = os.environ.get('PYTHONMEMORY', '256MB')
if memory_limit_str.endswith('MB'):
memory_limit_mb = int(memory_limit_str[:-2])
else:
memory_limit_mb = 256
# Convert MB to bytes
memory_limit_bytes = memory_limit_mb * 1024 * 1024
# Set memory limit
resource.setrlimit(resource.RLIMIT_AS, (memory_limit_bytes, memory_limit_bytes))
print(f"Memory limit set to {memory_limit_mb}MB")
except Exception as e:
print(f"Warning: Could not set memory limit: {e}")
print("Continuing without memory limit...")
# Main function
def main():
"""Run unit tests"""
# Set Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'amc_tracker.settings')
# Try to set memory limit (only on Unix-like systems)
if HAS_RESOURCE:
try:
set_memory_limit()
except:
pass
# Set up Django
django.setup()
# Import test configuration
try:
from tracker.test_config import should_disable_tests, MAX_TESTS_PER_BATCH
if should_disable_tests():
print("Tests are disabled in production to prevent memory issues.")
return 0
except ImportError:
MAX_TESTS_PER_BATCH = 5
# Import test runner
from django.test.runner import DiscoverRunner
# Run tests with optimized settings
test_runner = DiscoverRunner(
interactive=False,
keepdb=True,
verbosity=2,
failfast=True,
buffer=True,
debug_mode=False,
debug_sql=False,
parallel=1, # Disable parallel to reduce memory usage
)
# Run only essential tests
failures = test_runner.run_tests(['tracker.tests.BasicModelTestCase'])
return failures
if __name__ == '__main__':
sys.exit(main())