Testing Bash scripts

When you need to test a bash script you can use bats for that. That's how it is done:

#!/usr/bin/env bats

@test "addition using bc" {
  result="$(echo 2+2 | bc)"
  [ "$result" -eq 4 ]
}

@test "addition using dc" {
  result="$(echo 2 2+p | dc)"
  [ "$result" -eq 4 ]
}

bats uses special @test syntax to define tests. Inside the test's body every line should return 0 exit code or the test will fail.

Read full article here with different tools compared: https://medium.com/wemake-services/testing-bash-applications-85512e7fe2de

Did you find this post useful? Show some love!
Classic DEV Post from May 10

Elixir vs Ruby - where to use?

The comparison of Elixir and Ruby features to consider for using for different.

READ POST
Follow @iriskatastic to see more of their posts in your feed.
Nikita Sobolev
Member since Jul 9, 2017
More from @sobolevn
Using better CLIs
#productivity #shell #python #beginners
Trending on dev.to
Judging from a profile picture
#discuss #judgment
Automatic `nvm use`
#bash #node #javascript #linux
How to Be Really Awesome at Your Job and Not Be A Complete Jerk
#interpersonalskills #leadership #career
The Shell Introduction I Wish I Had
#shell #productivity #beginners #bash
Why I love hiring Junior engineers
#engineers #culture #career
Shell Scripting for Newbies 01
#bash #shell #script #beginners
Devs using font ligatures, what's the selling point?
#discuss
Note taking as a developer
#discuss #notes