Search code examples
haskellhunithspec

Running HUnit tests with Hspec


I want to run HUnit tests inside a spec:

module SHCSpec (spec)
    where

import Test.Hspec
import Test.Hspec.Contrib.HUnit
import Test.HUnit

import SHC.Types
import SHC.Lix


spec :: Spec
spec = do
    fromHUnitTest ("SHC.Lix" ~: "toHit" ~:
        [ Irrelevant @=? toHit []
        , None       @=? toHit [False]
        , None       @=? toHit [False, False]
        , Partial    @=? toHit [False, True]
        , Partial    @=? toHit [True, False]
        , Partial    @=? toHit [False, False, True]
        , Partial    @=? toHit [False, True, False]
        , Partial    @=? toHit [True, False, False]
        , Full       @=? toHit [True]
        , Full       @=? toHit [True, True]
        ])

The above code works, but it produces this ugly output:

SHC
  SHC.Lix
    toHit
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>
      <unlabeled>

Is it possible to attach the label toHit to every test case? Something like this:

SHC
  SHC.Lix
    toHit
    toHit
    toHit
    toHit
    toHit
    toHit
    toHit
    toHit
    toHit
    toHit

Even better would be to append a number to each toHit case. I messed with TestList and map TestLabel to no avail.


Solution

  • How about attaching labels to all the tests with a function like:

    label ts =
      [ show i ~: t | (i,t) <- zip [(1::Int)..] ts ]
    

    Just prefix your list of tests with a call to label:

    spec :: Spec
    spec = do
        fromHUnitTest ("SHC.Lix" ~: "toHit" ~:
            label
            [ Irrelevant @=? toHit []
            , None       @=? toHit [False]
            ...