Skip to content
Kate Ward edited this page Sep 24, 2017 · 11 revisions

Cookbook

Passing arguments to test script

Passing command-line arguments to a test script is not a problem, as long as one small requirement is met. Before calling shunit2, use shift to shift all the command-line arguments off the stack.

The following code (saved as test_date_cmd.sh) demonstrates proper usage.

#!/bin/bash
  
# Echo any provided arguments.
[ $# -gt 0 ] && echo "ARGC: $# 1:$1 2:$2 3:$3"

test_date_cmd() {
  ( date '+%a' >/dev/null 2>&1 )
  local rtrn=$?
  assertTrue "unexpected date error; ${rtrn}" ${rtrn}
}

# Eat all command-line arguments before calling shunit2.
shift $#
. ~/lib/sh/shunit2

Calling the command without arguments.

$ ./test_date_cmd.sh 
test_date_cmd

Ran 1 test.

OK

Calling the command with arguments.

#:3 1:a 2:b 3:c
test_date_cmd

Ran 1 test.

OK
Clone this wiki locally