File: bash_unit.1

package info (click to toggle)
bash-unit 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,304 kB
  • sloc: sh: 1,134; makefile: 10
file content (1402 lines) | stat: -rw-r--r-- 33,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
'\" t
.\"     Title: bash_unit
.\"    Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 2.0.23
.\"      Date: 2025-01-08
.\"    Manual: \ \&
.\"    Source: \ \&
.\"  Language: English
.\"
.TH "BASH_UNIT" "1" "2025-01-08" "\ \&" "\ \&"
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.ss \n[.ss] 0
.nh
.ad l
.de URL
\fI\\$2\fP <\\$1>\\$3
..
.als MTO URL
.if \n[.g] \{\
.  mso www.tmac
.  am URL
.    ad l
.  .
.  am MTO
.    ad l
.  .
.  LINKSTYLE blue R < >
.\}
.SH "NAME"
bash_unit \- bash unit testing enterprise edition framework for professionals!
.SH "SYNOPSIS"
.sp
\fBbash_unit\fP [\-f tap] [\-p <pattern>] [\-s <pattern>] [\-r] [test_file]
.SH "DESCRIPTION"
.sp
\fBbash_unit\fP allows you to write unit tests (functions starting with \fBtest\fP),
run them and, in case of failure, displays the stack trace
with source file and line number indications to locate the problem.
.sp
You might want to take a look at \c
.URL "getting_started" "how to get started"
before continuing reading this documentation.
.sp
The following functions are available in your tests (see below for detailed documentation):
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRfail [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert <assertion> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_fail <assertion> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_status_code <expected_status_code> <assertion> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_equals <expected> <actual> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_not_equals <unexpected> <actual> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_matches <expected\-regex> <actual> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_not_matches <unexpected\-regex> <actual> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_within_delta <expected num> <actual num> <max delta> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRassert_no_diff <expected> <actual> [message]\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRskip_if <condition> <pattern>\fP
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.  sp -1
.  IP \(bu 2.3
.\}
\f(CRfake <command> [replacement code]\fP
.RE
.sp
\fI(by the way, the documentation you are reading is itself tested with bash\-unit)\fP
.sp
\fBbash_unit\fP is free software you may contribute to. See \c
.URL "CONTRIBUTING.md" "" "."
.SH "OPTIONS"
.sp
\fB\-p\fP \fIpattern\fP
.RS 4
filters tests to run based on the given pattern.
You can specify several patterns by repeating this option
for each pattern.
.RE
.sp
\fB\-s\fP \fIpattern\fP
.RS 4
skip tests which name matches the given pattern.
You can specify several patterns by repeating this option
for each pattern.
Tests will appear in \fBbash_unit\fP output as \fIskipped\fP.
(see also \fIskip_if\fP)
.RE
.sp
\fB\-r\fP
.RS 4
executes test cases in random order.
Only affects the order within a test file (files are always
executed in the order in which they are specified on the
command line).
.RE
.sp
\fB\-f\fP \fIoutput_format\fP
.RS 4
specify an alternative output format.
The only supported value is \fBtap\fP.
.RE
.sp
\fB\-q\fP
.RS 4
quiet mode.
Will only output the status of each test with no further
information even in case of failure.
.RE
.SS "GitHub Actions"
.sp
Here is an example of how you could integrate \fBbash_unit\fP with \c
.URL "https://docs.github.com/fr/actions" "GitHub Actions" ":"
.sp
.if n .RS 4
.nf
.fam C
name: bash_unit tests
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  ubuntu:
    runs\-on: ubuntu\-latest
    steps:
    \- uses: actions/checkout@v4
    \- name: Unit testing with bash_unit
      run: |
        curl \-s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash
        FORCE_COLOR=true ./bash_unit tests/test_*
.fam
.fi
.if n .RE
.sp
See this bash_unit \c
.URL "https://github.com/pgrange/bash_unit_getting_started" "getting started gitlab project" ""
for a working example.
.SS "GitLab CI"
.sp
Here is an example of how you could integrate \fBbash_unit\fP with \c
.URL "https://docs.gitlab.com/ee/ci/" "GitLab CI" ":"
.sp
.if n .RS 4
.nf
.fam C
test:
  image: debian
  script:
    \- apt\-get update
    \- apt\-get install \-\-no\-install\-recommends \-y curl ca\-certificates
    \- curl \-s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash
    \- FORCE_COLOR=true ./bash_unit tests/test_*
.fam
.fi
.if n .RE
.sp
See this bash_unit \c
.URL "https://gitlab.com/pgrange/bash_unit_getting_started" "getting started gitlab project" ""
for a working example.
.SS "pre\-commit hook"
.sp
You can run \f(CRbash_unit\fP as a \c
.URL "https://pre\-commit.com" "pre\-commit" ""
hook.
.sp
Add the following to your pre\-commit configuration. By default it will run scripts that are identified as shell scripts that match the path \f(CR^tests/(.*/)?test_.*\(rs.sh$\fP.
.sp
.if n .RS 4
.nf
.fam C
repos:
  \- repo: https://github.com/pgrange/bash_unit
    rev: v2.2.0
    hooks:
      \- id: bash\-unit
        always\-run: true
.fam
.fi
.if n .RE
.SH "HOW TO RUN TESTS"
.sp
To run tests, simply call \fBbash_unit\fP with all your tests files as parameter. For instance to run some \fBbash_unit\fP tests, from \fBbash_unit\fP directory:
.sp
.if n .RS 4
.nf
.fam C
\&./bash_unit tests/test_core.sh
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
Running tests in tests/test_core.sh
        Running test_assert_equals_fails_when_not_equal ... SUCCESS
        Running test_assert_equals_succeed_when_equal ... SUCCESS
        Running test_assert_fails ... SUCCESS
        Running test_assert_fails_fails ... SUCCESS
        Running test_assert_fails_succeeds ... SUCCESS
        Running test_assert_matches_fails_when_not_matching ... SUCCESS
        Running test_assert_matches_succeed_when_matching ... SUCCESS
        Running test_assert_no_diff_fails_when_diff ... SUCCESS
        Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS
        Running test_assert_not_equals_fails_when_equal ... SUCCESS
        Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS
        Running test_assert_not_matches_fails_when_matching ... SUCCESS
        Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS
        Running test_assert_shows_stderr_on_failure ... SUCCESS
        Running test_assert_shows_stdout_on_failure ... SUCCESS
        Running test_assert_status_code_fails ... SUCCESS
        Running test_assert_status_code_succeeds ... SUCCESS
        Running test_assert_succeeds ... SUCCESS
        Running test_assert_within_delta_fails ... SUCCESS
        Running test_assert_within_delta_succeeds ... SUCCESS
        Running test_fail_fails ... SUCCESS
        Running test_fail_prints_failure_message ... SUCCESS
        Running test_fail_prints_where_is_error ... SUCCESS
        Running test_fake_actually_fakes_the_command ... SUCCESS
        Running test_fake_can_fake_inline ... SUCCESS
        Running test_fake_echo_stdin_when_no_params ... SUCCESS
        Running test_fake_exports_faked_in_subshells ... SUCCESS
        Running test_fake_transmits_params_to_fake_code ... SUCCESS
        Running test_fake_transmits_params_to_fake_code_as_array ... SUCCESS
        Running test_should_pretty_format_even_when_LANG_is_unset ... SUCCESS
Overall result: SUCCESS
.fam
.fi
.if n .RE
.sp
You might also want to run only specific tests, you may do so with the
\fI\-p\fP option. This option accepts a pattern as parameter and filters test
functions against this pattern.
.sp
.if n .RS 4
.nf
.fam C
\&./bash_unit \-p fail_fails \-p assert tests/test_core.sh
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
Running tests in tests/test_core.sh
        Running test_assert_equals_fails_when_not_equal ... SUCCESS
        Running test_assert_equals_succeed_when_equal ... SUCCESS
        Running test_assert_fails ... SUCCESS
        Running test_assert_fails_fails ... SUCCESS
        Running test_assert_fails_succeeds ... SUCCESS
        Running test_assert_matches_fails_when_not_matching ... SUCCESS
        Running test_assert_matches_succeed_when_matching ... SUCCESS
        Running test_assert_no_diff_fails_when_diff ... SUCCESS
        Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS
        Running test_assert_not_equals_fails_when_equal ... SUCCESS
        Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS
        Running test_assert_not_matches_fails_when_matching ... SUCCESS
        Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS
        Running test_assert_shows_stderr_on_failure ... SUCCESS
        Running test_assert_shows_stdout_on_failure ... SUCCESS
        Running test_assert_status_code_fails ... SUCCESS
        Running test_assert_status_code_succeeds ... SUCCESS
        Running test_assert_succeeds ... SUCCESS
        Running test_assert_within_delta_fails ... SUCCESS
        Running test_assert_within_delta_succeeds ... SUCCESS
        Running test_fail_fails ... SUCCESS
Overall result: SUCCESS
.fam
.fi
.if n .RE
.sp
You can combine the \fI\-p\fP option with \fI\-s\fP to skip some of the tests. This option accepts a pattern
as parameter and mark as skipped any test function which matches this pattern.
.sp
.if n .RS 4
.nf
.fam C
\&./bash_unit \-p fail_fails \-p assert \-s no \-s status tests/test_core.sh
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
Running tests in tests/test_core.sh
        Running test_assert_equals_fails_when_not_equal ... SKIPPED
        Running test_assert_matches_fails_when_not_matching ... SKIPPED
        Running test_assert_no_diff_fails_when_diff ... SKIPPED
        Running test_assert_no_diff_succeeds_when_no_diff ... SKIPPED
        Running test_assert_not_equals_fails_when_equal ... SKIPPED
        Running test_assert_not_equals_succeeds_when_not_equal ... SKIPPED
        Running test_assert_not_matches_fails_when_matching ... SKIPPED
        Running test_assert_not_matches_succeed_when_not_matching ... SKIPPED
        Running test_assert_status_code_fails ... SKIPPED
        Running test_assert_status_code_succeeds ... SKIPPED
        Running test_assert_equals_succeed_when_equal ... SUCCESS
        Running test_assert_fails ... SUCCESS
        Running test_assert_fails_fails ... SUCCESS
        Running test_assert_fails_succeeds ... SUCCESS
        Running test_assert_matches_succeed_when_matching ... SUCCESS
        Running test_assert_shows_stderr_on_failure ... SUCCESS
        Running test_assert_shows_stdout_on_failure ... SUCCESS
        Running test_assert_succeeds ... SUCCESS
        Running test_assert_within_delta_fails ... SUCCESS
        Running test_assert_within_delta_succeeds ... SUCCESS
        Running test_fail_fails ... SUCCESS
Overall result: SUCCESS
.fam
.fi
.if n .RE
.sp
\fBbash_unit\fP supports the \c
.URL "http://testanything.org/" "Test Anything Protocol" ""
so you can ask for a tap formatted
output with the \fI\-f\fP option.
.sp
.if n .RS 4
.nf
.fam C
\&./bash_unit \-f tap tests/test_core.sh
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
# Running tests in tests/test_core.sh
ok \- test_assert_equals_fails_when_not_equal
ok \- test_assert_equals_succeed_when_equal
ok \- test_assert_fails
ok \- test_assert_fails_fails
ok \- test_assert_fails_succeeds
ok \- test_assert_matches_fails_when_not_matching
ok \- test_assert_matches_succeed_when_matching
ok \- test_assert_no_diff_fails_when_diff
ok \- test_assert_no_diff_succeeds_when_no_diff
ok \- test_assert_not_equals_fails_when_equal
ok \- test_assert_not_equals_succeeds_when_not_equal
ok \- test_assert_not_matches_fails_when_matching
ok \- test_assert_not_matches_succeed_when_not_matching
ok \- test_assert_shows_stderr_on_failure
ok \- test_assert_shows_stdout_on_failure
ok \- test_assert_status_code_fails
ok \- test_assert_status_code_succeeds
ok \- test_assert_succeeds
ok \- test_assert_within_delta_fails
ok \- test_assert_within_delta_succeeds
ok \- test_fail_fails
ok \- test_fail_prints_failure_message
ok \- test_fail_prints_where_is_error
ok \- test_fake_actually_fakes_the_command
ok \- test_fake_can_fake_inline
ok \- test_fake_echo_stdin_when_no_params
ok \- test_fake_exports_faked_in_subshells
ok \- test_fake_transmits_params_to_fake_code
ok \- test_fake_transmits_params_to_fake_code_as_array
ok \- test_should_pretty_format_even_when_LANG_is_unset
1..30
.fam
.fi
.if n .RE
.SH "HOW TO WRITE TESTS"
.sp
Write your test functions in a file. The name of a test function has to start with \fBtest\fP. Only functions starting with \fBtest\fP will be tested.
.sp
Use the \fBbash_unit\fP assertion functions in your test functions, see below.
.sp
You may write a \fBsetup\fP function that will be executed before each test is run.
.sp
You may write a \fBteardown\fP function that will be executed after each test is run.
.sp
You may write a \fBsetup_suite\fP function that will be executed only once before all the tests of your test file.
.sp
You may write a \fBteardown_suite\fP function that will be executed only once after all the tests of your test file.
.sp
If you write code outside of any bash function, this code will be executed once at test file loading time since
your file is a bash script and \fBbash_unit\fP sources it before running your tests. It is suggested to write a
\fBsetup_suite\fP function and avoid any code outside a bash function. you must not use any bash_unit assertion
in setup_suite or use exit in setup_suite for teardown_suite to be run.
See \c
.URL "https://github.com/pgrange/bash_unit/issues/43" "issue 43" ""
for more details.
.sp
If you want to keep an eye on a test not yet implemented, prefix the name of the function by \fBtodo\fP instead of test.
Test to do are not executed and do not impact the global status of your test suite but are displayed in \fBbash_unit\fP output.
.sp
\fBbash_unit\fP changes the current working directory to the one of the running test file. If you need to access files from your test code, for instance the script under test, use path relative to the test file.
.sp
You may need to change the behavior of some commands to create conditions for your code under test to behave as expected. The \fBfake\fP function may help you to do that, see below.
.SH "TEST FUNCTIONS"
.sp
\fBbash_unit\fP supports several shell oriented assertion functions.
.SS "\fBfail\fP"
.sp
.if n .RS 4
.nf
.fam C
fail [message]
.fam
.fi
.if n .RE
.sp
Fails the test and displays an optional message.
.sp
.if n .RS 4
.nf
.fam C
test_can_fail() {
  fail "this test failed on purpose"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_can_fail ... FAILURE
this test failed on purpose
doc:2:test_can_fail()
.fam
.fi
.if n .RE
.SS "\fBassert\fP"
.sp
.if n .RS 4
.nf
.fam C
assert <assertion> [message]
.fam
.fi
.if n .RE
.sp
Evaluates \fIassertion\fP and fails if \fIassertion\fP fails.
.sp
\fIassertion\fP fails if its evaluation returns a status code different from 0.
.sp
In case of failure, the standard output and error of the evaluated \fIassertion\fP is displayed. The optional message is also displayed.
.sp
.if n .RS 4
.nf
.fam C
test_assert_fails() {
  assert false "this test failed, obviously"
}
test_assert_succeed() {
  assert true
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_assert_fails ... FAILURE
this test failed, obviously
doc:2:test_assert_fails()
        Running test_assert_succeed ... SUCCESS
.fam
.fi
.if n .RE
.sp
But you probably want to assert less obvious facts.
.sp
.if n .RS 4
.nf
.fam C
code() {
  touch /tmp/the_file
}

test_code_creates_the_file() {
  code

  assert "test \-e /tmp/the_file"
}

test_code_makes_the_file_executable() {
  code

  assert "test \-x /tmp/the_file" "/tmp/the_file should be executable"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_creates_the_file ... SUCCESS
        Running test_code_makes_the_file_executable ... FAILURE
/tmp/the_file should be executable
doc:14:test_code_makes_the_file_executable()
.fam
.fi
.if n .RE
.sp
It may also be fun to use assert to check for the expected content of a file.
.sp
.if n .RS 4
.nf
.fam C
code() {
  echo \*(Aqnot so cool\*(Aq > /tmp/the_file
}

test_code_write_appropriate_content_in_the_file() {
  code

  assert "diff <(echo \*(Aqthis is cool\*(Aq) /tmp/the_file"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_write_appropriate_content_in_the_file ... FAILURE
out> 1c1
out> < this is cool
out> \-\-\-
out> > not so cool
doc:8:test_code_write_appropriate_content_in_the_file()
.fam
.fi
.if n .RE
.SS "\fBassert_fail\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_fail <assertion> [message]
.fam
.fi
.if n .RE
.sp
Asserts that \fIassertion\fP fails. This is the opposite of \fBassert\fP.
.sp
\fIassertion\fP fails if its evaluation returns a status code different from 0.
.sp
If the evaluated expression does not fail, then \fBassert_fail\fP will fail and display the standard output and error of the evaluated \fIassertion\fP. The optional message is also displayed.
.sp
.if n .RS 4
.nf
.fam C
code() {
  echo \*(Aqnot so cool\*(Aq > /tmp/the_file
}

test_code_does_not_write_cool_in_the_file() {
  code

  assert_fails "grep cool /tmp/the_file" "should not write \*(Aqcool\*(Aq in /tmp/the_file"
}

test_code_does_not_write_this_in_the_file() {
  code

  assert_fails "grep this /tmp/the_file" "should not write \*(Aqthis\*(Aq in /tmp/the_file"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_does_not_write_cool_in_the_file ... FAILURE
should not write \*(Aqcool\*(Aq in /tmp/the_file
out> not so cool
doc:8:test_code_does_not_write_cool_in_the_file()
        Running test_code_does_not_write_this_in_the_file ... SUCCESS
.fam
.fi
.if n .RE
.SS "\fBassert_status_code\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_status_code <expected_status_code> <assertion> [message]
.fam
.fi
.if n .RE
.sp
Checks for a precise status code of the evaluation of \fIassertion\fP.
.sp
It may be useful if you want to distinguish between several error conditions in your code.
.sp
In case of failure, the standard output and error of the evaluated \fIassertion\fP is displayed. The optional message is also displayed.
.sp
.if n .RS 4
.nf
.fam C
code() {
  exit 23
}

test_code_should_fail_with_code_25() {
  assert_status_code 25 code
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_should_fail_with_code_25 ... FAILURE
 expected status code 25 but was 23
doc:6:test_code_should_fail_with_code_25()
.fam
.fi
.if n .RE
.SS "\fBassert_equals\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_equals <expected> <actual> [message]
.fam
.fi
.if n .RE
.sp
Asserts for equality of the two strings \fIexpected\fP and \fIactual\fP.
.sp
.if n .RS 4
.nf
.fam C
test_obvious_inequality_with_assert_equals(){
  assert_equals "a string" "another string" "a string should be another string"
}
test_obvious_equality_with_assert_equals(){
  assert_equals a a
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_obvious_equality_with_assert_equals ... SUCCESS
        Running test_obvious_inequality_with_assert_equals ... FAILURE
a string should be another string
 expected [a string] but was [another string]
doc:2:test_obvious_inequality_with_assert_equals()
.fam
.fi
.if n .RE
.SS "\fBassert_not_equals\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_not_equals <unexpected> <actual> [message]
.fam
.fi
.if n .RE
.sp
Asserts for inequality of the two strings \fIunexpected\fP and \fIactual\fP.
.sp
.if n .RS 4
.nf
.fam C
test_obvious_equality_with_assert_not_equals(){
  assert_not_equals "a string" "a string" "a string should be different from another string"
}
test_obvious_inequality_with_assert_not_equals(){
  assert_not_equals a b
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_obvious_equality_with_assert_not_equals ... FAILURE
a string should be different from another string
 expected different value than [a string] but was the same
doc:2:test_obvious_equality_with_assert_not_equals()
        Running test_obvious_inequality_with_assert_not_equals ... SUCCESS
.fam
.fi
.if n .RE
.SS "\fBassert_matches\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_matches <expected\-regex> <actual> [message]
.fam
.fi
.if n .RE
.sp
Asserts that the string \fIactual\fP matches the regex pattern \fIexpected\-regex\fP.
.sp
.if n .RS 4
.nf
.fam C
test_obvious_notmatching_with_assert_matches(){
  assert_matches "a str.*" "another string" "\*(Aqanother string\*(Aq should not match \*(Aqa str.*\*(Aq"
}
test_obvious_matching_with_assert_matches(){
  assert_matches "a[nN].t{0,1}.*r str.*" "another string"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_obvious_matching_with_assert_matches ... SUCCESS
        Running test_obvious_notmatching_with_assert_matches ... FAILURE
\*(Aqanother string\*(Aq should not match \*(Aqa str.*\*(Aq
 expected regex [a str.*] to match [another string]
doc:2:test_obvious_notmatching_with_assert_matches()
.fam
.fi
.if n .RE
.SS "\fBassert_not_matches\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_not_matches <unexpected\-regex> <actual> [message]
.fam
.fi
.if n .RE
.sp
Asserts that the string \fIactual\fP does not match the regex pattern \fIunexpected\-regex\fP.
.sp
.if n .RS 4
.nf
.fam C
test_obvious_matching_with_assert_not_matches(){
  assert_not_matches "a str.*" "a string" "\*(Aqa string\*(Aq should not match \*(Aqa str.*\*(Aq"
}
test_obvious_notmatching_with_assert_not_matches(){
  assert_not_matches "a str.*" "another string"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_obvious_matching_with_assert_not_matches ... FAILURE
\*(Aqa string\*(Aq should not match \*(Aqa str.*\*(Aq
 expected regex [a str.*] should not match but matched [a string]
doc:2:test_obvious_matching_with_assert_not_matches()
        Running test_obvious_notmatching_with_assert_not_matches ... SUCCESS
.fam
.fi
.if n .RE
.SS "\fBassert_within_delta\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_within_delta <expected num> <actual num> <max delta> [message]
.fam
.fi
.if n .RE
.sp
Asserts that the expected num matches the actual num up to a given max delta.
This function only support integers.
Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7:
.sp
.if n .RS 4
.nf
.fam C
test_matches_within_delta(){
  assert_within_delta 5 3 2
  assert_within_delta 5 4 2
  assert_within_delta 5 5 2
  assert_within_delta 5 6 2
  assert_within_delta 5 7 2
}
test_does_not_match_within_delta(){
  assert_within_delta 5 2 2
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_does_not_match_within_delta ... FAILURE
 expected value [5] to match [2] with a maximum delta of [2]
doc:9:test_does_not_match_within_delta()
        Running test_matches_within_delta ... SUCCESS
.fam
.fi
.if n .RE
.SS "\fBassert_no_diff\fP"
.sp
.if n .RS 4
.nf
.fam C
assert_no_diff <expected> <actual> [message]
.fam
.fi
.if n .RE
.sp
Asserts that the content of the file \fIactual\fP does not have any differences to the one \fIexpected\fP.
.sp
.if n .RS 4
.nf
.fam C
test_obvious_notmatching_with_assert_no_diff(){
  assert_no_diff <(echo foo) <(echo bar)
}
test_obvious_matching_with_assert_assert_no_diff(){
  assert_no_diff bash_unit bash_unit
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_obvious_matching_with_assert_assert_no_diff ... SUCCESS
        Running test_obvious_notmatching_with_assert_no_diff ... FAILURE
 expected \*(Aqdoc\*(Aq to be identical to \*(Aqdoc\*(Aq but was different
out> 1c1
out> < foo
out> \-\-\-
out> > bar
doc:2:test_obvious_notmatching_with_assert_no_diff()
.fam
.fi
.if n .RE
.SH "\fBSKIP_IF\fP FUNCTION"
.sp
.if n .RS 4
.nf
.fam C
skip_if <condition> <pattern>
.fam
.fi
.if n .RE
.sp
If \fIcondition\fP is true, will skip all the tests in the current file which match the given \fIpattern\fP.
.sp
This can be useful when one has tests that are dependent on system environment, for instance:
.sp
.if n .RS 4
.nf
.fam C
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"
}
.fam
.fi
.if n .RE
.sp
will output, on a Linux system:
.sp
.if n .RS 4
.nf
.fam C
        Running test_darwin_proc_does_not_exist ... SKIPPED
        Running test_linux_proc_exists ... SUCCESS
.fam
.fi
.if n .RE
.SH "\fBFAKE\fP FUNCTION"
.sp
.if n .RS 4
.nf
.fam C
fake <command> [replacement code]
.fam
.fi
.if n .RE
.sp
Fakes \fIcommand\fP and replaces it with \fIreplacement code\fP (if code is specified) for the rest of the execution of your test. If no replacement code is specified, then it replaces command by one that echoes stdin of fake. This may be useful if you need to simulate an environment for you code under test.
.sp
For instance:
.sp
.if n .RS 4
.nf
.fam C
fake ps echo hello world
ps
.fam
.fi
.if n .RE
.sp
will output:
.sp
.if n .RS 4
.nf
.fam C
hello world
.fam
.fi
.if n .RE
.sp
We can do the same using \fIstdin\fP of fake:
.sp
.if n .RS 4
.nf
.fam C
fake ps << EOF
hello world
EOF
ps
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
hello world
.fam
.fi
.if n .RE
.SS "Using stdin"
.sp
Here is an example, parameterizing fake with its \fIstdin\fP to test that code fails when some process does not run and succeeds otherwise:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_code_succeeds_if_apache_runs() {
  fake ps <<EOF
  PID TTY\&          TIME CMD
13525 pts/7\&    00:00:01 bash
24162 pts/7\&    00:00:00 ps
 8387 ?\&            0:00 /usr/sbin/apache2 \-k start
EOF

  assert code "code should succeed when apache is running"
}

test_code_fails_if_apache_does_not_run() {
  fake ps <<EOF
  PID TTY\&          TIME CMD
13525 pts/7\&    00:00:01 bash
24162 pts/7\&    00:00:00 ps
EOF

  assert_fails code "code should fail when apache is not running"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_fails_if_apache_does_not_run ... SUCCESS
        Running test_code_succeeds_if_apache_runs ... SUCCESS
.fam
.fi
.if n .RE
.SS "Using a function"
.sp
In a previous example, we faked \fIps\fP by specifying code inline:
.sp
.if n .RS 4
.nf
.fam C
fake ps echo hello world
ps
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
hello world
.fam
.fi
.if n .RE
.sp
If you need to write more complex code to fake your command, you may abstract this code in a function:
.sp
.if n .RS 4
.nf
.fam C
_ps() {
  echo hello world
}
fake ps _ps
ps
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
hello world
.fam
.fi
.if n .RE
.sp
Be careful however that your _ps function is not exported to sub\-processes. It means that, depending on how your code under test works, _ps may not be defined in the context where ps will be called. For instance:
.sp
.if n .RS 4
.nf
.fam C
_ps() {
  echo hello world
}
fake ps _ps

bash \-c ps
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
environment: line 1: _ps: command not found
.fam
.fi
.if n .RE
.sp
It depends on your code under test but it is safer to just export functions needed by your fake so that they are available in sub\-processes:
.sp
.if n .RS 4
.nf
.fam C
_ps() {
  echo hello world
}
export \-f _ps
fake ps _ps

bash \-c ps
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
hello world
.fam
.fi
.if n .RE
.sp
\fBfake\fP is also limited by the fact that it defines a \fIbash\fP function to
override the actual command. In some context the command can not be
overridden by a function. For instance if your code under test relies on \fIexec\fP to launch \fIps\fP, \fBfake\fP will have no effect.
.sp
\fBfake\fP may also imply strange behaviors from bash_unit when you try to
fake really basic stuff. bash_unit tries to be as much immune to this as
possible but there are some limits. Especially and as surprising as it
might seem, bash allows creating functions named after builtin commands
and bash_unit won\(cqt resist that kind of situation. So, for instance, do
not try to fake: \f(CRexit\fP; \f(CRlocal\fP; \f(CRtrap\fP; \f(CReval\fP; \f(CRexport\fP; \f(CRif\fP; \f(CRthen\fP; \f(CRelse\fP; \f(CRfi\fP; \f(CRwhile\fP; \f(CRdo\fP; \f(CRdone\fP; \f(CR$\fP; \f(CRecho\fP; \f(CR[\fP (I know, this is not a builtin but don\(cqt).
.SS "\fBfake\fP parameters"
.sp
\fBfake\fP stores parameters given to the fake in the global variable \fIFAKE_PARAMS\fP so that you can use them inside your fake.
.sp
It may be useful if you need to adapt the behavior on the given parameters.
.sp
It can also help in asserting the values of these parameters ... but this may be quite tricky.
.sp
For instance, in our previous code that checks apache is running, we have an issue since our code does not use \fIps\fP with the appropriate parameters. So we will try to check that parameters given to ps are \fIax\fP.
.sp
To do that, the first naive approach would be:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_code_gives_ps_appropriate_parameters() {
  _ps() {
    cat <<EOF
  PID TTY\&          TIME CMD
13525 pts/7\&    00:00:01 bash
24162 pts/7\&    00:00:00 ps
 8387 ?\&            0:00 /usr/sbin/apache2 \-k start
EOF
    assert_equals ax "${FAKE_PARAMS[@]}"
  }
  export \-f _ps
  fake ps _ps

  code >/dev/null
}
.fam
.fi
.if n .RE
.sp
This test calls \fIcode\fP, which calls \fIps\fP, which is actually implemented by \fI_ps\fP. Since \fIcode\fP does not use \fIax\fP but only \fIa\fP as parameters, this test should fail. But ...
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_gives_ps_appropriate_parameters ... SUCCESS
.fam
.fi
.if n .RE
.sp
The problem here is that \fIps\fP fail (because of the failed \fBassert_equals\fP assertion). But \fIps\fP is piped with \fIgrep\fP:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}
.fam
.fi
.if n .RE
.sp
With bash, the result code of a pipeline equals the result code of the last command of the pipeline. The last command is \fIgrep\fP and since grep succeeds, the failure of \fI_ps\fP is lost and our test succeeds. We have only succeeded in messing with the test output, nothing more.
.sp
An alternative may be to activate bash \fIpipefail\fP option but this may introduce unwanted side effects. We can also simply not output anything in \fI_ps\fP so that \fIgrep\fP fails:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_code_gives_ps_appropriate_parameters() {
  _ps() {
    assert_equals ax "${FAKE_PARAMS[@]}"
  }
  export \-f _ps
  fake ps _ps

  code >/dev/null
}
.fam
.fi
.if n .RE
.sp
The problem here is that we use a trick to make the code under test fail but the
failure has nothing to do with the actual \fBassert_equals\fP failure. This is really
bad, don\(cqt do that.
.sp
Moreover, \fBassert_equals\fP output is captured by \fIps\fP and this just messes with the display of our test results:
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_gives_ps_appropriate_parameters ...
.fam
.fi
.if n .RE
.sp
The only correct alternative is for the fake \fIps\fP to write \fIFAKE_PARAMS\fP in a file descriptor
so that your test can grab them after code execution and assert their value. For instance
by writing to a file:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_code_gives_ps_appropriate_parameters() {
  _ps() {
    echo ${FAKE_PARAMS[@]} > /tmp/fake_params
  }
  export \-f _ps
  fake ps _ps

  code || true

  assert_equals ax "$(head \-n1 /tmp/fake_params)"
}

setup() {
  rm \-f /tmp/fake_params
}
.fam
.fi
.if n .RE
.sp
Here our fake writes to \fI/tmp/fake\fP. We delete this file in \fBsetup\fP to be
sure that we do not get inappropriate data from a previous test. We assert
that the first line of \fI/tmp/fake\fP equals \fIax\fP. Also, note that we know
that \fIcode\fP will fail and write this to ignore the error: \f(CRcode || true\fP.
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_gives_ps_appropriate_parameters ... FAILURE
 expected [ax] but was [a]
doc:14:test_code_gives_ps_appropriate_parameters()
.fam
.fi
.if n .RE
.sp
We can also compact the fake definition:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_code_gives_ps_appropriate_parameters() {
  fake ps \*(Aqecho ${FAKE_PARAMS[@]} >/tmp/fake_params\*(Aq

  code || true

  assert_equals ax "$(head \-n1 /tmp/fake_params)"
}

setup() {
  rm \-f /tmp/fake_params
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_code_gives_ps_appropriate_parameters ... FAILURE
 expected [ax] but was [a]
doc:10:test_code_gives_ps_appropriate_parameters()
.fam
.fi
.if n .RE
.sp
Finally, we can avoid the \fI/tmp/fake_params\fP temporary file by using \fIcoproc\fP:
.sp
.if n .RS 4
.nf
.fam C
code() {
  ps a | grep apache
}

test_get_data_from_fake() {
  #Fasten you seat belt ...
  coproc cat
  exec {test_channel}>&${COPROC[1]}
  fake ps \*(Aqecho ${FAKE_PARAMS[@]} >&$test_channel\*(Aq

  code || true

  assert_equals ax "$(head \-n1 <&${COPROC[0]})"
}
.fam
.fi
.if n .RE
.sp
.if n .RS 4
.nf
.fam C
        Running test_get_data_from_fake ... FAILURE
 expected [ax] but was [a]
doc:13:test_get_data_from_fake()
.fam
.fi
.if n .RE