-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate-test-summary-pytest.sh
More file actions
70 lines (56 loc) · 2.31 KB
/
generate-test-summary-pytest.sh
File metadata and controls
70 lines (56 loc) · 2.31 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
#!/bin/bash
set -e
# Generate Test Summary from Pytest JUnit XML Output
# Usage: ./generate-test-summary-pytest.sh <path-to-junit-xml>
XML_FILE="${1:-test-results.xml}"
echo "## Test Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Parse test results from JUnit XML
if [ -f "$XML_FILE" ]; then
# Extract test counts from XML
# JUnit XML structure: <testsuite tests="N" failures="N" errors="N" skipped="N">
tests=$(grep -oP 'tests="\K[0-9]+' "$XML_FILE" | head -1)
failures=$(grep -oP 'failures="\K[0-9]+' "$XML_FILE" | head -1)
errors=$(grep -oP 'errors="\K[0-9]+' "$XML_FILE" | head -1)
skipped=$(grep -oP 'skipped="\K[0-9]+' "$XML_FILE" | head -1)
# Default to 0 if values are empty
tests=${tests:-0}
failures=${failures:-0}
errors=${errors:-0}
skipped=${skipped:-0}
passed=$((tests - failures - errors - skipped))
echo "| Status | Count |" >> $GITHUB_STEP_SUMMARY
echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| ✅ Passed | $passed |" >> $GITHUB_STEP_SUMMARY
echo "| ❌ Failed | $((failures + errors)) |" >> $GITHUB_STEP_SUMMARY
echo "| ⏭️ Skipped | $skipped |" >> $GITHUB_STEP_SUMMARY
echo "| **Total** | **$tests** |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# List failed tests if any
if [ $((failures + errors)) -gt 0 ]; then
echo "### ❌ Failed Tests" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Extract failed test names from XML
failed_tests_file=$(mktemp)
# Find testcase elements with failure or error children
grep -oP '<testcase[^>]*classname="[^"]*"[^>]*name="[^"]*"[^>]*>.*?<(failure|error)' "$XML_FILE" | \
grep -oP 'classname="\K[^"]*|name="\K[^"]*' | \
paste -d '.' - - >> "$failed_tests_file" 2>/dev/null || true
if [ -s "$failed_tests_file" ]; then
while IFS= read -r test; do
echo "- \`$test\`" >> $GITHUB_STEP_SUMMARY
done < "$failed_tests_file"
else
echo "_Unable to parse individual test names_" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "❌ **Tests failed!**" >> $GITHUB_STEP_SUMMARY
rm -f "$failed_tests_file"
exit 1
else
echo "✅ **All tests passed!**" >> $GITHUB_STEP_SUMMARY
fi
else
echo "⚠️ No test results found at: $XML_FILE" >> $GITHUB_STEP_SUMMARY
exit 1
fi