标签: hunit

优化之间的交互和错误调用的测试

我在模块中有一个函数,看起来像这样:

module MyLibrary (throwIfNegative) where

throwIfNegative :: Integral i => i -> String
throwIfNegative n | n < 0 = error "negative"
                  | otherwise = "no worries"
Run Code Online (Sandbox Code Playgroud)

我当然可以返回Maybe String或者其他一些变体,但我认为可以说这是一个程序员错误,用负数调用这个函数,所以error在这里使用是合理的.

现在,因为我喜欢100%的测试覆盖率,所以我希望有一个测试用例来检查这种行为.我试过这个

import Control.Exception
import Test.HUnit

import MyLibrary

case_negative =
    handleJust errorCalls (const $ return ()) $ do
        evaluate $ throwIfNegative (-1)
        assertFailure "must throw when given a negative number"
  where errorCalls (ErrorCall _) = Just ()

main = runTestTT $ TestCase case_negative
Run Code Online (Sandbox Code Playgroud)

它有点工作,但在使用优化进行编译时失败:

$ ghc --make -O …
Run Code Online (Sandbox Code Playgroud)

optimization haskell exception ghc hunit

5
推荐指数
1
解决办法
293
查看次数

找不到模块Test.HUnit

我试图在Haskell中进行一些单元测试,这基本上就是我在代码中所做的:

module Test where
import Test.HUnit
test = TestList [TestLabel "running all the tests!"
$ TestList [
. . . . .
]]  

run = runTestTT tests   
Run Code Online (Sandbox Code Playgroud)

当我尝试使用gchi编译它时,我收到此消息:

 Could not find module ‘Test.HUnit’
Use -v to see a list of the files searched for.
Failed, modules loaded: none
Run Code Online (Sandbox Code Playgroud)

我如何让HUnit工作?

我使用GHCi版本7.8.3

谢谢

编辑:

我尝试通过cabal安装HUnit作为建议的答案,但后来我得到了错误:

Could not find module ‘Test.HUnit’ Perhaps you haven't installed the "dyn" libraries for 
package ‘HUnit-1.2.5.2’? 
Use -v to see a list of the files searched for.
Run Code Online (Sandbox Code Playgroud)

然后我使用了命令: …

haskell hunit

5
推荐指数
1
解决办法
4539
查看次数

模拟IO操作:getArgs和putStrLn

我正在尝试测试一个带有命令行参数并将其输出到屏幕的小功能(或更确切地说是IO Action)。我原来的(无法调试的)功能是:

-- In Library.hs
module Library where

import System.Environment (getArgs)

run :: IO ()
run = do
  args <- getArgs
  putStrLn $ head args
Run Code Online (Sandbox Code Playgroud)

细算这个答案约嘲讽,我想出了一个办法来嘲笑getArgsputStrLn使用类型的类约束类型。因此,以上功能变为:

-- In Library.hs
module Library where

class Monad m => SystemMonad m where
  getArgs :: m [String]
  putStrLn :: String -> m ()

instance SystemMonad IO where
  getArgs = System.Environment.getArgs
  putStrLn = Prelude.putStrLn

run :: SystemMonad m => m ()
run = do
  args <- Library.getArgs
  Library.putStrLn $ …
Run Code Online (Sandbox Code Playgroud)

monads haskell unit-testing mocking hunit

5
推荐指数
1
解决办法
394
查看次数

从文件系统动态生成Tasty`TestTree`

我已经使用该Parsec库编写了一个文件解析器.我想使用Tasty测试框架编写一个高级单元测试,以确保解析器正确解析某些给定的文件.

我在以下目录结构中有三个格式良好的文件:

path/to/files -+
               |-> fileA
               |-> fileB
               |-> fileC
Run Code Online (Sandbox Code Playgroud)

我想要:

  1. 获取所有文件 path/to/files
  2. 阅读每个文件的内容
  3. testCase为每个文件创建一个文件,以确保成功解析文件的内容
  4. 这是动态完成的,这样我以后可以添加更多文件,永远不会更改代码

我设法构建了以下内容:

