Skip to content

Document how to add debug output to bats testingΒ #744

Open
@glennj

Description

@glennj

Because bats slurps up both stdout and stderr, it's a bit tricky to add debugging output:

$ cat hello_world.sh
#!/usr/bin/env bash

printf '%(%T)T -- %s\n' -1 'a debugging statement' >&2
echo "Hello, World!"

$ exercism test
Running tests via `bats hello_world.bats`

1..1
not ok 1 Say Hi!
# (from function `assert_output' in file bats-extra.bash, line 394,
#  in test file hello_world.bats, line 13)
#   `assert_output "Hello, World!"' failed
#
# -- output differs --
# expected (1 lines):
#   Hello, World!
# actual (2 lines):
#   11:58:21 -- a debugging statement
#   Hello, World!
# --
#

$ echo $?
1

bats provides file descriptor 3 for just this purpose: stdout and stderr are captured for the tests, and fd 3 is available for debug output: change the printf redirection to >&3 and:

$ exercism test
Running tests via `bats hello_world.bats`

1..1
11:59:10 -- a debugging statement
ok 1 Say Hi!

$ echo $?
0

This should be documented where it can be helpful to students.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussiondocumentationenhancementx:action/improveImprove existing functionality/contentx:type/codingWrite code that is not student-facing content (e.g. test-runners, generators, but not exercises)x:type/docsWork on Documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions