Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 我应该如何测试单片可执行程序包?_Haskell_Testing_Automated Tests_Cabal - Fatal编程技术网

Haskell 我应该如何测试单片可执行程序包?

Haskell 我应该如何测试单片可执行程序包?,haskell,testing,automated-tests,cabal,Haskell,Testing,Automated Tests,Cabal,我有一个带有多个模块的单片可执行包。(我所说的“单片”的意思是,它在cabal文件中只有一个子句,即可执行文件)它目前以黑盒方式使用shell脚本进行测试。我本想为一些单独的函数编写单元测试,但cabal不同意: % cabal new-test cabal: Cannot test the package x-0.0.0.0 because none of the components are available to build: the test suite 'x-test' is not

我有一个带有多个模块的单片
可执行
包。(我所说的“单片”的意思是,它在
cabal
文件中只有一个子句,即
可执行文件
)它目前以黑盒方式使用shell脚本进行测试。我本想为一些单独的函数编写单元测试,但
cabal
不同意:

% cabal new-test
cabal: Cannot test the package x-0.0.0.0 because none of the
components are available to build: the test suite 'x-test' is not
available because the solver did not find a plan that included the test suites
以下是包.阴谋集团的相关章节:

executable x
  ...
  other-modules: ...
  ...

test-suite x-test
    type: exitcode-stdio-1.0
    main-is: Test.hs
    build-depends: base, x
    hs-source-dirs: test
我的理解是,我应该将尽可能多的模块移动到一个内部库中,这将使测试套件能够依赖它们。然而,我不确定维护者是否会同意这种激进的改变。有没有一种侵入性较小的方法


我的另一个担忧是,只要
Main.hs
executable x
子句中,并且我们不能将其导入
x-test
,其中的函数(至少
Main
)将不可用于测试。那么,除了shell脚本之外,我应该如何测试这些函数呢?

完全可以将模块移动到
library
节(如果不想公开这些模块,也可以移动到内部库节)

除了shell脚本之外,我应该如何测试它

在Haskell world中,有一种常见的做法是将所有内容(甚至是
main
函数)移动到
library
中,因此您的
main.hs
如下所示:

module Main where

import MyLib as Lib (main)

main :: IO ()
main = Lib.main
使用这种方法,您可以通过Haskell单元测试库完全测试所有内容

然而,我不确定维护者是否会同意这种激进的改变。有没有一种侵入性较小的方法


好的,如果维护人员关心他们的包,如果他们想要更好的测试,他们应该同意这种重构。

如果将包移动到(可能是内部)库不是一个选项,您可以简单地将可执行文件的所有源添加到测试套件的
hs source dirs
,并将可执行文件的依赖项添加到测试套件的
构建依赖项中


这样做的缺点是,您将编译两次相同的文件:一次用于可执行文件,一次用于测试套件。

碰巧您知道在一个或多或少为人所知的包中将
main
移动到库中的示例?@IgnatInsarov我知道这个库:我经常看到类似的包(在库中使用
main
)但我想不出另外一个例子了。@IgnatInsarov
pandoc
就是一个例子。可执行文件是可执行的,但它将所有内容都委托给
Text.Pandoc.App
模块,但我最终会得到两个名为Main的模块,对吗?@IgnatInsarov在你的问题中提到,
Main
不应该暴露在测试套件中,所以最好不要包含它。我的措辞不清楚。不暴露主要内容不是一个要求,这只是我认为的阴谋集团形式的一个限制。顺便问一下,这真的发生在野外吗?你能想到一个例子吗?在或多或少广为人知的软件包中?@IgnatInsarov我想不起一个例子。