Stability | stable |
---|---|
Safe Haskell | None |
Language | Haskell2010 |
Test.Hspec
Contents
Description
Hspec is a testing framework for Haskell.
This is the library reference for Hspec. The User's Manual contains more in-depth documentation.
Synopsis
- type Spec = SpecWith ()
- type SpecWith a = SpecM a ()
- type family Arg e :: Type
- class Example e
- module Test.Hspec.Expectations
- it :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- specify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- describe :: HasCallStack => String -> SpecWith a -> SpecWith a
- context :: HasCallStack => String -> SpecWith a -> SpecWith a
- example :: Expectation -> Expectation
- parallel :: SpecWith a -> SpecWith a
- runIO :: IO r -> SpecM a r
- pending :: HasCallStack -> Expectation
- pendingWith :: HasCallStack -> String -> Expectation
- xit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- xspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- xdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a
- xcontext :: HasCallStack => String -> SpecWith a -> SpecWith a
- focus :: SpecWith a -> SpecWith a
- fit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- fspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- fdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a
- fcontext :: HasCallStack => String -> SpecWith a -> SpecWith a
- type ActionWith a = a -> IO ()
- before :: IO a -> SpecWith a -> Spec
- before_ :: IO () -> SpecWith a -> SpecWith a
- beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b
- beforeAll :: IO a -> SpecWith a -> Spec
- beforeAll_ :: IO () -> SpecWith a -> SpecWith a
- after :: ActionWith a -> SpecWith a -> SpecWith a
- after_ :: IO () -> SpecWith a -> SpecWith a
- afterAll :: ActionWith a -> SpecWith a -> SpecWith a
- afterAll_ :: IO () -> SpecWith a -> SpecWith a
- around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
- around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
- aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
- hspec :: Spec -> IO ()
Types
type family Arg e :: Type Source #
Instances
type Arg Bool | |
Defined in Test.Hspec.Core.Example | |
type Arg Property | |
Defined in Test.Hspec.Core.Example | |
type Arg Result | |
Defined in Test.Hspec.Core.Example | |
type Arg Expectation | |
Defined in Test.Hspec.Core.Example | |
type Arg (a -> Property) | |
Defined in Test.Hspec.Core.Example | |
type Arg (a -> Expectation) | |
Defined in Test.Hspec.Core.Example | |
type Arg (a -> Bool) | |
Defined in Test.Hspec.Core.Example | |
type Arg (a -> Result) | |
Defined in Test.Hspec.Core.Example |
A type class for examples
Minimal complete definition
Instances
Setting expectations
module Test.Hspec.Expectations
Defining a spec
it :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
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
specify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
specify
is an alias for it
.
describe :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
The describe
function combines a list of specs into a larger spec.
context :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
context
is an alias for describe
.
example :: Expectation -> Expectation Source #
example
is a type restricted version of id
. It can be used to get better
error messages on type mismatches.
Compare e.g.
it "exposes some behavior" $ example $ do putStrLn
with
it "exposes some behavior" $ do putStrLn
parallel :: SpecWith a -> SpecWith a Source #
parallel
marks all spec items of the given spec to be safe for parallel
evaluation.
runIO :: IO r -> SpecM a r Source #
Run an IO action while constructing the spec tree.
SpecM
is a monad to construct a spec tree, without executing any spec
items. 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.
Pending spec items
During a test run a pending spec item is:
- not executed
- reported as "pending"
pending :: HasCallStack -> Expectation Source #
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
pendingWith :: HasCallStack -> String -> Expectation Source #
pendingWith
is similar to pending
, but it takes an additional string
argument that can be used to specify the reason for why the spec item is pending.
xspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
xspecify
is an alias for xit
.
xcontext :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
xcontext
is an alias for xdescribe
.
Focused spec items
During a test run, when a spec contains focused spec items, all other spec items are ignored.
fit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
fit
is an alias for fmap focus . it
fspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
fspecify
is an alias for fit
.
fdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
fdescribe
is an alias for fmap focus . describe
fcontext :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
fcontext
is an alias for fdescribe
.
Hooks
type ActionWith a = a -> IO () Source #
An IO
action that expects an argument of type a
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b Source #
Run a custom action before every spec item.
beforeAll_ :: IO () -> SpecWith a -> SpecWith a Source #
Run a custom action before the first spec item.
after :: ActionWith a -> SpecWith a -> SpecWith a Source #
Run a custom action after every spec item.
afterAll :: ActionWith a -> SpecWith a -> SpecWith a Source #
Run a custom action after the last spec item.
afterAll_ :: IO () -> SpecWith a -> SpecWith a Source #
Run a custom action after the last spec item.
around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec Source #
Run a custom action before and/or after every spec item.
around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a Source #
Run a custom action before and/or after every spec item.
aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b Source #
Run a custom action before and/or after every spec item.