[英]hspec defined tests invoked with stack throw an error when test file is defined as a module
我試圖弄清楚為什么包含被定義為模塊的單元測試的測試文件在使用stack build --test
運行時失敗的原因。
假設有一個從頭開始定義的簡單測試模塊:
stack new test-module
cd test-module
vim package.yaml # remove "executables" section, add "hspec" as tests dependency
按照 Hspec 文檔中的“入門”說明,我修改了以下文件:
-- file test/Spec.hs
module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
-- file src/Lib.hs
module Lib (divides) where
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0
運行stack build --test
會拋出以下錯誤:
<no location info>: error:
output was redirected with -o, but no output will be generated
because there is no Main module.
當我從test/Spec.hs
文件中注釋掉“模塊定義”行時,構建成功並且單元測試通過:
-- file test/Spec.hs
-- Notice the next line is commented out:
-- module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
是 Hspec 相關還是 Stack 相關? 或者,也許我錯過了一些明顯的東西?
它是語言 Haskell 的一部分。
Haskell 程序是模塊的集合,按照慣例,其中一個必須稱為
Main
並且必須導出值main
。允許使用僅由模塊主體組成的模塊的縮寫形式。 如果使用它,則假定 header 是
module Main(main) where
.
Haskell 2010 報告,第 5 節(模塊)
另請參閱 cabal 文檔,關於package.yaml
是代理的配置,該字段包含測試/可執行文件:
main-is
: (...) 雖然文件名可能不同,但模塊本身必須命名為 Main。
https://www.haskell.org/cabal/users-guide/developing-packages.html#pkg-field-executable-main-is
GHC 有一個選項-main-is MyModule.mymain
來覆蓋此行為( 記錄在 GHC 用戶指南中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.