Community
Modules 18

Test.QuickCheck.Property

Combinators for constructing properties.

9 generation diagnostics
  • unattached documentation comment at 1072:1
  • unattached documentation comment at 1082:1
  • unattached documentation comment at 82:1
  • unattached documentation comment at 184:1
  • unattached documentation comment at 189:1
  • unattached documentation comment at 193:1
  • unattached documentation comment at 252:1
  • unattached documentation comment at 364:1
  • unattached documentation comment at 384:1

class Testable prop

The class of properties, i.e., types which QuickCheck knows how to test. Typically a property will be a function returning Bool or Property.

property :: prop -> Property

Convert the thing to a property.

propertyForAllShrinkShow :: Gen a -> (a -> [a]) -> (a -> [String]) -> (a -> prop) -> Property

Optional; used internally in order to improve shrinking. Tests a property but also quantifies over an extra value (with a custom shrink and show function). The Testable instance for functions defines propertyForAllShrinkShow in a way that improves shrinking.

data Discard

If a property returns Discard, the current test case is discarded, the same as if a precondition was false.

An example is the definition of ==>:

(==>) :: Testable prop => Bool -> prop -> Property
False ==> _ = property Discard
True  ==> p = property p

ioProperty :: Testable prop => IO prop -> Property

Do I/O inside a property.

Warning: any random values generated inside of the argument to ioProperty will not currently be shrunk. For best results, generate all random values before calling ioProperty, or use idempotentIOProperty if that is safe.

idempotentIOProperty :: Testable prop => IO prop -> Property

Do I/O inside a property.

Warning: during shrinking, the I/O may not always be re-executed. Instead, the I/O may be executed once and then its result retained. If this is not acceptable, use ioProperty instead.

protect :: (AnException -> a) -> IO a -> IO a

data Rose a

MkRose :: a -> [Rose a] -> Rose a

IORose :: (IO (Rose a)) -> Rose a

ioRose :: IO (Rose Result) -> Rose Result

reduceRose :: Rose Result -> IO (Rose Result)

Execute the IORose bits of a rose tree, returning a tree constructed by MkRose.

onRose :: (a -> [Rose a] -> Rose a) -> Rose a -> Rose a

Apply a function to the outermost MkRose constructor of a rose tree. The function must be total!

protectRose :: IO (Rose Result) -> IO (Rose Result)

Wrap a rose tree in an exception handler.

protectProp :: Prop -> Prop

Wrap the top level of a Prop in an exception handler.

protectResults :: Rose Result -> Rose Result

Wrap all the Results in a rose tree in exception handlers.

data Callback

Different kinds of callbacks

PostTest :: CallbackKind -> (State -> Result -> IO ()) -> Callback

(State -> Result -> IO ())

Called just after a test

PostFinalFailure :: CallbackKind -> (State -> Result -> IO ()) -> Callback

(State -> Result -> IO ())

Called with the final failing test-case

data Witness

Wit :: forall a. (Typeable a, Show a) => a -> Witness

data Result

The result of a single test.

MkResult :: Maybe Bool -> Bool -> String -> Maybe AnException -> Bool -> Maybe Int -> Maybe Confidence -> Maybe Int -> Maybe Int -> Maybe Int -> [String] -> [(String, Bool)] -> [(String, String)] -> [(Maybe String, String, Double)] -> [Callback] -> [String] -> [Witness] -> Result

ok :: Result -> Maybe Bool

result of the test case; Nothing = discard

expect :: Result -> Bool

indicates what the expected result of the property is

reason :: Result -> String

a message indicating what went wrong

theException :: Result -> Maybe AnException

the exception thrown, if any

abort :: Result -> Bool

if True, the test should not be repeated

maybeNumTests :: Result -> Maybe Int

stop after this many tests

maybeDiscardedRatio :: Result -> Maybe Int

maximum number of discarded tests per successful test

labels :: Result -> [String]

test case labels

classes :: Result -> [(String, Bool)]

test case classes

tables :: Result -> [(String, String)]

test case tables

requiredCoverage :: Result -> [(Maybe String, String, Double)]

required coverage

callbacks :: Result -> [Callback]

the callbacks for this test case

testCase :: Result -> [String]

the generated test case

exception :: String -> AnException -> Result

