簡體   English   中英

如何在堆棧中使用 HUnit

[英]How to use HUnit in Stack

我有一個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 將您要測試的所有代碼移動到您的庫中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM