ContentsIndex
Test.HUnit.Base
Contents
Declaring tests
Making assertions
Extending the assertion functionality
Test execution
Description

Basic definitions for the HUnit library.

This module contains what you need to create assertions and test cases and combine them into test suites. It also provides infrastructure for implementing test controllers (which are used to execute tests). For an exemplary implementation of a test controller, see Test.HUnit.Text.

Synopsis
data Test
= TestCase Assertion
| TestList [Test]
| TestLabel String Test
(~=?) :: (Eq a, Show a) => a -> a -> Test
(~?=) :: (Eq a, Show a) => a -> a -> Test
(~:) :: Testable t => String -> t -> Test
(~?) :: AssertionPredicable t => t -> String -> Test
assertBool :: String -> Bool -> Assertion
assertEqual :: (Eq a, Show a) => String -> a -> a -> Assertion
assertString :: String -> Assertion
assertFailure :: String -> Assertion
type Assertion = IO ()
(@=?) :: (Eq a, Show a) => a -> a -> Assertion
(@?=) :: (Eq a, Show a) => a -> a -> Assertion
(@?) :: AssertionPredicable t => t -> String -> Assertion
class Assertable t where
assert :: t -> Assertion
class ListAssertable t where
listAssert :: [t] -> Assertion
type AssertionPredicate = IO Bool
class AssertionPredicable t where
assertionPredicate :: t -> AssertionPredicate
data Node
= ListItem Int
| Label String
type Path = [Node]
testCaseCount :: Test -> Int
class Testable t where
test :: t -> Test
data Counts = Counts {
cases, tried, errors, failures :: Int
}
data State = State {
path :: Path
counts :: Counts
}
type ReportStart us = State -> us -> IO us
type ReportProblem us = String -> State -> us -> IO us
testCasePaths :: Test -> [Path]
performTest :: ReportStart us -> ReportProblem us -> ReportProblem us -> us -> Test -> IO (Counts, us)
Declaring tests
data Test
Constructors
TestCase Assertion
TestList [Test]
TestLabel String Test
show/hide Instances
(~=?)
:: (Eq a, Show a)
=> athe expected value
-> athe actual value
-> Test
shorthand for a test case that asserts equality (with the expected value on the left-hand side, and the actual value on the right-hand side).
(~?=)
:: (Eq a, Show a)
=> athe actual value
-> athe expected value
-> Test
shorthand for a test case that asserts equality (with the actual value on the left-hand side, and the expected value on the right-hand side).
(~:) :: Testable t => String -> t -> Test
creates a test from the specified Testable, with the specified label attached to it.
(~?)
:: AssertionPredicable t
=> ta value of which the asserted condition is predicated
-> Stringa message that is displayed on test failure
-> Test
creates a test case resulting from asserting the condition obtained from the specified AssertionPredicable.
Making assertions
assertBool
:: Stringa message that is displayed if the assertion fails
-> Boolthe condition that is asserted to hold
-> Assertion
asserts that the specified condition holds.
assertEqual
:: (Eq a, Show a)
=> Stringa message that is displayed if the assertion fails
-> athe expected value
-> athe actual value
-> Assertion
asserts that the specified actual value is equal to the expected value.
assertString
:: Stringa message that is displayed with the assertion failure
-> Assertion
signals an assertion failure if a non-empty message is passed.
assertFailure
:: Stringa message that is displayed with the assertion failure
-> Assertion
signals an assertion failure with a given message.
type Assertion = IO ()
An assertion is an IO computation with trivial result. An assertion or a sequence of assertions makes up a test case.
(@=?)
:: (Eq a, Show a)
=> athe expected value
-> athe actual value
-> Assertion
asserts that the specified actual value is equal to the expected value (with the expected value on the left-hand side).
(@?=)
:: (Eq a, Show a)
=> athe actual value
-> athe expected value
-> Assertion
asserts that the specified actual value is equal to the expected value (with the actual value on the left-hand side).
(@?)
:: AssertionPredicable t
=> ta value of which the asserted condition is predicated
-> Stringa message that is displayed if the assertion fails
-> Assertion
asserts that the condition obtained from the specified AssertionPredicable holds.
Extending the assertion functionality
class Assertable t where
Methods
assert :: t -> Assertion
show/hide Instances
class ListAssertable t where
Methods
listAssert :: [t] -> Assertion
show/hide Instances
type AssertionPredicate = IO Bool
class AssertionPredicable t where
Methods
assertionPredicate :: t -> AssertionPredicate
show/hide Instances
data Node
Constructors
ListItem Int
Label String
show/hide Instances
Eq Node
Read Node
Show Node
type Path = [Node]
testCaseCount :: Test -> Int
recursively counts all test cases contained in the specified test.
class Testable t where
Methods
test :: t -> Test
show/hide Instances
Test execution
Note: the rest of the functionality in this module is intended for implementors of test controllers. If you just want to run your tests cases, simply use a test controller, such as the text-based controller in Test.HUnit.Text.
data Counts
Constructors
Counts
cases, tried, errors, failures :: Int
show/hide Instances
Eq Counts
Read Counts
Show Counts
data State
Constructors
State
path :: Path
counts :: Counts
show/hide Instances
Eq State
Read State
Show State
type ReportStart us = State -> us -> IO us
report generator for reporting the start of a test run.
type ReportProblem us = String -> State -> us -> IO us
report generator for reporting problems that have occurred during a test run. Problems may be errors or assertion failures.
testCasePaths :: Test -> [Path]
performTest
:: ReportStart usreport generator for the test run start
-> ReportProblem usreport generator for errors during the test run
-> ReportProblem usreport generator for assertion failures during the test run
-> us
-> Testthe test to be executed
-> IO (Counts, us)

performs a test run with the specified report generators.

Note that the counts in a start report do not include the test case being started, whereas the counts in a problem report do include the test case just finished. The principle is that the counts are sampled only between test case executions. As a result, the number of test case successes always equals the difference of test cases tried and the sum of test case errors and failures.

Produced by Haddock version 0.8