mapResult :: Testable prop => (Result -> Result) -> prop -> Property

mapTotalResult :: Testable prop => (Result -> Result) -> prop -> Property

mapRoseResult :: Testable prop => (Rose Result -> Rose Result) -> prop -> Property

mapProp :: Testable prop => (Prop -> Prop) -> prop -> Property

mapSize :: Testable prop => (Int -> Int) -> prop -> Property

Adjust the test case size for a property, by transforming it with the given function.

shrinking :: Testable prop => (a -> [a]) -> a -> (a -> prop) -> Property

Shrinks the argument to a property if it fails. Shrinking is done automatically for most types. This function is only needed when you want to override the default behavior.

(a -> [a])

shrink-like function.

a

The original argument

noShrinking :: Testable prop => prop -> Property

Disables shrinking for a property altogether. Only quantification inside the call to noShrinking is affected.

callback :: Testable prop => Callback -> prop -> Property

Adds a callback

counterexample :: Testable prop => String -> prop -> Property

Adds the given string to the counterexample if the property fails.

printTestCase :: Testable prop => String -> prop -> Property

Use counterexample instead

Adds the given string to the counterexample if the property fails.

whenFail :: Testable prop => IO () -> prop -> Property

Performs an IO action after the last failure of a property.

whenFail' :: Testable prop => IO () -> prop -> Property

Performs an IO action every time a property fails. Thus, if shrinking is done, this can be used to keep track of the failures along the way.

withProgress :: Testable prop => (TestProgress -> IO ()) -> prop -> Property

Performs an IO action every time a property is tested, after every test. The IO action is allowed to depend on TestProgress, which contains information regarding how testing is progressing.

Note: QC invokes callbacks before the internal state has been updated to reflect the most recent test. The means that e.g. currentPassed will, after the first test has been executed, still show 0.

verbose :: Testable prop => prop -> Property

Prints out the generated test case every time the property is tested. Only variables quantified over inside the verbose are printed.

Note: for technical reasons, the test case is printed out after the property is tested. To debug a property that goes into an infinite loop, use within to add a timeout instead.

verboseShrinking :: Testable prop => prop -> Property

Prints out the generated test case every time the property fails, including during shrinking. Only variables quantified over inside the verboseShrinking are printed.

Note: for technical reasons, the test case is printed out after the property is tested. To debug a property that goes into an infinite loop, use within to add a timeout instead.

expectFailure :: Testable prop => prop -> Property

Indicates that a property is supposed to fail. QuickCheck will report an error if it does not fail.

once :: Testable prop => prop -> Property

Modifies a property so that it only will be tested once. Opposite of again.

again :: Testable prop => prop -> Property

Modifies a property so that it will be tested repeatedly. Opposite of once.

withMaxSuccess :: Testable prop => Int -> prop -> Property

Use withNumTests instead

Configures how many times a property will be tested.

For example,

quickCheck (withMaxSuccess 1000 p)

will test p up to 1000 times.

withNumTests :: Testable prop => Int -> prop -> Property

Configures how many times a property will be tested.

For example,

quickCheck (withNumTests 1000 p)

will test p up to 1000 times.

withDiscardRatio :: Testable prop => Int -> prop -> Property

Configures how many times a property is allowed to be discarded before failing.

For example,

quickCheck (withDiscardRatio 10 p)

will allow p to fail up to 10 times per successful test.

withMaxShrinks :: Testable prop => Int -> prop -> Property

Configure the maximum number of times a property will be shrunk.

For example,

quickCheck (withMaxShrinks 100 p)

will cause p to only attempt 100 shrinks on failure.

withMaxSize :: Testable prop => Int -> prop -> Property

Configure the maximum size a property will be tested at.

witness :: (Typeable a, Show a, Testable prop) => a -> prop -> Property

Return a value in the witnesses field of the Result returned by quickCheckResult. Witnesses are returned outer-most first.

In ghci, for example:








checkCoverage :: Testable prop => prop -> Property

Check that all coverage requirements defined by cover and coverTable are met, using a statistically sound test, and fail if they are not met.

Ordinarily, a failed coverage check does not cause the property to fail. This is because the coverage requirement is not tested in a statistically sound way. If you use cover to express that a certain value must appear 20% of the time, QuickCheck will warn you if the value only appears in 19 out of 100 test cases - but since the coverage varies randomly, you may have just been unlucky, and there may not be any real problem with your test generation.

