lehmannro / assert.sh Goto Github PK
View Code? Open in Web Editor NEWbash unit testing framework
License: GNU Lesser General Public License v3.0
bash unit testing framework
License: GNU Lesser General Public License v3.0
This way I could have info messages like:
echo "$TEST_NUMBER: testing foo"
assert something something
echo "$TEST_NUMBER: testing bar"
assert something something
Great project, and much needed. Have you ever considered supporting a "refute" function? It would allow you to test that a command returns anything BUT a certain value. Example:
#check that $count hasn't been defined as 1
refute "echo $count", 1
When writing turly portable shell scripts, one would resort to use the POSIX compatible subset, sh
. However, the assert.sh
script uses some bashisms, so it will not work in POSIX-compatible mode.
Reproduce the issue by running:
sh assert.sh # Will complain about redirects; probably due to the use of <<<
I'm not sure how tests.sh runs on other Mac OS X machines, but when I run it I get the following errors, none of them look to me like they should work on other Mac OS X systems.
Recreate:
git clone [email protected]:lehmannro/assert.sh.git assert.sh-framework
cd assert.sh-framework
./tests.sh
Output:
all 9 demo tests passed in 0.000s.
test #15 "_clean; skip_if 'cat /etc/passwd | grep $(echo $USER)';
assert_raises false; assert_end;" failed:
expected "all 0 tests passed."
got "test #1 "false" failed:\n program terminated with code 1 instead of 0\n1 of 1 tests failed."
1 of 15 output tests failed in 0.000s.
test #11 "shopt -o errexit" failed:
program terminated with code 1 instead of 0
1 of 15 interaction tests failed in 1.000s.
test #10 "echo -n %s | wc -c" failed:
expected "2"
got " 2"
1 of 11 regression tests failed in 0.000s.
wc
is the BSD version and not the gnu version, and the output seems to differ with the BSD version.(I tried both master
branch and v1.1
tag).
Is it possible to implement a mode which makes any unit test fail if it outputs anything to standard error or has an exit code different from 0?
An excellent project by the way, it comes in very handy.
Here is my test file for mvb
:
#! /bin/bash
. assert.sh
# `mvb a b` should fail if 'b' doesn't exist
assert_raises 'mvb a b' 1
# `mvb a b` should rename 'b' to 'a'
touch b
assert_raises 'mvb a b' 0
assert_raises '[ -f a ]' 0
rm a
# `mvb a b/` should mv 'b/a' in 'a'
mkdir b
touch b/a
assert_raises 'mvb a b/' 0
assert_raises '[ -f a ]' 0
rm a
rmdir b
assert_end 'mvb'
I tested on Travis and bc
is not installed by default, here is the (end of the) log:
$ cd tests;bash ./tests.sh
assert.sh: line 81: bc: command not found
all 5 mvb tests passed in s.
�The command "bash ./tests.sh" exited with 0.�
Done. Your build exited with 0.
Would be awesome to optionally get a JUnit XML test report at the end.
Any suggestions as to how that could best be accomplished?
I add some doc lines, but CI tell me that build failed. (#21)
Which state of this project? Is it supported?
Hey dude, GitHub have a dependency management bit, and it's showing up incorrectly due to how it gets information between npm and GitHub. Your name
in the package.json
is assert
, but that's already an npm package, so GitHub is getting super confused.
Can you remove the name field from your package.json
file. I'd recommend using make
or another build tool instead of npm for non-node repos.
The image below shows that there are repos with your dependency, but actually they are depending on commonjs-assert
, which is what is actually aliased to that name in NPM.
https://www.npmjs.com/package/assert <- this is what you are saying your repo is
Cheers.
tap-function's link in the Related Projects does no longer work.
Looks like a nice lightweight tool.
But there is always a but :-)
The script I want to test has this line:
set -u
to protect me against uninitialized variables.
Unfortunately DISCOVERONLY seems to be one such and it cause assert to crash.
Is this project still maintained and stable? Last commit on master
branch was 10 months ago and build on Travis CI failing from that time. I would like to use this project, but I'm not sure about its stability.
Hi Robert.
Here's the assert_contains() function I wrote for regex matching.
Usage is: assert_contains "str" "regex" [0|1], where 0=expected match, 1=nomatch
assert_contains() {
# assert_contains [expected](%28 tests_ran++ %29) || :
[[ -n "$DISCOVERONLY" ]] && return || true
str=${1:-}
regex=${2:-}
expected=${3:-0}
# status = 0 match, 1 = no match
status=1
# try the match. there's 3 possible outcomes:
#
#1) it may succeed and be captured if user specified parentheses: status=0 and match="something"
#2) it may succeed and not be captured if parentheses not used: status=0 and match=""
#3) it may fail to match: status=1 and match=""
match=""
if [[ $str =~ $regex ]]; then
status=0
match=${BASH_REMATCH[1]} # maybe some kind of capture is available
fi
if [[ "$status" -eq "$expected" ]]; then
[[ -n "$DEBUG" ]] && echo -n . || true
return
fi
if [ $status -eq 0 ] && [ -z "$match" ]; then
if [[ $str =~ ($regex) ]]; then # the previous regex succeeded but no capture in $match, so try again with capturing parenthesess for debugging
match=${BASH_REMATCH[1]}
fi
fi
_assert_fail "match terminated with code $status and match '$match' instead of $expected" "$str" "$expected"
}
bc
is currently used to calculate test time, but the installation instructions say that only sourcing assert.sh
is required. The instructions should be updated to ensure people install bc
.
Could you give any advice on how to test stderr? E.g. for assert_raises
I'd love to assert on some expected error message.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.