Skip to content

Instantly share code, notes, and snippets.

@ggorlen
Last active February 18, 2021 23:55
Show Gist options
  • Select an option

  • Save ggorlen/9f4ef17737c8e66c21ed9d16a9815f9b to your computer and use it in GitHub Desktop.

Select an option

Save ggorlen/9f4ef17737c8e66c21ed9d16a9815f9b to your computer and use it in GitHub Desktop.
bats poc

bats poc

resources

setup

mkdir bats
cd bats
git init
git submodule init
git submodule add https://github.com/bats-core/bats-core test/libs/bats
git submodule add https://github.com/ztombol/bats-assert test/libs/bats-assert
git submodule add https://github.com/ztombol/bats-support test/libs/bats-support
git add .
git commit -m 'installed bats'

tree

$ tree -L 2
.
├── greeter.sh
├── README.md
└── test
    ├── greeter.bats
    ├── test-helper.sh
    └── libs

2 directories, 4 files

TAP output

$ ./test/libs/bats/bin/bats -t test/greeter.bats
1..2
# [LOG] testing a debug log from the solution code
not ok 1 with name provided
# (from function `assert_output' in file test/libs/bats-assert/src/assert.bash, line 239,
#  in test file test/greeter.bats, line 24)
#   `assert_output "Hello, John!"' failed
# 
# -- output differs --
# expected : Hello, John!
# actual   : Hello, john!
# --
# 
# [LOG] testing a debug log from the solution code
# [LOG] testing log from within a test case
ok 2 without name provided
#!/usr/bin/env ./libs/bats/bin/bats
load "libs/bats-support/load"
load "libs/bats-assert/load"
load "test-helper.sh"
#fixtures suite
#setup_file() {
# echo "# setup file" >&3
#}
#setup() {
# echo "# setup test" >&3
#}
#teardown() {
# echo "# teardown test" >&3
#}
#teardown_file() {
# echo "# teardown file" >&3
#}
@test "with name provided" {
run bash greeter.sh greet john
assert_output "Hello, John!"
}
@test "without name provided" {
run bash greeter.sh greet
debug "testing log from within a test case"
assert_output "Hello there!"
}
#!/usr/bin/env bash
greet() {
debug "testing a debug log from the solution code"
if [ -z $1 ]; then
echo "Hello there!"
else
echo "Hello, $1!"
fi
}
"$@"
#!/usr/bin/env bash
debug() {
# https://bats-core.readthedocs.io/en/latest/writing-tests.html#printing-to-the-terminal
echo "# [LOG] ${*}" >&3
}
export -f debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment