Safe Haskell | None |
---|---|
Language | Haskell98 |
This module includes almost everything you need to get started writing property tests with Hedgehog.
It is designed to be used alongside Hedgehog.Gen and Hedgehog.Range, which should be imported qualified. You also need to enable Template Haskell so the Hedgehog test runner can find your properties.
{-# LANGUAGE TemplateHaskell #-} import Hedgehog import qualified Hedgehog.Gen as Gen import qualified Hedgehog.Range as Range
Once you have your imports set up, you can write a simple property:
prop_reverse :: Property prop_reverse = property $ do xs <- forAll $ Gen.list (Range.linear 0 100) Gen.alpha reverse (reverse xs) === xs
And add the Template Haskell splice which will discover your properties:
tests :: IO Bool tests = checkParallel $$(discover)
If you prefer to avoid macros, you can specify the group of properties to run manually instead:
tests :: IO Bool tests = checkParallel $ Group "Test.Example" [ ("prop_reverse", prop_reverse) ]
You can then load the module in GHCi, and run it:
λ tests ━━━ Test.Example ━━━ ✓ prop_reverse passed 100 tests.
- data Group = Group {
- groupName :: !GroupName
- groupProperties :: ![(PropertyName, Property)]
- data GroupName
- data Property
- data PropertyName
- data Test m a
- data TestLimit
- data DiscardLimit
- data ShrinkLimit
- data Gen m a
- data Range a
- newtype Size = Size {}
- data Seed = Seed {}
- property :: Test IO () -> Property
- withTests :: TestLimit -> Property -> Property
- withDiscards :: DiscardLimit -> Property -> Property
- withShrinks :: ShrinkLimit -> Property -> Property
- check :: MonadIO m => Property -> m Bool
- recheck :: MonadIO m => Size -> Seed -> Property -> m ()
- discover :: TExpQ Group
- checkParallel :: MonadIO m => Group -> m Bool
- checkSequential :: MonadIO m => Group -> m Bool
- forAll :: (Monad m, Show a, HasCallStack) => Gen m a -> Test m a
- forAllWith :: (Monad m, HasCallStack) => (a -> String) -> Gen m a -> Test m a
- annotate :: (Monad m, HasCallStack) => String -> Test m ()
- annotateShow :: (Monad m, Show a, HasCallStack) => a -> Test m ()
- footnote :: Monad m => String -> Test m ()
- footnoteShow :: (Monad m, Show a) => a -> Test m ()
- success :: Monad m => Test m ()
- discard :: Monad m => Test m a
- failure :: (Monad m, HasCallStack) => Test m a
- assert :: (Monad m, HasCallStack) => Bool -> Test m ()
- (===) :: (Monad m, Eq a, Show a, HasCallStack) => a -> a -> Test m ()
- liftEither :: (Monad m, Show x, HasCallStack) => Either x a -> Test m a
- liftExceptT :: (Monad m, Show x, HasCallStack) => ExceptT x m a -> Test m a
- withExceptT :: (Monad m, Show x, HasCallStack) => Test (ExceptT x m) a -> Test m a
- withResourceT :: MonadResourceBase m => Test (ResourceT m) a -> Test m a
- tripping :: HasCallStack => Applicative f => Monad m => Show b => Show (f a) => Eq (f a) => a -> (a -> b) -> (b -> f a) -> Test m ()
- distribute :: (Distributive g, Transformer f g m) => g (f m) a -> f (g m) a
Documentation
A named collection of property tests.
Group | |
|
The name of a group of properties.
A property test to check, along with some configurable limits like how many times to run the test.
data PropertyName Source #
The name of a property.
A property test.
MonadTrans Test Source # | |
Distributive Test Source # | |
MonadBase b m => MonadBase b (Test m) Source # | |
MonadError e m => MonadError e (Test m) Source # | |
MonadReader r m => MonadReader r (Test m) Source # | |
MonadState s m => MonadState s (Test m) Source # | |
Monad m => Monad (Test m) Source # | |
Functor m => Functor (Test m) Source # | |
Monad m => Applicative (Test m) Source # | |
MonadIO m => MonadIO (Test m) Source # | |
Monad m => Alternative (Test m) Source # | |
Monad m => MonadPlus (Test m) Source # | |
MonadCatch m => MonadCatch (Test m) Source # | |
MonadThrow m => MonadThrow (Test m) Source # | |
PrimMonad m => PrimMonad (Test m) Source # | |
MonadResource m => MonadResource (Test m) Source # | |
MFunctor * Test Source # | |
type Transformer t Test m Source # | |
type PrimState (Test m) Source # | |
The number of successful tests that need to be run before a property test is considered successful.
data DiscardLimit Source #
The number of discards to allow before giving up.
data ShrinkLimit Source #
The number of shrinks to try before giving up on shrinking.
Generator for random values of a
.
MMonad Gen Source # | |
MonadTrans Gen Source # | |
Distributive Gen Source # | |
MonadBase b m => MonadBase b (Gen m) Source # | |
MonadError e m => MonadError e (Gen m) Source # | |
MonadReader r m => MonadReader r (Gen m) Source # | |
MonadState s m => MonadState s (Gen m) Source # | |
MonadWriter w m => MonadWriter w (Gen m) Source # | |
Monad m => Monad (Gen m) Source # | |
Functor m => Functor (Gen m) Source # | |
Monad m => Applicative (Gen m) Source # | |
MonadIO m => MonadIO (Gen m) Source # | |
Monad m => Alternative (Gen m) Source # | |
Monad m => MonadPlus (Gen m) Source # | |
MonadCatch m => MonadCatch (Gen m) Source # | |
MonadThrow m => MonadThrow (Gen m) Source # | |
PrimMonad m => PrimMonad (Gen m) Source # | |
MonadResource m => MonadResource (Gen m) Source # | |
MFunctor * Gen Source # | |
type Transformer t Gen m Source # | |
type PrimState (Gen m) Source # | |
A range describes the bounds of a number to generate, which may or may not
be dependent on a Size
.
Tests are parameterized by the size of the randomly-generated data, the meaning of which depends on the particular generator used.
A splittable random number generator.
Property
withTests :: TestLimit -> Property -> Property Source #
Set the number times a property should be executed before it is considered successful.
withDiscards :: DiscardLimit -> Property -> Property Source #
Set the number times a property is allowed to discard before the test runner gives up.
withShrinks :: ShrinkLimit -> Property -> Property Source #
Set the number times a property is allowed to shrink before the test runner gives up and prints the counterexample.
recheck :: MonadIO m => Size -> Seed -> Property -> m () Source #
Check a property using a specific size and seed.
discover :: TExpQ Group Source #
Discover all the properties in a module.
Functions starting with prop_
are assumed to be properties.
checkParallel :: MonadIO m => Group -> m Bool Source #
Check a group of properties in parallel.
Warning: although this check function runs tests faster than
checkSequential
, it should be noted that it may cause problems with
properties that are not self-contained. For example, if you have a group
of tests which all use the same database table, you may find that they
interfere with each other when being run in parallel.
Using Template Haskell for property discovery:
tests :: IO Bool tests = checkParallel $$(discover)
With manually specified properties:
tests :: IO Bool tests = checkParallel $ Group "Test.Example" [ ("prop_reverse", prop_reverse) ]
checkSequential :: MonadIO m => Group -> m Bool Source #
Check a group of properties sequentially.
Using Template Haskell for property discovery:
tests :: IO Bool tests = checkSequential $$(discover)
With manually specified properties:
tests :: IO Bool tests = checkSequential $ Group "Test.Example" [ ("prop_reverse", prop_reverse) ]
Test
forAll :: (Monad m, Show a, HasCallStack) => Gen m a -> Test m a Source #
Generates a random input for the test by running the provided generator.
forAllWith :: (Monad m, HasCallStack) => (a -> String) -> Gen m a -> Test m a Source #
annotate :: (Monad m, HasCallStack) => String -> Test m () Source #
Annotates the source code with a message that might be useful for debugging a test failure.
annotateShow :: (Monad m, Show a, HasCallStack) => a -> Test m () Source #
Annotates the source code with a value that might be useful for debugging a test failure.
footnote :: Monad m => String -> Test m () Source #
Logs a message to be displayed as additional information in the footer of the failure report.
footnoteShow :: (Monad m, Show a) => a -> Test m () Source #
Logs a value to be displayed as additional information in the footer of the failure report.
assert :: (Monad m, HasCallStack) => Bool -> Test m () Source #
Fails the test if the condition provided is False
.
(===) :: (Monad m, Eq a, Show a, HasCallStack) => a -> a -> Test m () infix 4 Source #
Fails the test if the two arguments provided are not equal.
liftEither :: (Monad m, Show x, HasCallStack) => Either x a -> Test m a Source #
liftExceptT :: (Monad m, Show x, HasCallStack) => ExceptT x m a -> Test m a Source #
withExceptT :: (Monad m, Show x, HasCallStack) => Test (ExceptT x m) a -> Test m a Source #
withResourceT :: MonadResourceBase m => Test (ResourceT m) a -> Test m a Source #
tripping :: HasCallStack => Applicative f => Monad m => Show b => Show (f a) => Eq (f a) => a -> (a -> b) -> (b -> f a) -> Test m () Source #
Test that a pair of encode / decode functions are compatible.
Transformers
distribute :: (Distributive g, Transformer f g m) => g (f m) a -> f (g m) a Source #