teSHt is a microframework for functional testing in Bash scripts. With its simple and intuitive assertions, you can easily verify the behavior of your scripts.
-
Add teSHt to your project as a submodule by following the instructions here.
-
Write your tests using the provided assertions.
-
Run all tests using the Docker container.
# Test the creation of a directory
test mkdir -p /tmp/foo/bar
assert_success
assert_dir /tmp/foo/bar
# Test changing to a directory and checking the current directory
test cd /tmp/foo/bar
assert_success
test pwd
assert_stdout /tmp/foo/bar
# Test handling an error
test cd /nonexistent_dir
assert_fail
assert_stderr "cd: /nonexistent_dir: No such file or directory"
# Test the presence of a file
test touch /tmp/foo/bar/test.txt
assert_success
assert_file /tmp/foo/bar/test.txt
To create test files with teSHt, follow these steps:
-
Create a directory named
tests
in your project, where you will store your test files. -
In the
tests
directory, create a file with a.tsh
extension. This file should contain your assertions and tests. -
For example, the following code tests the creation of a directory:
# ./test/mkdir.tsh - Test the creation of a directory
test mkdir -p /tmp/foo/bar
assert_success
assert_dir /tmp/foo/bar
With these simple steps, you're ready to create your test files with teSHt!
Running tests with teSHt is easy and flexible. You can run your tests using any version of Bash that you want, simply by using Docker.
Here's how:
-
Make sure you have Docker installed on your system.
-
Choose the version of Bash you want to run your tests with teSHt.
-
Run the following command, replacing
<version>
depending on the version of Bash you want to use:
$ docker run -it --rm -v `pwd`:/`basename $(pwd)` -w /`basename $(pwd)` bash:<version> ./tesht.sh "tests/*.tsh"
teSHt will run all test files with a .tsh extension in the tests directory, and display the results in the terminal. That's it! You can now run your tests with any version of Bash you want, using Docker and teSHt.
teSHt can be easily integrated with Continuous Integration (CI) systems, allowing you to automate your testing process.
The tests are executed using the Docker container, and in the case of a failed test scenario, teSHt returns a non-zero exit code, indicating a failure. This exit code can be captured by your CI system, allowing you to immediately detect and address any issues with your code.
To integrate teSHt with your CI system, simply add the docker run
command to your CI pipeline. This will run the tests every time your code is pushed or a new build is created.
In this way, teSHt helps you ensure that your code always meets the expected standards and specifications, and that any issues are caught and addressed quickly, keeping your development process efficient and streamlined.