HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

ModuleHUnit-1.6.2.0Haskell2010

Test.HUnit.Base

Basic definitions for the HUnit library.

This module contains what you need to create assertions and test cases and combine them into test suites.

This module also provides infrastructure for implementing test controllers (which are used to execute tests). See Test.HUnit.Text for a great example of how to implement a test controller.

  • 9 types
  • 4 classes
  • 14 values
  • PackageHUnit-1.6.2.0
  • Exports27
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceBase.hs

Declaring tests

5 declarations
datadata Test
#

The basic structure used to create an annotated tree of test cases.

Constructors

Instances2Show, Testable
  • Show TestDefined in HUnit-1.6.2.0 · Test.HUnit.Base
  • Testable TestDefined in HUnit-1.6.2.0 · Test.HUnit.Base
value(~=?)
  1. :: (HasCallStack, Eq a, Show a)
  2. => a

    The expected value

  3. -> a

    The actual value

  4. -> 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).

value(~?=)
  1. :: (HasCallStack, Eq a, Show a)
  2. => a

    The actual value

  3. -> a

    The expected value

  4. -> 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).

Making assertions

8 declarations
valueassertEqual
  1. :: (HasCallStack, Eq a, Show a)
  2. => String

    The message prefix

  3. -> a

    The expected value

  4. -> a

    The actual value

  5. -> 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.

valueassertString
  1. :: HasCallStack
  2. => String

    The message that is displayed with the assertion failure

  3. -> Assertion
#

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

typetype 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.

value(@=?)
  1. :: (HasCallStack, Eq a, Show a)
  2. => a

    The expected value

  3. -> a

    The actual value

  4. -> Assertion
#

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

value(@?=)
  1. :: (HasCallStack, Eq a, Show a)
  2. => a

    The actual value

  3. -> a

    The expected value

  4. -> Assertion
#

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

Extending the assertion functionality

5 declarations
classclass Assertable t where
#

Allows the extension of the assertion mechanism.

Since an Assertion can be a sequence of Assertions 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, Tests and Testable should be used.

Methods

Instances4Assertable
typetype 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.

Test execution

9 declarations

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.

datadata State
#

Keeps track of the remaining tests and the results of the performed tests. As each test is performed, the path is removed and the counts are updated as appropriate.

Constructors

Instances3Eq, Read, Show
  • Eq StateDefined in HUnit-1.6.2.0 · Test.HUnit.Base
  • Read StateDefined in HUnit-1.6.2.0 · Test.HUnit.Base
  • Show StateDefined in HUnit-1.6.2.0 · Test.HUnit.Base
typetype Path = [Node]
#

Uniquely describes the location of a test within a test hierarchy. Node order is from test case to root.

typetype ReportStart us = State -> us -> IO us
#

Report generator for reporting the start of a test run.

typetype ReportProblem us = Maybe SrcLoc -> String -> State -> us -> IO us
#

Report generator for reporting problems that have occurred during a test run. Problems may be errors or assertion failures.

valueperformTest
  1. :: ReportStart us

    report generator for the test run start

  2. -> ReportProblem us

    report generator for errors during the test run

  3. -> ReportProblem us

    report generator for assertion failures during the test run

  4. -> us
  5. -> Test

    the test to be executed

  6. -> IO (Counts, us)
#

Performs a test run with the specified report generators.

This handles the actual running of the tests. Most developers will want to use HUnit.Text.runTestTT instead. A developer could use this function to execute tests via another IO system, such as a GUI, or to output the results in a different manner (e.g., upload XML-formatted results to a webservice).

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.