Haskell 测试工作流程 [英] Haskell testing workflow

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

问题描述

我刚刚开始了一个新的 Haskell 项目,并希望从一开始就建立一个良好的测试工作流程.似乎 Haskell 拥有许多出色且独特的测试工具以及许多不同的集成方式.

I just started a new Haskell project and wanted to set up a good testing workflow from the beginning. It seems like Haskell has a lot of excellent and unique testing tools and many different ways to integrate them.

我已经调查过:

这些似乎在他们的领域都运行良好,但我正在寻找一种全面的测试方法,并想知道哪些对其他人运行良好.

Which all seem to work very well in their domains, but I'm looking for a comprehensive approach to testing and was wondering what has worked well for other people.

推荐答案

正确的单元测试、代码覆盖率和基准测试主要是选择正确的工具.

Getting unit testing, code coverage, and benchmarks right is mostly about picking the right tools.

  • test-framework 提供了一个一站式商店来运行您所有的 HUnit 测试用例和 QuickCheck 属性全部来自一个线束.
  • 代码覆盖率以 HPC 工具的形式内置于 GHC 中.
  • Criterion 提供了一些非常棒的基准测试机制
  • test-framework provides a one-stop shop to run all your HUnit test-cases and QuickCheck properties all from one harness.
  • Code coverage is built into GHC in the form of the HPC tool.
  • Criterion provides some pretty great benchmarking machinery

我将使用一个刚开始启用单元测试、代码覆盖率和基准测试的包作为运行示例:

I'll use as a running example a package that I just started enabling with unit testing, code coverage, and benchmarks:

http://github.com/ekmett/speculation

您可以通过为它们添加部分并将它们隐藏在标志之后,将您的测试和基准直接集成到您的 cabal 文件中,这样它们就不会使您的库的每个用户都必须访问(并且想要自己使用)您选择的测试工具的确切版本.

You can integrate your tests and benchmarks directly into your cabal file by adding sections for them, and masking them behind flags so that they don't make it so that every user of your library has to have access to (and want to use for themselves) the exact version of the testing tools you've chosen.

http://github.com/ekmett/speculation/blob/master/投机阴谋

然后,您可以告诉 cabal 如何运行您的测试套件.由于 Cabal 测试尚不存在——我们有一名学生在为今年夏天的代码编写它!-- 我们拥有的最好的机制是 这里是如何使用 cabal 的用户钩子机制.这意味着使用 cabal 切换到自定义"构建并设置 testHook.运行使用 test-framework 编写的测试程序的 testHook 示例,然后将 hpc 应用于配置文件,可以在此处找到:

Then, you can tell cabal about how to run your test suite. As cabal test doesn't yet exist -- we have a student working on it for this year's summer of code! -- the best mechanism we have is Here is how to use cabal's user hook mechanism. This means switching to a 'Custom' build with cabal and setting up a testHook. An example of a testHook that runs a test program written with test-framework, and then applies hpc to profile it can be found here:

http://github.com/ekmett/speculation/blob/master/设置.lhs

然后您可以使用 test-framework 将 QuickCheck 和 HUnit 测试捆绑到一个程序中:

And then you can use test-framework to bundle up QuickCheck and HUnit tests into one program:

http://github.com/ekmett/speculation/blob/master/测试.hs

那里的 cabal 文件小心地开启 -fhpc 以启用代码覆盖率测试,然后 Setup.lhs 中的 testHook 手动运行 hpc 并将其输出写入您的 dist 目录.

The cabal file there is careful to turn on -fhpc to enable code coverage testing, and then the testHook in Setup.lhs manually runs hpc and writes its output into your dist dir.

对于基准测试,这个故事有点手动,没有cabal benchmark"选项.您可以将您的基准测试连接到您的测试挂钩中,但我喜欢手动运行它们,因为 Criterion 有很多图形报告选项.您可以将基准测试添加到 cabal 文件中,如上所示,为它们提供单独的编译标志,将它们隐藏在 cabal 标志后面,然后使用 Criterion 完成所有繁重的工作:

For benchmarking, the story is a little more manual, there is no 'cabal benchmark' option. You could wire your benchmarks into your test hook, but I like to run them by hand, since Criterion has so many graphical reporting options. You can add your benchmarks to the cabal file as shown above, give them separate compilation flags, hide them behind a cabal flag, and then use Criterion to do all the heavy lifting:

http://github.com/ekmett/speculation/blob/master/基准.hs

然后您可以从命令行运行您的基准测试,并获得带有基准测试结果等的弹出 KDE 窗口.

You can then run your benchmarks from the command line and get pop-up KDE windows with benchmark results, etc.

由于实际上您在开发 Haskell 代码时无论如何都生活在 cabal 中,因此将您的工具链与其集成很有意义.

Since in practice you're living in cabal anyways while developing Haskell code, it makes a lot of sense to integrate your toolchain with it.

编辑:Cabal 测试支持现在确实存在.参见 http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/developing-packages.html#test-suites

Edit: Cabal test support now does exist. See http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/developing-packages.html#test-suites

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

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