组织Haskell测试 [英] Organizing Haskell Tests
问题描述
为简单起见,我们从以下开始:
src / Clue / Cards.hs#定义Clue.Cards模块
testsuite / tests / Clue / Cards.hs#tests Clue.Cards模块
其中之一,我不确定在 testsuite / tests / Clue / Cards.hs
中包含测试代码,另外,我不知道如何编译我的测试代码,以便链接到我的源代码: >%ghc -c testsuite / tests / Clue / Cards.hs -L src
testsuite / tests / Clue / Cards.hs:5:0:
无法加载`Clue.Cards'界面:
使用-v查看搜索的文件列表。
我使用 Snap Framework ,它们的测试套件基本归结为:
- 使用测试框架,例如 haskell-test-framework 或 HTF
-
将包含测试的模块命名为将
.Tests
附加到包含IUT的模块名称,例如:
module Clue.Cards where ... - 包含IUT
通过使用单独的命名空间,你可以把你的测试放在一个单独的源文件夹中:
模块的模块Clue.Cards.Tests其中... - 包含IUT测试的模块
tests / $ c $
然后你可以使用一个单独的Cabal构建目标(另见
cabal test
-build-target support in最近的Cabal版本),其中包含 hs-source-dirs
设置中的额外源文件夹,例如: 可执行线索
hs-source-dirs:src
...
可执行线索测试套件
hs -source-dirs:src测试
...
这可以起作用,因为没有命名空间您的IUT中的模块和测试套件之间发生冲突。
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
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.
I use myself the approach taken by Snap Framework for their test-suites, which basically boils down to:
- 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
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 alsocabal test
-build-target support in recent Cabal versions) for the test-suite which includes the additional source folder in itshs-source-dirs
setting, e.g.:Executable clue hs-source-dirs: src ... Executable clue-testsuite hs-source-dirs: src tests ...
This works, since there's no namespace collision between the modules in your IUT and the test-suite anymore.
这篇关于组织Haskell测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!