Hspec
1. Recap
code example (This code use the Automatic Spec Discovery)
module <Module name>Spec where
import Test.Hspec
import ModuleBeingTest.AandB
spec :: Spec
spec = do -- more than one `describe`
describe "FunctionA" $
do -- more than one `it`
it "can do this with A" f1 `shouldBe` resultA1
it "can do that with A" f2 `shouldBe` resultA2
describe "FunctionB" $
do
it "can do this with B" B1 `shouldBe` resultB1
it "can do that with B" B2 `shouldBe` resultB2
stack test
<Module name> -- Test Module name with no `Spec`
FunctionA -- Text after `describe`
it can do this with A -- Text after `it`
it can do that with A
FunctionB
it can do this with B FAILED [1] -- Test FAIL!!
it can do that with B
Failures:
<file contains failed test>:20:63:
1) <Module name> Test the test-suit, it can do this with B
expected: <resultB1>
but got: <B1>
...
Randomized with seed 1521981406
...
Finished in 0.0004 seconds
4 examples, 1 failure
...
<project name>-test-suite: exited with: ExitFailure 1
Logs printed to console
Test a particular describe
stact test --test-arguments=--match="<Module name>.FunctionB"
This will only test the second describe part FunctionB
TODO: This command doesn’t work if there is any space in the describe string
2. Automatic Spec Discovery in Stack Project
Edit
package.yaml>package.yamlis a wrapper of<projectname>.cabal. This free me from dealing with the differences betweenexitcode-stdio-1anddetailed-0.9, both are differentcabalfile types. > - package.yaml > - cabal keystests: source-dirs: test main: Spec.hscreate folder
test/in project.create file
Spec.hsintest/-- file test/Spec.hs {-# OPTIONS_GHC -F -pgmF hspec-discover #-}creat test file
TestOneSpec.hsorTestTwoSpec.hsintest/.- Assume
TestOneSpec.hsis forModelOne, etc. - These file names must end with
Spec.hs. Each file has to export a function
specof typeSpec.spec :: Spec spec = do describe "Some functionality A" $ do it "can do this" f1 `shouldBe` resultA1 it "can do that" f2 `shouldBe` resultA2 describe "Some functionality B" $ do it "can do this" B1 `shouldBe` resultB1 it "can do that" B2 `shouldBe` resultB2Check official doc for more information.
- Assume
3. Use QuickCheck together with Hspec
propertyfunction: Official doc
TODO
- Test option examples: Official doc
- Examples about using
before_ & after_ & around_ - Examples using other
shouldfunctions.