[racket] rackunit vs test-engine

From: Joe Gilray (jgilray at gmail.com)
Date: Wed Aug 8 19:10:13 EDT 2012

OK, I get it... thanks for your patience.

On Wed, Aug 8, 2012 at 3:00 PM, Matthias Felleisen <matthias at ccs.neu.edu>wrote:

>
> #lang racket
>
> (provide drop to-energy)
>
> (module+ test
>   (require rackunit)
>   (define ε 1e-10))
>
> (define (drop t)
>   (* 1/2 9.8 t t))
>
> (module+ test
>   (define-test-suite test1
>     (check-= (drop 0) 0 ε)
>     (check-= (drop 10) 490 ε)))
>
> (define (to-energy m)
>   (* m (expt 299792458.0 2)))
>
> (module+ test
>   (define-test-suite test2
>     (check-= (to-energy 0) 0 ε)
>     (check-= (to-energy 1) 9e+16 1e+15)))
>
> (module+ test
>   (time (run-test (make-test-suite "both" (list test1 test2)))))
>
>
> -- Matthias
>
>
>
> On Aug 8, 2012, at 5:50 PM, Joe Gilray wrote:
>
> Sorry Matthias, but I don't understand the mechanics.  I've read the
> guide, but I don't see how to do what I'd like without timing outside of
> the module.
>
> To be clear, what do I add to the following file (from the docs) in order
> to time the duration of all the four checks combined?
>
> #lang<http://pre.racket-lang.org/docs/html/guide/Module_Syntax.html#(part._hash-lang)>
>  racket <http://pre.racket-lang.org/docs/html/reference/index.html> (
> module+<http://pre.racket-lang.org/docs/html/reference/module.html#(form._((lib._racket/private/base..rkt)._module+))>
>  test   (require<http://pre.racket-lang.org/docs/html/reference/require.html#(form._((lib._racket/private/base..rkt)._require))>
>  rackunit)   (define<http://pre.racket-lang.org/docs/html/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
>  ε 1e-10))   (provide<http://pre.racket-lang.org/docs/html/reference/require.html#(form._((lib._racket/private/base..rkt)._provide))>
>  drop          to-energy)   (define<http://pre.racket-lang.org/docs/html/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
>  (drop t)   (*<http://pre.racket-lang.org/docs/html/reference/generic-numbers.html#(def._((quote._~23~25kernel)._*))>
>  1/2 9.8 t t))   (module+<http://pre.racket-lang.org/docs/html/reference/module.html#(form._((lib._racket/private/base..rkt)._module+))>
>  test   (check-=<http://pre.racket-lang.org/docs/html/rackunit/api.html#(def._((lib._rackunit/main..rkt)._check-~3d))>
>  (drop 0) 0 ε)   (check-=<http://pre.racket-lang.org/docs/html/rackunit/api.html#(def._((lib._rackunit/main..rkt)._check-~3d))>
>  (drop 10) 490 ε))   (define<http://pre.racket-lang.org/docs/html/reference/define.html#(form._((lib._racket/private/base..rkt)._define))>
>  (to-energy m)   (*<http://pre.racket-lang.org/docs/html/reference/generic-numbers.html#(def._((quote._~23~25kernel)._*))>
>  m (expt<http://pre.racket-lang.org/docs/html/reference/generic-numbers.html#(def._((quote._~23~25kernel)._expt))>
>  299792458.0 2)))   (module+<http://pre.racket-lang.org/docs/html/reference/module.html#(form._((lib._racket/private/base..rkt)._module+))>
>  test   (check-=<http://pre.racket-lang.org/docs/html/rackunit/api.html#(def._((lib._rackunit/main..rkt)._check-~3d))>
>  (to-energy 0) 0 ε)   (check-=<http://pre.racket-lang.org/docs/html/rackunit/api.html#(def._((lib._rackunit/main..rkt)._check-~3d))>
>  (to-energy 1) 9e+16 1e+15))
> I know that running the file in DrRacket will run the tests, but I'd like
> to time them.
>
> Thanks (again!),
> -Joe
>
> On Wed, Aug 8, 2012 at 1:46 PM, Matthias Felleisen <matthias at ccs.neu.edu>wrote:
>
>>
>> The submodules are woven together so wrapping (test) with (time ...)
>> should work fine.
>>
>>
>> On Aug 8, 2012, at 4:18 PM, Joe Gilray wrote:
>>
>> Thanks Matthias,
>>
>> Is there an easy way to "internally" time the duration of all tests?
>>  with test-engine I could use (time (test)), with all the tests in a test
>> submodule do I have to do the timing externally?  I.e "TimeThis raco test
>> myfile.rkt"?
>>
>> -Joe
>>
>> On Wed, Aug 8, 2012 at 6:31 AM, Matthias Felleisen <matthias at ccs.neu.edu>wrote:
>>
>>>
>>> This first example shows how to use module+ test with test-engine:
>>>
>>> #lang racket
>>>
>>> (module+ test
>>>   (require test-engine/racket-tests))
>>>
>>> ;; Int -> Int
>>> ;; adds 2 to n
>>>
>>> (module+ test ;; setting up examples before you define the function
>>>   (check-expect (add2 3) 4)
>>>   (check-expect (add2 3) 5))
>>>
>>> (define (add2 n)
>>>   (+ n 3))
>>>
>>> (module+ test
>>>   (test)) ;; calling this function controls when you run the 'test suite'
>>>
>>> All you need to know is that drracket requires submodules named test
>>> when you run the program, though this default can be changed via the
>>> language preference (see submodules to run, drop down menu). At the command
>>> line, racket test foo.rkt will require the test submodules but otherwise
>>> they are not run.
>>>
>>> ;; ---
>>>
>>> This second example translates the first to rackunit:
>>>
>>> #lang racket
>>>
>>> (module+ test
>>>   (require rackunit))
>>>
>>> ;; Int -> Int
>>> ;; adds 2 to n
>>>
>>> (module+ test
>>>   (check-equal? (add2 3) 4)
>>>   (check-equal? (add2 3) 5))
>>>
>>> (define (add2 n)
>>>   (+ n 2))
>>>
>>> The tests are always run when you require the test submodule (see
>>> above).
>>>
>>> ;; ---
>>>
>>> With rackunit, you can also define test-suites (see docs, especially
>>> define/provide-test-suite. You compose these test suites, provide them, and
>>> run them if and when you wish by loading the proper module.
>>>
>>> Please search for an earlier post of mine where I explain a specific
>>> arrangement of separate modules to make all of this convenient.
>>>
>>> With submodules, you can stick these test suites into submodules and
>>> require those in some global test module.
>>>
>>> -- Matthias
>>>
>>>
>>>
>>>
>>>
>>> On Aug 7, 2012, at 10:31 PM, Joe Gilray wrote:
>>>
>>> Hi Matthias,
>>>
>>> I will take you up on your offer of an example... thanks!
>>>
>>> I've read about test-suite and test-case, but I'm not sure of the best
>>> way to test each utility in a file.
>>>
>>> Ideally the tests would be grouped with the functions:
>>>
>>> (define f1 ...)
>>> (module+ test
>>>   (test-equal? "f1-tests" (f1 1 2) 1)
>>>   (test-equal? "f1-tests" (f1 3 4) 4))
>>>
>>> (define f2 ...)
>>> (module+ test
>>>   (test-equal? "f2-tests" (f2 1 2) 1)
>>>   (test-equal? "f2-tests" (f2 3 4) 4))
>>>
>>> etc.
>>>
>>> I believe that the above scheme would work and run every time the
>>> enclosing file/module is run... right?
>>>
>>> What if I want to control when all the tests are run?  Can I somehow
>>> build a trigger to fire off all the tests?  From the docs it looks like
>>> this is the purpose of test-suite, but I don't know the mechanics when the
>>> test cases are spread out in the file... maybe that isn't allowed and I
>>> will need to group the tests?
>>>
>>> Thanks again,
>>> -Joe
>>>
>>> On Tue, Aug 7, 2012 at 6:00 PM, Matthias Felleisen <matthias at ccs.neu.edu
>>> > wrote:
>>>
>>>>
>>>> On Aug 7, 2012, at 8:24 PM, Joe Gilray wrote:
>>>>
>>>> > Now that 5.3 is out, I've been reading about submodules and their
>>>> support for testing.  In the past I used test-engine/racket-tests for
>>>> testing.
>>>> >
>>>> > Can someone please give me a rundown of when to use rackunit and
>>>> advantages/disadvantages of test-engine and rackunit?
>>>>
>>>> -- test-engine provides test support for the teaching languages of
>>>> DrRacket.
>>>> -- rackunit is for 'adult' programmers, meaning programmers who have
>>>> outgrown teaching languages.
>>>>
>>>> You can still use test-engine in plain #lang racket, and you could use
>>>> rackunit in teaching languages.
>>>>
>>>> You can also use both with submodules especially (module+ test ...).
>>>> Holler if you need examples -- Matthias
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120808/a55f163f/attachment-0001.html>

Posted on the users mailing list.