[plt-scheme] Setup and teardown in SchemeUnit

From: Michał Kwiatkowski (constant.beta at gmail.com)
Date: Sun Nov 30 18:17:28 EST 2008

2008/11/30 Noel Welsh <noelwelsh at gmail.com>:
> I'm heading to sleep, so quickly:
>
> Either:
>
> (check-equal? (before (display "before\n") 0) 0)
>
> Or make the abstraction:
>
> (define (printing-check-equal? v1 v2)
>  (before (display "before\n")
>              (check-equal? v1 v2)))
>
> I'm a bit surprised you want before/after when using checks.  I
> thought they'd only be useful when using test-begin/test-case.

No, you misunderstood me. Sample code was really only a sample code.
:-) I *do* want to use them with test-case.

I want this because I have a piece of setup code that needs to be run
for some subset of tests. Each test needs a clean state, so "before"
macro doesn't work for me here. To draw an example from a different
language, a Python test runner seeing this class:

class TestSuite:
    def setup(self):
        # ...
    def teardown(self):
        # ...
    def test_something(self):
        # ...
    def test_something_else(self):
        # ...

would run tests like that:

  setup()
  test_something()
  teardown()
  setup()
  test_something_else()
  teardown()

As you see, setup & teardown pairs are executed before & after each
test, setting up a well-defined context (a fresh fixture) for each
test case. This is a fairly common testing pattern and probably a main
reason to put test cases into test suites.

"before" and "after" macros in SchemeUnit run given code only once, so
they are no good in this context.

Cheers,
mk


Posted on the users mailing list.