|
|
pl:dydaktyka:pp:haskell:lab-hunit [2018/06/08 23:26] esimon [Funkcje i typy w HUnit] |
pl:dydaktyka:pp:haskell:lab-hunit [2019/06/27 15:50] |
====== Testy jednostkowe z HUnit ====== | |
Haskell udostępnia framework do testów jednostkowych o nazwie HUnit, który (podobno ;) ) inspirowany jest JUnitem z Javy. | |
Pełna dokumentacja: [[http://hackage.haskell.org/package/HUnit|HUnit Documentation]] | |
| |
===== Instalacja ===== | |
HUnit nie jest domyślnie instalowany wraz z ghc/ghci. | |
Można go doinstalować z repozytorium: | |
<code>sudo apt-get install libghc-hunit-dev</code> | |
| |
===== Funkcje i typy w HUnit ===== | |
Podstawowym typem w HUnit 'Assertion' produkująca w wyniku zawsze 'IO ()', aby można było testować fragmenty kodu z tzw. side-effects. Type ten zwracany jest przez szereg funkcji opisanych w tabeli poniżej i to za ich pomocą budować będziemy testy. | |
| |
`Assertion` stanowią z kolei podstawowy element `Test`, który jest juz gotowym testem jednoskowym (lub ich listą). | |
Testy uruchamiamy funkcją runtestTT, która zwraca typ 'Counts', stanowiący podsumowanie testów. | |
| |
| |
^Typ ^ Definicja^ Opis^ | |
|Assertion|<code>type Assertion = IO ()</code>|Podstawowy element każdego testu| | |
|Count|<code haskell>data Counts = Counts { cases, tried, errors, failures :: Int } | |
deriving (Eq, Show, Read)</code>|Raport z wykonania testów| | |
|Test|<code haskell>data Test = TestCase Assertion | |
| TestList [Test] | |
| TestLabel String Test</code>|Test jednostkowy| | |
| |
^Funckja^ Sygnatura^ | |
|assert | <code haskell>Assertable t => t -> Assertion</code>| | |
|assertFailure | <code haskell>assertFailure :: String -> Assertion</code>| | |
|assertBool | <code haskell>String -> Bool -> Assertion</code>| | |
|assertEqual | <code haskell>(Show a, Eq a) => String -> a -> a -> Assertion</code>| | |
|assertionPredicate | <code haskell>AssertionPredicable t => t -> AssertionPredicate</code>| | |
|assertString | <code haskell>String -> Assertion</code>| | |
|runTestTT | <code haskell>Test -> IO Counts</code>| | |
| |
| |
| |
| |
==== Przykłady ==== | |
| |
===== Operatory w HUnit ===== | |
| |
| |
^Opis^ Operator ^Sygnatura^ | |
|Assert Bool (True)| <code>(@?)</code> | <code haskell>(AssertionPredicable t) => t -> String -> Assertion</code>| | |
|Assert Equal| <code>(@=?)</code> |<code haskell>(Show a, Eq a) => a: expected -> a: value -> Assertion</code>| | |
|Assert Equal | <code>(@?=)</code> |<code haskell>(Eq a, Show a) => a: value -> a: expected -> Assertion</code>| | |
|Tworzy test o danej etykiecie|<code>(~:)</code> |<code haskell>Testable t => String -> t -> Test</code>| | |
|Tworzy test assertEqual |<code>(~?=)</code> |<code haskell>(Show a, Eq a) => a: value -> a: expected -> Test</code>| | |
|Tworzy test assertEqual |<code>(~=?) </code>|<code haskell>(Show a, Eq a) => a: expected -> a: value -> Test</code>| | |
==== Przykłady ==== | |
| |
===== Zadania ===== | |
- Napisz testy jednostkowe dla implementacji drzewa binarnego z zajęć XX | |
- XX | |
| |
| |
| |
| |
| |
| |
| |
| |