Test Support
1 Using Check Forms
(require test-engine/racket-tests) | package: htdp-lib |
This module provides test forms for use in Racket programs, as well as parameters to configure the behavior of test reports.
Each check form may only occur at the top-level; results are collected and reported by the test function. Note that the check forms only register checks to be performed. The checks are actually run by the test function. Furthermore, syntax errors in check forms are intentionally delayed to run time so that students can write tests without necessarily writing complete function headers.
syntax
(check-expect expr expected-expr)
It is an error for expr or expected-expr to produce a function value or an inexact number.
syntax
(check-random expr expected-expr)
The form supplies the same random-number generator to both parts. If both parts request random numbers from the same interval in the same order, they receive the same random numbers.
> (check-random (random 10) (random 10))
> (check-random (begin (random 100) (random 200)) (begin (random 100) (random 200))) > (test) Both tests passed!
If the two parts call random for different intervals, they are likely to fail:
> (check-random (begin (random 100) (random 200)) (begin (random 200) (random 100))) > (test)
Ran 1 test.
0 tests passed.
Check failures:
┌─────┐ ┌────┐
Actual value │ 187 │ differs from │ 93 │, the expected value.
└─────┘ └────┘
at line 2, column 0
It is an error for expr or expected-expr to produce a function value or an inexact number.
syntax
(check-satisfied expr property?)
> (check-satisfied 1 odd?) > (check-satisfied 1 even?) > (test)
Ran 2 tests.
1 of the 2 tests failed.
Check failures:
┌───┐
Actual value │ 1 │ does not satisfy even?.
└───┘
at line 3, column 0
Changed in version 1.1 of package htdp-lib: allow the above examples to run in BSL and BSL+
syntax
(check-within expr expected-expr delta-expr)
delta-expr : number?
It is an error for expr or expected to produce a function value.
syntax
(check-error expr)
(check-error expr msg-expr)
msg-expr : string?
syntax
(check-member-of expr expected-expr ...)
It is an error for expr or any of the expected-exprs to produce a function value or an inexact number.
syntax
(check-range expr min-expr max-expr)
expr : number?
min-expr : number?
max-expr : number?
syntax
(test)
parameter
(test-silence) → boolean?
(test-silence silence?) → void? silence? : any/c
parameter
(test-execute) → boolean?
(test-execute execute?) → void? execute? : any/c
2 Running Tests and Inspecting Test Results
(require test-engine/test-engine) | package: htdp-lib |
This module defines language-agnostic procedures for running test code to execute checks, and recording and inspecting their results.
A test is a piece of code run for testing, a check is a single assertion within that code: Typically the tests are first registered, then they are run, and then their results are inspected. Both tests and the results of failed checks are recorded in a data structure called a test object. There is always a current test object associated with the current namespace.
struct
(struct test-object ( tests successful-tests failed-checks signature-violations)) tests : (listof (-> boolean?)) successful-tests : (listof (-> boolean?)) failed-checks : (listof failed-check?) signature-violations : (listof signature-violation?)
The first one is the list of tests (each represented by a thunk), the others are succeeded tests, failed checks and signature violations, respectively.
The thunks are expected to always run to completion. They shouöd return #t upon success, and #f upon failure.
procedure
thunk : (-> boolean?)
struct
(struct unexpected-error fail-reason (srcloc expected exn))
srcloc : srcloc? expected : any/c exn : exn?
struct
(struct unexpected-error/markup unexpected-error ( srcloc expected exn error-markup)) srcloc : srcloc? expected : any/c exn : exn? error-markup : markup?
struct
(struct unequal fail-reason (srcloc actual expected))
srcloc : srcloc? actual : any/c expected : any/c
struct
(struct not-within fail-reason (srcloc actual expected range))
srcloc : srcloc? actual : any/c expected : any/c range : real?
struct
(struct incorrect-error fail-reason (srcloc expected exn))
srcloc : srcloc? expected : any/c exn : exn?
struct
(struct incorrect-error/markup incorrect-error ( srcloc expected exn error-markup)) srcloc : srcloc? expected : any/c exn : exn? error-markup : markup?
struct
(struct expected-error fail-reason (srcloc message value))
srcloc : srcloc? message : (or/c #f string?) value : any/c
struct
(struct not-mem fail-reason (srcloc actual set))
srcloc : srcloc? actual : any/c set : (listof any/c)
struct
(struct not-range fail-reason (srcloc actual min max))
srcloc : srcloc? actual : real? min : real? max : real?
struct
(struct satisfied-failed fail-reason (srcloc actual name))
srcloc : srcloc? actual : any/c name : string?
struct
(struct unsatisfied-error fail-reason (srcloc name exn))
srcloc : srcloc? name : string? exn : exn?
struct
(struct unsatisfied-error/markup unsatisfied-error ( srcloc name exn error-markup)) srcloc : srcloc? name : string? exn : exn? error-markup : markup?
struct
(struct violated-signature fail-reason ( srcloc obj signature blame-srcloc)) srcloc : srcloc? obj : any/c signature : signature? blame-srcloc : (or/c #f srcloc?)
struct
(struct signature-got (value))
value : any/c
struct
(struct signature-violation ( obj signature message srcloc blame-srcloc)) obj : any/c signature : signature? message : (or/c string? signature-got?) srcloc : (or/c #f srcloc?) blame-srcloc : (or/c #f srcloc?)
3 Printing Test Results
This module is responsible for output of test results: Where the output goes, and some aspects of the formatting can be customized via parameters.
(require test-engine/test-markup) | package: htdp-lib |
parameter
(render-value-parameter) → (any/c . -> . string?)
(render-value-parameter render-value-proc) → void? render-value-proc : (any/c . -> . string?)
parameter
(display-test-results-parameter) → (markup? . -> . any)
(display-test-results-parameter display-test-proc) → void? display-test-proc : (markup? . -> . any)
procedure
(display-test-results! markup) → any
markup : markup?
parameter
→ (exn? . -> . string?) (get-rewritten-error-message-parameter get-rewritten-error-message-proc) → void? get-rewritten-error-message-proc : (exn? . -> . string?)
procedure
(get-rewritten-error-message exn) → string?
exn : exn?
procedure
(test-object->markup test-object) → markup?
test-object : test-object?