How to use detailed-0.9 in cabal testing
Asked Answered
C

2

6

I'm having a surprising amount of difficulty getting the unit tests to run under cabal. I've copied the test code verbatim from the cabal documentation, with the exception of changing the module name

{-# LANGUAGE FlexibleInstances #-}
module Test.Integral ( tests ) where

import Distribution.TestSuite

instance TestOptions (String, Bool) where
    name = fst
    options = const []
    defaultOptions _ = return (Options [])
    check _ _ = []

instance PureTestable (String, Bool) where
    run (name, result) _ | result == True = Pass
                         | result == False = Fail (name ++ " failed!")

test :: (String, Bool) -> Test
test = pure

-- In actual usage, the instances 'TestOptions (String, Bool)' and
-- 'PureTestable (String, Bool)', as well as the function 'test', would be
-- provided by the test framework.

tests :: [Test]
tests =
    [ test ("bar-1", True)
    , test ("bar-2", False)
    ]

However, when I try to build the tests, I get the following messages:

Test/Integral.hs:6:10:
    Not in scope: type constructor or class `TestOptions'

Test/Integral.hs:12:10:
    Not in scope: type constructor or class `PureTestable'

I tried importing them directly from Distribution.TestSuite, but it said that they weren't exported. This is simple enough that I have to be doing something stupid, but I can't see what it is.

Crossbreed answered 20/8, 2013 at 19:14 Comment(1)
TestOptions et al appear to be referring to an old OLD version of quickcheck. I suggest you use a modern test framework (it appears what you are looking at is just a framework for running the test suite via cabal, not building the actual suite - learn tasty or test-framework).Luca
M
5

But for what it's worth, here is some code that works:

module Main (tests) where

import Distribution.TestSuite

tests :: IO [Test]
tests = do
  return [   
      test "foo" Pass
    , test "bar" (Fail "It did not work out!")
    ]

test :: String -> Result -> Test
test name r = Test t
  where          
    t = TestInstance {
        run = return (Finished r)
      , name = name
      , tags = []
      , options = []
      , setOption = \_ _ -> Right t
      }
Mclendon answered 8/9, 2013 at 17:13 Comment(0)
M
3

There is not much support for detailed-0.9 out there. It's possible to hook up existing testing libraries to use it, but even then you will not get progress information as tests pass.

I recommend to use the exitcode-stdio-1.0 interface together with an existing testing framework + use GHCi during development.

A full example for Hspec is here https://github.com/sol/hspec-example.

Mclendon answered 8/9, 2013 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.