views:

61

answers:

3

I inherited a shell-script application that is a combination of kshell scripts, awk, and java programs. I have written JUnit tests for the java pieces.

Is there a good way to do something similar for the kshell scripts and awk programs?

I have considered using JUnit and System.exec() to call the scripts, but it seems like there should be a better way.

+1  A: 

I have found shUnit2 and will try that.

Update with the results of trying out shUnit

shUnit works as expected. Script files are written with test functions defined and then a call to shUnit.

Example:

#!/bin/sh
testFileCreated()
{
  TESTFILE=/tmp/testfile.txt
  # some code that creates the $TESTFILE
  assertTrue 'Test file missing' "[ -s '${TESTFILE}' ]"
}
# load shunit2
. /path/to/shUnit/shunit2-2.1.5/src/shell/shunit2

Result

Ran 1 test.

OK

The 'OK' would be replaced with 'FAILED' if the file did not exist.

G_A
A: 

You might want to try Expect. It was designed for automating interactive programs. Of course Expect was written on top of TCL, which is an abominable scripting language. So there are interfaces for Python (Pexpect) and perhaps other languages that are more programmer friendly. But there is lots of documentation laying around for TCL/Expect that is still useful.

Steve K
A: 

This is not a direct answer to your question, but you may consider using a simple Makefile to run bash scripts with different parameters.

For example, write something like this:

cat >Makefile

test_all: test1 test2 test3

test1:
    script1 -parameter1 -parameter2

test2: $(addprefix test2file_, $TESTFILES)
test2file_%:
    script2 -filename $*

test3:
    grep|awk|gawk|sed....

By calling 'make test_all' you will execute all the scripts automatically, and the syntax is not so difficult to learn - you just have to define a rule name (test1, test_all...) and the commands associated with it.

dalloliogm