+ - 0:00:00
Notes for current slide
Notes for next slide

Testing CLI oriented tools with BATS

live presentation
twitter, medium

Shameless promotion: you can also read my political blog
1 / 19

BATS

BATS

Stands for: Bash Automated Testing System

2 / 19

PROBLEM: Test driven development for CLI apps

3 / 19

PROBLEM: Test driven development for CLI apps

  • I would like to functional test (or integration, depends on lingo) the same as I do with webapps. from outside in
4 / 19

PROBLEM: Test driven development for CLI apps

  • I would like to functional test (or integration, depends on lingo) the same as I do with webapps. from outside in
  • Why? verification, reproducible results, regression safeguard.
5 / 19

PROBLEM: Test driven development for CLI apps

  • I would like to functional test (or integration, depends on lingo) the same as I do with webapps. from outside in
  • Why? verification, reproducible results, regression safeguard.

  • How? BATS to the rescue!

6 / 19

BATS

  • Allows setup/teardown
  • test suites
  • syntactic sugar around common CLI testing checks
7 / 19

Real Live example. testing "swamp" a process runner

8 / 19

Simple test

@test "Check that the swamp client is available" {
command -v swamp
}
  • Would Simply fail if exit code is different from 0
  • everything* in BATS is run with set -x
9 / 19

Testing output

@test "Check swamp -h prints usage" {
run swamp -h
[ "$status" -eq 0 ]
[ "${lines[0]}" = " Usage: swamp [options]" ]
}
  • run - command to encapsulate calling our command (allows exiting with different value then 0)
  • $status special variable with the status code
  • $output
  • ${lines[0]} special array contains the output lines and accessible as array
10 / 19

Skipping

@test "Check Swamp respects boot order and wait for ready" {
skip "not implemented yet! coming soon"
}
11 / 19

Shebang

#! /usr/bin/env bats
  • Needed in the beginning of the test suite
12 / 19

Teardown and setup

teardown() {
echo "Teardown test"
echo "$BATS_TEST_NAME"
run swamp -p bats/valid/ -H
run swamp -p bats/invalid/ -H
}
13 / 19

How would it look?

$ cd test && bats bats
1..12
ok 1 Check that the swamp client is available
ok 2 Check swamp -h prints usage
ok 3 Check swamp -d runs as daemon mode and swamp -H stops it
ok 4 Check stopping and starting a service actually starts the service
14 / 19

Installation

sudo add-apt-repository ppa:duggan/bats
sudo apt-get update
sudo apt-get install bats
15 / 19

Open source rocks!

17 / 19

Thanks for listening!

18 / 19
19 / 19

BATS

BATS

Stands for: Bash Automated Testing System

2 / 19
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow