[英]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.