{-# LANGUAGE BangPatterns, FlexibleContexts #-}

module Test.MyParser
  ( testSuite
  ) where

import Control.Arrow              ((&&&))
import Data.Map                   (Map,fromList,toList)
import System.Directory
import System.IO.Unsafe           (unsafePerformIO) -- This is used for a hack
import Test.Tasty                 (TestTree,testGroup,withResource)
import Test.Tasty.HUnit
import Text.Parsec

-- | Determine if an Either is a Right or Left value
--   Useful for determining if a parse attempt was successful …
Run Code Online (Sandbox Code Playgroud)

haskell unit-testing dynamically-generated hunit io-monad

5
推荐指数
1
解决办法
241
查看次数

使用 IO monad 的 Haskell 单元测试

我正在尝试为返回 IO monad 的 haskell 函数编写 HUnit 测试,因为它们执行文件 I/O。有什么办法可以做到这一点吗?现在我正在尝试编写一个仅返回 Bool 的方法,这可以作为我的测试

\n\n
combine :: FilePath -> FilePath -> Bool\ncombine fp1 fp2 = do\n  cs <- readFile fp1\n  let (_,y,z) = strToHuff cs\n  let _ = writeToFile fp2 z y\n  (a, b) <- readFromFile fp2\n  z == a && b == y\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这给了我以下错误:

\n\n
FileWriter.hs:153:3: Couldn\'t match type \xe2\x80\x98IO b0\xe2\x80\x99 with \xe2\x80\x98Bool\xe2\x80\x99 \xe2\x80\xa6\n    Expected type: IO String -> (String -> IO b0) -> Bool\n      Actual type: IO String -> (String …
Run Code Online (Sandbox Code Playgroud)

io monads haskell ghci hunit

5
推荐指数
1
解决办法
1439
查看次数

是否可以在HUnit中断言错误情况?

如果我有一个函数会导致某个输入出现错误,是否可以编写一个测试来验证该输入是否发生了错误?

我在HUnit中找不到此“断言错误”功能。它可以在HUnit或其他测试包中使用吗?

error-handling haskell hunit

5
推荐指数
1
解决办法
335
查看次数

无法将HUnit导入ghci

我刚刚安装了HUnit,并希望将其导入ghci.

Prelude> import HUnit

<no location info>:
    Could not find module `HUnit':
      Use -v to see a list of the files searched for.
Run Code Online (Sandbox Code Playgroud)

另外,我不知道如何使用它-v来帮助我.

Prelude> import -v HUnit

<interactive>:1:8: parse error on input `-'

Prelude> import -v

<interactive>:1:8: parse error on input `-'

Prelude> -v

<interactive>:1:2: Not in scope: `v'
Run Code Online (Sandbox Code Playgroud)

我该如何导入它?

haskell ghci hunit

4
推荐指数
1
解决办法
1630
查看次数

cabal 无法安装 hunit

这里的 Haskell 生态系统非常陌生。我正在尝试安装 hunit,但是当我运行时,cabal install hunit我收到以下消息:

Warning: The install command is a part of the legacy v1 style of cabal usage.

Please switch to using either the new project style and the new-install
command or the legacy v1-install alias as new-style projects will become the
default in the next version of cabal-install. Please file a bug if you cannot
replicate a working v1- use case with the new-style commands.

For more information, see: https://wiki.haskell.org/Cabal/NewBuild

cabal: There is no …
Run Code Online (Sandbox Code Playgroud)

haskell cabal hunit

4
推荐指数
1
解决办法
3411
查看次数

如何使用带参数化类型的assertEqual

我正在尝试使用HUnit以TDD方式在Real World Haskell中进行练习.你可能已经猜到我还没有走得太远,所以当谈到Haskell时,我是一个绝对的初学者.鉴于以下代码,我如何解决ghci产生的以下错误:

不明确的类型变量在List_Test.hs中显示由于使用"assertEqual"而产生的" a' in the constraints: 使用assertEqual' at List_Test.hs:6:27-58 Eq a":6:27-58可能的修复:添加修复这些类型变量的类型签名

List_Test.hs:

module List_Test
where
import List
import Test.HUnit

fromEmptyList = TestCase $ assertEqual "" [] (toList (Nil))

main = runTestTT fromEmptyList
Run Code Online (Sandbox Code Playgroud)

List.hs:

module List
where
data List a = Cons a (List a)
            | Nil
              deriving (Show)

toList Nil = []
toList (Cons a b) = (:) a (toList b) 
Run Code Online (Sandbox Code Playgroud)

我已经尝试将类型约束添加到List声明和toList定义,但没有成功.互联网搜索也没有提供任何信息.

testing haskell hunit

3
推荐指数
1
解决办法
264
查看次数

带有类型错误的HUnit TestCase

我写了一个类似于LISP的函数flatten:

data NestedList a = Elem a | List [NestedList a]

flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List xs) = concatMap flatten xs
Run Code Online (Sandbox Code Playgroud)

