|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
4 | | -# Generate Test Summary from Pytest JUnit XML Output |
5 | | -# Usage: ./generate-test-summary-pytest.sh <path-to-junit-xml> |
| 4 | +# Generate Detailed Test Summary from Multiple Pytest JUnit XML Output Files |
| 5 | +# Shows breakdown by test type (unit vs integration) |
| 6 | +# Usage: ./generate-test-summary-pytest-detailed.sh <unit-xml> <integration-xml> |
6 | 7 |
|
7 | | -XML_FILE="${1:-test-results.xml}" |
| 8 | +UNIT_XML="${1:-}" |
| 9 | +INTEGRATION_XML="${2:-}" |
8 | 10 |
|
9 | 11 | echo "## Test Results" >> $GITHUB_STEP_SUMMARY |
10 | 12 | echo "" >> $GITHUB_STEP_SUMMARY |
11 | 13 |
|
12 | | -# Parse test results from JUnit XML |
13 | | -if [ -f "$XML_FILE" ]; then |
14 | | - # Extract test counts from XML |
15 | | - # JUnit XML structure: <testsuite tests="N" failures="N" errors="N" skipped="N"> |
| 14 | +# Function to parse XML file |
| 15 | +parse_xml() { |
| 16 | + local xml_file="$1" |
| 17 | + local test_type="$2" |
16 | 18 |
|
17 | | - tests=$(grep -oP 'tests="\K[0-9]+' "$XML_FILE" | head -1) |
18 | | - failures=$(grep -oP 'failures="\K[0-9]+' "$XML_FILE" | head -1) |
19 | | - errors=$(grep -oP 'errors="\K[0-9]+' "$XML_FILE" | head -1) |
20 | | - skipped=$(grep -oP 'skipped="\K[0-9]+' "$XML_FILE" | head -1) |
| 19 | + if [ ! -f "$xml_file" ]; then |
| 20 | + echo "0 0 0 0 0" |
| 21 | + return |
| 22 | + fi |
| 23 | + |
| 24 | + tests=$(grep -oP 'tests="\K[0-9]+' "$xml_file" | head -1) |
| 25 | + failures=$(grep -oP 'failures="\K[0-9]+' "$xml_file" | head -1) |
| 26 | + errors=$(grep -oP 'errors="\K[0-9]+' "$xml_file" | head -1) |
| 27 | + skipped=$(grep -oP 'skipped="\K[0-9]+' "$xml_file" | head -1) |
21 | 28 |
|
22 | | - # Default to 0 if values are empty |
23 | 29 | tests=${tests:-0} |
24 | 30 | failures=${failures:-0} |
25 | 31 | errors=${errors:-0} |
26 | 32 | skipped=${skipped:-0} |
27 | | - |
28 | 33 | passed=$((tests - failures - errors - skipped)) |
29 | 34 |
|
30 | | - echo "| Status | Count |" >> $GITHUB_STEP_SUMMARY |
31 | | - echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY |
32 | | - echo "| ✅ Passed | $passed |" >> $GITHUB_STEP_SUMMARY |
33 | | - echo "| ❌ Failed | $((failures + errors)) |" >> $GITHUB_STEP_SUMMARY |
34 | | - echo "| ⏭️ Skipped | $skipped |" >> $GITHUB_STEP_SUMMARY |
35 | | - echo "| **Total** | **$tests** |" >> $GITHUB_STEP_SUMMARY |
| 35 | + echo "$tests $failures $errors $skipped $passed" |
| 36 | +} |
| 37 | + |
| 38 | +# Parse both files |
| 39 | +read -r unit_tests unit_failures unit_errors unit_skipped unit_passed <<< "$(parse_xml "$UNIT_XML" "Unit")" |
| 40 | +read -r int_tests int_failures int_errors int_skipped int_passed <<< "$(parse_xml "$INTEGRATION_XML" "Integration")" |
| 41 | + |
| 42 | +# Calculate totals |
| 43 | +total_tests=$((unit_tests + int_tests)) |
| 44 | +total_failures=$((unit_failures + int_failures)) |
| 45 | +total_errors=$((unit_errors + int_errors)) |
| 46 | +total_skipped=$((unit_skipped + int_skipped)) |
| 47 | +total_passed=$((unit_passed + int_passed)) |
| 48 | +total_failed=$((total_failures + total_errors)) |
| 49 | + |
| 50 | +# Display detailed breakdown |
| 51 | +echo "### Summary by Test Type" >> $GITHUB_STEP_SUMMARY |
| 52 | +echo "" >> $GITHUB_STEP_SUMMARY |
| 53 | +echo "| Test Type | Passed | Failed | Skipped | Total |" >> $GITHUB_STEP_SUMMARY |
| 54 | +echo "|-----------|--------|--------|---------|-------|" >> $GITHUB_STEP_SUMMARY |
| 55 | + |
| 56 | +if [ -f "$UNIT_XML" ]; then |
| 57 | + echo "| 🔧 Unit Tests | $unit_passed | $((unit_failures + unit_errors)) | $unit_skipped | $unit_tests |" >> $GITHUB_STEP_SUMMARY |
| 58 | +fi |
| 59 | + |
| 60 | +if [ -f "$INTEGRATION_XML" ]; then |
| 61 | + echo "| 🔗 Integration Tests | $int_passed | $((int_failures + int_errors)) | $int_skipped | $int_tests |" >> $GITHUB_STEP_SUMMARY |
| 62 | +fi |
| 63 | + |
| 64 | +echo "| **Total** | **$total_passed** | **$total_failed** | **$total_skipped** | **$total_tests** |" >> $GITHUB_STEP_SUMMARY |
| 65 | +echo "" >> $GITHUB_STEP_SUMMARY |
| 66 | + |
| 67 | +# Overall status |
| 68 | +echo "### Overall Status" >> $GITHUB_STEP_SUMMARY |
| 69 | +echo "" >> $GITHUB_STEP_SUMMARY |
| 70 | +echo "| Status | Count |" >> $GITHUB_STEP_SUMMARY |
| 71 | +echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY |
| 72 | +echo "| ✅ Passed | $total_passed |" >> $GITHUB_STEP_SUMMARY |
| 73 | +echo "| ❌ Failed | $total_failed |" >> $GITHUB_STEP_SUMMARY |
| 74 | +echo "| ⏭️ Skipped | $total_skipped |" >> $GITHUB_STEP_SUMMARY |
| 75 | +echo "| **Total** | **$total_tests** |" >> $GITHUB_STEP_SUMMARY |
| 76 | +echo "" >> $GITHUB_STEP_SUMMARY |
| 77 | + |
| 78 | +# List failed tests if any |
| 79 | +if [ $total_failed -gt 0 ]; then |
| 80 | + echo "### ❌ Failed Tests" >> $GITHUB_STEP_SUMMARY |
36 | 81 | echo "" >> $GITHUB_STEP_SUMMARY |
37 | 82 |
|
38 | | - # List failed tests if any |
39 | | - if [ $((failures + errors)) -gt 0 ]; then |
40 | | - echo "### ❌ Failed Tests" >> $GITHUB_STEP_SUMMARY |
41 | | - echo "" >> $GITHUB_STEP_SUMMARY |
42 | | - |
43 | | - # Extract failed test names from XML |
44 | | - failed_tests_file=$(mktemp) |
45 | | - |
46 | | - # Find testcase elements with failure or error children |
47 | | - grep -oP '<testcase[^>]*classname="[^"]*"[^>]*name="[^"]*"[^>]*>.*?<(failure|error)' "$XML_FILE" | \ |
48 | | - grep -oP 'classname="\K[^"]*|name="\K[^"]*' | \ |
49 | | - paste -d '.' - - >> "$failed_tests_file" 2>/dev/null || true |
50 | | - |
51 | | - if [ -s "$failed_tests_file" ]; then |
52 | | - while IFS= read -r test; do |
53 | | - echo "- \`$test\`" >> $GITHUB_STEP_SUMMARY |
54 | | - done < "$failed_tests_file" |
55 | | - else |
56 | | - echo "_Unable to parse individual test names_" >> $GITHUB_STEP_SUMMARY |
| 83 | + failed_tests_file=$(mktemp) |
| 84 | + |
| 85 | + # Extract failed tests from both files |
| 86 | + for xml_file in "$UNIT_XML" "$INTEGRATION_XML"; do |
| 87 | + if [ -f "$xml_file" ]; then |
| 88 | + grep -oP '<testcase[^>]*classname="[^"]*"[^>]*name="[^"]*"[^>]*>.*?<(failure|error)' "$xml_file" | \ |
| 89 | + grep -oP 'classname="\K[^"]*|name="\K[^"]*' | \ |
| 90 | + paste -d '.' - - >> "$failed_tests_file" 2>/dev/null || true |
57 | 91 | fi |
58 | | - |
59 | | - echo "" >> $GITHUB_STEP_SUMMARY |
60 | | - echo "❌ **Tests failed!**" >> $GITHUB_STEP_SUMMARY |
61 | | - rm -f "$failed_tests_file" |
62 | | - exit 1 |
| 92 | + done |
| 93 | + |
| 94 | + if [ -s "$failed_tests_file" ]; then |
| 95 | + while IFS= read -r test; do |
| 96 | + echo "- \`$test\`" >> $GITHUB_STEP_SUMMARY |
| 97 | + done < "$failed_tests_file" |
63 | 98 | else |
64 | | - echo "✅ **All tests passed!**" >> $GITHUB_STEP_SUMMARY |
| 99 | + echo "_Unable to parse individual test names_" >> $GITHUB_STEP_SUMMARY |
65 | 100 | fi |
66 | | -else |
67 | | - echo "⚠️ No test results found at: $XML_FILE" >> $GITHUB_STEP_SUMMARY |
| 101 | + |
| 102 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 103 | + echo "❌ **Tests failed!**" >> $GITHUB_STEP_SUMMARY |
| 104 | + rm -f "$failed_tests_file" |
68 | 105 | exit 1 |
| 106 | +else |
| 107 | + echo "✅ **All tests passed!**" >> $GITHUB_STEP_SUMMARY |
69 | 108 | fi |
70 | 109 |
|
0 commit comments