当将测试文件定义为模块时,使用hspec定义的通过堆栈调用的测试会引发错误 [英] hspec defined tests invoked with stack throw an error when test file is defined as a module

查看:99
本文介绍了当将测试文件定义为模块时,使用hspec定义的通过堆栈调用的测试会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么使用stack build --test运行包含单元测试(定义为模块)的测试文件失败的原因.

I'm trying to get my head around the reason why the test file containing unit-tests which is defined as a module fails when run with stack build --test.

假设使用以下内容从头开始定义一个简单的测试模块:

Say having a simple test module defined from scratch with:

stack new test-module
cd test-module

vim package.yaml # remove "executables" section, add "hspec" as tests dependency

遵循Hspec文档中的"入门"指示,我已经修改了此类文件为:

Following "getting started" instructions from Hspec documentation I've modified files such as:

-- 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文件中注释掉模块定义"行时,构建成功并且单元测试通过:

When I comment out the "module definition" line from the test/Spec.hs file the build succeeds and the unit-test passes:

-- 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相关?还是我缺少明显的东西?

Is that Hspec related or Stack related? Or maybe am I missing something obvious?

推荐答案

它是Haskell语言的一部分.

It's part of Haskell the language.

Haskell程序是模块的集合,按照惯例,其中一个模块必须称为Main,并且必须导出值main.

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

An abbreviated form of module, consisting only of the module body, is permitted. If this is used, the header is assumed to be module Main(main) where.

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

The Haskell 2010 report, section 5 (Modules) https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005


另请参阅组织文档,以了解有关package.yaml为其代理的配置的信息,该字段包含测试/可执行文件:


See also the cabal documentation, about the configuration that package.yaml is a proxy for, the field containing the test/executable file:

main-is:(...)尽管文件​​名可能有所不同,但模块本身必须命名为Main.

main-is: (...) while the name of the file may vary, the module itself must be named Main.

https://www.haskell.org/cabal/users-guide/developing-packages.html#pkg-field-executable-main-is


GHC有一个选项-main-is MyModule.mymain可以覆盖此行为(


GHC has an option -main-is MyModule.mymain to override this behavior (documented in the GHC user guide).

这篇关于当将测试文件定义为模块时,使用hspec定义的通过堆栈调用的测试会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