Submitted by Moonside in programming
So I finally decided to learn some kind of a testing framework or library for Haskell. I chose the widely imitated QuickCheck - over 30 libraries in different languages have sprung up to imitate its basic concept. In short, it lets you specify laws that your functions ought to obey. QuickCheck then hammers the functions with random inputs and reports if they've been naughty. This as opposed to unit testing, where you check that functions against single input values - yes, indeed the square root of 4 is 2 and so on. Thus QuickCheck-like system have the promise of testing more with less effort, although it's not a replacement for unit testing.
There are is no end to tutorials about the small scale details, writing the laws, or propositions as they known in the library and writing the generators that create random data for testing. Thus I could write a test like the following quickly enough:
prop_Associativity :: Interval -> Interval -> Interval -> Bool
prop_Associativity x y z =
x <> (y <> z) == (x <> y) <> z
which specifies that the diamond operator <>
, also known as mappend
, must be associative or else. With a little more fighting I figured out how to write generators too and could then expand the test coverage to the core part of the program and wrote almost 30 of them.
But there's a lack of information on how to actually make QuickCheck tests run sensibly. I wanted to make to make a test module for each module of my program and then run the tests centrally and then run them using the cabal test
command which supposedly integrates testing with the building and packaging system. Neither the QuickCheck nor the Cabal manuals offered help, but I got enough hints obliquely from some random's The Design and Use of QuickCheck blogpost to make it work after like third hour of research. There was a tutorial for Stack, which is another building/packaging system but I ain't using that.
This is utterly bollocks imo. Now that I know how to actually do what I wanted to, I could suddenly find more sources just by Googling, but that's too little too late.
Also, unrelatedly, I discovered some crashing bug ('panic') in the compiler, GHC, that apparently ought to be reported but I can't be arsed to since I did something to make it go away. I beat the compiler into submission and reign over it on my bone throne and it's all I care about.
musou wrote
yeesh that sounds like no fun at all. i tried using the erlang version of that quickcheck library for property based testing in elixir and also found it hard to use effectively. the docs weren't great and i couldn't always figure out how to make the generators specific enough. i just stick with type annotations and unit tests and that's usually good enough for the kind of thing i do.
for what little its worth ive had an easier time using stack with haskell instead of cabal, but only because the book i was learning from used it. but googling for solutions to haskell problems has gotten more difficult now that there's two competing toolchains