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

Modulehspec-core-2.11.14Haskell2010

Test.Hspec.Core.Spec

This module provides access to Hspec's internals. It is less stable than other parts of the API. For most users Test.Hspec is more suitable!

  • 16 types
  • 1 class
  • 41 values

Defining a spec

10 declarations
valueit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
#

The it function creates a spec item.

A spec item consists of:

  • a textual description of a desired behavior

  • an example for that behavior

describe "absolute" $ do
  it "returns a positive number when given a negative number" $
    absolute (-1) == 1

Example a optionally accepts an argument Arg a, which is then given to the test body. This is useful for provisioning resources for a test which are created and cleaned up outside the test itself. See Arg for details.

Note that this function is often on the scene of nasty type errors due to GHC failing to infer the type of do notation in the test body. It can be helpful to use TypeApplications to explicitly specify the intended Example type.

pending can be used to mark a spec item as pending.

If you want to textually specify a behavior but do not have an example yet, use this:

describe "fancyFormatter" $ do
  it "can format text in a way that everyone likes" $
    pending

Focused spec items

7 declarations

During a test run, when a spec contains focused spec items, all other spec items are ignored.

The SpecM monad

12 declarations
typetype Spec = SpecWith ()
#

A SpecWith that can be evaluated directly by the hspec function as it does not require any parameters.

newtypenewtype SpecM a r
#

A writer monad for SpecTree forests.

This is used by describe and is used to construct the forest of spec items.

It allows for dynamically generated spec trees, for example, by using data obtained by performing IO actions with runIO.

Constructors

Instances3Monad, Functor, Applicative
  • Monad (SpecM a)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Spec.Monad
  • Functor (SpecM a)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Spec.Monad
  • Applicative (SpecM a)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Spec.Monad
valuerunIO :: IO r -> SpecM a r
#

Run an IO action while constructing the spec tree.

SpecM is a monad to construct a spec tree, without executing any spec items itself. runIO allows you to run IO actions during this construction phase. The IO action is always run when the spec tree is constructed (e.g. even when --dry-run is specified). If you do not need the result of the IO action to construct the spec tree, beforeAll may be more suitable for your use case.

Get the path of describe labels, from the root all the way in to the call-site of this function.

Example
Example1 expression
:{runSpecM $ do  describe "foo" $ do    describe "bar" $ do      getSpecDescriptionPath >>= runIO . print:}["foo","bar"]

A type class for examples

12 declarations
classclass Example e where
#

A type class for examples, that is to say, test bodies as used in it and similar functions.

Associated types

  • type family Arg e

    The argument type that is needed to run this Example. If Arg is (), no argument is required and the Example can be run as-is.

    The value of Arg is the difference between Test.Hspec.Core.Spec.Spec (aka Test.Hspec.Core.Hspec.SpecWith ()), which can be executed, and Test.Hspec.Core.Spec.SpecWith a, which cannot be executed without turning it into Test.Hspec.Core.Spec.Spec first.

    To supply an argument to examples, use the functions in Test.Hspec.Core.Hooks such as around, before, mapSubject and similar.

Methods

  • evaluateExample :: e -> Params -> (ActionWith (Arg e) -> IO ()) -> ProgressCallback -> IO Result

    Evaluates an example.

    evaluateExample is expected to execute the test body inside the IO action passed to the hook. It's often necessary to use an IORef to pass data out like whether the test succeeded to the outer IO action so it can be returned as a Result.

    Example:

    newtype MyAction = MyAction (Int -> IO Bool)
    
    instance Example MyAction where
      type Arg MyAction = Int
    
      evaluateExample (MyAction act) _params hook _progress = do
        result <- newIORef (Result "" Success)
        hook $ arg -> do
          -- e.g. determines if arg is 42
          ok <- act arg
          let result' = Result "" $ if ok then Success else Failure Nothing NoReason
          writeIORef result result'
        readIORef result
    
Instances8Example, …
  • Example PropertyDefined in hspec-core-2.11.14 · Test.Hspec.Core.QuickCheck · orphan
  • Example BoolDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example ResultDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example ExpectationDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example (a -> Property)Defined in hspec-core-2.11.14 · Test.Hspec.Core.QuickCheck · orphan
  • Example (a -> Bool)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example (a -> Result)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example (a -> Expectation)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
typetype ActionWith a = a -> IO ()
#

An IO action that expects an argument of type a.

This type is what Examples are ultimately unlifted into for execution.

typetype ProgressCallback = Progress -> IO ()
#

Callback used by composite test items that contain many tests to report their progress towards finishing them all.

This is used, for example, to report how many QuickCheck examples are finished.

datadata Result
#

The result of running an example

Instances5Show, Example, Arg
  • Show ResultDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example ResultDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • Example (a -> Result)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • type Arg Result = ()Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
  • type Arg (a -> Result) = aDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example

Internal representation of a spec tree

15 declarations
typetype SpecTree a = Tree (IO ()) (Item a)
#

A tree is used to represent a spec internally. The tree is parameterized over the type of cleanup actions and the type of the actual spec items.

datadata Tree c a
#

Internal tree data structure

Constructors

Instances5Functor, Foldable, Traversable, Eq, Show
  • Functor (Tree c)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Tree
  • Foldable (Tree c)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Tree
  • Traversable (Tree c)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Tree
  • (Eq c, Eq a) => Eq (Tree c a)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Tree
  • (Show c, Show a) => Show (Tree c a)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Tree
datadata Item a
#

Item is used to represent spec items internally. A spec item consists of:

  • a textual description of a desired behavior

  • an example for that behavior

  • additional meta information

Everything that is an instance of the Example type class can be used as an example, including QuickCheck properties, Hspec expectations and HUnit assertions.

Constructors

Re-exports

2 declarations
typetype HasCallStack = IP "callStack" CallStack
#

Request a CallStack.

NOTE: The implicit parameter ?callStack :: CallStack is an implementation detail and should not be considered part of the CallStack API, we may decide to change the implementation in the future.