https://github.com/thingsym/bats-assertion
Bats Assertion is a helper script for Bats
https://github.com/thingsym/bats-assertion
assertion bats helper-script
Last synced: about 1 month ago
JSON representation
Bats Assertion is a helper script for Bats
- Host: GitHub
- URL: https://github.com/thingsym/bats-assertion
- Owner: thingsym
- License: mit
- Created: 2018-04-08T08:52:44.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-12-09T08:25:57.000Z (almost 5 years ago)
- Last Synced: 2025-04-13T20:36:04.950Z (6 months ago)
- Topics: assertion, bats, helper-script
- Language: Shell
- Size: 9.77 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bats Assertion
Bats Assertion is a helper script for [Bats](https://github.com/bats-core/bats-core/).
[](https://travis-ci.org/thingsym/bats-assertion)
## Example
```
#!/usr/bin/env batsload bats-assertion
@test "assert_success" {
run bash -c 'exit 0'
assert_success
}@test "assert_failure" {
run bash -c 'exit 1'
assert_failurerun bash -c 'exit 123'
assert_failure
}@test "assert_status " {
run bash -c 'exit 0'
assert_status 0run bash -c 'exit 1'
assert_status 1run bash -c 'exit 123'
assert_status 123
}@test "assert_equal " {
run bash -c 'echo "abc"'
assert_equal "abc" "abc"
}@test "assert_match " {
run bash -c 'echo "abc"'
assert_match "ab" "abc"# regular expression matching
run assert_match "^ab" "abc"
run assert_match "bc$" "abc"
}@test "assert_lines_equal " {
run bash -c 'echo -e "abc\ndef\nghi"'
assert_lines_equal "abc" 0
assert_lines_equal "def" 1
assert_lines_equal "ghi" 2
assert_lines_equal "ghi" -1
}@test "assert_lines_match " {
run bash -c 'echo -e "abc\ndef\nghi"'
assert_lines_match "ab" 0
assert_lines_match "de" 1
assert_lines_match "gh" 2
assert_lines_match "gh" -1# regular expression matching
assert_lines_match "^ab" 0
assert_lines_match "hi$" 2
assert_lines_match "^[sfd][aec][emf]$" 1
assert_lines_match "^[sfd][aec][emf]$" -2
}
```### Failure output Example
```
✗ assert_status
(from function `assert_status ' in file bats-assertion.bash, line 30,
in test file sample.bats, line 7)
`assert_status 1' failed
Expected: 1
Actual : 0✗ assert_equal
(from function `assert_equal' in file bats-assertion.bash, line 40,
in test file sample.bats, line 31)
`assert_equal "cba" "abc"' failed
Expected: cba
Actual : abc✗ assert_lines_equal
(from function `assert_lines_equal' in file bats-assertion.bash, line 94,
in test file sample.bats, line 45)
`assert_lines_equal "abc" 1' failed
Expected: abc
Actual : def
Index : 1
```## Getting Started
### 1. Download Bats Assertion
```
git clone https://github.com/thingsym/bats-assertion
```### 2. Import Bats Assertion in bats file
```
load bats-assertion/bats-assertion
```### 3. Write tests using Bats Assertion
```
@test "assert_status" {
run ./bin/foo
assert_status 0
}
```## Assertion Reference
### assert_success
Reports an error if `$status` is not 0.
```
assert_success
```### assert_failure
Reports an error if `$status` is 0.
```
assert_failure
```### assert_status
Reports an error if the two variables `` and `$status` are not equal.
```
assert_status
```### assert_equal
Reports an error if the two variables `` and `` are not equal.
```
assert_equal
```Using `$output` as ``
```
assert_equal
```### assert_fail_equal
Reports an error if the two variables `` and `` are equal.
```
assert_fail_equal
```Using `$output` as ``
```
assert_fail_equal
```### assert_match
Reports an error if the two variables `` and `` are not match.
```
assert_match
```Using `$output` as ``
```
assert_match
```### assert_fail_match
Reports an error if the two variables `` and `` are match.
```
assert_fail_match
```Using `$output` as ``
```
assert_fail_match
```### assert_lines_equal
Reports an error if the two variables `` and the line of output in the `$lines` array passing the number of lines as a variable `` are not equal.
```
assert_lines_equal
```### assert_fail_lines_equal
Reports an error if the two variables `` and the line of output in the `$lines` array passing the number of lines as a variable `` are equal.
```
assert_fail_lines_equal
```Using reserved word, `first` and `last`
Assert the `first` line of `$lines` array.
```
assert_fail_lines_equal "first"
```Assert the `last` line of `$lines` array.
```
assert_fail_lines_equal "last"
```Assert all elements of `$lines` array.
```
assert_fail_lines_equal
```### assert_lines_match
Reports an error if the two variables `` and the line of output in the `$lines` array passing the number of lines as a variable `` are not match.
```
assert_lines_match
```Using reserved word, `first` and `last`
Assert the `first` line of `$lines` array.
```
assert_lines_match "first"
```Assert the `last` line of `$lines` array.
```
assert_lines_match "last"
```Assert all elements of `$lines` array.
```
assert_lines_match
```### assert_fail_lines_match
Reports an error if the two variables `` and the line of output in the `$lines` array passing the number of lines as a variable `` are match.
```
assert_fail_lines_match
```Using reserved word, `first` and `last`
Assert the `first` line of `$lines` array.
```
assert_fail_lines_match "first"
```Assert the `last` line of `$lines` array.
```
assert_fail_lines_match "last"
```Assert all elements of `$lines` array.
```
assert_fail_lines_match
```## Helper
### _dump
Dumps information about a variable.
```
_dump
```Using `$output` as ``
```
_dump
```## Contribution
### Patches and Bug Fixes
Small patches and bug reports can be submitted a issue tracker in Github. Forking on Github is another good way. You can send a pull request.
1. Fork [Bats Assertion](https://github.com/thingsym/bats-assertion) from GitHub repository
2. Create a feature branch: git checkout -b my-new-feature
3. Commit your changes: git commit -am 'Add some feature'
4. Push to the branch: git push origin my-new-feature
5. Create new Pull Request## Changelog
* Version 0.1.0
* Initial release.## License
Licensed under the MIT License.
## Author
[thingsym](https://github.com/thingsym)
Copyright (c) 2018 thingsym