HORIZON HASKELLDocslts/ghc-9.14.x3e2d5682026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11

Modulechell-0.5.0.2GHC2021

Test.Chell

Chell is a simple and intuitive library for automated testing. It natively supports assertion-based testing, and can use companion libraries such as chell-quickcheck to support more complex testing strategies.

An example test suite, which verifies the behavior of arithmetic operators.

{-# LANGUAGE TemplateHaskell #-}

import Test.Chell

suite_Math :: Suite
suite_Math = suite "math"
   [ test_Addition
   , test_Subtraction
   ]

test_Addition :: Test
test_Addition = assertions "addition" $ do
   $expect (equal (2 + 1) 3)
   $expect (equal (1 + 2) 3)

test_Subtraction :: Test
test_Subtraction = assertions "subtraction" $ do
   $expect (equal (2 - 1) 1)
   $expect (equal (1 - 2) (-1))

main :: IO ()
main = defaultMain [suite_Math]
$ ghc --make chell-example.hs
$ ./chell-example
PASS: 2 tests run, 2 tests passed
  • 8 types
  • 3 classes
  • 47 values
  • Packagechell-0.5.0.2
  • Exports58
  • LanguageGHC2021
  • LicenceMIT
  • SourceChell.hs

Main

1 declaration
valuedefaultMain :: [Suite] -> IO ()
#

A simple default main function, which runs a list of tests and logs statistics to stdout.

Test suites

4 declarations
datadata Suite
#

A suite is a named collection of tests.

Note: earlier versions of Chell permitted arbitrary nesting of test suites. This feature proved too unwieldy, and was removed. A similar result can be achieved with suiteTests; see the documentation for suite.

Instances2Show, SuiteOrTest
valuesuite :: String -> [Test] -> Suite
#

Define a new Suite, with the given name and children.

Note: earlier versions of Chell permitted arbitrary nesting of test suites. This feature proved too unwieldy, and was removed. A similar result can be achieved with suiteTests:

test_Addition :: Test
test_Subtraction :: Test
test_Show :: Test

suite_Math :: Suite
suite_Math = suite "math"
   [ test_Addition
   , test_Subtraction
   ]

suite_Prelude :: Suite
suite_Prelude = suite "prelude"
   (
     [ test_Show
     ]
     ++ suiteTests suite_Math
   )
valuesuiteName :: Suite -> String
#

Get a suite's name. Suite names may be any string, but are typically plain ASCII so users can easily type them on the command line.

$ ghci chell-example.hs
Ok, modules loaded: Main.

*Main> suiteName tests_Math
"math"
valuesuiteTests :: Suite -> [Test]
#

Get the full list of tests contained within this Suite. Each test is given its full name within the test hierarchy, where names are separated by periods.

$ ghci chell-example.hs
Ok, modules loaded: Main.

*Main> suiteTests tests_Math
[Test "math.addition",Test "math.subtraction"]

Skipping some tests

valueskipIf :: SuiteOrTest a => Bool -> a -> a
#

Conditionally skip tests. Use this to avoid commenting out tests which are currently broken, or do not work on the current platform.

tests :: Suite
tests = suite "tests"
   [ test_Foo
   , skipIf builtOnUnix test_WindowsSpecific
   , test_Bar
   ]
valueskipWhen :: SuiteOrTest a => IO Bool -> a -> a
#

Conditionally skip tests, depending on the result of a runtime check. The predicate is checked before each test is started.

tests :: Suite
tests = suite "tests"
   [ test_Foo
   , skipWhen noNetwork test_PingGoogle
   , test_Bar
   ]

Basic testing library

14 declarations
valueassertions :: String -> Assertions a -> Test
#

Convert a sequence of pass/fail assertions into a runnable test.

test_Equality :: Test
test_Equality = assertions "equality" $ do
    $assert (1 == 1)
    $assert (equal 1 1)
datadata Assertion
#

A single pass/fail assertion. Failed assertions include an explanatory message.

Instances3Eq, Show, IsAssertion
valuenote :: String -> String -> Assertions ()
#

Attach a note to a test run. Notes will be printed to stdout and included in reports, even if the test fails or aborts. Notes are useful for debugging failing tests.

valueafterTest :: IO () -> Assertions ()
#

Register an IO action to be run after the test completes. This action will run even if the test failed or aborted.

Built-in assertions

valuethrows :: Exception err => (err -> Bool) -> IO a -> IO Assertion
#

Assert that some computation throws an exception matching the provided predicate. This is mostly useful for exception types which do not have an instance for Eq, such as ErrorCall.

valuethrowsEq :: (Eq err, Exception err, Show err) => err -> IO a -> IO Assertion
#

Assert that some computation throws an exception equal to the given exception. This is better than just checking that the correct type was thrown, because the test can also verify the exception contains the correct information.

valuesameItems
  1. :: (Foldable container, Show item, Ord item)
  2. => container item
  3. -> container item
  4. -> Assertion
#

Assert that two containers have the same items, in any order.

valueequalItems
  1. :: (Foldable container, Show item, Ord item)
  2. => container item
  3. -> container item
  4. -> Assertion
#

Assert that two containers have the same items, in the same order.

classclass IsText a where
#

Class for types which can be treated as text; see equalLines.

Instances5IsText
valueequalLines :: (Ord a, IsText a) => a -> a -> Assertion
#

Assert that two pieces of text are equal. This uses a diff algorithm to check line-by-line, so the error message will be easier to read on large inputs.

Custom test types

4 declarations
datadata Test
#

A Test is, essentially, an IO action that returns a TestResult. Tests are aggregated into suites (see Suite).

Instances2Show, SuiteOrTest

Test results

datadata TestResult
#

The result of running a test.

To support future extensions to the testing API, any users of this module who pattern-match against the TestResult constructors should include a default case. If no default case is provided, a warning will be issued.

Constructors

Instances2Eq, Show

Failures

datadata Failure
#

Contains details about a test failure.

Instances2Eq, Show
  • Eq FailureDefined in chell-0.5.0.2 · Test.Chell.Types
  • Show FailureDefined in chell-0.5.0.2 · Test.Chell.Types

Failure locations

datadata Location
#

Contains details about a location in the test source file.

Instances2Eq, Show
  • Eq LocationDefined in chell-0.5.0.2 · Test.Chell.Types
  • Show LocationDefined in chell-0.5.0.2 · Test.Chell.Types

Test options

datadata TestOptions
#

Test options are passed to each test, and control details about how the test should be run.

Instances2Eq, Show

Default test options.

$ ghci
Prelude> import Test.Chell

Test.Chell> testOptionSeed defaultTestOptions
0

Test.Chell> testOptionTimeout defaultTestOptions
Nothing

Get the RNG seed for this test run. The seed is generated once, in defaultMain, and used for all tests. It is also logged to reports using a note.

When using defaultMain, users may specify a seed using the --seed command-line option.

testOptionSeed is a field accessor, and can be used to update a TestOptions value.

An optional timeout, in milliseconds. Tests which run longer than this timeout will be aborted.

When using defaultMain, users may specify a timeout using the --timeout command-line option.

testOptionTimeout is a field accessor, and can be used to update a TestOptions value.