我应该如何去测试一个单一的可执行包? [英] How should I go about testing a monolithic executable package?

查看:15
本文介绍了我应该如何去测试一个单一的可执行包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个模块的单片 executable 包.(我所说的单体"是指它的cabal文件中只有一个子句,即executable.)目前是以黑盒方式使用 shell 脚本进行测试.我以为我想为某些单独的功能编写单元测试,但 cabal 不同意:

I have a monolithic executable package with several modules. (What I mean by "monolithic" is that it only has one clause in its cabal file, and that is executable.) It is currently tested with shell scripts, in a black box manner. I thought I would like to write unit tests for some individual functions, but cabal does not agree:

% 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

这里是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

我的理解是,我应该将尽可能多的模块移动到内部库中,这将使测试套件能够依赖它们.但是,我不确定维护者是否会赞成这种激进的改变.有没有侵入性较小的方法?

My understanding is that I should move as many modules as possible to an internal library, which would make it possible for the test suite to depend on them. However, I am not sure the maintainers will approve of such radical change. Is there a less invasive way?

我的另一个担心是,只要 Main.hsexecutable x 子句中,我们不能将其导入 x-test,其中的功能(至少 main)将无法用于测试.那么,除了 shell 脚本,我应该如何测试这些功能呢?

My other concern is that, insofar as Main.hs is in the executable x clause, and we cannot import that into x-test, the functions therein (at the very least main) will be unavailable for testing. How should I go about testing these functions then, beside shell scripts?

推荐答案

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

It's completely okay to move modules to library stanza (or internal library stanza if you don't want to expose those modules).

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

How should I go about testing it, beside shell scripts?

在 Haskell 世界中有一种常见的做法是将所有内容(甚至 main 函数)移动到 library 中,因此您的 Main.hs 看起来像这样:

There's a common practice in Haskell world to move everything (even main function) into library so your Main.hs would look like this:

module Main where

import MyLib as Lib (main)

main :: IO ()
main = Lib.main

使用这种方法,您可以通过 Haskell 单元测试库对所有内容进行全面测试.

With this approach you can test completely everything via Haskell unit testing libraries.

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

However, I am not sure the maintainers will approve of such radical change. Is there a less invasive way?

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

Well, if maintainers care about their package, they should agree to this refactoring if they want better testing.

这篇关于我应该如何去测试一个单一的可执行包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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