Changeset 39313 in project
- Timestamp:
- 11/19/20 08:13:28 (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wiki/eggref/5/srfi-64
r38809 r39313 1 1 [[tags: egg]] 2 2 3 == srfi-64 4 5 See [[//srfi.schemers.org/srfi-64/srfi-64.html|srfi.schemers.org/srfi-64/srfi-64.html]] 3 == SRFI-64: A Scheme API for test suites 4 === Abstract 5 This defines an API for writing test suites, to make it easy to portably test Scheme APIs, libraries, applications, and implementations. A test suite is a collection of test cases that execute in the context of a test-runner. This specifications also supports writing new test-runners, to allow customization of reporting and processing the result of running test suites. 6 7 For more information see: [[//srfi.schemers.org/srfi-64/|SRFI-64: A Scheme API for test suites]] 8 === Rationale 9 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. 10 11 There are other testing frameworks written in Scheme, including [[http://schematics.sourceforge.net/schemeunit.html|SchemeUnit]]. However SchemeUnit is not portable. It is also a bit on the verbose side. It would be useful to have a bridge between this framework and SchemeUnit so SchemeUnit tests could run under this framework and vice versa. There exists also at least one Scheme wrapper providing a Scheme interface to the `standard' [[http://www.junit.org/|JUnit]] 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. 12 13 This API makes use of implicit dynamic state, including an implicit `test runner'. This makes the API convenient and terse to use, but it may be a little less elegant and `compositional' 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. 14 15 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. 16 17 All names defined by the API start with the prefix test-. 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. 18 === Specification 19 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. 20 === Writing basic test suites 21 Let's start with a simple example. This is a complete self-contained test-suite. 22 23 <enscript highlight="scheme"> 24 ;; Initialize and give a name to a simple testsuite. 25 (test-begin "vec-test") 26 (define v (make-vector 5 99)) 27 ;; Require that an expression evaluate to true. 28 (test-assert (vector? v)) 29 ;; Test that an expression is eqv? to some other expression. 30 (test-eqv 99 (vector-ref v 2)) 31 (vector-set! v 2 7) 32 (test-eqv 7 (vector-ref v 2)) 33 ;; Finish the testsuite, and report results. 34 (test-end "vec-test") 35 </enscript> 36 37 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). 38 39 Test cases are executed in the context of a test runner, which is a 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 test-end will cause a summary to be displayed in an implementation-specified manner. 40 === Simple test-cases 41 Primitive test cases test that a given condition is true. They may have a name. The core test case form is test-assert: 42 43 <enscript highlight="scheme"> 44 (test-assert [test-name] expression) 45 </enscript> 46 47 This evaluates the expression. 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 test-name is a string that names the test case. (Though the test-name 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 test-assert if there is no current test runner. 48 ==== The following forms may be more convenient than using test-assert directly: 49 <enscript highlight="scheme"> 50 (test-eqv [test-name] expected test-expr) 51 </enscript> 52 ==== This is equivalent to: 53 <enscript highlight="scheme"> 54 (test-assert [test-name] (eqv? expected test-expr)) 55 </enscript> 56 ==== Similarly test-equal and test-eq are shorthand for test-assert combined with equal? or eq?, respectively: 57 <enscript highlight="scheme"> 58 (test-equal [test-name] expected test-expr) 59 (test-eq [test-name] expected test-expr) 60 </enscript> 61 ==== Here is a simple example: 62 <enscript highlight="scheme"> 63 (define (mean x y) (/ (+ x y) 2.0)) 64 (test-eqv 4 (mean 3 5)) 65 </enscript> 66 ==== For testing approximate equality of inexact reals we can use test-approximate: 67 <enscript highlight="scheme"> 68 (test-approximate [test-name] expected test-expr error) 69 </enscript> 70 ==== This is equivalent to (except that each argument is only evaluated once): 71 <enscript highlight="scheme"> 72 (test-assert [test-name] 73 (and (>= test-expr (- expected error)) 74 (<= test-expr (+ expected error)))) 75 </enscript> 76 === Tests for catching errors 77 We need a way to specify that evaluation should fail. 78 ==== This verifies that errors are detected when required. 79 <enscript highlight="scheme"> 80 (test-error [[test-name] error-type] test-expr) 81 </enscript> 82 83 Evaluating test-expr is expected to signal an error. The kind of error is indicated by error-type. 84 85 If the error-type is left out, or it is #t, it means "some kind of unspecified error should be signaled". 86 ===== For example: 87 <enscript highlight="scheme"> 88 (test-error #t (vector-ref '#(1 2) 9)) 89 </enscript> 90 ==== This specification leaves it implementation-defined (or for a future specification) what form test-error may take, though all implementations must allow #t. 91 ==== Some implementations may support [[https://srfi.schemers.org/srfi-35/srfi-35.html|SRFI-35's conditions]], but these are only standardized for [[https://srfi.schemers.org/srfi-36/srfi-36.html|SRFI-36's I/O conditions]], which are seldom useful in test suites. An implementation may also allow implementation-specific `exception types'. 92 ===== For example Java-based implementations may allow the names of Java exception classes: 93 <enscript highlight="scheme"> 94 ;; Kawa-specific example 95 (test-error <java.lang.IndexOutOfBoundsException> (vector-ref '#(1 2) 9)) 96 </enscript> 97 98 An implementation that cannot catch exceptions should skip test-error forms. 99 === Testing syntax 100 Testing syntax is tricky, especially if we want to check that invalid syntax is causes an error. The following utility function can help: 101 102 <enscript highlight="scheme"> 103 (test-read-eval-string string) 104 </enscript> 105 106 This function parses string (using read) and evaluates the result. The result of evaluation is returned from test-read-eval-string. An error is signalled if there are unread characters after the read is done. For example: {{(test-read-eval-string "(+ 3 4)")}} evaluates to 7. {{(test-read-eval-string "(+ 3 4") signals an error. (test-read-eval-string "(+ 3 4) ")}} signals an error, because there is extra `junk' (i.e. a space) after the list is read. 107 ==== The test-read-eval-string used in tests: 108 <enscript highlight="scheme"> 109 (test-equal 7 (test-read-eval-string "(+ 3 4)")) 110 (test-error (test-read-eval-string "(+ 3")) 111 (test-equal #\newline (test-read-eval-string "#\\newline")) 112 (test-error (test-read-eval-string "#\\newlin")) 113 114 ;; Skip the next 2 tests unless srfi-62 is available. 115 (test-skip (cond-expand (srfi-62 0) (else 2))) 116 (test-equal 5 (test-read-eval-string "(+ 1 #;(* 2 3) 4)")) 117 (test-equal '(x z) (test-read-string "(list 'x #;'y 'z)")) 118 </enscript> 119 === Test groups and paths 120 A test group is a named sequence of forms containing testcases, expressions, and definitions. Entering a group sets the test group name; 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. 121 122 A test group may contain nested inner test groups. The test group path is a list of the currently-active (entered) test group names, oldest (outermost) first. 123 124 <procedure>(test-begin suite-name [count])</procedure> 125 126 A test-begin enters a new test group. The suite-name becomes the current test group name, and is added to the end of the test group path. Portable test suites should use a sting literal for suite-name; the effect of expressions or other kinds of literals is unspecified. 127 128 Rationale: 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. 129 130 The optional count 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. 131 132 Additionally, if there is no currently executing test runner, one is installed in an implementation-defined manner. 133 134 <procedure>(test-end [suite-name])</procedure> 135 136 A test-end leaves the current test group. An error is reported if the suite-name does not match the current test group name. 137 138 Additionally, if the matching {{test-begin}} installed a new test-runner, then the {{test-end}} will de-install it, after reporting the accumulated test results in an implementation-defined manner. 139 140 <procedure>(test-group suite-name decl-or-expr ...)</procedure> 141 142 Equivalent to: 143 <enscript highlight="scheme"> 144 (if (not (test-to-skip% suite-name)) 145 (dynamic-wind 146 (lambda () (test-begin suite-name)) 147 (lambda () decl-or-expr ...) 148 (lambda () (test-end suite-name)))) 149 </enscript> 150 151 This is usually equivalent to executing the {{decl-or-exprs}} within the named test group. However, the entire group is skipped if it matched an active {{test-skip}} (see later). Also, the {{test-end}} is executed in case of an exception. 152 === Handling set-up and cleanup 153 <procedure>(test-group-with-cleanup suite-name decl-or-expr ... cleanup-form)</procedure> 154 155 Execute each of the {{decl-or-expr}} forms in order (as in a {{<body>}}), and then execute the cleanup-form. The latter should be executed even if one of a {{decl-or-expr}} forms raises an exception (assuming the implementation has a way to catch exceptions). 156 ==== For example: 157 <enscript highlight="scheme"> 158 (let ((f (open-output-file "log"))) 159 (test-group-with-cleanup "test-file" 160 (do-a-bunch-of-tests f) 161 (close-output-port f))) 162 </enscript> 163 ==== Erratum note: [[https://github.com/scheme-requests-for-implementation/srfi-64/blob/4470ffdec71b1cf61633b664958a3ce5e6997710/srfi-64.html|Earlier versions]] had a non-working example. 164 === Conditonal test-suites and other advanced features 165 The following describes features for controlling which tests to execute, or specifing that some tests are expected to fail. 166 === Test specifiers 167 Sometimes we want to only run certain tests, or we know that certain tests are expected to fail. A test specifier is 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 count and name. 168 ==== A simple example is: 169 <enscript highlight="scheme"> 170 (if some-condition 171 (test-skip 2)) ;; skip next 2 tests 172 </enscript> 173 174 <procedure>(test-match-name name)</procedure> 175 176 The resulting specifier matches if the current test name (as returned by test-runner-test-name) is {{equals?}} to name. 177 178 <procedure>(test-match-nth n [count])</procedure> 179 180 This evaluates to a stateful predicate: A counter keeps track of how many times it has been called. The predicate matches the {{n}}'th time it is called (where 1 is the first time), and the next {{(- count 1)}} times, where count defaults to 1. 181 182 <procedure>(test-match-any specifier ...)</procedure> 183 184 The resulting specifier matches if any specifier matches. Each specifier is applied, in order, so side-effects from a later specifier happen even if an earlier specifier is true. 185 186 <procedure>(test-match-all specifier ...)</procedure> 187 188 The resulting specifier matches if each specifier matches. Each specifier is applied, in order, so side-effects from a later specifier happen even if an earlier specifier is false. 189 190 <procedure>count (i.e. an integer)</procedure> 191 192 Convenience short-hand for: 193 <enscript highlight="scheme"> 194 (test-match-nth 1 count). 195 </enscript> 196 197 <procedure>name (i.e. a string)</procedure> 198 199 Convenience short-hand for 200 <enscript highlight="scheme"> 201 (test-match-name name). 202 </enscript> 203 === Skipping selected tests 204 In some cases you may want to skip a test. 205 206 <procedure>(test-skip specifier)</procedure> 207 208 Evaluating {{test-skip}} adds the resulting specifier to the set of currently active skip-specifiers. Before each test (or test-group) the set of active skip-specifiers are applied to the active test-runner. If any specifier matches, then the test is skipped. 209 210 For convenience, if the specifier is a string that is syntactic sugar for {{(test-match-name specifier)}}. 211 ==== For example: 212 <enscript highlight="scheme"> 213 (test-skip "test-b") 214 (test-assert "test-a") ;; executed 215 (test-assert "test-b") ;; skipped 216 </enscript> 217 ==== Any skip specifiers introduced by a test-skip are removed by a following non-nested test-end. 218 <enscript highlight="scheme"> 219 (test-begin "group1") 220 (test-skip "test-a") 221 (test-assert "test-a") ;; skipped 222 (test-end "group1") ;; Undoes the prior test-skip 223 (test-assert "test-a") ;; executed 224 </enscript> 225 === Expected failures 226 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. 227 228 <enscript highlight="scheme"> 229 (test-expect-fail specifier) 230 </enscript> 231 232 Matching tests (where matching is defined as in {{test-skip}}) are expected to fail. This only affects test reporting, not test execution. For example: 233 234 <enscript highlight="scheme"> 235 (test-expect-fail 2) 236 (test-eqv ...) ;; expected to fail 237 (test-eqv ...) ;; expected to fail 238 (test-eqv ...) ;; expected to pass 239 </enscript> 240 === Test-runner 241 A test-runner 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, 242 243 <procedure>(test-runner? value)</procedure> 244 245 True iff {{value}} is a {{test-runner}} object. 246 247 <procedure>(test-runner-current)</procedure> 248 249 250 <procedure>(test-runner-current runner)</procedure> 251 252 Get or set the current test-runner. If an implementation supports parameter objects (as in [[https://srfi.schemers.org/srfi-39/srfi-39.html|SRFI-39]]), then {{test-runner-current}} can be a parameter object. Alternatively, {{test-runner-current}} may be implemented as a macro or function that uses a fluid or thread-local variable, or a plain global variable. 253 254 <procedure>(test-runner-get)</procedure> 255 256 Same as {{(test-runner-current)}}, but throws an exception if there is no current test-runner. 257 258 <procedure>(test-runner-simple)</procedure> 259 260 Creates a new simple test-runner, that prints errors and a summary on the standard output port. 261 262 <procedure>(test-runner-null)</procedure> 263 264 Creates a new test-runner, that does nothing with the test results. This is mainly meant for extending when writing a custom runner. 265 ==== Implementations may provide other test-runners, perhaps a {{(test-runner-gui)}}. 266 267 <procedure>(test-runner-create)</procedure> 268 269 Create a new test-runner. Equivalent to {{((test-runner-factory))}}. 270 271 <procedure>(test-runner-factory)</procedure> 272 273 274 <procedure>(test-runner-factory factory)</procedure> 275 276 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 {{test-runner-simple}}, but implementations may provide a way to override the default. As with {{test-runner-current}}, this may be a parameter object, or use a per-thread, fluid, or global variable. 277 === Running specific tests with a specified runner 278 279 <procedure>(test-apply [runner] specifier ... procedure)</procedure> 280 281 Calls procedure with no arguments using the specified runner as the current test-runner. If runner is omitted, then {{(test-runner-current)}} is used. (If there is no current runner, one is created as in {{test-begin}}.) If one or more specifiers are listed then only tests matching the specifiers are executed. A specifier has the same form as one used for test-skip. A test is executed if it matches any of the specifiers in the {{test-apply}} and does not match any active {{test-skip}} specifiers. 282 283 <procedure>(test-with-runner runner decl-or-expr ...)</procedure> 284 285 Executes each {{decl-or-expr}} in order in a context where the current test-runner is runner. 286 === Test results 287 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. 288 ==== Result kind 289 Running a test may yield one of the following status symbols: 290 * {{'pass}} 291 The passed, as expected. 292 * {{'fail}} 293 The test failed (and was not expected to). 294 * {{'xfail}} 295 The test failed and was expected to. 296 * {{'xpass}} 297 The test passed, but was expected to fail. 298 * {{'skip}} 299 The test was skipped. 300 301 <procedure>(test-result-kind [runner])</procedure> 302 303 Return one of the above result codes from the most recent tests. Returns {{#f}} 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 'xfail is the test is expected to fail, 'skip is the test is supposed to be skipped, or {{#f}} otherwise. 304 305 <procedure>(test-passed? [runner])</procedure> 306 307 True if the value of (test-result-kind [runner]) is one of {{'pass}} or {{'xpass}}. This is a convenient shorthand that might be useful in a test suite to only run certain tests if the previous test passed. 308 ==== Test result properties 309 A test runner also maintains a set of more detailed "result properties" 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. 310 311 <procedure>(test-result-ref runner 'pname [default])</procedure> 312 313 Returns the property value associated with the {{pname}} property name. If there is no value associated with {{'pname}} return default, or #f if default isn't specified. 314 315 <procedure>(test-result-set! runner 'pname value)</procedure> 316 317 Sets the property value associated with the {{pname}} property name to value. Usually implementation code should call this function, but it may be useful for a custom test-runner to add extra properties. 318 319 <procedure>(test-result-remove runner 'pname)</procedure> 320 321 Remove the property with the name {{'pname}}. 322 323 <procedure>(test-result-clear runner)</procedure> 324 325 Remove all result properties. The implementation automatically calls {{test-result-clear}} at the start of a test-assert and similar procedures. 326 327 <procedure>(test-result-alist runner)</procedure> 328 329 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 {{test-result-set!}} or {{test-result-remove}} calls. However, a {{test-result-clear}} does not modify the returned alist. Thus you can "archive" result objects from previous runs. 330 ==== Standard result properties 331 The set of available result properties is implementation-specific. However, it is suggested that the following might be provided: 332 * {{'result-kind}} 333 The result kind, as defined previously. This is the only mandatory result property. 334 335 <procedure>(test-result-kind runner)</procedure> 336 337 is equivalent to: 338 339 <procedure>(test-result-ref runner 'result-kind)</procedure> 340 * {{'source-file}} 341 * {{'source-line}} 342 If known, the location of test statements (such as test-assert) in test suite source code.. 343 * {{'source-form}} 344 The source form, if meaningful and known. 345 * {{'expected-value}} 346 The expected non-error result, if meaningful and known. 347 * {{'expected-error}} 348 The error-type specified in a test-error, if it meaningful and known. 349 * {{'actual-value}} 350 The actual non-error result value, if meaningful and known. 351 * {{'actual-error}} 352 The error value, if an error was signalled and it is known. The actual error value is implementation-defined. 353 === Writing a new test-runner 354 This section specifies how to write a test-runner. It can be ignored if you just want to write test-cases. 355 ==== Call-back functions 356 These call-back functions are "methods" (in the object-oriented sense) of a test-runner. A method {{test-runner-on-event}} is called by the implementation when event happens. 357 358 To define (set) the callback function for event use the following expression. (This is normally done when initializing a test-runner.) 359 360 <procedure>(test-runner-on-event! runner event-function)</procedure> 361 362 An event-function takes a test-runner argument, and possibly other arguments, depending on the event. 363 364 To extract (get) the callback function for event do this: 365 366 <procedure>(test-runner-on-event runner)</procedure> 367 368 To extract call the callback function for event use the following expression. (This is normally done by the implementation core.) 369 370 <procedure>((test-runner-on-event runner) runner other-args ...)</procedure> 371 372 The following call-back hooks are available. 373 374 <procedure>(test-runner-on-test-begin runner)</procedure> 375 376 377 <procedure>(test-runner-on-test-begin! runner on-test-begin-function)</procedure> 378 379 380 <procedure>(on-test-begin-function runner)</procedure> 381 382 The {{on-test-begin-function}} is called at the start of an individual testcase, before the test expression (and expected value) are evaluated. 383 384 <procedure>(test-runner-on-test-end runner)</procedure> 385 386 387 <procedure>(test-runner-on-test-end! runner on-test-end-function)</procedure> 388 389 390 <procedure>(on-test-end-function runner)</procedure> 391 392 The {{on-test-end-function}} is called at the end of an individual testcase, when the result of the test is available. 393 394 <procedure>(test-runner-on-group-begin runner)</procedure> 395 396 397 <procedure>(test-runner-on-group-begin! runner on-group-begin-function)</procedure> 398 399 400 <procedure>(on-group-begin-function runner suite-name count)</procedure> 401 402 The {{on-group-begin-function}} is called by a {{test-begin}}, including at the start of a test-group. The suite-name is a Scheme string, and count is an integer or {{#f}}. 403 404 <procedure>(test-runner-on-group-end runner)</procedure> 405 406 407 <procedure>(test-runner-on-group-end! runner on-group-end-function)</procedure> 408 409 410 <procedure>(on-group-end-function runner)</procedure> 411 412 The {{on-group-end-function}} is called by a test-end, including at the end of a test-group. 413 414 <procedure>(test-runner-on-bad-count runner)</procedure> 415 416 417 <procedure>(test-runner-on-bad-count! runner on-bad-count-function)</procedure> 418 419 420 <procedure>(on-bad-count-function runner actual-count expected-count)</procedure> 421 422 Called from {{test-end}} (before the {{on-group-end-function}} is called) if an {{expected-count}} was specified by the matching {{test-begin}} and the {{expected-count}} does not match the actual-count of tests actually executed or skipped. 423 424 <procedure>(test-runner-on-bad-end-name runner)</procedure> 425 426 427 <procedure>(test-runner-on-bad-end-name! runner on-bad-end-name-function)</procedure> 428 429 430 <procedure>(on-bad-end-name-function runner begin-name end-name)</procedure> 431 432 Called from {{test-end}} (before the on-group-end-function is called) if a {{suite-name}} was specified, and it did not that the name in the matching {{test-begin}}. 433 434 <procedure>(test-runner-on-final runner)</procedure> 435 436 437 <procedure>(test-runner-on-final! runner on-final-function)</procedure> 438 439 440 <procedure>(on-final-function runner)</procedure> 441 442 The {{on-final-function}} takes one parameter (a test-runner) and typically displays a summary (count) of the tests. The {{on-final-function}} is called after called the {{on-group-end-function}} correspondiong to the outermost {{test-end}}. The default value is {{test-on-final-simple}} which writes to the standard output port the number of tests of the various kinds. 443 ==== The default test-runner returned by test-runner-simple uses the following call-back functions: 444 445 <procedure>(test-on-test-begin-simple runner)</procedure> 446 447 448 <procedure>(test-on-test-end-simple runner)</procedure> 449 450 451 <procedure>(test-on-group-begin-simple runner suite-name count)</procedure> 452 453 454 <procedure>(test-on-group-end-simple runner)</procedure> 455 456 457 <procedure>(test-on-bad-count-simple runner actual-count expected-count)</procedure> 458 459 460 <procedure>(test-on-bad-end-name-simple runner begin-name end-name)</procedure> 461 462 You can call those if you want to write your own test-runner. 463 === Test-runner components 464 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. 465 466 <procedure>(test-runner-pass-count runner)</procedure> 467 468 Returns the number of tests that passed, and were expected to pass. 469 470 <procedure>(test-runner-fail-count runner)</procedure> 471 472 Returns the number of tests that failed, but were expected to pass. 473 474 <procedure>(test-runner-xpass-count runner)</procedure> 475 476 Returns the number of tests that passed, but were expected to fail. 477 478 <procedure>(test-runner-xfail-count runner)</procedure> 479 480 Returns the number of tests that failed, and were expected to pass. 481 482 <procedure>(test-runner-skip-count runner)</procedure> 483 484 Returns the number of tests or test groups that were skipped. 485 486 <procedure>(test-runner-test-name runner)</procedure> 487 488 Returns the name of the current test or test group, as a string. During execution of test-begin 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. 489 490 <procedure>(test-runner-group-path runner)</procedure> 491 492 A list of names of groups we're nested in, with the outermost group first. 493 494 <procedure>(test-runner-group-stack runner)</procedure> 495 496 A list of names of groups we're nested in, with the outermost group last. 497 498 (This is more efficient than {{test-runner-group-path}}, since it doesn't require any copying.) 499 500 <procedure>(test-runner-aux-value runner)</procedure> 501 502 503 <procedure>(test-runner-aux-value! runner on-test)</procedure> 504 505 Get or set the {{aux-value}} field of a test-runner. This field is not used by this API or the test-runner-simple test-runner, but may be used by custom test-runners to store extra state. 506 507 <procedure>(test-runner-reset runner)</procedure> 508 509 Resets the state of the runner to its initial state. 510 ==== Example 511 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. 512 513 <enscript highlight="scheme"> 514 (define (my-simple-runner filename) 515 (let ((runner (test-runner-null)) 516 (port (open-output-file filename)) 517 (num-passed 0) 518 (num-failed 0)) 519 (test-runner-on-test-end! runner 520 (lambda (runner) 521 (case (test-result-kind runner) 522 ((pass xpass) (set! num-passed (+ num-passed 1))) 523 ((fail xfail) (set! num-failed (+ num-failed 1))) 524 (else #t)))) 525 (test-runner-on-final! runner 526 (lambda (runner) 527 (format port "Passing tests: ~d.~%Failing tests: ~d.~%" 528 num-passed num-failed) 529 (close-output-port port))) 530 runner)) 531 532 (test-runner-factory 533 (lambda () (my-simple-runner "/tmp/my-test.log"))) 534 </enscript> 535 === Implementation 536 The test implementation uses cond-expand ([[https://srfi.schemers.org/srfi-0/srfi-0.html|SRFI-0]]) to select different code depending on certain SRFI names (srfi-9, srfi-34, srfi-35, srfi-39), or implementations (kawa). It should otherwise be portable to any R5RS implementation. 537 538 [[https://srfi.schemers.org/srfi-64/testing.scm|testing.scm]] 539 === Examples 540 Here is [[https://srfi.schemers.org/srfi-64/srfi-25-test.scm|srfi-25-test.scm]], based converted from Jussi Piitulainen's [[https://srfi.schemers.org/srfi-25/test.scm|test.scm]] for [[https://srfi.schemers.org/srfi-25/srfi-25.html|SRFI-25]]. 541 === Test suite 542 Of course we need a test suite for the testing framework itself. This suite srfi-64-test.scm was contributed by Donovan Kolbly <donovan@rscheme.org>. 543 === Author 544 * Per Bothner <per@bothner.com> 545 === Copyright 546 Copyright (C) Per Bothner (2005, 2006) 547 548 Permission is hereby granted, free of charge, to any person obtaining 549 a copy of this software and associated documentation files (the 550 "Software"), to deal in the Software without restriction, including 551 without limitation the rights to use, copy, modify, merge, publish, 552 distribute, sublicense, and/or sell copies of the Software, and to 553 permit persons to whom the Software is furnished to do so, subject to 554 the following conditions: 555 556 The above copyright notice and this permission notice shall be 557 included in all copies or substantial portions of the Software. 558 559 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 560 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 561 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 562 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 563 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 564 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 565 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note: See TracChangeset
for help on using the changeset viewer.