如何使用bash测试cabal

Mik*_*cki 5 haskell cabal

对于我的项目,我已经将一些单元测试写成了bash脚本.确实没有合理的方法在Haskell中编写测试.

我想在输入时运行这些脚本cabal test.我该如何做到这一点?

Gab*_*lez 2

首先,您需要将测试套件添加到您的 cabal 文件中。为此,您将使用exitcode-stdio测试套件,它看起来像这样:

Name:           foo
Version:        1.0
License:        BSD3
Cabal-Version:  >= 1.9.2
Build-Type:     Simple

Test-Suite test-foo
    type:       exitcode-stdio-1.0
    main-is:    test-foo.hs
    build-depends: base
Run Code Online (Sandbox Code Playgroud)

上面的示例测试套件取自Cabal 测试套件文档

然后在您的test-foo.hs文件中,您将运行 bash 脚本并因非零退出代码异常而死亡。您可以使用以下方法执行此操作System.Process:

-- test-foo.hs
import System.Exit (ExitSuccess)
import System.Process (system)

main = do
    -- This dies with a pattern match failure if the shell command fails
    ExitSuccess <- system "./myprog"
    return ()
Run Code Online (Sandbox Code Playgroud)

然后您可以使用以下命令运行上述测试cabal test,如果您的 shell 程序有非零退出代码,它将报告测试失败。