Skip to content

Commit dd74582

Browse files
author
Wolfram Rösler
committed
Add option -r: Execute test cases in random order
Test files are always executed in the order in which they are specified on the command line. If -r is specified, the test cases within a file are executed in random order.
1 parent c962f40 commit dd74582

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.adoc

Lines changed: 7 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>] [test_file]
15+
*bash_unit* [-f tap] [-p <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+
*-r*::
40+
executes test cases in random order.
41+
Only affects the order within a test file (files are always
42+
executed in the order in which they are specified on the
43+
command line).
44+
3945
*-f* _output_format_::
4046
specify an alternative output format.
4147
The only supported value is *tap*.

bash_unit

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CAT="$(which cat)"
3030
SED="$(which sed)"
3131
GREP="$(which grep)"
3232
RM="$(which rm)"
33+
SHUF="$(which shuf)"
3334

3435
fail() {
3536
local message=${1:-}
@@ -162,6 +163,10 @@ run_setup_suite() {
162163
fi
163164
}
164165

166+
maybe_shuffle() {
167+
((randomise)) && $SHUF || $CAT
168+
}
169+
165170
run_tests() {
166171
local failure=0
167172

@@ -172,7 +177,7 @@ run_tests() {
172177
done
173178

174179

175-
for test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::')
180+
for test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)
176181
do
177182
(
178183
local status=0
@@ -201,10 +206,11 @@ run_teardown_suite() {
201206

202207
usage() {
203208
echo "$1" >&2
204-
echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] ... <test_file1> <test_file2> ..." >&2
209+
echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] [-r] ... <test_file1> <test_file2> ..." >&2
205210
echo >&2
206211
echo "Runs tests in test files that match <pattern>s" >&2
207212
echo "<output format> is optional only supported value is tap" >&2
213+
echo "-r to execute test cases in random order" >&2
208214
echo "-v to get current version information" >&2
209215
echo "See https://github.com/pgrange/bash_unit" >&2
210216
exit 1
@@ -342,7 +348,8 @@ tap_format() {
342348
output_format=text
343349
test_pattern=""
344350
separator=""
345-
while getopts "vp:f:" option
351+
randomise=0
352+
while getopts "vp:f:r" option
346353
do
347354
case "$option" in
348355
p)
@@ -352,6 +359,9 @@ do
352359
f)
353360
output_format="${OPTARG}"
354361
;;
362+
r)
363+
randomise=1
364+
;;
355365
v)
356366
echo "bash_unit $VERSION"
357367
exit

0 commit comments

Comments
 (0)