This manual explains the basic usage of stl.
stl is a testing library designed to simplify writing tests for shell scripts. Here's a simple test example.
- Test target:
app/sample.sh
#!/bin/sh
echo "$1"
- Test code:
stl/code/sample.sh
setup() {
target_script="${PRJ_ROOT_DIR}/app/sample.sh"
}
teardown() {
:
}
stl_echo_test() {
assert_equal "\"${target_script}\" hello" "hello"
}
$ ./stl/start_stl.sh
[OK] sample.sh:10 stl_echo_test
The meaning of this output is as follows.
[OK]
: Indicates that the test case succeededsample.sh
: The name of the file where the test was executed:10
: Indicates the line number where the assertion function was executedstl_echo_test
: The name of the executed test case function
Here's an example of a failed test.
Add a failing test case function as follows.
stl_echo_test_ng() {
assert_equal "\"${target_script}\" NG" "hello"
}
$ ./stl/start_stl.sh
[OK] sample.sh:10 stl_echo_test
[NG] sample.sh:14 stl_echo_test_ng
[EXPECTED]
hello
[ACTUAL]
NG
The meaning of this output is as follows.
[NG]
: Indicates that the test case failedsample.sh
: The name of the file where the test was executed:14
: Indicates the line number where the assertion function was executedstl_echo_test_ng
: The name of the executed test case function
When a test fails, additional information is displayed.
[EXPECTED]
: Shows the expected result in the test[ACTUAL]
: Shows the actual result obtained in the test
The setup()
and teardown()
functions have special roles.
setup()
: Called before each test case function is executed. It prepares for the test.teardown()
: Called after each test case function is executed. It performs cleanup after the test.
These function definitions cannot be omitted.
For usage of each assertion function, please refer to the following document.
All *.sh
files found recursively under the stl/code
directory are determined to be test code files.
Test case functions must start with the stl_
prefix.
For example, function names like these.
stl_echo_test
stl_file_exists_test
stl_error_handling_test
By following this naming convention, stl automatically identifies and executes test case functions.
It is recommended to create test code files for each test target. This makes test management easier and allows grouping of related test cases.
Grouping test case functions with long execution times is also one of the criteria for splitting. You can reduce test execution time by running only specific test code files.
Test code execution follows the following flow.
- Detection of test code files
- Import for each test code file
- Specifically, it is read with the shell command
.
- Normal shell script code such as function definitions and variable definitions in the test code file is executed at this stage
- Specifically, it is read with the shell command
- Sequential execution of test case functions in the test code file
The behavior changes depending on whether you use {}
or ()
when defining test case functions.
Executes the function in the current shell environment. Variable changes affect other test case functions.
stl_echo_test() {
assert_equal "\"${target_script}\" hello" "hello"
}
Executes the function in a subshell. Variable changes do not affect other test case functions.
stl_echo_test() (
assert_equal "\"${target_script}\" hello" "hello"
)
To maintain test independence, it is generally recommended to use ()
.
Additionally, the following writing style is also possible.
stl_echo_test() {
(
assert_equal "\"${target_script}\" hello" "hello"
)
}
-f <filename>
: Executes only the specified test case file.
# When executing a single test code file
$ ./stl/start_stl.sh -f ./stl/code/sample.sh
# When executing multiple test code files
$ ./stl/start_stl.sh -f ./stl/code/sample_1.sh -f stl/code/sample_2.sh
The main environment variables available in test code are as follows.
PRJ_ROOT_DIR
: Absolute path of the parent directory of thestl
directory (STL_ROOT_DIR
)STL_ROOT_DIR
: Absolute path of the stl root directory (stl
directory)STL_FUNCTION_DIR
: Absolute path of thestl/func
directory where stl functions are storedSTL_TEST_CODE_DIR
: Absolute path of thestl/code
directory where test code is storedSTL_TEST_DATA_DIR
: Absolute path of thestl/data
directory where test data is storedSTL_MODULE_DIR
: Absolute path of thestl/mod
directory where stl modules are storedTEST_FILE_SELECTION
: Indicates whether a specific test file has been selected using the-f
option. When set to "true", it means a particular test file was specified for execution
By using these environment variables, you can use relative paths in your test code that do not depend on the project structure.
- Reference: Assertion Functions. Detailed explanation of assertion functions
- Design and Implementation of stl (Shell Testing Library): Overview of stl's design philosophy, structure, and implementation features
- Advanced Practical Guide. Advanced usage and practical techniques
- Glossary: Definitions of key terms used in stl