UNB/ CS/ David Bremner/ teaching/ cs4613/ tutorials/ Testing in racket

# Unit Tests in Racket

Prerequisites
recursion, modules

This part is based on an example from RackUnit QuickStart. The Beautiful Racket unit test explainer is a better reference, since we'll skip some of the fancier features of `rackunit` and move straight to having a test submodule.

• Save the following as "arith.rkt"
```#lang racket/base

(define (my-+ a b)
(if (zero? a)
b

(define (my-* a b)
(if (zero? a)
b
(my-* (sub1 a) (my-+ b b))))

(provide my-+
my-*)
```
• add a test submodule with the following tests
```(check-equal? (my-+ 1 1) 2 "Simple addition")
(check-equal? (my-* 1 2) 2 "Simple multiplication")```

• run your "arith.rkt" in `DrRacket`; observe that one of the two tests fails.

• Fix the recursive definition of my-* so that it at least works for non-negative integers. There are several different approaches which work, one simple way is based on the equations

``````  0 * b = 0                     (base case)
a * b = (a - 1) * b + b       (recursion)
``````
• Observe there is no output from the tests now when you run the code in `DrRacket`. This means success.

# Test Coverage

• This activity continues with the same file from the previous activity.

• Under `Language -> Choose Language -> Show Details -> Dynamic Properties`, enable

``````⊙ Syntactic test suite coverage
``````
• run your code in `DrRacket` again

• most likely you will have some code highlighted with orange text and black foreground. This means that code is not covered by your test suite. Add another test to cover each piece of uncovered code.

In this course, for all `racket` assignments you will lose marks if you don't have complete test coverage.