gpt4 book ai didi

haskell - 如何在堆栈中使用HUnit

转载 作者:行者123 更新时间:2023-12-02 10:09:05 25 4
gpt4 key购买 nike

我有一个 stackproject stapro ,其中包含一个文件 app/Main.hs

module Main where

import Lib

main = putStrLn "This is main"

foo::Int ->Int
foo = (+1)

和一个文件test/Spec.hs

module Spec where

import Test.HUnit
import Main (foo)

main :: IO ()
main = putStrLn "Test suite not yet implemented"

testFoo :: Test
testFoo = TestCase $ assertEqual "Should return 2" 2 (foo 1)

当我尝试执行测试时

$ stack test
While constructing the BuildPlan the following exceptions were encountered:

-- While attempting to add dependency,
Could not find package Main in known packages

-- Failure when adding dependencies:
Main: needed (-any), stack configuration has no specified version
needed for package stapro-0.1.0.0

我的 .cabal 文件是

name:                stapro
version: 0.1.0.0
...
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
default-language: Haskell2010

executable stapro-exe
hs-source-dirs: app
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, stapro
default-language: Haskell2010

test-suite stapro-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base
, stapro
, HUnit
, Main
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

...

最佳答案

看起来您正在尝试依赖可执行文件(test-suite 部分的 build-depends 中的 Main),这样您就可以在您的测试套件中对其进行测试。这是行不通的,事实上你根本无法真正测试你的可执行文件。

build-depends中删除Main。将您想要测试的所有代码移至您的库中。

关于haskell - 如何在堆栈中使用HUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com