DEV Community

Nikita Sobolev
Nikita Sobolev

Posted on

3 1

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 ]
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay