Our great sponsors
-
git-auto-commit-action
Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
If you have questions or suggestions feel free to open a Discussion on the GitHub repository or contact me via Twitter or email.
Note: I'm by no means an expert when it comes to writing tests in Bash. All my knowledge comes from maintaining a GitHub Action called git-auto-commit. While I've developed the tests for git-auto-commit, I wished there was a resource which would guide me through all the steps necessary to add a test suite to my bash scripts. That's why this guide exists.
In this guide I would like to give you an introduction on how you can start writing tests for your Bash scripts by using Bats.
I won't add mocking capabilities to our example project here, but I would like to give a short overview on how you can add Mocking to your Bats tests. For my git-auto-commit project I've used Shellmock for a period of time, to mock all calls to git. (In hindsight, that was a mistake. Full story can be read here).
For example for me assert_line 'usage: foo ' is much easier to read and understand than ["${lines[0]}" = "usage: foo " ]. (Head over to the bats-assert repository to learn more about the available assertions).
bats-assert and bats-support are two libraries which will make writing Bash tests much more intuitive.