-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsrfi-64.html
862 lines (821 loc) · 37.6 KB
/
srfi-64.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>SRFI 64: A Scheme API for test suites</title>
<meta charset="utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<link href="https://srfi.schemers.org/srfi.css" rel="stylesheet" type="text/css" />
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png" />
<style type="text/css">
/* So var inside pre gets same font as var in paragraphs. */
var { font-family: monospace; }
</style>
</head>
<body>
<h1><a href="https://srfi.schemers.org/"><img alt="SRFI surfboard logo" class="srfi-logo" src="https://srfi.schemers.org/srfi-logo.svg" /></a>64: A Scheme API for test suites</h1>
<p>by Per Bothner<br />
<code><a href="mailto:per@bothner.com"><per@bothner.com></a></code></p>
<h2>Status</h2>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+64%20%20+at+srfi+dotschemers+dot+org">srfi-64 @<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-64">archive</a>.</p>
<ul>
<li>Received: 2005-01-07</li>
<li>Draft: 2005-01-28--2005-03-28</li>
<li>Revised: <a href="https://srfi.schemers.org/srfi-64/srfi-64-1.3.html">2005-10-18</a>
</li><li>Revised: <a href="https://srfi.schemers.org/srfi-64/srfi-64-1.5.html">2006-02-24</a>
</li><li>Final: <a href="https://srfi.schemers.org/srfi-64/srfi-64-1.6.html">2006-06-18</a></li>
<li>Revised to fix errata: 2016-08-11</li>
<li>Revised to fix links and to refer to RackUnit, the new name of SchemeUnit: 2023-11-29</li>
</ul>
<h2>Abstract</h2>
<p>
This defines an API for writing <dfn>test suites</dfn>, to make it easy
to portably test Scheme APIs, libraries, applications, and implementations.
A test suite is a collection of <dfn>test cases</dfn> that execute
in the context of a <dfn>test-runner</dfn>. This specification
also supports writing new test-runners, to allow customization
of reporting and processing the result of running test suites.</p>
<h2>Rationale</h2>
<p>The Scheme community needs a standard for writing test suites.
Every SRFI or other library should come with a test suite.
Such a test suite must be portable, without requiring any
non-standard features, such as modules. The test suite implementation
or "runner" need not be portable, but it is desirable that it be
possible to write a portable basic implementation.</p>
<p>
There are other testing frameworks written in Scheme, including
<a href="https://docs.racket-lang.org/rackunit/">RackUnit</a>.
However RackUnit is not portable.
It is also a bit on the verbose side.
It would be useful to have a bridge between this framework and RackUnit
so RackUnit tests could run under this framework and vice versa.
There exists also at least one Scheme wrapper providing a Scheme interface
to the <q>standard</q> <a href="https://www.junit.org/">JUnit</a> API for Java.
It would be useful to have a bridge so that tests written using this
framework can run under a JUnit runner.
Neither of these features are part of this specification.</p>
<p>
This API makes use of implicit dynamic state, including an
implicit <q>test runner</q>. This makes the API convenient
and terse to use, but it may be a little less elegant and <q>compositional</q>
than using explicit test objects, such as JUnit-style frameworks.
It is not claimed to follow either object-oriented or functional design
principles, but I hope it is useful and convenient to use and extend.</p>
<p>
This proposal allows converting a Scheme source file to a
test suite by just adding a few macros. You don't have to
write the entire file in a new form, thus you don't have to
re-indent it.</p>
<p>
All names defined by the API start with the prefix <code>test-</code>.
All function-like forms are defined as syntax. They may be implemented
as functions or macros or built-ins. The reason for specifying them as
syntax is to allow specific tests to be skipped without evaluating sub-expressions, or for implementations
to add features such as printing line numbers or catching exceptions.</p>
<h2>Specification</h2>
<p>While this is a moderately complex specification,
you should be able to write simple test suites after just reading the
first few sections below. More advanced functionality, such
as writing a custom test-runner, is at the end of the specification.</p>
<h3>Writing basic test suites</h3>
<p>Let's start with a simple example.
This is a complete self-contained test-suite.</p>
<pre>
;; Initialize and give a name to a simple testsuite.
(test-begin "vec-test")
(define v (make-vector 5 99))
;; Require that an expression evaluate to true.
(test-assert (vector? v))
;; Test that an expression is eqv? to some other expression.
(test-eqv 99 (vector-ref v 2))
(vector-set! v 2 7)
(test-eqv 7 (vector-ref v 2))
;; Finish the testsuite, and report results.
(test-end "vec-test")
</pre>
<p>
This testsuite could be saved in its own source file.
Nothing else is needed:
We do not require any top-level forms, so it is easy
to wrap an existing program or test to this form, without adding indentation.
It is also easy to add new tests, without having to name individual
tests (though that is optional).</p>
<p>
Test cases are executed in the context of a <dfn>test runner</dfn>,
which is an object that accumulates and reports test results.
This specification defines how to create and use custom test runners,
but implementations should also provide a default test runner.
It is suggested (but not required) that loading the above
file in a top-level environment will cause the
tests to be executed using an implementation-specified default test runner,
and <code>test-end</code> will cause a summary to be displayed
in an implementation-specified manner.</p>
<h4>Simple test-cases</h4>
<p>
Primitive test cases test that a given condition is true.
They may have a name.
The core test case form is <code>test-assert</code>:</p>
<pre>
(<b>test-assert</b> [<var>test-name</var>] <var>expression</var>)
</pre>
<p>
This evaluates the <var>expression</var>.
The test passes if the result
is true; if the result is false, a test failure is reported.
The test also fails if an exception is raised, assuming the implementation
has a way to catch exceptions.
How the failure is reported depends on the test runner environment.
The <var>test-name</var> is a string that names the test case.
(Though the <var>test-name</var> is a string literal in the examples,
it is an expression. It is evaluated only once.)
It is used when reporting errors, and also when skipping tests,
as described below.
It is an error to invoke <code>test-assert</code>
if there is no current test runner.</p>
<p>
The following forms may be more convenient than
using <code>test-assert</code> directly:</p>
<pre>
(<b>test-eqv</b> [<var>test-name</var>] <var>expected</var> <var>test-expr</var>)
</pre>
<p>
This is equivalent to:</p>
<pre>
(test-assert [<var>test-name</var>] (eqv? <var>expected</var> <var>test-expr</var>))
</pre>
<p>
Similarly <code>test-equal</code> and <code>test-eq</code>
are shorthand for <code>test-assert</code> combined with
<code>equal?</code> or <code>eq?</code>, respectively:</p>
<pre>
(<b>test-equal</b> [<var>test-name</var>] <var>expected</var> <var>test-expr</var>)
(<b>test-eq</b> [<var>test-name</var>] <var>expected</var> <var>test-expr</var>)</pre>
<p>
Here is a simple example:</p>
<pre>
(define (mean x y) (/ (+ x y) 2.0))
(test-eqv 4 (mean 3 5))
</pre>
<p>For testing approximate equality of inexact reals
we can use <code>test-approximate</code>:</p>
<pre>
(<b>test-approximate</b> [<var>test-name</var>] <var>expected</var> <var>test-expr</var> <var>error</var>)
</pre>
<p>
This is equivalent to (except that each argument is only evaluated once):</p>
<pre>
(test-assert [test-name]
(and (>= test-expr (- expected error))
(<= test-expr (+ expected error))))
</pre>
<h4>Tests for catching errors</h4>
<p>
We need a way to specify that evaluation <em>should</em> fail.
This verifies that errors are detected when required.</p>
<pre>
(<b>test-error</b> [[<var>test-name</var>] <var>error-type</var>] <var>test-expr</var>)
</pre>
<p>
Evaluating <var>test-expr</var> is expected to signal an error.
The kind of error is indicated by <var>error-type</var>.</p>
<p>
If the <var>error-type</var> is left out, or it is
<code>#t</code>, it means "some kind of unspecified error should be signaled".
For example:</p>
<pre>
(test-error #t (vector-ref '#(1 2) 9))
</pre>
<p>
This specification leaves it implementation-defined (or for a future
specification) what form <code><var>test-error</var></code> may take,
though all implementations must allow <code>#t</code>.
Some implementations may support
<a href="https://srfi.schemers.org/srfi-35/srfi-35.html">SRFI-35's conditions</a>,
but these are only standardized for
<a href="https://srfi.schemers.org/srfi-36/srfi-36.html">SRFI-36's I/O conditions</a>, which are seldom useful in test suites.
An implementation may also allow implementation-specific
<q>exception types</q>.
For example Java-based implementations may allow
the names of Java exception classes:</p>
<pre>
;; Kawa-specific example
(test-error <java.lang.IndexOutOfBoundsException> (vector-ref '#(1 2) 9))
</pre>
<p>
An implementation that cannot catch exceptions should skip
<code>test-error</code> forms.</p>
<h4>Testing syntax</h4>
<p>
Testing syntax is tricky, especially if we want to
check that invalid syntax is causing an error.
The following utility function can help:</p>
<pre>
(<b>test-read-eval-string</b> <var>string</var>)
</pre>
<p>
This function parses <var>string</var> (using <code>read</code>)
and evaluates the result.
The result of evaluation is returned from <code>test-read-eval-string</code>.
An error is signalled if there are unread characters after the
<code>read</code> is done.
For example:<br />
<code>(test-read-eval-string "(+ 3 4)")</code> <i>evaluates to</i> <code>7</code>.<br />
<code>(test-read-eval-string "(+ 3 4")</code> <i>signals an error</i>.<br />
<code>(test-read-eval-string "(+ 3 4) ")</code> <i>signals an error</i>,
because there is extra <q>junk</q> (<i>i.e.</i> a space) after the
list is read.
</p>
<p>
The <code>test-read-eval-string</code> used in tests:</p>
<pre>
(test-equal 7 (test-read-eval-string "(+ 3 4)"))
(test-error (test-read-eval-string "(+ 3"))
(test-equal #\newline (test-read-eval-string "#\\newline"))
(test-error (test-read-eval-string "#\\newlin"))
;; Skip the next 2 tests unless srfi-62 is available.
(test-skip (cond-expand (srfi-62 0) (else 2)))
(test-equal 5 (test-read-eval-string "(+ 1 #;(* 2 3) 4)"))
(test-equal '(x z) (test-read-string "(list 'x #;'y 'z)"))
</pre>
<h4>Test groups and paths</h4>
<p>A <dfn>test group</dfn> is a named sequence of forms containing testcases,
expressions, and definitions.
Entering a group sets the <dfn>test group name</dfn>; leaving a
group restores the previous group name.
These are dynamic (run-time) operations, and a group has no
other effect or identity.
Test groups are informal groupings: they are neither
Scheme values, nor are they syntactic forms.</p>
<!--(More formal <q>test suite</q> values are introduced below.)-->
<p>
A test group may contain nested inner test groups.
The <dfn>test group path</dfn> is a list of the currently-active
(entered) test group names, oldest (outermost) first.</p>
<pre>
(<b>test-begin</b> <var>suite-name</var> [<var>count</var>])
</pre>
<p>A <code>test-begin</code> enters a new test group.
The <var>suite-name</var> becomes the current test group name,
and is added to the end of the test group path.
Portable test suites should use a string literal for <var>suite-name</var>;
the effect of expressions or other kinds of literals is unspecified.</p>
<p>
<b>Rationale:</b> In some ways using symbols would be preferable.
However, we want human-readable names, and standard Scheme does not
provide a way to include spaces or mixed-case text in
literal symbols.</p>
<p>
The optional <var>count</var> must match the number of
test-cases executed by this group.
(Nested test groups count as a single test case for this count.)
This extra test may be useful to catch cases where a test doesn't
get executed because of some unexpected error.</p>
<p>
Additionally, if there is no currently executing test runner,
one is installed in an implementation-defined manner.</p>
<pre>
(<b>test-end</b> [<var>suite-name</var>])
</pre>
<p>
A <code>test-end</code> leaves the current test group.
An error is reported if the <var>suite-name</var> does not
match the current test group name.
<!--If it does match an earlier
name in the test group path, intervening groups are left.
--></p>
<p>
Additionally, if the matching <code>test-begin</code>
installed a new test-runner, then the <code>test-end</code>
will uninstall it, after reporting the accumulated test
results in an implementation-defined manner.</p>
<pre>
(<b>test-group</b> <var>suite-name</var> <var>decl-or-expr</var> ...)
</pre>
<p>
Equivalent to:</p>
<pre>
(if (not (test-to-skip% <var>suite-name</var>))
(dynamic-wind
(lambda () (test-begin <var>suite-name</var>))
(lambda () <var>decl-or-expr</var> ...)
(lambda () (test-end <var>suite-name</var>))))
</pre>
<p>This is usually equivalent to executing the <var>decl-or-expr</var>s
within the named test group. However, the entire group is skipped
if it matched an active <code>test-skip</code> (see later).
Also, the <code>test-end</code> is executed in case of an exception.</p>
<h4>Handling set-up and cleanup</h4>
<pre>
(<b>test-group-with-cleanup</b> <var>suite-name</var>
<var>decl-or-expr</var> ...
<var>cleanup-form</var>)
</pre>
<p>
Execute each of the <code><var>decl-or-expr</var></code> forms in order
(as in a <code><var><body></var></code>),
and then execute the <code><var>cleanup-form</var></code>.
The latter should be executed even if
one of a <code><var>decl-or-expr</var></code> forms raises an exception
(assuming the implementation has a way to catch exceptions).</p>
<p>For example:</p>
<pre>
(let ((f (open-output-file "log")))
(test-group-with-cleanup "test-file"
(do-a-bunch-of-tests f)
(close-output-port f)))
</pre>
<p><b>Erratum note:</b> <a href="https://github.com/scheme-requests-for-implementation/srfi-64/blob/4470ffdec71b1cf61633b664958a3ce5e6997710/srfi-64.html">Earlier versions</a> had a non-working example.
</p>
<!--
<h3>Test suites</h3>
<p>
<i>(Not sure how useful this is, given <code>test-group</code>).</i>
<p>A <dfn>test suite</dfn> is a test group that can (and must) be
executed explicitly.
<pre>
(test-suite <var>suite-name</var> <var>decl-or-expr</var> ...)
</pre>
<p>
The <df>test suite path</dfn> is the list of names of currently
running testsuites, from outermost (oldest) to innermost (newest).
<p>A <code>test-suite</code> form is equivalent to:
<pre>
(test-suite-register <var>suite-name</var>
(test-group <var>suite-name</var> <var>decl-or-expr</var> ...))
</pre>
You can run previously registered test suite:
<pre>
(test-suite-run <var>suite-name</var>)
</pre>
-->
<h3>Conditonal test-suites and other advanced features</h3>
<p>
The following describes features for controlling which tests to execute,
or specifying that some tests are <em>expected</em> to fail.</p>
<h4>Test specifiers</h4>
<p>Sometimes we want to only run certain tests, or we know that
certain tests are expected to fail.
A <dfn>test specifier</dfn> is a one-argument function that takes a test-runner
and returns a boolean. The specifier may be run before a test is performed,
and the result may control whether the test is executed.
For convenience, a specifier may also be a non-procedure value,
which is coerced to a specifier procedure, as described below for
<code><var>count</var></code> and <code><var>name</var></code>.</p>
<p>
A simple example is:</p>
<pre>
(if <var>some-condition</var>
(test-skip 2)) ;; skip next 2 tests
</pre>
<p>
<code>(<b>test-match-name</b> <var>name</var>)</code><br />
The resulting specifier matches if the current test name (as
returned by <code>test-runner-test-name</code>) is <code>equal?</code> to
<var>name</var>.</p>
<p>
<code>(<b>test-match-nth</b> <var>n</var> [<var>count</var>])</code><br />
This evaluates to a <em>stateful</em> predicate: A counter keeps track of
how many times it has been called.
The predicate matches the <var>n</var>'th time it is called
(where <code>1</code> is the first time), and
the next <code>(- <var>count</var> 1)</code> times,
where <var>count</var> defaults to <code>1</code>.</p>
<p>
<code>(<b>test-match-any</b> <var>specifier</var> ...)</code><br />
The resulting specifier matches if any <var>specifier</var>
matches.
Each <code><var>specifier</var></code> is applied, in order,
so side-effects from a later <code><var>specifier</var></code> happen
even if an earlier <code><var>specifier</var></code> is true.</p>
<p>
<code>(<b>test-match-all</b> <var>specifier</var> ...)</code><br />
The resulting specifier matches if each <var>specifier</var>
matches.
Each <code><var>specifier</var></code> is applied, in order,
so side-effects from a later <code><var>specifier</var></code> happen
even if an earlier <code><var>specifier</var></code> is false.</p>
<p>
<code><var>count</var> </code><i>(i.e. an integer)</i><br />
Convenience short-hand for: <code>(test-match-nth 1 <var>count</var>)</code>.</p>
<p>
<code><var>name</var> </code><i>(i.e. a string)</i><br />
Convenience short-hand for <code>(test-match-name <var>name</var>)</code>.</p>
<h4>Skipping selected tests</h4>
<p>In some cases you may want to skip a test.</p>
<pre>
(<b>test-skip</b> <var>specifier</var>)
</pre>
<p>Evaluating <code>test-skip</code> adds the
resulting <var>specifier</var>
to the set of currently active skip-specifiers.
Before each test (or <code>test-group</code>)
the set of active skip-specifiers are applied to the active test-runner.
If any specifier matches, then the test is skipped.</p>
<p>
For convenience, if the <var>specifier</var> is a string that
is syntactic sugar for <code>(test-match-name <var>specifier</var>)</code>.
For example:</p>
<pre>
(test-skip "test-b")
(test-assert "test-a") ;; executed
(test-assert "test-b") ;; skipped
</pre>
<p>
Any skip specifiers introduced by a <code>test-skip</code>
are removed by a following non-nested <code>test-end</code>.</p>
<pre>
(test-begin "group1")
(test-skip "test-a")
(test-assert "test-a") ;; skipped
(test-end "group1") ;; Undoes the prior test-skip
(test-assert "test-a") ;; executed
</pre>
<h4>Expected failures</h4>
<p>
Sometimes you know a test case will fail, but you don't have time
to or can't fix it. Maybe a certain feature only works on certain platforms.
However, you want the test-case to be there
to remind you to fix it. You want to note that
such tests are expected to fail.</p>
<pre>
(<b>test-expect-fail</b> <var>specifier</var>)
</pre>
<p>
Matching tests (where matching is defined as in <code>test-skip</code>)
are expected to fail. This only affects test reporting,
not test execution. For example:</p>
<pre>
(test-expect-fail 2)
(test-eqv ...) ;; expected to fail
(test-eqv ...) ;; expected to fail
(test-eqv ...) ;; expected to pass
</pre>
<h3>Test-runner</h3>
<p>
A <dfn>test-runner</dfn> is an object that runs a test-suite,
and manages the state. The test group path, and the sets skip and
expected-fail specifiers are part of the test-runner.
A test-runner will also typically accumulate statistics about executed tests.
</p>
<p>
<code>(<b>test-runner?</b> <var>value</var>)</code><br />
True iff <code><var>value</var></code> is a test-runner object.</p>
<p>
<code>(<b>test-runner-current</b>)</code><br />
<code>(<b>test-runner-current</b> <var>runner</var>)</code><br />
Get or set the current test-runner.
If an implementation supports parameter objects
(as in <a href="https://srfi.schemers.org/srfi-39/srfi-39.html">SRFI-39</a>),
then <code>test-runner-current</code> can be a parameter object.
Alternatively, <code>test-runner-current</code> may be implemented
as a macro or function
that uses a fluid or thread-local variable, or a plain global variable.</p>
<p>
<code>(<b>test-runner-get</b>)</code><br />
Same as <code>(test-runner-current)</code>, but throws an exception
if there is no current test-runner.</p>
<p>
<code>(<b>test-runner-simple</b>)</code><br />
Creates a new simple test-runner, that prints errors and a summary
on the standard output port.</p>
<p>
<code>(<b>test-runner-null</b>)</code><br />
Creates a new test-runner, that does nothing with the test results.
This is mainly meant for extending when writing a custom runner.</p>
<p>
Implementations <em>may</em> provide other test-runners, perhaps
a <code>(test-runner-gui)</code>.</p>
<p><code>(<b>test-runner-create</b>)</code><br />
Create a new test-runner. Equivalent to
<code>((test-runner-factory))</code>.</p>
<p>
<code>(<b>test-runner-factory</b>)</code><br />
<code>(<b>test-runner-factory</b> <var>factory</var>)</code><br />
Get or set the current test-runner factory.
A factory is a zero-argument function that creates a new test-runner.
The default value is <code>test-runner-simple</code>,
but implementations may provide a way to override the default.
As with <code>test-runner-current</code>, this may be a parameter object,
or use a per-thread, fluid, or global variable.</p>
<h4>Running specific tests with a specified runner</h4>
<p>
<code>(<b>test-apply</b> [<var>runner</var>] <var>specifier</var> ... <var>procedure</var>)</code><br />
Calls <var>procedure</var> with no arguments using the specified
<var>runner</var> as the current test-runner.
If <var>runner</var> is omitted,
then <code>(test-runner-current)</code> is used.
(If there is no current runner, one is created as in <code>test-begin</code>.)
If one or more <var>specifier</var>s are listed then only tests matching
the <var>specifier</var>s are executed. A <var>specifier</var> has the same form
as one used for <code>test-skip</code>. A test is executed
if it matches any of the <var>specifier</var>s in the
<code>test-apply</code> <em>and</em> does not match any
active <code>test-skip</code> specifiers.</p>
<p>
<code>(<b>test-with-runner</b> <var>runner</var> <var>decl-or-expr</var> ...)</code><br />
Executes each <var>decl-or-expr</var> in order in a context
where the current test-runner is <var>runner</var>.</p>
<h3>Test results</h3>
<p>Running a test sets various status properties in the current test-runner.
This can be examined by a custom test-runner,
or (more rarely) in a test-suite.</p>
<h4>Result kind</h4>
<p>Running a test may yield one of the following
status symbols:</p>
<dl>
<dt><code>'pass</code></dt><dd>The test passed, as expected.</dd>
<dt><code>'fail</code></dt><dd>The test failed (and was not expected to).</dd>
<dt><code>'xfail</code></dt><dd>The test failed and was expected to.</dd>
<dt><code>'xpass</code></dt><dd>The test passed, but was expected to fail.</dd>
<dt><code>'skip</code></dt><dd>The test was skipped.</dd>
</dl>
<p>
<code>(<b>test-result-kind</b> [<var>runner</var>])</code><br />
Returns one of the above result codes from the most recent tests.
Returns <code>#f</code> if no tests have been run yet.
If we've started on a new test, but don't have a result yet,
then the result kind is <code>'xfail</code> if the test is expected to fail,
<code>'skip</code> if the test is supposed to be skipped,
or <code>#f</code> otherwise.</p>
<p>
<code>(<b>test-passed?</b> [<var>runner</var>])</code><br />
True if the value of <code>(test-result-kind [<var>runner</var>])</code>
is one of <code>'pass</code> or <code>'xpass</code>.
This is a convenient shorthand that might be useful
in a test suite to only run certain tests if the previous test passed.</p>
<h4>Test result properties</h4>
<p>
A test runner also maintains a set of more detailed <q>result properties</q>
associated with the current or most recent test. (I.e. the properties of the
most recent test are available as long as a new test hasn't started.)
Each property has a name (a symbol) and a value (any value).
Some properties are standard or set by the implementation;
implementations can add more.</p>
<p>
<code>(<b>test-result-ref</b> <var>runner</var> '<var>pname</var> [<var>default</var>])</code><br />
Returns the property value associated with the <var>pname</var> property name.
If there is no value associated with <code>'<var>pname</var></code>
return <var>default</var>,
or <code>#f</code> if <var>default</var> isn't specified.</p>
<p>
<code>(<b>test-result-set!</b> <var>runner</var> '<var>pname</var> <var>value</var>)</code><br />
Sets the property value associated with the <var>pname</var>
property name to <var>value</var>.
Usually implementation code should call this function, but it may be
useful for a custom test-runner to add extra properties.</p>
<p>
<code>(<b>test-result-remove</b> <var>runner</var> '<var>pname</var>)</code><br />
Remove the property with the name <code>'<var>pname</var></code>.</p>
<p>
<code>(<b>test-result-clear</b> <var>runner</var>)</code><br />
Remove all result properties.
The implementation automatically calls <code>test-result-clear</code>
at the start of a <code>test-assert</code> and similar procedures.</p>
<p>
<code>(<b>test-result-alist</b> <var>runner</var>)</code><br />
Returns an association list of the current result properties.
It is unspecified if the result shares state with the test-runner.
The result should not be modified; on the other hand, the result
may be implicitly modified by future <code>test-result-set!</code> or
<code>test-result-remove</code> calls.
However, a <code>test-result-clear</code> does not modify the returned
alist. Thus you can <q>archive</q> result objects from previous runs.</p>
<h4>Standard result properties</h4>
<p>
The set of available result properties is implementation-specific.
However, it is suggested that the following might be provided:</p>
<dl>
<dt><code>'result-kind</code></dt>
<dd>The result kind, as defined previously.
This is the only mandatory result property.<br />
<code>(test-result-kind <var>runner</var>)</code> is equivalent to:<br />
<code>(test-result-ref <var>runner</var> 'result-kind)</code>
</dd>
<dt><code>'source-file</code></dt>
<dt><code>'source-line</code></dt>
<dd>If known, the location of test statements (such as <code>test-assert</code>)
in test suite source code.</dd>
<dt><code>'source-form</code></dt>
<dd>The source form, if meaningful and known.</dd>
<dt><code>'expected-value</code></dt>
<dd>The expected non-error result, if meaningful and known.</dd>
<dt><code>'expected-error</code></dt>
<dd>The <code><var>error-type</var></code>
specified in a <code>test-error</code>, if it meaningful and known.</dd>
<dt><code>'actual-value</code></dt>
<dd>The actual non-error result value, if meaningful and known.</dd>
<dt><code>'actual-error</code></dt>
<dd>The error value, if an error was signalled and it is known.
The actual error value is implementation-defined.</dd>
</dl>
<h3>Writing a new test-runner</h3>
<p>This section specifies how to write a test-runner.
It can be ignored if you just want to write test-cases.</p>
<h4>Call-back functions</h4>
<p>
These call-back functions are <q>methods</q> (in the object-oriented sense)
of a test-runner. A method <code>test-runner-on-<var>event</var></code>
is called by the implementation when <var>event</var> happens.</p>
<p>
To define (set) the callback function for <var>event</var> use the following expression.
(This is normally done when initializing a test-runner.)
<br />
<code>(test-runner-on-<var>event</var>! <var>runner</var> <var>event-function</var>)</code></p>
<p>
An <var>event-function</var> takes a test-runner argument, and possibly other arguments, depending on the <var>event</var>.</p>
<p>
To extract (get) the callback function for <var>event</var> do this:<br />
<code>(test-runner-on-<var>event</var> <var>runner</var>)</code></p>
<p>
To extract call the callback function for <var>event</var> use the following expression.
(This is normally done by the implementation core.)<br />
<code>((test-runner-on-<var>event</var> <var>runner</var>) <var>runner</var> <var>other-args</var> ...)</code></p>
<p>
The following call-back hooks are available.</p>
<p>
<code>(<b>test-runner-on-test-begin</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-test-begin!</b> <var>runner</var> <var>on-test-begin-function</var>)</code><br />
<code>(<var>on-test-begin-function</var> <var>runner</var>)</code><br />
The <var>on-test-begin-function</var> is called at the start of an
individual testcase, before the test expression (and expected value) are
evaluated.
</p>
<p>
<code>(<b>test-runner-on-test-end</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-test-end!</b> <var>runner</var> <var>on-test-end-function</var>)</code><br />
<code>(<var>on-test-end-function</var> <var>runner</var>)</code><br />
The <var>on-test-end-function</var> is called at the end of an
individual testcase, when the result of the test is available.</p>
<p>
<code>(<b>test-runner-on-group-begin</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-group-begin!</b> <var>runner</var> <var>on-group-begin-function</var>)</code><br />
<code>(<var>on-group-begin-function</var> <var>runner</var> <var>suite-name</var> <var>count</var>)</code><br />
The <var>on-group-begin-function</var> is called by a <code>test-begin</code>,
including at the start of a <code>test-group</code>.
The <var>suite-name</var> is a Scheme string,
and <var>count</var> is an integer or <code>#f</code>.</p>
<p>
<code>(<b>test-runner-on-group-end</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-group-end!</b> <var>runner</var> <var>on-group-end-function</var>)</code><br />
<code>(<var>on-group-end-function</var> <var>runner</var>)</code><br />
The <var>on-group-end-function</var> is called by a <code>test-end</code>,
including at the end of a <code>test-group</code>.</p>
<p>
<code>(<b>test-runner-on-bad-count</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-bad-count!</b> <var>runner</var> <var>on-bad-count-function</var>)</code><br />
<code>(<var>on-bad-count-function</var> <var>runner</var> <var>actual-count</var> <var>expected-count</var>)</code><br />
Called from <code>test-end</code> (before the <var>on-group-end-function</var>
is called) if an <var>expected-count</var> was specified by the matching
<code>test-begin</code> and the <var>expected-count</var> does not match
the <var>actual-count</var> of tests actually executed or skipped.</p>
<p>
<code>(<b>test-runner-on-bad-end-name</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-bad-end-name!</b> <var>runner</var> <var>on-bad-end-name-function</var>)</code><br />
<code>(<var>on-bad-end-name-function</var> <var>runner</var> <var>begin-name</var> <var>end-name</var>)</code><br />
Called from <code>test-end</code> (before the <var>on-group-end-function</var>
is called) if a <var>suite-name</var> was specified, and it did not that the
name in the matching <code>test-begin</code>.</p>
<p>
<code>(<b>test-runner-on-final</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-on-final!</b> <var>runner</var> <var>on-final-function</var>)</code><br />
<code>(<var>on-final-function</var> <var>runner</var>)</code><br />
The <var>on-final-function</var> takes one parameter (a test-runner)
and typically displays a summary (count) of the tests.
The <var>on-final-function</var> is called after called the
<var>on-group-end-function</var> correspondiong to the outermost
<code>test-end</code>.
The default value is <code>test-on-final-simple</code> which writes
to the standard output port the number of tests of the various kinds.
</p>
<p>
The default test-runner returned by <code>test-runner-simple</code>
uses the following call-back functions:<br />
<code>(<b>test-on-test-begin-simple</b> <var>runner</var>)</code><br />
<code>(<b>test-on-test-end-simple</b> <var>runner</var>)</code><br />
<code>(<b>test-on-group-begin-simple</b> <var>runner</var> <var>suite-name</var> <var>count</var>)</code><br />
<code>(<b>test-on-group-end-simple</b> <var>runner</var>)</code><br />
<code>(<b>test-on-bad-count-simple</b> <var>runner</var> <var>actual-count</var> <var>expected-count</var>)</code><br />
<code>(<b>test-on-bad-end-name-simple</b> <var>runner</var> <var>begin-name</var> <var>end-name</var>)</code><br />
You can call those if you want to write your own test-runner.</p>
<h4>Test-runner components</h4>
<p>
The following functions are for accessing the other components of a test-runner.
They would normally only be used to write a new test-runner or
a match-predicate.</p>
<p>
<code>(<b>test-runner-pass-count</b> <var>runner</var>)</code><br />
Returns the number of tests that passed, and were expected to pass.</p>
<p>
<code>(<b>test-runner-fail-count</b> <var>runner</var>)</code><br />
Returns the number of tests that failed, but were expected to pass.</p>
<p>
<code>(<b>test-runner-xpass-count</b> <var>runner</var>)</code><br />
Returns the number of tests that passed, but were expected to fail.</p>
<p>
<code>(<b>test-runner-xfail-count</b> <var>runner</var>)</code><br />
Returns the number of tests that failed, and were expected to pass.</p>
<p>
<code>(<b>test-runner-skip-count</b> <var>runner</var>)</code><br />
Returns the number of tests or test groups that were skipped.</p>
<p>
<code>(<b>test-runner-test-name</b> <var>runner</var>)</code><br />
Returns the name of the current test or test group, as a string.
During execution of <code>test-begin</code> this is the name of the
test group; during the execution of an actual test, this is the name
of the test-case.
If no name was specified, the name is the empty string.</p>
<p>
<code>(<b>test-runner-group-path</b> <var>runner</var>)</code><br />
A list of names of groups we're nested in, with the outermost group first.</p>
<p>
<code>(<b>test-runner-group-stack</b> <var>runner</var>)</code><br />
A list of names of groups we're nested in, with the outermost group last.
(This is more efficient than <code>test-runner-group-path</code>,
since it doesn't require any copying.)</p>
<p>
<code>(<b>test-runner-aux-value</b> <var>runner</var>)</code><br />
<code>(<b>test-runner-aux-value!</b> <var>runner</var> <var>on-test</var>)</code><br />
Get or set the <code>aux-value</code> field of a test-runner.
This field is not used by this API or the <code>test-runner-simple</code>
test-runner, but may be used by custom test-runners to store extra state.</p>
<p>
<code>(<b>test-runner-reset</b> <var>runner</var>)</code><br />
Resets the state of the <var>runner</var> to its initial state.
</p>
<h4>Example</h4>
<p>This is an example of a simple custom test-runner.
Loading this program before running a test-suite will install
it as the default test runner.</p>
<pre>
(define (my-simple-runner filename)
(let ((runner (test-runner-null))
(port (open-output-file filename))
(num-passed 0)
(num-failed 0))
(test-runner-on-test-end! runner
(lambda (runner)
(case (test-result-kind runner)
((pass xpass) (set! num-passed (+ num-passed 1)))
((fail xfail) (set! num-failed (+ num-failed 1)))
(else #t))))
(test-runner-on-final! runner
(lambda (runner)
(format port "Passing tests: ~d.~%Failing tests: ~d.~%"
num-passed num-failed)
(close-output-port port)))
runner))
(test-runner-factory
(lambda () (my-simple-runner "/tmp/my-test.log")))
</pre>
<h2>Implementation</h2>
<p>
The test implementation uses <code>cond-expand</code>
(<a href="https://srfi.schemers.org/srfi-0/srfi-0.html">SRFI-0</a>)
to select different code depending on certain SRFI names (<code>srfi-9</code>,
<code>srfi-34</code>, <code>srfi-35</code>, <code>srfi-39</code>),
or implementations (<code>kawa</code>).
It should otherwise be portable to any R5RS implementation.</p>
<p><a href="testing.scm">testing.scm</a></p>
<h3>Examples</h3>
<p>Here is <a href="srfi-25-test.scm"><code>srfi-25-test.scm</code></a>,
based converted from Jussi Piitulainen's
<a href="https://srfi.schemers.org/srfi-25/test.scm"><code>test.scm</code></a>
for <a href="https://srfi.schemers.org/srfi-25/srfi-25.html">SRFI-25</a>.</p>
<h3>Test suite</h3>
<p>
Of course we need a test suite for the testing framework itself.
This suite <code><a href="srfi-64-test.scm">srfi-64-test.scm</a></code>
was contributed by Donovan Kolbly
<a href="mailto:donovan@rscheme.org"><code><donovan@rscheme.org></code></a>.</p>
<h2>Copyright</h2>
<p>
Copyright (C) Per Bothner (2005, 2006)</p>
<p>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>
<p>
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<hr />
<address>Author: <a href="mailto:per@bothner.com">Per Bothner</a></address>
<address>Editor: <a href="mailto:srfi%20minus%20editors%20at%20srfi%20dot%20schemers%20dot%20org">Francisco Solsona</a></address>
<p>
<!-- Created: Sat Sep 4 12:26:07 PDT 2004 -->
<!-- hhmts start -->
Last modified: Thu Mar 31 19:49:52 MST 2011
<!-- hhmts end -->
</p>
</body>
</html>