ozten ([info]ozten) wrote,
@ 2007-09-17 20:40:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:eval scheme apply learnings, libraries, lisp, scheme, unit testing

Unit Testing Scheme code
Today I played around with three testing frameworks for PLT DrScheme.

SRFI-78 "check.ss", SRFI-64 "testing.ss", and lastly SchemeUnit "test.ss".

I started with SRFI-78 since it was a more recent standard. It has a very minimal API.
(require (lib "check.ss" "srfi" "78"))
(check-set-mode! 'report-failed ) ;report-failed)
(load "image-filters.scm")
(check (get-nth-filter 0) => identity-filter)
(check (get-nth-filter 1) => sharpen-filter)
(check (get-nth-filter 1) => identity-filter)
(check-report)

(get-nth-filter 1)
=> ((0 -2 0 -2 11 -2 0 -2 0) 3 0)
; *** failed ***
; expected result: ((1 1 1 1 1 1 1 1 1) 9 0)


; *** checks *** : 2 correct, 1 failed. First failed example:

(get-nth-filter 1)
=> ((0 -2 0 -2 11 -2 0 -2 0) 3 0)
; *** failed ***
; expected result: ((1 1 1 1 1 1 1 1 1) 9 0)


Loading the file causes the tests to be run. I didn't like the output, but found "check-set-model" would keep it to only reporting failed tests. This is pretty good, but very minimal.

As I read more, I realized that this library wasn't the only SRFI testing standard, so I also loaded up 64 and gave it a shot.

(require (lib "testing.ss" "srfi" "64"))
(load "image-filters.scm")
(test-begin "Image Filters")
(test-eqv identity-filter (get-nth-filter 0))
(test-eqv sharpen-filter (get-nth-filter 1))
(test-eqv identity-filter (get-nth-filter 1))
(test-error "Bad input" (get-nth-filter 100))
(test-end)

Again, loading this fail causes the test runner to run with the results:
%%%% Starting test Image Filters (Writing full log to "Image Filters.log")
:14: FAIL
Nth filter : 100
# of expected passes 3
# of unexpected failures 1

Both of these libraries dis a xUnit style library SchemeUnit, so I had to give this one a go also.

(require (planet "test.ss" ("schematics" "schemeunit.plt" 2)))
(load "image-filters.scm")

(define file-tests
(test-suite
"Test image-filters.scm"
(test-equal? "First element is zero indexed" (get-nth-filter 0) identity-filter)
(test-equal? "Normal case" (get-nth-filter 1) sharpen-filter)
(test-equal? "Pretend error" (get-nth-filter 1) identity-filter )))

Which comes with two test runners, a text and gui. Let's run them both...

Text Test Runner

Test image-filters.scm > Pretend error
Pretend error has a FAILURE
name: check-equal?
location: #>struct:object:...pper stepper-tool.ss:618:8=""<:31:3
actual: ((0 -2 0 -2 11 -2 0 -2 0) 3 0)
expected: ((1 1 1 1 1 1 1 1 1) 9 0)
2 success(es) 1 failure(s) 0 error(s) 3 test(s) run
1

and Gui Test Runner
SchemeUnit

I think SRFI-64 and SchemeUnit are both usable. I am more familiar with xUnit APIs so I am going to start coding against SchemeUnit for now.




Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…