5

我试图弄清楚为什么包含被定义为模块的单元测试的测试文件在使用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 相关?或者,也许我错过了一些明显的东西?

4

1 回答 1

3

它是 Haskell 语言的一部分。

Haskell 程序是一组模块,按照惯例,其中一个必须被调用Main并且必须导出值main

允许使用仅由模块主体组成的模块的缩写形式。如果使用它,则假定标题为module Main(main) where.


Haskell 2010 报告,第 5 节(模块)https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005


另请参阅 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 用户指南中)。

于 2019-11-20T01:53:39.850 回答