Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Bash scripts

People also ask

Can you unit test bash?

Requirements for unit testing In Bash you can define functions to capture part of the business logic. Functions can be invoked from the same script file but it's also possible to import definitions to other scripts by using the source command. These tools provide the foundations for unit testing.

How do I test a bash file?

In order to check if a file exists in Bash, you have to use the “-f” option (for file) and specify the file that you want to check. For example, let's say that you want to check if the file “/etc/passwd” exists on your filesystem or not. In a script, you would write the following if statement.

What is bash testing?

A test in bash is not a test that your program works. It's a way of writing an expression that can be true or false. Tests in bash are constructs that allow you to implement conditional expressions. They use square brackets (ie [ and ] ) to enclose what is being tested.


There is actually a shunit2, an xUnit based unit test framework for Bourne based shell scripts. I haven't used it myself, but it might be worth checking out.

Similar questions have been asked before:

  • Unit Testing for Shell Scripts
  • Test Anything Protocol in Shell Scripts

TAP-compliant Bash testing: Bash Automated Testing System

TAP, the Test Anything Protocol, is a simple text-based interface between testing modules in a test harness. TAP started life as part of the test harness for Perl but now has implementations in C, C++, Python, PHP, Perl, Java, JavaScript, and others.

bats-core


I got the following answer from a discussion group:

it's possible to import (include, whatever) a procedure (function, whatever it's named) from an external file. That's the key to writing a testing script: you break up your script into independent procedures that can then be imported into both your running script and your testing script, and then you have your running script be as simple as possible.

This method is like dependency injection for scripts and sounds reasonable. Avoiding Bash scripts and using more testable and less obscure language is preferable.


Nikita Sobolev wrote an excellent blog post comparing a few different Bash test frameworks: Testing Bash applications

For the impatient: Nikita's conclusion was to use Bats, but it appears that Nikita missed the Bats-core project which appear to me to be the one to use going forward as the original Bats project has not been actively maintained since 2013.


I created shellspec, because I wanted a easy-to-use and useful tool.

It written by a pure POSIX shell script. It has been tested with many shells more than shunit2. It has more powerful features than bats/bats-core.

For example, it supports nested block, easy to mock/stub, easy to skip/pending, parameterized tests, assertion line number, execute by line number, parallel execution, random execution, TAP/JUnit formatter, coverage and CI integration, profiler, etc.

See the demo on the project page.

  • https://github.com/shellspec/shellspec
  • https://shellspec.info/

Epoxy is a Bash test framework I designed mainly for testing other software, but I use it to test Bash modules as well, including itself and Carton.

The main advantages are relatively low coding overhead, unlimited assertion nesting and flexible selection of assertions to verify.

I made a presentation comparing it to BeakerLib - a framework used by some at Red Hat.