Shellspec
Project directory
.
├── .shellspec
├── lib
│ └── hello.sh
└── spec
├── hello_spec.sh
└── spec_helper.sh
A .shellspec
file is required in the project directory. It can be created with shellspec --init
.
--require spec_helper
## Default kcov (coverage) options
# --kcov-options "--include-path=. --path-strip-level=1"
# --kcov-options "--include-pattern=.sh"
# --kcov-options "--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/"
## Example: Include script "myprog" with no extension
# --kcov-options "--include-pattern=.sh,myprog"
## Example: Only specified files/directories
# --kcov-options "--include-pattern=myprog,/lib/"
Running shellcheck -s bash
in the above .
directory:
$ shellspec
Running: /bin/sh [bash 5.2.21(1)-release]
.
Finished in 0.43 seconds (user 0.31 seconds, sys 0.09 seconds)
1 example, 0 failures
./lib/hello.sh
#!/usr/bin/bash
hello() {
echo "Hello ${1}!"
}
./spec/hello_spec.sh
Describe 'hello.sh'
Include lib/hello.sh
It 'says hello'
When call hello ShellSpec
The output should equal 'Hello ShellSpec!'
End
End
Examples of _spec.sh
files are in https://github.com/shellspec/shellspec/tree/master/examples/spec
ShellSpec has its own DSL to write tests. It may seem like a distinctive code because DSL starts with a capital letter, but the syntax is compatible with shell scripts, and you can embed shell functions and use ShellCheck to check the syntax.
Basic Structure
- ExampleGroup
- A block for grouping example groups or examples.
- Describe
- An alias for ExampleGroup.
- Context
- An alias for ExampleGroup.
- Example
- A block for writing an evaluation and expectations.
- It
- An alias for Example
- Specify
- An alias for Example
- When
- call or run a shell function or command.
- The
- Expectation begins with The which does the verification.
- BeforeEach
- AfterEach
- BeforeAll
- AfterAll
- Include
- Include a shell script to test.
- Set
- Set shell options before executing each example.
- Data
- The input data is specified after #| in the Data or Data:expand block.
- Parameters
- defines a block of parameters.
Evaluation
./spec/spec_helper.sh
# shellcheck shell=sh
# Defining variables and functions here will affect all specfiles.
# Change shell options inside a function may cause different behavior,
# so it is better to set them here.
# set -eu
# This callback function will be invoked only once before loading specfiles.
spec_helper_precheck() {
# Available functions: info, warn, error, abort, setenv, unsetenv
# Available variables: VERSION, SHELL_TYPE, SHELL_VERSION
: minimum_version "0.29.0"
}
# This callback function will be invoked after a specfile has been loaded.
spec_helper_loaded() {
:
}
# This callback function will be invoked after core modules has been loaded.
spec_helper_configure() {
# Available functions: import, before_each, after_each, before_all, after_all
: import 'support/custom_matcher'
}
Four-Phase Test
- set up the test fixture
- Call system under test (SUT)
- Was expected outcome obtained?
- teardown the test fixture