组织 Haskell 测试 [英] Organizing Haskell Tests
问题描述
所以我正在尝试遵循 建议的 Haskell 项目结构,但我遇到了一些问题组织我的测试.
So I'm trying to follow the suggested structure of a Haskell project, and I'm having a couple problems organizing my tests.
为了简单起见,让我们从:
For simplicity, let's start with:
src/Clue/Cards.hs # defines Clue.Cards module
testsuite/tests/Clue/Cards.hs # tests Clue.Cards module
一方面,我不确定在包含测试代码的 testsuite/tests/Clue/Cards.hs
中的模块该命名什么,另一方面,我不确定如何命名编译我的测试代码,以便我可以链接到我的源代码:
For one, I'm not sure what to name the module in testsuite/tests/Clue/Cards.hs
that contains the test code, and for another, I'm no sure how to compile my test code so that I can link to my source:
% ghc -c testsuite/tests/Clue/Cards.hs -L src
testsuite/tests/Clue/Cards.hs:5:0:
Failed to load interface for `Clue.Cards':
Use -v to see a list of the files searched for.
推荐答案
我自己采用了 Snap Framework 的方法对于他们的测试套件,基本上可以归结为:
I use myself the approach taken by Snap Framework for their test-suites, which basically boils down to:
- 使用测试框架,例如 haskell-test-framework 或 HTF
通过将
.Tests
附加到包含 IUT 的模块名称来命名包含测试的模块,例如:
- Use a test-framework such as haskell-test-framework or HTF
Name the modules containing tests by appending
.Tests
to the module-name containing the IUT, e.g.:
module Clue.Cards where ... -- module containing IUT
module Clue.Cards.Tests where ... -- module containing tests for IUT
通过使用单独的命名空间,您可以将测试放在单独的源文件夹 tests/
中,然后您可以使用单独的 Cabal 构建目标(另请参见 cabal test
-build-target 支持在最近的 Cabal 版本中)用于测试套件,其中在其 hs-source-dirs
设置中包含额外的源文件夹,例如:
By using separate namespaces, you can put your tests in a separate source-folder tests/
, you can then use a separate Cabal build-target (see also cabal test
-build-target support in recent Cabal versions) for the test-suite which includes the additional source folder in its hs-source-dirs
setting, e.g.:
Executable clue
hs-source-dirs: src
...
Executable clue-testsuite
hs-source-dirs: src tests
...
这是有效的,因为您的 IUT 和测试套件中的模块之间不再存在命名空间冲突.
This works, since there's no namespace collision between the modules in your IUT and the test-suite anymore.
这篇关于组织 Haskell 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!