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!
DISCUSSION
Classic DEV Post from Mar 12

How do you handle days where you just aren't motivated?

Some days just aren't your day

READ POST
Follow @ben 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
ps aux output in cronjobs
#bash #beginners #linux #devops
How do you think of angularjs application
#discuss
#OneDevMinute: Go back to the Previous Directory
#onedevminute #devtip #bash #zsh
Do you hide dev signatures in your code?
#discuss
How long have you been programming?
#discuss
Lessons I learned from training with special forces that I use in software development
#beginners #career #motivation #dieliving
Shell Aliases For Easy Directory Navigation #OneDevMinute
#onedevminute #devtips #shell #zsh
Tried TDD and didn't realize the benefits? Try it the next time you get writer's block
#javascript #node #testing #unittesting