When you use checkCoverage, QuickCheck uses a statistical test to account for the role of luck in coverage failures. It will run as many tests as needed until it is sure about whether the coverage requirements are met. If a coverage requirement is not met, the property fails.

Example:

quickCheck (checkCoverage prop_foo)

checkCoverageWith :: Testable prop => Confidence -> prop -> Property

Check coverage requirements using a custom confidence level. See stdConfidence.

An example of making the statistical test less stringent in order to improve performance:

quickCheck (checkCoverageWith stdConfidence{certainty = 10^6} prop_foo)

stdConfidence :: Confidence

The standard parameters used by checkCoverage: certainty = 10^9, tolerance = 0.9. See Confidence for the meaning of the parameters.

label :: Testable prop => String -> prop -> Property

Attaches a label to a test case. This is used for reporting test case distribution.

For example:

prop_reverse_reverse :: [Int] -> Property
prop_reverse_reverse xs =
  label ("length of input is " ++ show (length xs)) $
    reverse (reverse xs) === xs

Each use of label in your property results in a separate table of test case distribution in the output. If this is not what you want, use tabulate.

collect :: (Show a, Testable prop) => a -> prop -> Property

Attaches a label to a test case. This is used for reporting test case distribution.

collect x = label (show x)

For example:

prop_reverse_reverse :: [Int] -> Property
prop_reverse_reverse xs =
  collect (length xs) $
    reverse (reverse xs) === xs

Each use of collect in your property results in a separate table of test case distribution in the output. If this is not what you want, use tabulate.

classify :: Testable prop => Bool -> String -> prop -> Property

Reports how many test cases satisfy a given condition.

For example:

prop_sorted_sort :: [Int] -> Property
prop_sorted_sort xs =
  sorted xs ==>
  classify (length xs > 1) "non-trivial" $
  sort xs === xs

Bool

True if the test case should be labelled.

String

Label.

cover :: Testable prop => Double -> Bool -> String -> prop -> Property

Checks that at least the given proportion of successful test cases belong to the given class. Discarded tests (i.e. ones with a false precondition) do not affect coverage.

Note: If the coverage check fails, QuickCheck prints out a warning, but the property does not fail. To make the property fail, use checkCoverage.

For example:

prop_sorted_sort :: [Int] -> Property
prop_sorted_sort xs =
  sorted xs ==>
  cover 50 (length xs > 1) "non-trivial" $
  sort xs === xs

Double

The required percentage (0-100) of test cases.

Bool

True if the test case belongs to the class.

String

Label for the test case class.

tabulate :: Testable prop => String -> [String] -> prop -> Property

Collects information about test case distribution into a table. The arguments to tabulate are the table's name and a list of values associated with the current test case. After testing, QuickCheck prints the frequency of all collected values. The frequencies are expressed as a percentage of the total number of values collected.

You should prefer tabulate to label when each test case is associated with a varying number of values. Here is a (not terribly useful) example, where the test data is a list of integers and we record all values that occur in the list:

prop_sorted_sort :: [Int] -> Property
prop_sorted_sort xs =
  sorted xs ==>
  tabulate "List elements" (map show xs) $
  sort xs === xs

Here is a more useful example. We are testing a chatroom, where the user can log in, log out, or send a message:

data Command = LogIn | LogOut | SendMessage String deriving (Data, Show)
instance Arbitrary Command where ...

There are some restrictions on command sequences; for example, the user must log in before doing anything else. The function valid :: [Command] -> Bool checks that a command sequence is allowed. Our property then has the form:

prop_chatroom :: [Command] -> Property
prop_chatroom cmds =
  valid cmds ==>
    ...

The use of ==> may skew test case distribution. We use collect to see the length of the command sequences, and tabulate to get the frequencies of the individual commands:

prop_chatroom :: [Command] -> Property
prop_chatroom cmds =
  wellFormed cmds LoggedOut ==>
  'collect' (length cmds) $
  'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $
    ...

coverTable :: Testable prop => String -> [(String, Double)] -> prop -> Property

Checks that the values in a given table appear a certain proportion of the time. A call to coverTable table [(x1, p1), ..., (xn, pn)] asserts that of the values in table, x1 should appear at least p1 percent of the time that table appears, x2 at least p2 percent of the time that table appears, and so on.

