Ign*_*rov 3 testing automated-tests haskell cabal
我有一个executable
带有几个模块的单片包.(我所说的"单片"的意思是它的cabal
文件中只有一个子句,就是这样executable
.)它目前使用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
Run Code Online (Sandbox Code Playgroud)
以下是相关部分package.cabal
:
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
Run Code Online (Sandbox Code Playgroud)
我的理解是,我应该将尽可能多的模块移动到内部库,这样可以使测试套件依赖于它们.但是,我不确定维护者是否会批准这种彻底的改变.是否存在侵入性较小的方式?
我的另一个问题是,Main.hs
就executable x
条款而言,我们无法将其导入x-test
,其中的功能(至少main
)将无法进行测试.在shell脚本旁边,我应该如何测试这些函数呢?
将模块移动到library
节(或者如果您不想公开这些模块的内部库节)是完全可以的.
除了shell脚本之外,我该如何进行测试呢?
有在Haskell世界的普遍做法的一切(甚至移动main
功能)到library
让你Main.hs
看起来像这样:
module Main where
import MyLib as Lib (main)
main :: IO ()
main = Lib.main
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您可以通过Haskell单元测试库完全测试所有内容.
但是,我不确定维护者是否会批准这种彻底的改变.是否存在侵入性较小的方式?
好吧,如果维护人员关心他们的软件包,他们应该同意这种重构,如果他们想要更好的测试.
如果无法将包移动到(可能是内部)库,您可以简单地将可执行文件的所有源添加到hs-source-dirs
测试套件的文件中,并将可执行文件的依赖项添加到build-depends
测试套件的文件中。
这样做的缺点是您将编译相同的文件两次:一次用于可执行文件,一次用于测试套件。