Skip to content

Commit

Permalink
Introduce skip_if
Browse files Browse the repository at this point in the history
On can now skip some tests in the file if some condition is true.
  • Loading branch information
pascal authored and pascal committed Feb 20, 2024
1 parent dabb535 commit 0ffe477
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
28 changes: 28 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ _(by the way, the documentation you are reading is itself tested with bash-unit)
You can specify several patterns by repeating this option
for each pattern.
Tests will appear in *bash_unit* output as _pending_.
(see also _skip_if_)

*-r*::
executes test cases in random order.
Expand Down Expand Up @@ -574,6 +575,33 @@ out> > bar
doc:2:test_obvious_notmatching_with_assert_no_diff()
```

== *skip_if* function

skip_if <condition> <pattern>

If _condition_ is true, will skip all the tests in the current file which match the given _pattern_.

This can be useful when one has tests that are dependent on system environment, for instance:

```test
skip_if "uname | grep Darwin" linux
skip_if "uname | grep Linux" darwin

test_linux_proc_exists() {
assert "ls /proc/" "there should exist /proc on Linux"
}
test_darwin_proc_does_not_exist() {
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
}
```

will output, on a Linux system:

```output
Running test_darwin_proc_does_not_exist ... PENDING
Running test_linux_proc_exists ... SUCCESS
```

== *fake* function

fake <command> [replacement code]
Expand Down
10 changes: 10 additions & 0 deletions bash_unit
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ tap_format() {
}
}

skip_if() {
local condition="$1"
local pattern="$2"
if eval $condition >/dev/null 2>&1
then
skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}"
skip_pattern_separator="|"
fi
}

output_format=text
test_pattern=""
test_pattern_separator=""
Expand Down
35 changes: 16 additions & 19 deletions getting_started/tests/test_skipping
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
# This test demonstrates some test skipping logic that
# one can implement with bash_unit

todo_will_always_be_siped() {
todo_will_always_be_skipped() {
fail "This test is always skipped"
}

if uname | grep Linux
then
test_proc_exists() {
assert "ls /proc/" "there should exist /proc on Linux"
}
test_another_test_to_run_only_on_linux() {
assert "mkdir /tmp/foo/bar -p"
}
fi
skip_if "uname | grep Darwin" linux
skip_if "uname | grep Linux" darwin

test_linux_proc_exists() {
assert "ls /proc/" "there should exist /proc on Linux"
}
test_another_test_to_run_only_on_linux() {
assert "mkdir /tmp/foo/bar -p"
}

test_that_always_run() {
assert true
}

if uname | grep Darwin
then
test_proc_does_not_exist() {
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
}
test_another_test_to_run_only_on_darwin() {
assert "mkdir -p /tmp/foo/bar"
}
fi
test_darwin_proc_does_not_exist() {
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
}
test_another_test_to_run_only_on_darwin() {
assert "mkdir -p /tmp/foo/bar"
}
11 changes: 9 additions & 2 deletions tests/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,26 @@ Overall result: SUCCESS" \
test_do_not_run_skipped_tests() {
assert "$BASH_UNIT -s two \
<(echo 'test_one() { echo -n ; }
test_two() { fail ; }') \
test_two() { fail ; }
test_three() { fail ; }
skip_if true three
') \
"
}

test_skipped_tests_appear_in_output() {
bash_unit_output=$($BASH_UNIT -s two \
<(echo 'test_one() { echo -n ; }
test_two() { fail ; }') \
test_two() { fail ; }
test_three() { fail ; }
skip_if true three
') \
| "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \
)

assert_equals "\
Running tests in test_file
Running test_three ... PENDING
Running test_two ... PENDING
Running test_one ... SUCCESS
Overall result: SUCCESS" \
Expand Down

0 comments on commit 0ffe477

Please sign in to comment.