Difference between revisions of "Bats Concepts"

From NovaOrdis Knowledge Base
Jump to: navigation, search
(Run a Command and Check the Status and Output)
(Setup/Teardown)
 
Line 59: Line 59:
  
 
If the BATS script includes setup() and teardown() functions, they are automatically executed by BATS before and after each test block runs.
 
If the BATS script includes setup() and teardown() functions, they are automatically executed by BATS before and after each test block runs.
 +
 +
<syntaxhighlight lang='bash'>
 +
function setup() {
 +
...
 +
}
 +
 +
function teardown() {
 +
...
 +
}
 +
</syntaxhighlight>
  
 
=Libraries being Tested and Helpers=
 
=Libraries being Tested and Helpers=

Latest revision as of 18:18, 11 October 2019

Internal

Overview

Shell scripts can be tested easier if the functionality is broken into many small, reusable and independently testable functions.

A library of such function can be tested by sourcing the library into the BATS test and run the functions as their usual calling layer would invoke them.

Test

An individual test is placed into a @test-annotated code block.

@test "something" {

    run my-command status
    [[ ${status} -eq 0 ]]
    [[ ${output} =~ We[[:space:]]are[[:space:]]good ]]
}

The label that follows @test may include spaces.

The functionality to be tested, most likely a function, must be executed with "run", as shown above. If executed that way, ${status} and ${output} are updated accordingly.

Tests are executed in the order they're specified in the file.

BATS uses exec to run each @test block as a separate subprocess. This makes it possible to export environment variables and even functions in one @test without affecting the other @tests or polluting the current shell session.

In all tests, $0 is /usr/local/Cellar/bats/0.4.0/libexec/bats-exec-test (or however BATS was installed on the system)

Run a Command and Check the Status and Output

some_command=....
[...]
run ${some_command}
[[ ${status} -eq 0 ]]
[[ ${output} =~ .*Stack[[:space:]]test[[:space:]]is[[:space:]]stable[[:space:]]and[[:space:]]running* ]]

${status}

${output}

$(output) coalesce lines:

some_command=....
[...]
run ${some_command}
[[ ${output} =~ .*Stack[[:space:]]test[[:space:]]is[[:space:]]stable[[:space:]]and[[:space:]]running* ]]

${lines}

Setup/Teardown

If the BATS script includes setup() and teardown() functions, they are automatically executed by BATS before and after each test block runs.

function setup() {
 ...
}

function teardown() {
 ...
}

Libraries being Tested and Helpers

Like any shell script or library, BATS test scripts can include helper libraries to share common code across tests. Libraries can be placed in the same test directory as the BATS scripts or in the test/libs directory, and loaded with the "load" function. The "load" function takes a path to a Bash file relative to the script being tested and sources that file. Files must end with the prefix .bash, but the path to the file passed to the load function can't include the prefix.

Also see:
Loading a Library to be Tested into a .bats Test

Environment Variables

https://github.com/sstephenson/bats#special-variables

BATS_TEST_DIRNAME

The directory in which the Bats test file is located.