tasty-hunit-0.9.2: HUnit support for the Tasty test framework.

Safe Haskell | None
Language | Haskell2010

Test.Tasty.HUnit

Description

Unit testing support for tasty, inspired by the HUnit package

Synopsis

- testCase :: TestName -> Assertion -> TestTree
- testCaseInfo :: TestName -> IO String -> TestTree
- testCaseSteps :: TestName -> ((String -> IO ()) -> Assertion) -> TestTree
- type Assertion = IO ()
- assertFailure :: String -> Assertion
- assertBool :: String -> Bool -> Assertion
- assertString :: String -> Assertion
- assertEqual :: (Eq a, Show a) => String -> a -> a -> Assertion
- class Assertable t where
- type AssertionPredicate = IO Bool
- class AssertionPredicable t where
- (@?) :: AssertionPredicable t => t -> String -> Assertion
- (@=?) :: (Eq a, Show a) => a -> a -> Assertion
- (@?=) :: (Eq a, Show a) => a -> a -> Assertion
- data HUnitFailure = HUnitFailure String

# Documentation

testCase :: TestName -> Assertion -> TestTree #

Create a `Test` for a HUnit `Assertion`

testCaseInfo :: TestName -> IO String -> TestTree #

Like `testCase`, except in case the test succeeds, the returned string will be shown as the description. If the empty string is returned, it will be ignored.

testCaseSteps :: TestName -> ((String -> IO ()) -> Assertion) -> TestTree #

Create a multi-step unit test.

Example:

    main = defaultMain $ testCaseSteps "Multi-step test" $ \step -> do
      step "Preparing..."
      -- do something

      step "Running part 1"
      -- do something

      step "Running part 2"
      -- do something
      assertFailure "BAM!"

      step "Running part 3"
      -- do something

The `step` calls are mere annotations. They let you see which steps were performed successfully, and which step failed.

You can think of `step` as `putStrLn`, except `putStrLn` would mess up the output with the console reporter and get lost with the others.

For the example above, the output will be

    Multi-step test: FAIL
      Preparing...
      Running part 1
      Running part 2
        BAM!

    1 out of 1 tests failed (0.00s)

Note that:

- Tasty still treats this as a single test, even though it consists of multiple steps.
- The execution stops after the first failure. When we are looking at a failed test, we know that all displayed steps but the last one were successful, and the last one failed. The steps after the failed one are not displayed, since they didn't run.

type Assertion = IO () #

When an assertion is evaluated, it will output a message if and only if the assertion fails.

Test cases are composed of a sequence of one or more assertions.

assertFailure #

Arguments

:: String | A message that is displayed with the assertion failure
-> Assertion |

Unconditionally signals that a failure has occured. All other assertions can be expressed with the form:

       if conditionIsMet
           then IO ()
           else assertFailure msg

assertBool #

Arguments

:: String | The message that is displayed if the assertion fails
-> Bool | The condition
-> Assertion |

Asserts that the specified condition holds.

assertString #

Arguments

:: String | The message that is displayed with the assertion failure
-> Assertion |

Signals an assertion failure if a non-empty message (i.e., a message other than `""`) is passed.

assertEqual #

Arguments

:: (Eq a, Show a) |
=> String | The message prefix
-> a | The expected value
-> a | The actual value
-> Assertion |

Asserts that the specified actual value is equal to the expected value. The output message will contain the prefix, the expected value, and the actual value.

If the prefix is the empty string (i.e., `""`), then the prefix is omitted and only the expected and actual values are output.

class Assertable t where #

Allows the extension of the assertion mechanism.

Since an `Assertion` can be a sequence of `Assertion`s and `IO` actions, there is a fair amount of flexibility of what can be achieved. As a rule, the resulting `Assertion` should be the body of a `TestCase` or part of a `TestCase`; it should not be used to assert multiple, independent conditions.

If more complex arrangements of assertions are needed, `Test`s and `Testable` should be used.

Minimal complete definition

assert

Methods

assert :: t -> Assertion #

Instances

Assertable Bool # |
Methods assert :: Bool -> Assertion #
Assertable () # |
Methods assert :: () -> Assertion #
Assertable String # |
Methods assert :: String -> Assertion #
Assertable t => Assertable (IO t) # |
Methods assert :: IO t -> Assertion #

type AssertionPredicate = IO Bool #

The result of an assertion that hasn't been evaluated yet.

Most test cases follow the following steps:

1. Do some processing or an action.
2. Assert certain conditions.

However, this flow is not always suitable. `AssertionPredicate` allows for additional steps to be inserted without the initial action to be affected by side effects. Additionally, clean-up can be done before the test case has a chance to end. A potential work flow is:

1. Write data to a file.
2. Read data from a file, evaluate conditions.
3. Clean up the file.
4. Assert that the side effects of the read operation meet certain conditions.
5. Assert that the conditions evaluated in step 2 are met.

class AssertionPredicable t where #

Used to signify that a data type can be converted to an assertion predicate.

Minimal complete definition

assertionPredicate

Methods

assertionPredicate :: t -> AssertionPredicate #

Instances

AssertionPredicable Bool # |
Methods assertionPredicate :: Bool -> AssertionPredicate #
AssertionPredicable t => AssertionPredicable (IO t) # |
Methods assertionPredicate :: IO t -> AssertionPredicate #

(@?) infix 1 #

Arguments

:: AssertionPredicable t |
=> t | A value of which the asserted condition is predicated
-> String | A message that is displayed if the assertion fails
-> Assertion |

Asserts that the condition obtained from the specified `AssertionPredicable` holds.

(@=?) infix 1 #

Arguments

:: (Eq a, Show a) |
=> a | The expected value
-> a | The actual value
-> Assertion |

Asserts that the specified actual value is equal to the expected value (with the expected value on the left-hand side).

(@?=) infix 1 #

Arguments

:: (Eq a, Show a) |
=> a | The actual value
-> a | The expected value
-> Assertion |

Asserts that the specified actual value is equal to the expected value (with the actual value on the left-hand side).

data HUnitFailure #

Exception thrown by `assertFailure` etc.

Constructors

HUnitFailure String |

Instances

Show HUnitFailure # |
Methods showsPrec :: Int -> HUnitFailure -> ShowS # show :: HUnitFailure -> String # showList :: [HUnitFailure] -> ShowS #
Exception HUnitFailure # |
Methods toException :: HUnitFailure -> SomeException # fromException :: SomeException -> Maybe HUnitFailure # displayException :: HUnitFailure -> String #