Skip to content

Instantly share code, notes, and snippets.

@hoodja
Last active December 14, 2015 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hoodja/5043784 to your computer and use it in GitHub Desktop.
Save hoodja/5043784 to your computer and use it in GitHub Desktop.
"bash" me all you want, but this is sweet. It turns the verbose output of Unity xUnit framework for C (yes, straight eff'n C) into JUnit results that Jenkins CI can eat.
use strict;
use warnings;
my $fname = shift or die 'filename!';
open my $fh, $fname or die "Could not open $fname: $!";
print "<testsuite>\n";
foreach(grep /^ *(IGNORE_)?TEST/, <$fh>) {
my ($status, $group, $test, $detail);
$status = $group = $test = $detail = "";
($status, $group, $test) = ($_ =~ m/(IGNORE)_TEST\(([^,]*), ([^)]*)\).*$/) or
($group, $test, $status) = ($_ =~ m/TEST\(([^,]*), ([^)]*)\) *(PASS).*$/) or
($group, $test, $status, $detail) = ($_ =~ m/TEST\(([^,]*), ([^)]*)\)[^:]*:[^:]*:[^:]*:[^:]*:(FAIL):(.*$)/);
print " <testcase classname=\"$group\" name=\"$test\">\n";
print " <skipped></skipped>\n" if $status eq "IGNORE";
print " <failure>\n <![CDATA[$detail]]>\n </failure>\n" if $status eq "FAIL";
print " </testcase>\n";
}
print "</testsuite>\n";
#!/bin/bash
#TODO: assert argument exists and is a file, etc.
cat << END_OF_RESULTS
<testsuite>
$(
cat $1 | grep -Ee "^ *(IGNORE_)?TEST" | while read line; do
command=$(echo $line | \
sed \
-e 's/IGNORE_TEST(\([^,]*\), \([^)]*\)).*$/group="\1"; test="\2"; status="IGNORE";/' \
-e 's/TEST(\([^,]*\), \([^)]*\)) \(PASS\).*$/group="\1"; test="\2"; status="\3";/' \
-e 's/TEST(\([^,]*\), \([^)]*\))[^:]*:\([^:]*\):\([^:]*\):\([^:]*\):\(FAIL\):\(.*$\)/group="\1"; test="\2"; status="\6"; detail="\7"/')
eval "$command"
echo " <testcase classname=\"$group\" name=\"$test\">"
[[ "$status" == "IGNORE" ]] && echo " <skipped></skipped>"
[[ "$status" == "FAIL" ]] && echo " <failure>
<![CDATA[
$detail
]]>
</failure>"
echo " </testcase>"
done
)
</testsuite>
END_OF_RESULTS
@hoodja
Copy link
Author

hoodja commented Feb 27, 2013

Input

Unity test run 1 of 1
TEST(Group1, test1) PASS
TEST(Group1, test2) PASS
TEST(Group2, test3) PASS
TEST(Group2, test4) PASS
TEST(Group3, test5)c:\path\to\sometests.c:51:TEST(Group3, test5):FAIL: Expected 2 Was 1

 -----------------------
 5 Tests 1 Failures 0 Ignored
 FAIL

Output

<testsuite>
  <testcase classname="Group1" name="test1">
  </testcase>
  <testcase classname="Group1" name="test2">
  </testcase>
  <testcase classname="Group2" name="test3">
  </testcase>
  <testcase classname="Group2" name="test4">
  </testcase>
  <testcase classname="Group3" name="test5">
    <failure>
      <![CDATA[
       Expected 2 Was 1
      ]]>
    </failure>
  </testcase>
</testsuite>

@rdammkoehler
Copy link

I added your file test in my fork

@hoodja
Copy link
Author

hoodja commented Apr 9, 2013

I added a perl implementation; surprisingly readable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment