Initial add with a couple of tests
This commit is contained in:
33
README
Normal file
33
README
Normal file
@@ -0,0 +1,33 @@
|
||||
= What is shunit
|
||||
|
||||
shunit is just a set of bash functions for producing unit test output from bash scripts. I wrote this library because I often found myself writing things in bash whose output I wanted to consume as discrete pass/fail tasks into Bamboo, so I wrote the junit functions. Then later on, I got sick and tired of (as a human) reading junit output, so I wrote the tunit functions, so my scripts could output tunit or junit depending on which flags I passed.
|
||||
|
||||
Complete documentation is inside the functions (each function has --help) in ./lib/*unit.sh. A pair of complete examples are in tests/*unit.sh.
|
||||
|
||||
= Example output
|
||||
|
||||
akesterson@akesterson-pc ~/source/upstream/git/shunit
|
||||
$ bash tests/tunit.sh
|
||||
|
||||
==== 0 TESTS in 0 SECONDS : 0 ERRORS, 0 FAILURES ====
|
||||
[super::class] someTest .... [OK]
|
||||
[super::class] someOtherTest .... [FAILED]
|
||||
generic failure : Yo dawg, I heard you like failures
|
||||
This is some raw data, please read it
|
||||
|
||||
|
||||
akesterson@akesterson-pc ~/source/upstream/git/shunit
|
||||
$ bash tests/junit.sh
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuite failures="1" time="0" timestamp="Wed Dec 19 22:51:02 EST 2012" errors="1" tests="2">
|
||||
<properties/>
|
||||
<testcase classname="super::class" time="31337" name="someTest">
|
||||
</testcase>
|
||||
<testcase classname="super::class" time="31337" name="someOtherTest">
|
||||
<failure type="generic failure" message="Yo dawg, I heard you like failures">
|
||||
<![CDATA[
|
||||
This is some raw data, please read it
|
||||
]]>
|
||||
</failure>
|
||||
</testcase>
|
||||
</testsuite>
|
||||
116
lib/junit.sh
Normal file
116
lib/junit.sh
Normal file
@@ -0,0 +1,116 @@
|
||||
#!/bin/bash
|
||||
|
||||
JUNIT_FAILURES=0
|
||||
JUNIT_ERRORS=0
|
||||
JUNIT_TESTS=0
|
||||
JUNIT_TIMERSTART=$(date "+%s")
|
||||
|
||||
function junit_header()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
junit_header()
|
||||
|
||||
This function doesn't actually do anything, it's a stub. Because junit
|
||||
is XML, which has to be ordered a certain way, this function does nothing
|
||||
but initialize some counters. Your unit test output won't actually get
|
||||
printed until you call junit_footer, which calls junit_header_real.
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
JUNIT_FAILURES=0
|
||||
JUNIT_ERRORS=0
|
||||
JUNIT_TESTS=0
|
||||
JUNIT_TIMERSTART=$(date "+%s")
|
||||
> /tmp/$$.junit
|
||||
}
|
||||
|
||||
function junit_header_real()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
junit_header_real(tests, errors, failures, elapsed[, encoding])
|
||||
|
||||
Generate a junit header, to display one or more junit_testcase()s.
|
||||
tests : total number of tests.
|
||||
errors : total number of errors
|
||||
failures : total number of failed test cases
|
||||
elapsed : The total number of seconds elapsed during testing
|
||||
encoding : Defaults to UTF-8, you should probably not change this.
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
|
||||
local tests errors failures elapsed encoding
|
||||
|
||||
tests=$1
|
||||
errors=$2
|
||||
failures=$3
|
||||
elapsed=$4
|
||||
encoding=$5
|
||||
|
||||
if [ "$encoding" == "" ]; then
|
||||
encoding="UTF-8"
|
||||
fi
|
||||
|
||||
echo '<?xml version="1.0" encoding="'$encoding'"?>'
|
||||
echo '<testsuite failures="'$failures'" time="'$elapsed'" timestamp="'$(date)'" errors="'$failures'" tests="'$tests'">'
|
||||
echo ' <properties/>'
|
||||
}
|
||||
|
||||
function junit_footer()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
junit_footer()
|
||||
|
||||
Print a junit footer. This will also call junit_header_real.
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
ELAPSED=$(expr $(date "+%s") - $JUNIT_TIMERSTART)
|
||||
junit_header_real $JUNIT_TESTS $JUNIT_ERRORS $JUNIT_FAILURES $ELAPSED "UTF-8"
|
||||
cat /tmp/$$.junit
|
||||
rm -f /tmp/$$.junit
|
||||
echo '</testsuite>'
|
||||
}
|
||||
|
||||
function junit_testcase()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
junit_testcase(classname, testname, elapsed[, failtype, failmsg, cdata])
|
||||
|
||||
Generate XML to describe a junit test case.
|
||||
|
||||
classname : the name of the class containing the test
|
||||
testname : the name of the test in this class
|
||||
elapsed : Number of seconds elapsed during testing
|
||||
failtype : A one-word description of the failure type (generally an exception name or code)
|
||||
failmsg : A brief message (<32 chars) describing the failure
|
||||
cdata : More detailed information about the failure.
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
|
||||
local classname testname elapsed failtype failmsg cdata
|
||||
|
||||
classname="$1"
|
||||
testname="$2"
|
||||
elapsed=$3
|
||||
failtype="$4"
|
||||
failmsg="$5"
|
||||
cdata="$6"
|
||||
JUNIT_TESTS=$(expr $JUNIT_TESTS + 1)
|
||||
echo ' <testcase classname="'$classname'" time="'$elapsed'" name="'$testname'">' >> /tmp/$$.junit
|
||||
if [ "$failtype" != "" ]; then
|
||||
JUNIT_ERRORS=$(expr $JUNIT_ERRORS + 1)
|
||||
JUNIT_FAILURES=$(expr $JUNIT_FAILURES + 1)
|
||||
echo ' <failure type="'$failtype'" message="'$failmsg'">' >> /tmp/$$.junit
|
||||
echo ' <![CDATA[' >> /tmp/$$.junit
|
||||
echo "${cdata}" >> /tmp/$$.junit
|
||||
echo ' ]]>' >> /tmp/$$.junit
|
||||
echo ' </failure>' >> /tmp/$$.junit
|
||||
fi
|
||||
echo ' </testcase>' >> /tmp/$$.junit
|
||||
}
|
||||
107
lib/tunit.sh
Normal file
107
lib/tunit.sh
Normal file
@@ -0,0 +1,107 @@
|
||||
#!/bin/bash
|
||||
|
||||
# tunit is short for "text unit", which is something I completely made
|
||||
# up. Every unit test is printed in the following format:
|
||||
#
|
||||
# [ TEST NAME ] ... [ OK | FAIL ]
|
||||
#
|
||||
# If COLOR=on, then the OK/FAIL bits are in RED or GREEN.
|
||||
|
||||
COLOR=${COLOR:-on}
|
||||
|
||||
if [ "$COLOR" == "on" ]; then
|
||||
COLOR_GREEN=$(echo -e '\033[0;32;40m');
|
||||
COLOR_RED=$(echo -e '\033[0;31;40m');
|
||||
COLOR_NORMAL=$(echo -e '\033[0m');
|
||||
else
|
||||
COLOR_GREEN=""
|
||||
COLOR_RED=""
|
||||
COLOR_NORMAL=""
|
||||
fi
|
||||
|
||||
|
||||
function tunit_header()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
tunit_header(tests, errors, failures, elapsed)
|
||||
|
||||
Generate a tunit header, to display one or more tunit_testcase()s.
|
||||
tests : total number of tests.
|
||||
errors : total number of errors
|
||||
failures : total number of failed test cases
|
||||
elapsed : The total number of seconds elapsed during testing
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
|
||||
local tests errors failures elapsed encoding
|
||||
local errcolor failcolor
|
||||
|
||||
tests=${1:-0}
|
||||
errors=${2:-0}
|
||||
failures=${3:-0}
|
||||
elapsed=${4:-0}
|
||||
|
||||
errcolor="${COLOR_GREEN}"
|
||||
failcolor="${COLOR_GREEN}"
|
||||
|
||||
if [ $errors -gt 0 ]; then
|
||||
errcolor="${COLOR_RED}"
|
||||
fi
|
||||
if [ $failures -gt 0 ]; then
|
||||
failcolor="${COLOR_RED}"
|
||||
fi
|
||||
echo
|
||||
echo "==== $tests TESTS in $elapsed SECONDS : ${errcolor}$errors ERRORS${COLOR_NORMAL}, ${errcolor}$failures FAILURES${COLOR_NORMAL} ===="
|
||||
}
|
||||
|
||||
function tunit_footer()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
tunit_footer()
|
||||
|
||||
Print a tunit footer.
|
||||
EOF
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
function tunit_testcase()
|
||||
{
|
||||
if [ "$1" == "--help" ]; then
|
||||
cat <<EOF
|
||||
tunit_testcase(classname, testname, elapsed[, failtype, failmsg, cdata])
|
||||
|
||||
Generate text to describe a tunit test case.
|
||||
classname : the name of the class containing the test
|
||||
testname : the name of the test in this class
|
||||
elapsed : Number of seconds elapsed during testing
|
||||
failtype : A one-word description of the failure type (generally an exception name or code)
|
||||
failmsg : A brief message (<32 chars) describing the failure
|
||||
cdata : More detailed information about the failure.
|
||||
EOF
|
||||
|
||||
return 1
|
||||
fi
|
||||
local classname testname elapsed failtype failmsg cdata
|
||||
|
||||
classname="$1"
|
||||
testname="$2"
|
||||
elapsed=$3
|
||||
failtype="$4"
|
||||
failmsg="$5"
|
||||
cdata="$6"
|
||||
|
||||
printf "[$classname] $testname .... "
|
||||
if [ "$failtype" != "" ]; then
|
||||
echo "${COLOR_RED}[FAILED]"
|
||||
echo " $failtype : $failmsg"
|
||||
echo "$cdata" | sed s/"^"/" "/g
|
||||
echo "${COLOR_NORMAL}"
|
||||
else
|
||||
echo "${COLOR_GREEN}[OK]${COLOR_NORMAL}"
|
||||
fi
|
||||
}
|
||||
10
tests/junit.sh
Normal file
10
tests/junit.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
TESTDIR=$(dirname ${BASH_SOURCE})
|
||||
. $TESTDIR/../lib/junit.sh
|
||||
|
||||
junit_header
|
||||
junit_testcase "super::class" "someTest" 31337
|
||||
junit_testcase "super::class" "someOtherTest" 31337 "generic failure" "Yo dawg, I heard you like failures" "This is some raw data, please read it"
|
||||
junit_footer
|
||||
|
||||
10
tests/tunit.sh
Normal file
10
tests/tunit.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
TESTDIR=$(dirname ${BASH_SOURCE})
|
||||
. $TESTDIR/../lib/tunit.sh
|
||||
|
||||
tunit_header
|
||||
tunit_testcase "super::class" "someTest" 31337
|
||||
tunit_testcase "super::class" "someOtherTest" 31337 "generic failure" "Yo dawg, I heard you like failures" "This is some raw data, please read it"
|
||||
tunit_footer
|
||||
|
||||
Reference in New Issue
Block a user