forked from caiorss/Functional-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest_Hunit.org
More file actions
628 lines (484 loc) · 17.6 KB
/
Copy pathUnitTest_Hunit.org
File metadata and controls
628 lines (484 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#+INCLUDE: ../theme/style.org
#+TITLE: Unit Testing with HUNit
* Unit Testing with HUnit
** Overview - HUnit functions and types
The package HUnit provides unit testing facilities for Haskell. It is
inspired by JUnit tool for Java.
Documentation: [[http://hackage.haskell.org/package/HUnit][HUnit: A unit testing framework for Haskell]]
*Install HUnit*
#+BEGIN_SRC haskell
$ stack install HUnit
#+END_SRC
*Assert Functions*
| Function | | Signature |
| | | |
| assert | :: | Assertable t => t -> Assertion |
| assertFailure | :: | assertFailure :: String -> Assertion |
| assertBool | :: | String -> Bool -> Assertion |
| assertEqual | :: | (Show a, Eq a) => String -> a -> a -> Assertion |
| assertionPredicate | :: | AssertionPredicable t => t -> AssertionPredicate |
| assertString | :: | String -> Assertion |
| | | |
*Types*
Type _Assertion_
#+BEGIN_SRC haskell
type Assertion = IO ()
#+END_SRC
Type: _Count_
#+BEGIN_SRC haskell
data Counts = Counts { cases, tried, errors, failures :: Int }
deriving (Eq, Show, Read)
#+END_SRC
Type: _Test_
#+BEGIN_SRC haskell
data Test = TestCase Assertion
| TestList [Test]
| TestLabel String Test
#+END_SRC
*Functions*
_assertFailure_ displays an error message.
#+BEGIN_SRC haskell
assertFailure :: String -> Assertion
assertFailure msg = ioError (userError ("HUnit:" ++ msg))
#+END_SRC
Example:
#+BEGIN_SRC haskell
> assertFailure "It is going to fail. Fatal Kernel error ... error code 0x42343"
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 4, locationColumn = 1}))
"It is going to fail. Fatal Kernel error ... error code 0x42343"
>
>
#+END_SRC
_assertBool_ shows an error message if the condition fails.
#+BEGIN_SRC haskell
assertBool :: String -> Bool -> Assertion
assertBool msg b = unless b (assertFailure msg)
#+END_SRC
Example:
#+BEGIN_SRC haskell
> assertBool "It should be true" True
> assertBool "It should be true" False
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 10, locationColumn = 1}))
"It should be true"
>
#+END_SRC
_assertString_ shows an error message if the the input string is not empty.
#+BEGIN_SRC haskell
assertString :: String -> Assertion
assertString s = unless (null s) (assertFailure s)
#+END_SRC
Example:
#+BEGIN_SRC haskell
> assertString "hello world"
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 19, locationColumn = 1}))
"hello world"
>
> assertString ""
>
#+END_SRC
_assertEqual_ shows an error message if the input values are not equal.
#+BEGIN_SRC haskell
assertEqual :: (Eq a, Show a) => String -> a -> a -> Assertion
assertEqual preface expected actual =
unless (actual == expected) (assertFailure msg)
where msg = (if null preface then "" else preface ++ "\n") ++
"expected: " ++ show expected ++ "\n but got: " ++ show actual
#+END_SRC
Example:
#+BEGIN_SRC haskell
> assertEqual "10 + 15 should be equal to 25" 25 (10 + 15)
>
> assertEqual "10 + 15 should be equal to 35" 35 (10 + 15)
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 17, locationColumn = 1}))
"10 + 15 should be equal to 35\nexpected: 35\n but got: 25"
>
#+END_SRC
*Run the test cases*
#+BEGIN_SRC haskell
> :t runTestTT
runTestTT :: Test -> IO Counts
#+END_SRC
Type Test
#+BEGIN_SRC haskell
data Test = TestCase Assertion
| TestList [Test]
| TestLabel String Test
#+END_SRC
Example: Running a single test
#+BEGIN_SRC haskell
> runTestTT (TestCase $ assertEqual "sum should be equal to 25" 25 (10 + 15))
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
> runTestTT (TestCase $ assertEqual "sum should be equal to 25" 25 (50 + 15))
### Failure:
<interactive>:161
sum should be equal to 25
expected: 25
but got: 65
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
#+END_SRC
Example: Running a single test with label
#+BEGIN_SRC haskell
> let testEqual25 = TestLabel "testEqual25" (TestCase $ assertEqual "sum should be equal to 25" 25 (10 + 15))
>
> runTestTT testEqual25
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
> let testEqual25_2 = TestLabel "testEqual25" (TestCase $ assertEqual "sum should be equal to 25" 25 (20 + 15))
>
> testEqual25_2
TestLabel testEqual25 TestCase _
>
> runTestTT testEqual25_2
### Failure in: testEqual25
<interactive>:153
sum should be equal to 25
expected: 25
but got: 35
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
>
#+END_SRC
Example: Running multiple test cases.
#+BEGIN_SRC haskell
> let testTrue = TestCase $ assertBool "It should be true" True
> let testFalse = TestCase $ assertBool "It is gonna fail." False
> let testEqual25 = TestCase $ assertEqual "The sum should be equal to 25" 25 (10 + 15)
> let testEqual25Fail = TestCase $ assertEqual "The sum should be equal to 25" 25 (150 + 25)
>
> runTestTT $ TestList [testTrue, testFalse, testEqual25, testEqual25Fail]
### Failure in: 1
<interactive>:164
It is gonna fail.
### Failure in: 3
<interactive>:166
The sum should be equal to 25
expected: 25
but got: 175
Cases: 4 Tried: 4 Errors: 0 Failures: 2
Counts {cases = 4, tried = 4, errors = 0, failures = 2}
>
:{
runTestTT $ TestList [TestLabel "testTrue" testTrue,
TestLabel "testFalse" testFalse,
TestLabel "testEqual25" testEqual25,
TestLabel "testEqual25Fail" testEqual25Fail
]
:}
-- Pasting this code in the terminal:
> :{
- runTestTT $ TestList [TestLabel "testTrue" testTrue,
- TestLabel "testFalse" testFalse,
- TestLabel "testEqual25" testEqual25,
- TestLabel "testEqual25Fail" testEqual25Fail
- ]
- :}
### Failure in: 1:testFalse
<interactive>:164
It is gonna fail.
### Failure in: 3:testEqual25Fail
<interactive>:166
The sum should be equal to 25
expected: 25
but got: 175
Cases: 4 Tried: 4 Errors: 0 Failures: 2
Counts {cases = 4, tried = 4, errors = 0, failures = 2}
>
#+END_SRC
*Operators*
| Description | Operator | | Signature |
|-------------------------------+----------+----+-----------------------------------------------------------|
| Assert Bool (True) | (@?) | :: | (AssertionPredicable t) => t -> String -> Assertion |
| | | | |
| Assert Equal | (@=?) | :: | (Show a, Eq a) => a: expected -> a: value -> Assertion |
| Assert Equal | (@?=) | :: | (Eq a, Show a) => a: value -> a: expected -> Assertion |
| | | | |
| | | | |
| Creates test case with label | (~:) | :: | Testable t => String -> t -> Test |
| | | | |
| Creates an equality test case | (~?=) | :: | (Show a, Eq a) => a: value -> a: expected -> Test |
| Creates an equality test case | (~=?) | :: | (Show a, Eq a) => a: expected -> a: value -> Test |
| | | | |
Example: (@=?)
#+BEGIN_SRC haskell
> :t (@=?)
(@=?) :: (Show a, Eq a) => a -> a -> Assertion
>
> 10 @=? (1 + 2 + 3 + 4)
> 10 @=? (1 + 2 + 3 + 4 + 5)
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 46, locationColumn = 1})) "expected: 10\n but got: 15"
>
> 120 @=? product [1, 2, 3, 4, 5]
> 120 @=? product [1, 2, 3, 4, 5, 6]
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 58, locationColumn = 1})) "expected: 120\n but got: 720"
>
#+END_SRC
Example: (@?)
#+BEGIN_SRC haskell
> 100 > 10 @? "It should be true"
> 100 < 10 @? "It should be true"
*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 286, locationColumn = 1})) "It should be true"
>
#+END_SRC
Example: (~:)
#+BEGIN_SRC haskell
> :t (~:)
(~:) :: Testable t => String -> t -> Test
>
> let test1 = "testSumIs25" ~: assertEqual "The sum is 25" 25 (10 + 15)
> runTestTT test1
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
> let test2 = "testSumIs25" ~: assertEqual "The sum is 25" 25 (13 + 15)
> runTestTT test2
### Failure in: testSumIs25
<interactive>:223
The sum is 25
expected: 25
but got: 28
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
> let test3 = "testSumIs25" ~: 25 @=? (10 + 15)
> runTestTT test3
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
> let test4 = "testSumIs25" ~: 25 @=? (1023 + 15)
> runTestTT test4
### Failure in: testSumIs25
<interactive>:234
expected: 25
but got: 1038
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
#+END_SRC
Example: (~?=)
#+BEGIN_SRC haskell
> runTestTT $ 20 ~?= 20
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
> runTestTT $ 10 ~?= 20
### Failure:
<interactive>:276
expected: 20
but got: 10
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
#+END_SRC
Example: (~=?)
#+BEGIN_SRC haskell
> runTestTT $ 10 ~=? 20
### Failure:
<interactive>:281
expected: 10
but got: 20
Cases: 1 Tried: 1 Errors: 0 Failures: 1
Counts {cases = 1, tried = 1, errors = 0, failures = 1}
>
> runTestTT $ 20 ~=? 20
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
>
#+END_SRC
#+BEGIN_SRC haskell
import Test.HUnit
import System.IO
:{
fact 1 = 1
fact n = n * fact (n - 1)
:}
:{
tests = TestList
[ "fact 1" ~: fact 1 ~?= 1
, "fact 2" ~: fact 2 ~?= 2
, "fact 3" ~: fact 3 ~?= 6
, "fact 4" ~: fact 4 ~?= 24
, "fact 5" ~: fact 5 ~?= 120
]
:}
> runTestTT tests
Cases: 5 Tried: 5 Errors: 0 Failures: 0
Counts {cases = 5, tried = 5, errors = 0, failures = 0}
>
#+END_SRC
** HUnit in the REPL
Example using =$ stack ghci=
#+BEGIN_SRC haskell
> import Test.HUnit
>
> :info Counts
data Counts
= Counts {cases :: Int,
tried :: Int,
errors :: Int,
failures :: Int}
-- Defined in ‘Test.HUnit.Base’
instance [safe] Eq Counts -- Defined in ‘Test.HUnit.Base’
instance [safe] Read Counts -- Defined in ‘Test.HUnit.Base’
instance [safe] Show Counts -- Defined in ‘Test.HUnit.Base’
>
> :info TestCase
data Test = TestCase Assertion | ...
-- Defined in ‘Test.HUnit.Base’
> :t TestCase
TestCase :: Assertion -> Test
> :info Assertion
type Assertion = IO () -- Defined in ‘Test.HUnit.Lang’
>
>
> :{
- addInt :: Int -> Int -> Int
- addInt x y = x + y
- :}
>
> addInt 10 20
30
>
> import Test.HUnit
>
> :t TestCase
TestCase :: Assertion -> Test
>
> assertEqual "add 2 10 should be 12" 12 (add 10 2)
>
> assertEqual "add 2 10 should be 12" 0 (add 10 2)
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 23, locationColumn = 1}))
"add 2 10 should be 12\nexpected: 0.0\n but got: 12.0"
>
> assertBool "It should be true" True
> assertBool "It should be true" False
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 50, locationColumn = 1}))
"It should be true"
>
>
> assertBool "It should be true" (10 > 5)
> assertBool "It should be true" (10 < 5)
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 54, locationColumn = 1}))
"It should be true"
>
:{
testFile filename msg = do
writeFile filename msg
content <- readFile filename
assertEqual ("File content should be equal to \"" ++ msg ++ "\"") msg content
:}
> testFile "/tmp/testfile1" "hello world"
>
:{
testFile2 filename msg msgExpect = do
writeFile filename msg
content <- readFile filename
assertEqual ("File content should be equal to \"" ++ msg ++ "\"") msgExpect content
:}
> testFile2 "/tmp/testfile2" "hello world" "hello world"
> testFile2 "/tmp/testfile2" "hello world" "hello"
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 90, locationColumn = 3}))
"File content should be equal to \"hello world\"\nexpected: \"hello\"\n but got: \"hello world\""
>
> testFile2 "/afile.txt" "hello" "hello"
,*** Exception: /afile.txt: openFile: permission denied (Permission denied)
>
> runTestTT $ TestCase $ testFile2 "/afile.txt" "hello" "hello"
### Error:
/afile.txt: openFile: permission denied (Permission denied)
Cases: 1 Tried: 1 Errors: 1 Failures: 0
Counts {cases = 1, tried = 1, errors = 1, failures = 0}
>
> import Text.Read (readMaybe)
>
> assertEqual "The parsed string should be equal to 100" (Just 100) ((readMaybe "100") :: Maybe Int)
>
> assertEqual "The parsed string should be equal to 100" (Just 100) ((readMaybe "1asd00") :: Maybe Int)
,*** Exception: HUnitFailure (Just (Location {locationFile = "<interactive>", locationLine = 115, locationColumn = 1}))
"The parsed string should be equal to 100\nexpected: Just 100\n but got: Nothing"
>
> let testSum = TestCase $ assertEqual "10 + 5 = 15" 15 (10 + 5)
> let testProd = TestCase $ assertEqual "10 * 15" 150 (10 * 15)
> let testPred = TestCase $ assertBool "10 > 5" (10 > 5)
>
> let testFailure = TestCase $ assertEqual "It will fail 10 + 2 = 15" (10 + 2) 15
:{
testlist = TestList [TestLabel "testSum" testSum,
TestLabel "testPred" testPred,
TestLabel "testFailure" testFailure,
TestLabel "testProd" testProd
]
:}
> runTestTT testlist
### Failure in: 2:testFailure
<interactive>:94
It will fail 10 + 2 = 15
expected: 12
but got: 15
Cases: 4 Tried: 4 Errors: 0 Failures: 1
Counts {cases = 4, tried = 4, errors = 0, failures = 1}
>
:{
testlist2 = TestList [TestLabel "testSum" testSum,
TestLabel "testPred" testPred,
-- TestLabel "testFailure" testFailure,
TestLabel "testProd" testProd
]
:}
> runTestTT testlist2
Cases: 3 Tried: 3 Errors: 0 Failures: 0
Counts {cases = 3, tried = 3, errors = 0, failures = 0}
>
#+END_SRC
** Example: Unit test with HUnit
#+BEGIN_SRC haskell :tangle /tmp/uniTestExample.hs
-- File: unitTestExample.hs
import Test.HUnit
testSum = TestCase $ assertEqual "10 + 5 = 15" 15 (10 + 5)
testProd = TestCase $ assertEqual "10 * 15" 150 (10 * 15)
testPred = TestCase $ assertBool "10 > 5" (10 > 5)
testFailure = TestCase $ assertEqual "It will fail 10 + 2 = 15" (10 + 2) 15
testlist = TestList [TestLabel "testSum" testSum,
TestLabel "testPred" testPred,
TestLabel "testFailure" testFailure,
TestLabel "testProd" testProd
]
main :: IO ()
main = do
runTestTT testlist
return ()
#+END_SRC
*Running the tests*
Run the test as a script with runhaskell:
#+BEGIN_SRC sh
$ stack exec -- runhaskell /tmp/uniTestExample.hs
### Failure in: 2:testFailure
/tmp/uniTestExample.hs:12
It will fail 10 + 2 = 15
expected: 12
but got: 15
Cases: 4 Tried: 4 Errors: 0 Failures: 1
#+END_SRC
Run the test compiling:
#+BEGIN_SRC sh
$ stack exec -- ghc --make /tmp/uniTestExample.hs
[1 of 1] Compiling Main ( /tmp/uniTestExample.hs, /tmp/uniTestExample.o )
Linking /tmp/uniTestExample ..
$ /tmp/uniTestExample
### Failure in: 2:testFailure
/tmp/uniTestExample.hs:12
It will fail 10 + 2 = 15
expected: 12
but got: 15
Cases: 4 Tried: 4 Errors: 0 Failures: 1
#+END_SRC
** References
- Official HUnit documentation: [[http://hackage.haskell.org/package/HUnit][HUnit: A unit testing framework for Haskell]]
- [[http://blog.ploeh.dk/2015/05/07/functional-design-is-intrinsically-testable/][Functional design is intrinsically testable]]
- [[http://www.seas.upenn.edu/~cis552/12fa/hw/hw1/][CIS 552: Advanced Programming- HW1 Haskell List Processing and recursion]]
- [[http://www.shakthimaan.com/posts/2015/10/09/testing/news.html][Shakthimaan's Blog | Introduction to Haskell - Testing]]