guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Failing fast in SRFI-64 test groups


From: Taylan Ulrich Bayırlı/Kammer
Subject: Re: Failing fast in SRFI-64 test groups
Date: Sun, 13 Aug 2017 13:35:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux)

Christopher Baines <address@hidden> writes:

> Hey,
>
> If each test in a group is effectively dependent on all the tests
> before it, then it would be useful to abort the group if any tests
> fail. I had a read through the docs, and a quick look at the code, but
> didn't spot a way of doing this (short of implementing a custom test
> runner). Is if possible to do this easily?
>
> Thanks,
>
> Chris

You could define a test runner with a custom on-test-end procedure that
throws an exception if the result was a failure, and wrap all your
test-group expressions in a corresponding exception handler, possibly
with a macro to cut some boilerplate.

    (define test-group-abort 'test-group-abort)
    
    (define my-test-runner (test-runner-simple))
    (test-runner-on-test-end!
      my-test-runner
      (lambda (runner)
        (test-on-test-end-simple runner)  ;call default implementation
        (when (not (test-passed? runner))
          (throw test-group-abort))))

    (define-syntax test-group+catch
      (syntax-rules ()
        ((_ <name> <expr> ...)
         (catch test-group-abort
           (lambda ()
             (test-group <name> <expr> ...))
           (lambda (key)
             #f)))))

    ;; usage example:
    (test-runner-current my-test-runner)
    (test-begin "foo")
    (test-group+catch "bar"
      (test-assert "failure" #f)
      (test-assert "success" #t))
    (test-end "foo")
    ;passed: 0
    ;failed: 1

If using Emacs, you can run the following elisp code to make Emacs use
correct indentation for 'test-group+catch':

    (put 'test-group+catch 'scheme-indent-function 1)

Taylan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]