Introduction | |
---|---|
shellUnit is a unit-test framework for bash-style and csh-style shells. It provides a way to separate the tests from the scripts, test functions main procedure and code blocks from a shell script. |
|
A set os exaples will we shown now. This document is automatically generated by the mkdoc.sh script in the documents directory so commands and outputs match. |
|
Sample | |
A test script is a plain shell script with a few characteristics. Test functions are declared csh-style and start with 'test', also, to be recogniced by automatic directory testing, its extension must be '.shu' |
|
Running | |
To execute a test file we use the 'shu' command. It needs at least one parameter, indicating either a test file: |
|
# shu sample-01.shu |
|
Or a directory containing various .shu files |
|
# shu ./sample-dir/ |
|
Targetting | |
To provide a target we use the 'shu_loadTarget' function which as a parameter has the path to the shell script to be tested. At pressent, loading a target executes its 'main' procedure (all commands not in a function) so be careful with that. |
|
The sample target file: |
|
The output of shu ./sample-02/ |
|
The test asserts that 'a_function' will echo the string 'TEST'. shellUnit provides many assertions for different situations. here is a test using all the 'basic' asserts. |
|
The result of executing this test file is: |
|
shellUnit works as a preprocessor for test files. This means that to provide meaningful messages and dispatch tests it reads a test file and its targets and produces a single shell script which is executed. Because of that, some variables won't be accesible from a shell script, most notably the return-value variable$?. To access the return value, the SHU_RV variable is provided. |
|
Executing the test will yield the following output. Notice that an assert, wether failed or not, will print the line it is located preceded by an '@', and if failed, a reason why will be provided. Also, any function not starting with 'test' will be ignored by shellUnit, this way we can make self-contained tests if wanted. |
|
For testing the 'main' function, as if we called the script from the shell, shellUnit provided the 'shu_main' function, which will behave like the shell script. |
|
Results | |
shellUnit results can be formatted in different ways, the results shown so far being the default output formatter. So far, all samples were using the default 'pretty' formatter. |
|
The 'raw' output formatter present the test result as CSV. Raw output can be activated by passing the --raw option to shellUnit. This format is intended to be used by other programs as its input. It provides various information about the tests and asserts being run. |
|
An example of the raw output, using the basic asserts files: |
|
# shu --raw basic-asserts.shu |
|
The CSV format is not yet frozen so no information on what each column is will be provided until then. |
|
Setup/Teardown | |
Test scripts may optionally have a 'setup' and 'teardown' function, which will be executed before and after every test respectively. |
|
Groups | |
Parametrizing |