这两个测试用例工作正常:

test1 = TestCase (assertEqual "test singleton" [5] (flatten (Elem 5)))
test2 = TestCase (assertEqual "test mixed" 
                              [1,2,3,4]
                              (flatten (List [Elem 1, 
                                              List [Elem 2, Elem 3],
                                              Elem 4])))
Run Code Online (Sandbox Code Playgroud)

但是这个报告了一个类型错误:

test3 = TestCase (assertEqual "test empty" [] (flatten (List [])))
Run Code Online (Sandbox Code Playgroud)

REPL的测试工作正常:

*Main> [] == flatten (List [])
True
Run Code Online (Sandbox Code Playgroud)

为什么我会收到错误,如何为空列表编写测试用例? …

haskell hunit

3
推荐指数
1
解决办法
581
查看次数

在HUnit中对ErrorCall进行Haskell单元测试

我有一个功能:

unify :: [Constraint] -> [Substitution]
Run Code Online (Sandbox Code Playgroud)

在某些情况下,它会抛出error函数的异常:

error "Circular constraint"
Run Code Online (Sandbox Code Playgroud)

我正在使用Test.HUnit单元测试,我想制作一个测试用例,声明这些错误会被抛出某些输入.我找到了这个,它提供了一种测试异常的方法,这些异常是实例Eq,但error似乎是一个ErrorCall异常,它不是一个实例Eq,所以我得到了错误:

No instance for (Eq ErrorCall)
  arising from a use of `assertException'
Run Code Online (Sandbox Code Playgroud)

如何编写被调用的TestCase断言error并且(最好)检查消息?

haskell hunit

3
推荐指数
1
解决办法
208
查看次数

HSpec(或HUnit)是否有可能将进一步的信息附加到仅在发生故障时才打印的断言上?

与quickcheck如何支持反例类似:

property \x ->
  counterexample ("Foo failed with: " ++ ...) $
    foo x
Run Code Online (Sandbox Code Playgroud)

但以某种方式与配合使用shouldBe,例如

failDetails (" details: " ++ baz a) $
  a `shouldBe` 2
Run Code Online (Sandbox Code Playgroud)

我希望它按照以下方式打印一些内容:

expected: 2
 but got: 3
 details: ...
Run Code Online (Sandbox Code Playgroud)

haskell hunit hspec

2
推荐指数
1
解决办法
125
查看次数

为什么我的测试文件不会导入我的数据结构?哈斯克尔

我目前正在为我的(非常简单的)二十一点游戏编写单元测试,我的测试文件(Tests.hs)似乎没有导入我在文件中声明的数据结构(我正在进行单元测试(HelpFunctions.hs)).我可以访问此文件中的函数/方法,但不能访问数据结构.有人可以帮我找到问题吗?

这是我的测试文件的顶部:

module Tests(performTests) where
import Test.HUnit
import HelpFunctions

cardList = [(Hearts, Ace)]
(...)
Run Code Online (Sandbox Code Playgroud)

这是我要编写测试文件的顶部

module HelpFunctions(Suit, Value, blackjack, cardDeck, shuffleOne, 
                     shuffleCards, getValue, addHand, dealCard, bust, 
                     getHighest
                    ) where

import System.Random
import Control.Monad(when)

{- Suit is one of the four suits or color of a playing card 
   ie Hearts, Clubs, Diamonds or Spades
   INVARIANT: Must be one of the specified.
 -}
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Show)

{- Value is the numeric value of …
Run Code Online (Sandbox Code Playgroud)

import haskell data-structures hunit

1
推荐指数
1
解决办法
100
查看次数