Here comes a unit testing framework for Bourne shell code similar to junit framework for java. Lets digest it.
1) Download shunit framework from here http://sourceforge.net/projects/shunit/files/shunit/1.5/ShUnit-1.5.zip/download
2) Untar this downloaded framework in any directory. I l use "/home" directory
3) Set a variable SHUNIT_HOME that points to the directory where ShUnit's files are. E.g., add 'export SHUNIT_HOME=/home/ShUnit'
4) Lets create a simple script "print_something" which will print something. You can create this script in /home folder too.
#!/bin/sh
# print_something - a testable script
print_something()
{
echo "$1"
}
invalid_print_something()
{
echo "nothinggggg"
}
Here, method print_something will print the parameter passed to it. Method invalid_print_something will print "nothinggggg" always.
5) To unit test this script we need to test the functionality of the above two functions. Lets create a test script
#!/bin/sh
# print_something.test - unit tests for print_something
. "$SHUNIT_HOME/shUnit"
# Include script to be tested
. ./print_something.sh
TestScriptExists()
{
shuNonZeroFile <path of file to test>
}
TestPrintSomething()
{
EXPECTED="this is a test"
RESULT=`print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test print_something" $?
}
TestInvalidPrintSomething()
{
EXPECTED="this is a test"
RESULT=`invalid_print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test invalid print_something" $?
}
InitFunction()
{
shuRegest TestScriptExists
shuRegTest TestPrintSomething
shuRegTest TestInvalidPrintSomething
}
shuStart InitFunction
Lets see this test script step by step
# print_something.test - unit tests for print_something
. "$SHUNIT_HOME/shUnit"
We include shunit from frmaework
# Include script to be tested
. ./print_something.sh
Include script to be tested
TestPrintSomething()
{
EXPECTED="this is a first test"
RESULT=`print_something "$EXPECTED"`
test "${EXPECTED}" = "${RESULT}"
shuAssert "Test print_something" $?
}
This method intern calls print_something method of print_something script and tallys the expected result.
Other method works in similar way
InitFunction()
{
shuRegest TestScriptExists
shuRegTest TestPrintSomething
shuRegTest TestInvalidPrintSomething
}
Call out test methods
Call "TestScriptExists" method first to check that script exists.
shuStart InitFunction
Call InitFunction as starting function
6) Expecting files print_something.sh, print_something_test.sh in same directory, We now run our test script
[root@sidd siddhesh]# sh print_something_test.sh
****** print_something_test.sh ******
2 tests to run:
Test 1: TestPrintSomething .
Test 2: TestInvalidPrintSomething E
"Test invalid print_something" failed.
2 tests run.
1 test succeeded.
1 test failed.
7) Lets see some more properties of SHUNIT's
We saw functionality of "shuAssert "Test invalid print_something" $?"
it will pass and display message if $? turns out to be ZERO. What if we want to test few negative things. In this case will use
shuDeny "I am negative test case" $?
Here, Test will pass if value of $? comes out as 1
8) Following method is executed at the begining of the test script file
shuSetUp()
{
// Create your directories,change file permissions etc etc
}
9) Following method is executed at the end of the test script file
shuTearDown()
{
// Delete your directories,change file permissions etc etc
}
10) We have used shuassert like this
shuassert <statement> $?
This will pass if $? is 0
We can use 'shuDeny ' which passes if $? is 1.
11) I dont think mocking anything will be difficult task with shunits. I havent tried it out yet
..Lets see..
but as of now CHEERS.. Happy testing !!