[racket] Rackunit best practices

From: Greg Hendershott (greghendershott at gmail.com)
Date: Thu Aug 22 18:14:25 EDT 2013

In my opinion the "most Rackety" way to do tests these days is what
Laurent mentioned: The `test` submodule.

1. It's easy to run the tests in foo.rkt file: `raco test -x foo.rkt`.

2. It's easy to write the tests -- no extra source file required.

3. You can even write tests adjacent to the definitions of the
functions they test. Roughly like how "literate programming" can
alternate prose and code, I suppose you could call this "literate
testing".

(module+ test
  (require rackunit))


(define (foo)
  ....)

(module+ test
  ;; tests for foo
  ))


(define (bar)
  ....)

(module+ test
  ;; tests for bar
  ))

;; and so on ...

(Although when I do this, occasionally I wish for "test-folding" to
hide the tests temporarily. Neat feature to add to DrRacket and/or
emacs Racket mode?)


Given that, I'm not familiar enough with Ruby "guard" to know what's
left for a Racket "guard" to do -- just the detection for re-running
some test automatically? As Laurent mentioned DrRacket already will do
that automatically as you edit each file. For users of other editors,
or for other scenarios like fetching from git, I suppose this would
amount to keeping some cache of the last-modified or MD5 of all the
sources, and running `raco test` ones that have changed?

But as I said there might be more to Ruby "guard", and which would be
worth doing, so please forgive my ignorance of that.


On Thu, Aug 22, 2013 at 2:27 AM, Laurent <laurent.orseau at gmail.com> wrote:
> Quickly: It is common to use a  submodule for tests, in the same source
> file:
> (module+ test
>   (require rackunit)
>   ....)
>
> then running `raco test <file.rkt> ...` will run all the tests. Such tests
> are also automatically run in DrRacket when you press run (along with the
> 'main' submodule).
>
> For packages that have a collection A, it is also common (and considered
> good practice) to have another collection 'test' with a subdirectory for A
> and its tests.
> See for example the xml-rpc repo: https://github.com/jeapostrophe/xml-rpc
> This is partly intended for use with DrDr (that runs `raco test`), a tool
> written by Jay for the development of Racket. I don't know much more about
> it, but other people on this list do.
>
> Laurent
>
>
> On Thu, Aug 22, 2013 at 7:54 AM, Chad Albers <calbers at neomantic.com> wrote:
>>
>> As a Ruby engineer, we have a wonderful tool called "guard" that
>> automatically runs our unit-tests every time a test-case changes or
>> every-time a source file changes.
>>
>> I have written a guard plugin that performs the same task for Racket's
>> rackunit.  Currently, it pretty primitive - guard runs all tests if
>> anything changes - and it makes several assumptions about how tests
>> are structured.
>>
>> guard's ruby unit test plug works so well because there is a set of
>> informal conventions that Ruby developers follow, and guard's ruby
>> plugin works so well because it can counts on those conventions.
>>
>> Before I release "guard-rackunit" to the general public, I am
>> wondering if similar conventions exist for rackunit.
>>
>> For instance, in my Racket project, the following conditions hold true
>> 1. all tests are located at the root of the project in a directory call
>> "tests"
>> 2. in the "tests" directory, I have a "test-runner.rkt" file that
>> performs all '(run-tests *).
>> 3. Each test-suite file contains one 'provide' which corresponds to
>> the name of the test-suite
>> 4. Each test-suite file is name after the file that it tests and
>> appends "-test.rkt"; eg. foo.rkt corresponds to foo-test.rkt
>>
>> If the Racket community follows a different set of conventions, I can
>> change my guard plugin to follow those conventions, and only have
>> guard run a test suite for the file it tests.
>>
>> Let me know 1) if there is any interest in this project, and 2) a set
>> of conventions that I can generally depend upon.
>>
>> Thanks,
>> Chad
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

Posted on the users mailing list.