将测试覆盖率与 HPC 相结合

jor*_*gen 7 haskell code-coverage haskell-program-coverage

我发现 HPC 确实令人困惑,即使在阅读了一些解释并进行了相当多的尝试之后也是如此。

我有一个库HML,以及两个测试套件fileio-testtypes-test,使用HTF(我计划转移到tasty)。我想运行这两个测试,然后查看两个测试在库中的综合覆盖率。

目前我使用构建库

cabal configure --enable-coverage
cabal build
Run Code Online (Sandbox Code Playgroud)

并使用运行测试

cabal configure --enable-coverage --enable-tests
cabal build
cabal test

hpc report --hpc-dir dist/hpc/vanilla/mix/fileio-test dist/hpc/vanilla/tix/fileio-test/fileio-test.tix
Run Code Online (Sandbox Code Playgroud)

这向我展示了一些报道,但不是正确的。我认为它显示了覆盖率,但仅来自其中一项测试,并且还包括测试本身的覆盖率。

我尝试使用

--hpc-dir dist/hpc/vanilla/mix/HML-0.1.0.0
Run Code Online (Sandbox Code Playgroud)

但是 HPC 抱怨它找不到它需要的模块文件。我还尝试将两个测试的覆盖范围结合起来,但没有成功。

有什么指点吗?

Pet*_*ich 2

我也尝试直接调用 HPC 并遇到类似的错误。

Cabal 3.6 应该能够为您生成 HPC 报告,而无需像您描述的那样调用 HPC。它可以绕过该错误。有一个警告: https://github.com/haskell/cabal/issues/6440#issuecomment-1133542171

添加cabal.project

package *
  coverage: True
  library-coverage: True
Run Code Online (Sandbox Code Playgroud)

然后cabal test。该报告应该位于 中的某个位置dist-newstyle


使用上述选项读取详细日志cabal test已向 HPC 显示了正确的参数。它解决了模块错误。这是 Mustache 的示例: https: //github.com/JustusAdam/mustache

添加cabal.project

package *
  coverage: True
  library-coverage: True
Run Code Online (Sandbox Code Playgroud)

然后cabal test -v all > foo.log

其中foo.log应该有对 HPC 的调用,例如:

~/.ghcup/ghc/8.10.7/bin/hpc markup \
dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/t/unit-tests/hpc/vanilla/tix/unit-tests/unit-tests.tix \
'--destdir=dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/t/unit-tests/hpc/vanilla/html/unit-tests' \
'--hpcdir=dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/hpc/vanilla/mix/unit-tests' \
'--hpcdir=dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/hpc/vanilla/mix/mustache-2.4.0' \
'--include=Text.Mustache' \
'--include=Text.Mustache.Types' \
'--include=Text.Mustache.Parser' \
'--include=Text.Mustache.Compile' \
'--include=Text.Mustache.Render'
Run Code Online (Sandbox Code Playgroud)

拿这个,替换markupreport,然后删除--destdir,这会给出类似的内容:

hpc report dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/t/unit-tests/hpc/vanilla/tix/unit-tests/unit-tests.tix \
'--hpcdir=dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/hpc/vanilla/mix/unit-tests' \
'--hpcdir=dist-newstyle/build/x86_64-linux/ghc-8.10.7/mustache-2.4.0/hpc/vanilla/mix/mustache-2.4.0' \
'--include=Text.Mustache' \
'--include=Text.Mustache.Types' \
'--include=Text.Mustache.Parser' \
'--include=Text.Mustache.Compile' \
'--include=Text.Mustache.Render'

Run Code Online (Sandbox Code Playgroud)

将其粘贴到 Mustache 项目根目录的终端中会产生:

 59% expressions used (635/1069)
 28% boolean coverage (4/14)
       0% guards (0/6), 1 always True, 5 unevaluated
      50% 'if' conditions (4/8), 1 always False, 3 unevaluated
     100% qualifiers (0/0)
 41% alternatives used (34/82)
 56% local declarations used (13/23)
 63% top-level declarations used (48/76)
Run Code Online (Sandbox Code Playgroud)