Skip to content

Commit c15f49a

Browse files
committed
Introduce a skip test option
One can now decide to not run some tests if they match a given pattern using the `-s` command line option.
1 parent 4401a13 commit c15f49a

3 files changed

Lines changed: 87 additions & 8 deletions

File tree

README.adoc

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bash_unit - bash unit testing enterprise edition framework for professionals!
1212

1313
== Synopsis
1414

15-
*bash_unit* [-f tap] [-p <pattern>] [-r] [test_file]
15+
*bash_unit* [-f tap] [-p <pattern>] [-s <pattern>] [-r] [test_file]
1616

1717
== Description
1818

@@ -36,6 +36,12 @@ _(by the way, the documentation you are reading is itself tested with bash-unit)
3636
You can specify several patterns by repeating this option
3737
for each pattern.
3838

39+
*-s* _pattern_::
40+
skip tests which name matches the given pattern.
41+
You can specify several patterns by repeating this option
42+
for each pattern.
43+
Tests will appear in *bash_unit* output as _pending_.
44+
3945
*-r*::
4046
executes test cases in random order.
4147
Only affects the order within a test file (files are always
@@ -199,6 +205,39 @@ Running tests in tests/test_core.sh
199205
Overall result: SUCCESS
200206
```
201207

208+
You can combine the _-p_ option with _-s_ to skip some of the tests. This option accepts a pattern
209+
as parameter and mark as pending any test function which matches this pattern.
210+
211+
```test
212+
./bash_unit -p fail_fails -p assert -s no -s status tests/test_core.sh
213+
```
214+
215+
```output
216+
Running tests in tests/test_core.sh
217+
Running test_assert_equals_fails_when_not_equal ... PENDING
218+
Running test_assert_matches_fails_when_not_matching ... PENDING
219+
Running test_assert_no_diff_fails_when_diff ... PENDING
220+
Running test_assert_no_diff_succeeds_when_no_diff ... PENDING
221+
Running test_assert_not_equals_fails_when_equal ... PENDING
222+
Running test_assert_not_equals_succeeds_when_not_equal ... PENDING
223+
Running test_assert_not_matches_fails_when_matching ... PENDING
224+
Running test_assert_not_matches_succeed_when_not_matching ... PENDING
225+
Running test_assert_status_code_fails ... PENDING
226+
Running test_assert_status_code_succeeds ... PENDING
227+
Running test_assert_equals_succeed_when_equal ... SUCCESS
228+
Running test_assert_fails ... SUCCESS
229+
Running test_assert_fails_fails ... SUCCESS
230+
Running test_assert_fails_succeeds ... SUCCESS
231+
Running test_assert_matches_succeed_when_matching ... SUCCESS
232+
Running test_assert_shows_stderr_on_failure ... SUCCESS
233+
Running test_assert_shows_stdout_on_failure ... SUCCESS
234+
Running test_assert_succeeds ... SUCCESS
235+
Running test_assert_within_delta_fails ... SUCCESS
236+
Running test_assert_within_delta_succeeds ... SUCCESS
237+
Running test_fail_fails ... SUCCESS
238+
Overall result: SUCCESS
239+
```
240+
202241
*bash_unit* supports the http://testanything.org/[Test Anything Protocol] so you can ask for a tap formatted
203242
output with the _-f_ option.
204243

bash_unit

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,19 @@ run_tests() {
240240
notify_test_pending "$pending_test"
241241
done
242242

243+
if [[ -n "$skip_pattern" ]]
244+
then
245+
for skipped_test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -E "$skip_pattern" | "$SED" -e 's: .*::')
246+
do
247+
notify_test_starting "$skipped_test"
248+
notify_test_pending "$skipped_test"
249+
done
250+
local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -v -E "$skip_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
251+
else
252+
local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
253+
fi
243254

244-
for test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)
255+
for test in $tests_to_run
245256
do
246257
(
247258
local status=0
@@ -270,9 +281,10 @@ run_teardown_suite() {
270281

271282
usage() {
272283
echo "$1" >&2
273-
echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] [-r] ... <test_file1> <test_file2> ..." >&2
284+
echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] [-s <skip_pattern1>] [-s <skip_pattern2>] [-r] ... <test_file1> <test_file2> ..." >&2
274285
echo >&2
275286
echo "Runs tests in test files that match <pattern>s" >&2
287+
echo "Test that match the skip patterns are skipped and displayed as PENDING" >&2
276288
echo "<output format> is optional only supported value is tap" >&2
277289
echo "-r to execute test cases in random order" >&2
278290
echo "-v to get current version information" >&2
@@ -427,14 +439,20 @@ tap_format() {
427439

428440
output_format=text
429441
test_pattern=""
430-
separator=""
431-
randomize=0
432-
while getopts "vp:f:r" option
442+
test_pattern_separator=""
443+
skip_pattern=""
444+
skip_pattern_separator=""
445+
randomise=0
446+
while getopts "vp:s:f:r" option
433447
do
434448
case "$option" in
435449
p)
436-
test_pattern="${test_pattern}${separator}${OPTARG}"
437-
separator="|"
450+
test_pattern="${test_pattern}${test_pattern_separator}${OPTARG}"
451+
test_pattern_separator="|"
452+
;;
453+
s)
454+
skip_pattern="${skip_pattern}${skip_pattern_separator}${OPTARG}"
455+
skip_pattern_separator="|"
438456
;;
439457
f)
440458
output_format="${OPTARG}"

tests/test_cli.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ Overall result: SUCCESS" \
8585
"$bash_unit_output"
8686
}
8787

88+
test_do_not_run_skipped_tests() {
89+
assert "$BASH_UNIT -s two \
90+
<(echo 'test_one() { echo -n ; }
91+
test_two() { fail ; }') \
92+
"
93+
}
94+
95+
test_skipped_tests_appear_in_output() {
96+
bash_unit_output=$($BASH_UNIT -s two \
97+
<(echo 'test_one() { echo -n ; }
98+
test_two() { fail ; }') \
99+
| "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \
100+
)
101+
102+
assert_equals "\
103+
Running tests in test_file
104+
Running test_two ... PENDING
105+
Running test_one ... SUCCESS
106+
Overall result: SUCCESS" \
107+
"$bash_unit_output"
108+
}
109+
88110
test_fails_when_test_file_does_not_exist() {
89111
assert_fails "$BASH_UNIT /not_exist/not_exist"
90112
}

0 commit comments

Comments
 (0)