组织Haskell测试 [英] Organizing Haskell Tests

查看:108
本文介绍了组织Haskell测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图遵循 Haskell项目的建议结构,而且我遇到了一些问题组织我的测试。



为简单起见,我们从以下开始:

  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 ,它们的测试套件基本归结为:


  1. 使用测试框架,例如 haskell-test-framework HTF

  2. 将包含测试的模块命名为将 .Tests 附加到包含IUT的模块名称,例如:

      module Clue.Cards where ...  - 包含IUT 

    模块的模块Clue.Cards.Tests其中... - 包含IUT测试的模块
    通过使用单独的命名空间,你可以把你的测试放在一个单独的源文件夹中: tests /

  3. 然后你可以使用一个单独的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:

  1. Use a test-framework such as haskell-test-framework or HTF
  2. 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
    

  3. 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
      ...
    

    This works, since there's no namespace collision between the modules in your IUT and the test-suite anymore.

这篇关于组织Haskell测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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