View on GitHub

Expect

🧐 expect { BASH testing } toBe "lovely"

Download this project as a .zip file Download this project as a tar.gz file

Under Construction

Check back soon for the first release of Expect v2

Expect v2 Pre-Alpha TODO

Text, List, Array only (no explicit support for numbers or files/paths/directories)

Filters

Matchers

Expect v2 TODO

  • Checked means that it’s done for text, list, int, number, array, path, file, and directory

Filters

Matchers

Non-Goals for v2


🧐 Expect

Modern assertions for Shell Scripting in the 2020s


Download the latest version or install via:

curl -o- https://expect.specs.sh/install.sh | bash

Expectations for Everyone

Choose your own preferred syntax for test assertions:

output="$( ls dir 2>&1 )" # Example of a command that fails
exitcode=$?

Classic-style Assertions

assertEquals 2 $exitcode
assertContains "No such file or directory" "$output"

BASH-style Assertions

[: $exitcode -eq 2 ]
[[: "$output" = "*No such file or directory*" ]]

expect-style Assertions

expect { ls dir } to fail with exitcode = 2  \
                  and stdout containing "No such file or directory"

assertThat-style Assertions

assertThat { ls dir } fails with exitcode = 2  \
                      and stdout contains "No such file or directory"

should-style Assertions

:{ ls dir } should fail with exitcode = 2  \
            and stdout containing "No such file or directory"

The Expect library brings all of these lovely assertions styles that you love to BASH! 💖

All running on top of the same core code which provides lovely assertion failure messages.

Documentation

Download / Install

Download the latest version or install via:

curl -o- https://expect.specs.sh/install.sh | bash

Output

Expect 2.0.0 successfully installed

🧐 Downloaded files (and example syntax)

» assertThat.sh - assertThat { ls } contains "assertThat.sh"
» assertions.sh - assertContains "assertions.sh" "$( ls )"
» brackets.sh   - [[: "$( ls )" = "*brackets.sh*" ]]
» expect.sh     - expect { ls } to contain "expect.sh"
» should.sh     - :{ ls } should contain "should.sh"

To get started, source any of the provided files in your tests.

Visit https://expect.specs.sh for documentation

Getting Started

Filters

Expect provides a number of built-in helper functions to allow expectations such as:

# Get the first line in a string
expect { cat README } lines first to contain "Hello"

# Split a string into a list using a separator
{{ "Hello, world!" }} uppercase split " " first should equal "HELLO,"

# Join a string from a list using
assertThat [ Hello World ] join ":" equals "Hello:World"

# Work with array variables
items=( A B C )
expect items array last to equal "C"

Built-in Helpers

Click or tap one of the options below for description and examples:

Array ... hello
Array ... hello

Matchers

Click or tap one of the options below for description and examples:

Array ... hello
Array ... hello