Note: If the coverage check fails, QuickCheck prints out a warning, but the property does not fail. To make the property fail, use checkCoverage.

Continuing the example from the tabulate combinator...

data Command = LogIn | LogOut | SendMessage String deriving (Data, Show)
prop_chatroom :: [Command] -> Property
prop_chatroom cmds =
  wellFormed cmds LoggedOut ==>
  'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $
    ...

...we can add a coverage requirement as follows, which checks that LogIn, LogOut and SendMessage each occur at least 25% of the time:

prop_chatroom :: [Command] -> Property
prop_chatroom cmds =
  wellFormed cmds LoggedOut ==>
  coverTable "Commands" [("LogIn", 25), ("LogOut", 25), ("SendMessage", 25)] $
  'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $
    ... property goes here ...

(==>) :: Testable prop => Bool -> prop -> Property

Implication for properties: The resulting property holds if the first argument is False (in which case the test case is discarded), or if the given property holds. Note that using implication carelessly can severely skew test case distribution: consider using cover to make sure that your test data is still good quality.

within :: Testable prop => Int -> prop -> Property

Considers a property failed if it does not complete within the given number of microseconds.

Note: if the property times out, variables quantified inside the `within` will not be printed. Therefore, you should use `within` only in the body of your property.

Good: prop_foo a b c = within 1000000 ...

Bad: prop_foo = within 1000000 $ \a b c -> ...

Bad: prop_foo a b c = ...; main = quickCheck (within 1000000 prop_foo)

discardAfter :: Testable prop => Int -> prop -> Property

Discards the test case if it does not complete within the given number of microseconds. This can be useful when testing algorithms that have pathological cases where they run extremely slowly.

onTimeout :: Testable prop => Result -> Int -> prop -> Property

forAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> Property

Explicit universal quantification: uses an explicitly given test case generator.

forAllShow :: Testable prop => Gen a -> (a -> String) -> (a -> prop) -> Property

Like forAll, but with an explicitly given show function.

forAllBlind :: Testable prop => Gen a -> (a -> prop) -> Property

Like forAll, but without printing the generated value.

forAllShrink :: (Show a, Testable prop) => Gen a -> (a -> [a]) -> (a -> prop) -> Property

Like forAll, but tries to shrink the argument for failing test cases.

forAllShrinkShow :: Testable prop => Gen a -> (a -> [a]) -> (a -> String) -> (a -> prop) -> Property

Like forAllShrink, but with an explicitly given show function.

forAllShrinkBlind :: Testable prop => Gen a -> (a -> [a]) -> (a -> prop) -> Property

Like forAllShrink, but without printing the generated value.

(.&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property

Nondeterministic choice: p1 .&. p2 picks randomly one of p1 and p2 to test. If you test the property 100 times it makes 100 random choices.

(.&&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property

Conjunction: p1 .&&. p2 passes if both p1 and p2 pass.

conjoin :: Testable prop => [prop] -> Property

Take the conjunction of several properties.

(.||.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property

Disjunction: p1 .||. p2 passes unless p1 and p2 simultaneously fail.

disjoin :: Testable prop => [prop] -> Property

Take the disjunction of several properties.

(===) :: (Eq a, Show a) => a -> a -> Property

(=/=) :: (Eq a, Show a) => a -> a -> Property

total :: NFData a => a -> Property

Checks that a value is total, i.e., doesn't crash when evaluated.

Instances

14
  • instance Typeable Property
  • instance Testable Discard
  • instance Testable ()
  • instance Testable prop => Testable (Maybe prop)
  • instance Testable Bool
  • instance Testable Result
  • instance Testable Prop
  • instance Testable prop => Testable (Gen prop)
  • instance Testable Property
  • instance (Arbitrary a, Show a, Testable prop) => Testable (a -> prop)
  • instance Functor Rose
  • instance Applicative Rose
  • instance Monad Rose
  • instance Show Witness
Build record and source files

Community contribution. The service has not verified this documentation. The generator reported 30 diagnostics.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-aa6debbd-web-3

Metadata snapshot: f17ab6aeb2a7b6daf9fba7614097e138beeef85fa92c3fb7fdfed9ed0e49728c

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.