Using ShUnit : short & sweet tutorial

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

# 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 :P..Lets see..
but as of now CHEERS.. Happy testing !!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值