首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开始在Haskell中使用HSpec和Tasty?

开始在Haskell中使用HSpec和Tasty?
EN

Stack Overflow用户
提问于 2019-12-15 16:20:06
回答 1查看 719关注 0票数 1

我是Haskell的新手,我正在尝试让hspecTasty一起工作(使用tasty-hspec)和Stack。我见过一个在HUnit中使用tasty的example,如下所示:

代码语言:javascript
复制
import Test.Tasty
import Test.Tasty.SmallCheck as SC
import Test.Tasty.QuickCheck as QC
import Test.Tasty.HUnit

import Data.List
import Data.Ord

main = defaultMain tests

tests :: TestTree
tests = testGroup "Tests" [properties, unitTests]

properties :: TestTree
properties = testGroup "Properties" [scProps, qcProps]

scProps = testGroup "(checked by SmallCheck)"
  [ SC.testProperty "sort == sort . reverse" $
      \list -> sort (list :: [Int]) == sort (reverse list)
  , SC.testProperty "Fermat's little theorem" $
      \x -> ((x :: Integer)^7 - x) `mod` 7 == 0
  -- the following property does not hold
  , SC.testProperty "Fermat's last theorem" $
      \x y z n ->
        (n :: Integer) >= 3 SC.==> x^n + y^n /= (z^n :: Integer)
  ]

qcProps = testGroup "(checked by QuickCheck)"
  [ QC.testProperty "sort == sort . reverse" $
      \list -> sort (list :: [Int]) == sort (reverse list)
  , QC.testProperty "Fermat's little theorem" $
      \x -> ((x :: Integer)^7 - x) `mod` 7 == 0
  -- the following property does not hold
  , QC.testProperty "Fermat's last theorem" $
      \x y z n ->
        (n :: Integer) >= 3 QC.==> x^n + y^n /= (z^n :: Integer)
  ]

unitTests = testGroup "Unit tests"
  [ testCase "List comparison (different length)" $
      [1, 2, 3] `compare` [1,2] @?= GT

  -- the following test does not hold
  , testCase "List comparison (same length)" $
      [1, 2, 3] `compare` [1,2,2] @?= LT
  ]

但我不想使用hunit、smallcheck或quickcheck,而是使用hspec。我尝试过以下几种方法:

代码语言:javascript
复制
module Spec where

import Game

import Test.Tasty
import Test.Tasty.Hspec

spec_beats :: Spec
spec_beats = do

  it "hello" $
    Rock `beats` Scissors `shouldBe` True

tests :: TestTree
tests = testGroup "Tests" [spec_beats]

main = defaultMain tests

但这不能编译:

代码语言:javascript
复制
    • Couldn't match type ‘hspec-core-2.7.1:Test.Hspec.Core.Spec.Monad.SpecM
                             () ()’
                     with ‘TestTree’
      Expected type: TestTree
        Actual type: Spec
    • In the expression: spec_beats
      In the second argument of ‘testGroup’, namely ‘[spec_beats]’
      In the expression: testGroup "Tests" [spec_beats]
   |        
15 | tests = testGroup "Tests" [spec_beats]
   |                            ^^^^^^^^^^

因此,我的问题是,如何让我的hspec示例与tasty一起工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-15 22:41:57

tasty hspec文档中的Examples section有一个解决方案:使用testSpecSpec转换为TestTree

代码语言:javascript
复制
spec_beats :: Spec
spec_beats = ...

main :: IO ()
main = do
  tests <- testSpec "beats" spec_beats
  defaultMain tests
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59342263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档