BASIS  r3148
shtap.sh File Reference

Unit testing framework for BASH based on the Test Anything Protocol. More...

Go to the source code of this file.

Functions

function diag (in msg)
 Print diagnostic message.
function fail ()
 Fail in any case and print reason.
function is (in result, in expected, in name)
 Compare actual and expected result.
function isnt (in result, in expected, in name)
 Test whether a given result is not equal the expected result.
function like (in result, in pattern, in name)
 Test whether a given result matches an expected pattern.
function ok (in expression, in name)
 Evaluate test expression and fail if it does not evaluate to 0 or check return value of function.
function okx (in command)
 Execute command and check return value.
function pass ()
 Print plan.
function plan_no_plan ()
 Choose not to plan number of tests in advance.
function plan_skip_all ()
 Plan to skip all tests.
function plan_tests ()
 Plan a certain number of tests and stick to it.
function SHTAP_BAIL_OUT (in reason)
 Bail out.
function skip (in condition, in reason, in n)
 Skip tests under a certain condition.
function unlike (in result, in pattern, in name)
 Test whether a given result does not match an expected pattern.

Variables

string SHTAP_VERSION = "1.02-basis"

Detailed Description

Unit testing framework for BASH based on the Test Anything Protocol.

Author:
Patrick LeBoutillier <patl at cpan.org>
Note:
This file is a copy of the tap-functions file which is part of the JTap project (http://svn.solucorp.qc.ca/repos/solucorp/JTap/trunk/). The original implementation has been modified by Andreas Schuh as part of the BASIS project at SBIA.

Plan:

Test:

 ok RESULT [NAME]
 okx COMMAND
 is RESULT EXPECTED [NAME]
 isnt RESULT EXPECTED [NAME]
 like RESULT PATTERN [NAME]
 unlike RESULT PATTERN [NAME]
 pass [NAME]
 fail [NAME]

Skip:

 skip [CONDITION] [REASON] [NB_TESTS=1]

 skip $feature_not_present "feature not present" 2 ||
 {
     is $a "a"
     is $b "b"
 }

Specify TODO mode by setting the TODO variable:

 TODO="not implemented yet"
 ok $result "some not implemented test"
 unset TODO

Other:

 diag MSG

Example:

 #! /usr/bin/env bash

 source shtap.sh

 plan_tests 7

 # test identity
 {
     me=${USER}
     is ${USER} ${me} "I am myself"
     like ${HOME} ${me} "My home is mine"
     like "`id`" ${me} "My id matches myself"
 }

 # test ls
 {
     ls ${HOME} 1>&2
     ok $? "ls ${HOME}"
     # same thing using okx shortcut
     okx ls ${HOME}
 }

 # test only for root
 {
     [[ "`id -u`" != "0" ]]
     i_am_not_root=$?
     skip ${i_am_not_root} "Must be root" ||
     {
         okx ls /root
     }
 }

 # test TODO
 {
     TODO="figure out how to become root..."
     okx [ "$HOME" == "/root" ]
     unset TODO
 }

Copyright (c) Patrick LeBoutillier.
Copyright (c) 2011, University of Pennsylvania.
All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

See also:
http://testanything.org/wiki/index.php/Tap-functions

Contact: SBIA Group <sbia-software at uphs.upenn.edu>

Definition in file shtap.sh.


Function Documentation

function diag ( in  msg)

Print diagnostic message.

Parameters:
[in]msgDiagnostic message.
Returns:
Always 1.
function fail ( )

Fail in any case and print reason.

Returns:
Nothing.
function is ( in  result,
in  expected,
in  name 
)

Compare actual and expected result.

Parameters:
[in]resultActual result.
[in]expectedExpected result. Diagnostic message for is().
[in]resultActual result.
[in]expectedExpected result. Test whether a given result is equal to the expected result.
[in]resultActual result.
[in]expectedExpected result.
[in]nameOptional name of test.
Returns:
Whether the results are equal.
Return values:
0On equality.
1Otherwise.
function isnt ( in  result,
in  expected,
in  name 
)

Test whether a given result is not equal the expected result.

Parameters:
[in]resultActual result.
[in]expectedExpected result.
[in]nameOptional name of test.
Returns:
Whether the results were not equal.
Return values:
0Otherwise.
1On equality.
function like ( in  result,
in  pattern,
in  name 
)

Test whether a given result matches an expected pattern.

Parameters:
[in]resultActual result.
[in]patternExpected pattern.
[in]nameOptional name of test.
Returns:
Whether the result matched the pattern.
Return values:
0On match.
1Otherwise.
function ok ( in  expression,
in  name 
)

Evaluate test expression and fail if it does not evaluate to 0 or check return value of function.

This is the workhorse method that actually prints the tests result.

Parameters:
[in]expressionTest expression or return value.
[in]nameName of test.
Returns:
Nothing.
function okx ( in  command)

Execute command and check return value.

Parameters:
[in]commandCommand to run.
Returns:
Nothing.
function pass ( )

Print plan.

Pass in any case and print reason.

Returns:
Nothing.
function plan_no_plan ( )

Choose not to plan number of tests in advance.

Returns:
Nothing.
function plan_skip_all ( )

Plan to skip all tests.

Returns:
Nothing.
function plan_tests ( )

Plan a certain number of tests and stick to it.

Returns:
Nothing.
function SHTAP_BAIL_OUT ( in  reason)

Bail out.

Parameters:
[in]reasonReason for bailing out.
Returns:
Nothing. Instead, exits the process with error code 255.
function skip ( in  condition,
in  reason,
in  n 
)

Skip tests under a certain condition.

Parameters:
[in]conditionThe condition for skipping the tests. If 0, the tests are skipped, otherwise not.
[in]reasonAn explanation for why skipping the tests.
[in]nThe number of tests which will be skipped.
Returns:
Whether the tests were skipped.
Return values:
0If tests are to be skipped.
1Otherwise.
function unlike ( in  result,
in  pattern,
in  name 
)

Test whether a given result does not match an expected pattern.

Parameters:
[in]resultActual result.
[in]patternExpected pattern.
[in]nameOptional name of test.
Returns:
Whether the result did not match the pattern.
Return values:
0Otherwise.
1On match.

Variable Documentation

string SHTAP_VERSION = "1.02-basis"

Definition at line 117 of file shtap.sh.