Modulehspec-2.11.14Haskell2010
Test.Hspec
Hspec is a testing framework for Haskell.
This is the library reference for Hspec. The User's Manual contains more in-depth documentation.
- 6 types
- 1 class
- 56 values
- Packagehspec-2.11.14
- Exports63
- LanguageHaskell2010
- LicenceMIT
- SourceHspec.hs
Types
3 declarationsA SpecWith a represents a test or group of tests that require an a
value to run.
In the common case, a Spec is a SpecWith () which requires () and
can thus be executed with hspec.
To supply an argument to SpecWith tests to turn them into Spec, use functions from Test.Hspec.Core.Hooks such as around, before, mapSubject and similar.
Values of this type are created by it, describe and similar.
A type class for examples, that is to say, test bodies as used in it and similar functions.
Associated types
type family Arg eThe 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), which can be executed, andTest.Hspec.Core.Hspec.SpecWith(), which cannot be executed without turning it intoTest.Hspec.Core.Spec.SpecWithaTest.Hspec.Core.Spec.Specfirst.To supply an argument to examples, use the functions in Test.Hspec.Core.Hooks such as around, before, mapSubject and similar.
Instances8Example, …
Example PropertyDefined in hspec-core-2.11.14 · Test.Hspec.Core.QuickCheck · orphanExample BoolDefined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample ResultDefined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample ExpectationDefined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample (a -> Property)Defined in hspec-core-2.11.14 · Test.Hspec.Core.QuickCheck · orphanExample (a -> Bool)Defined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample (a -> Result)Defined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample (a -> Expectation)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Example
Setting expectations
21 declarationsRequest 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.
Instances4Example, Arg
Example ExpectationDefined in hspec-core-2.11.14 · Test.Hspec.Core.ExampleExample (a -> Expectation)Defined in hspec-core-2.11.14 · Test.Hspec.Core.Exampletype Arg Expectation = ()Defined in hspec-core-2.11.14 · Test.Hspec.Core.Exampletype Arg (a -> Expectation) = aDefined in hspec-core-2.11.14 · Test.Hspec.Core.Example
A Selector is a predicate; it can simultaneously constrain the type and
value of an exception.
actual `shouldBe` expected sets the expectation that actual is equal
to expected.
list `shouldContain` sublist sets the expectation that sublist is contained,
wholly and intact, anywhere in list.
list `shouldEndWith` suffix sets the expectation that list ends with suffix,
xs `shouldMatchList` ys sets the expectation that xs has the same
elements that ys has, possibly in another order
actual `shouldNotBe` notExpected sets the expectation that actual is not
equal to notExpected
list `shouldNotContain` sublist sets the expectation that sublist is not
contained anywhere in list.
action `shouldNotReturn` notExpected sets the expectation that action
does not return notExpected.
v `shouldNotSatisfy` p sets the expectation that p v is False.
action `shouldReturn` expected sets the expectation that action
returns expected.
v `shouldSatisfy` p sets the expectation that p v is True.
list `shouldStartWith` prefix sets the expectation that list starts with prefix,
action `shouldThrow` selector sets the expectation that action throws
an exception. The precise nature of the expected exception is described
with a Selector.
Defining a spec
8 declarationsThe 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) == 1Example 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.
specify is an alias for it.
The describe function combines a list of specs into a larger spec.
context is an alias for describe.
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
putStrLnwith
it "exposes some behavior" $ do
putStrLnparallel marks all spec items of the given spec to be safe for parallel evaluation.
sequential marks all spec items of the given spec to be evaluated sequentially.
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.
Pending spec items
6 declarationsDuring a test run a pending spec item is:
not executed
reported as "pending"
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" $
pendingpendingWith 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 is an alias for xit.
xcontext is an alias for xdescribe.
Focused spec items
5 declarationsDuring a test run, when a spec contains focused spec items, all other spec items are ignored.
fit is an alias for fmap focus . it
fspecify is an alias for fit.
fdescribe is an alias for fmap focus . describe
fcontext is an alias for fdescribe.
Hooks
19 declarationsRun a custom action before every spec item.
Run a custom action before every spec item.
Run a custom action before every spec item.
Run a custom action before the first spec item.
Run a custom action before the first spec item.
Run a custom action with an argument before the first spec item.
Run a custom action after every spec item.
Run a custom action after every spec item.
Run a custom action after the last spec item.
Run a custom action after the last spec item.
around :: (ActionWith a -> IO ())Function provided with an action to run the spec item as argument. It should return the action to actually execute the item.
-> SpecWith aSpec to modify
-> Spec
Run a custom action before and/or after every spec item, supplying it with an argument obtained via IO.
This is useful for tasks like creating a file handle or similar resource before a test and destroying it after the test.
Run a custom action before and/or after every spec item.
Run a custom action before and/or after every spec item.
Wrap an action around the given spec.
Wrap an action around the given spec.
Wrap an action around the given spec. Changes the arg type inside.
Modify the subject under test.
Note that this resembles a contravariant functor on the first type parameter of SpecM. This is because the subject is passed inwards, as an argument to the spec item.
Ignore the subject under test for a given spec.
Running a spec
1 declarationRun a given spec and write a report to stdout. Exit with exitFailure if at least one spec item fails.
Note: hspec handles command-line options and reads config files. This is not always desirable. Use evalSpec and runSpecForest if you need more control over these aspects.