-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·316 lines (274 loc) · 9.62 KB
/
run_tests.sh
File metadata and controls
executable file
·316 lines (274 loc) · 9.62 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
#!/bin/bash
# Test Runner Script for ELI5 Server
# Provides convenient commands for running different types of tests
set -e # Exit on any error
# Global variables
COVERAGE_AVAILABLE=false
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_colored() {
echo -e "${1}${2}${NC}"
}
# Function to print section headers
print_header() {
echo
print_colored $BLUE "=================================="
print_colored $BLUE "$1"
print_colored $BLUE "=================================="
echo
}
# Function to check if dependencies are installed
check_dependencies() {
print_header "Checking Dependencies"
if ! command -v python3 &> /dev/null; then
print_colored $RED "❌ Python3 is not installed"
exit 1
fi
if ! python3 -c "import pytest" &> /dev/null; then
print_colored $RED "❌ pytest is not installed"
print_colored $YELLOW "Install with: pip install -r requirements.txt"
exit 1
fi
# Check for pytest-cov specifically when running coverage tests
if ! python3 -c "import pytest_cov" &> /dev/null; then
print_colored $YELLOW "⚠️ pytest-cov is not installed"
print_colored $YELLOW "Coverage features will be disabled"
print_colored $YELLOW "Install with: pip install pytest-cov"
COVERAGE_AVAILABLE=false
else
COVERAGE_AVAILABLE=true
fi
print_colored $GREEN "✅ All core dependencies are available"
}
# Function to run all tests
run_all_tests() {
print_header "Running All Tests"
if [ "$COVERAGE_AVAILABLE" = true ]; then
python3 -m pytest tests/ -v --cov=. --cov-report=term-missing --cov-report=html
else
print_colored $YELLOW "⚠️ Running tests without coverage (pytest-cov not available)"
python3 -m pytest tests/ -v
fi
}
# Function to run unit tests only
run_unit_tests() {
print_header "Running Unit Tests"
python3 -m pytest tests/test_auth.py tests/test_database.py tests/test_services.py tests/test_schemas.py -v
}
# Function to run integration tests only
run_integration_tests() {
print_header "Running Integration Tests"
python3 -m pytest tests/test_main.py tests/test_integration.py -v
}
# Function to run tests with coverage
run_coverage() {
print_header "Running Tests with Coverage"
if [ "$COVERAGE_AVAILABLE" = true ]; then
python3 -m pytest tests/ --cov=. --cov-report=term-missing --cov-report=html --cov-fail-under=80
if [ -d "htmlcov" ]; then
print_colored $GREEN "📊 Coverage report generated in htmlcov/index.html"
fi
else
print_colored $RED "❌ Coverage testing requires pytest-cov"
print_colored $YELLOW "Install with: pip install pytest-cov"
exit 1
fi
}
# Function to run specific test file
run_specific_test() {
if [ -z "$1" ]; then
print_colored $RED "❌ Please specify a test file"
print_colored $YELLOW "Usage: $0 test <test_file>"
print_colored $YELLOW "Example: $0 test auth"
exit 1
fi
TEST_FILE="tests/test_${1}.py"
if [ ! -f "$TEST_FILE" ]; then
print_colored $RED "❌ Test file $TEST_FILE not found"
exit 1
fi
print_header "Running Tests from $TEST_FILE"
python3 -m pytest "$TEST_FILE" -v
}
# Function to run tests in watch mode
run_watch_mode() {
print_header "Running Tests in Watch Mode"
print_colored $YELLOW "Note: Install pytest-xdist for watch mode"
print_colored $YELLOW "pip install pytest-xdist"
# Basic file watching with inotify (Linux) or fswatch (macOS)
if command -v inotifywait &> /dev/null; then
print_colored $GREEN "🔄 Watching for file changes..."
while inotifywait -r -e modify,create,delete --include='.*\.py$' .; do
print_header "Files Changed - Running Tests"
python3 -m pytest tests/ -x --tb=short
done
else
print_colored $YELLOW "⚠️ Install inotify-tools for file watching"
print_colored $YELLOW "Running tests once..."
python3 -m pytest tests/ -v
fi
}
# Function to run performance tests
run_performance_tests() {
print_header "Running Performance Tests"
print_colored $YELLOW "⏱️ Running performance-related tests..."
python3 -m pytest tests/test_integration.py::TestPerformanceIntegration -v
}
# Function to run linting
run_linting() {
print_header "Running Code Quality Checks"
# Check if flake8 is installed
if python3 -c "import flake8" &> /dev/null; then
print_colored $BLUE "🧹 Running flake8..."
python3 -m flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
python3 -m flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
else
print_colored $YELLOW "⚠️ flake8 not installed, skipping linting"
fi
# Check if black is installed
if python3 -c "import black" &> /dev/null; then
print_colored $BLUE "🎨 Checking code formatting with black..."
python3 -m black --check .
else
print_colored $YELLOW "⚠️ black not installed, skipping format check"
fi
}
# Function to generate test report
generate_test_report() {
print_header "Generating Comprehensive Test Report"
REPORT_DIR="test_reports"
mkdir -p "$REPORT_DIR"
# Run tests with detailed output
if [ "$COVERAGE_AVAILABLE" = true ]; then
python3 -m pytest tests/ \
--cov=. \
--cov-report=html:"$REPORT_DIR/coverage" \
--cov-report=xml:"$REPORT_DIR/coverage.xml" \
--junit-xml="$REPORT_DIR/junit.xml" \
--html="$REPORT_DIR/report.html" \
--self-contained-html \
-v
print_colored $GREEN "📋 Test reports generated in $REPORT_DIR/"
print_colored $GREEN " - HTML Report: $REPORT_DIR/report.html"
print_colored $GREEN " - Coverage: $REPORT_DIR/coverage/index.html"
print_colored $GREEN " - JUnit XML: $REPORT_DIR/junit.xml"
else
python3 -m pytest tests/ \
--junit-xml="$REPORT_DIR/junit.xml" \
--html="$REPORT_DIR/report.html" \
--self-contained-html \
-v
print_colored $GREEN "📋 Test reports generated in $REPORT_DIR/"
print_colored $GREEN " - HTML Report: $REPORT_DIR/report.html"
print_colored $GREEN " - JUnit XML: $REPORT_DIR/junit.xml"
print_colored $YELLOW "⚠️ Coverage report not available (pytest-cov not installed)"
fi
}
# Function to clean test artifacts
clean_test_artifacts() {
print_header "Cleaning Test Artifacts"
rm -rf htmlcov/
rm -rf test_reports/
rm -rf .coverage
rm -rf .pytest_cache/
rm -rf **/__pycache__/
rm -f test_eli5.db
print_colored $GREEN "🧹 Test artifacts cleaned"
}
# Function to setup test environment
setup_test_environment() {
print_header "Setting Up Test Environment"
# Install test dependencies
print_colored $BLUE "📦 Installing test dependencies..."
pip install -r requirements.txt
# Create test database if needed
print_colored $BLUE "🗄️ Setting up test database..."
python3 -c "
from database import create_tables
create_tables()
print('✅ Test database initialized')
"
print_colored $GREEN "🚀 Test environment setup complete"
}
# Function to show usage
show_usage() {
print_header "ELI5 Server Test Runner"
echo "Usage: $0 <command> [options]"
echo
echo "Commands:"
echo " all Run all tests with coverage"
echo " unit Run unit tests only"
echo " integration Run integration tests only"
echo " coverage Run tests with coverage report"
echo " test <name> Run specific test file (e.g., 'auth', 'database')"
echo " watch Run tests in watch mode"
echo " performance Run performance tests"
echo " lint Run code quality checks"
echo " report Generate comprehensive test report"
echo " clean Clean test artifacts"
echo " setup Setup test environment"
echo " help Show this help message"
echo
echo "Examples:"
echo " $0 all # Run all tests"
echo " $0 test auth # Run authentication tests"
echo " $0 coverage # Run with coverage"
echo " $0 unit # Run unit tests only"
echo
}
# Main script logic
main() {
case "${1:-help}" in
"all")
check_dependencies
run_all_tests
;;
"unit")
check_dependencies
run_unit_tests
;;
"integration")
check_dependencies
run_integration_tests
;;
"coverage")
check_dependencies
run_coverage
;;
"test")
check_dependencies
run_specific_test "$2"
;;
"watch")
check_dependencies
run_watch_mode
;;
"performance")
check_dependencies
run_performance_tests
;;
"lint")
run_linting
;;
"report")
check_dependencies
generate_test_report
;;
"clean")
clean_test_artifacts
;;
"setup")
setup_test_environment
;;
"help"|*)
show_usage
;;
esac
}
# Run main function with all arguments
main "$@"