我试图弄清楚为什么包含被定义为模块的单元测试的测试文件在使用stack build --test
.
假设有一个从头开始定义的简单测试模块:
stack new test-module
cd test-module
vim package.yaml # remove "executables" section, add "hspec" as tests dependency
按照 Hspec 文档中的“入门”说明,我修改了以下文件:
第 1 步:描述您想要的行为
-- 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
第 2 步:编写一些代码
-- 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 相关?或者,也许我错过了一些明显的东西?