IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

いつも忘れるのでHaskellのテストの最小のひな型をメモ

package.yaml

tastyだけでなくtastyから利用するテストパッケージもdependenciesに追加する。(これを忘れていつも嵌る)

tests:
  testproj-test:
    main:                Test.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - testproj
    - tasty
    - tasty-discover
    - tasty-hunit
    - tasty-hspec
    - tasty-quickcheck
    - tasty-smallcheck
    - bytestring 

Hspecのひな型

テストされる側のファイルとテストファイルは同じ名前にはできない。 sは小文字

module Foo.Bar.TestBuzz

where

-- test
import Test.Tasty
import Test.Tasty.Hspec

-- my library
import Foo.Bar.Buzz

getContext = return "dummy"

spec_template = do
    before getContext $ do
      describe "some descriptions" $ do
        it "descripe it" $ \_-> True `shouldBe` True

ghciをテスト側で呼び出す

stack ghci :testproj-test

実行方法

*> hspec spec_template

some descriptions
  descripe it FAILED [1]

Failures:

  test\Foo\Bar\TestBuzz.hs:17:
  1) some descriptions descripe it
       expected: True
        but got: False

Randomized with seed 285726028

Finished in 0.0081 seconds
1 example, 1 failure
*** Exception: ExitFailure 1