Cabal 测试覆盖率报告为空

Ref*_*ker 5 haskell cabal

这是我的项目结构:

.git
src/
  exe/
    Main.hs
  lib/
    ModuleA/
    ModuleB/
    UnitTests/
      UnitTests.hs
    MainLib.hs
giter.cabal
Run Code Online (Sandbox Code Playgroud)

我有一个可执行文件,它唯一做的就是调用main库中的函数。以下是 的内容src/exe/Main.hs

.git
src/
  exe/
    Main.hs
  lib/
    ModuleA/
    ModuleB/
    UnitTests/
      UnitTests.hs
    MainLib.hs
giter.cabal
Run Code Online (Sandbox Code Playgroud)

以下是giter.cabal文件的相关部分:

library 
    import: shared-properties
    exposed-modules: MainLib
    other-modules:
        ModuleA
        ModuleB

    build-depends:
      , brick
      , directory
      , fsnotify

    hs-source-dirs:   src/lib

executable giter
    import: shared-properties
    main-is:          Main.hs
    other-modules:

    build-depends:
        , giter
        , base
        , brick
    hs-source-dirs:   src/exe

test-suite giter-test
    type:             exitcode-stdio-1.0
    hs-source-dirs:   src/lib
    main-is:          UnitTests/UnitTests.hs
    default-language: Haskell2010
    other-modules:
        ModuleA
        ModuleB
    build-depends:
        , base
        , directory
        , HUnit
        , QuickCheck

Run Code Online (Sandbox Code Playgroud)

这是结果cabal new-test all --enable-coverage

.
.
.
[2 of 2] Linking /home/refaelsh/repos/giter/dist-newstyle/build/x86_64-linux/ghc-9.4.6/giter-0.1.0/build/giter/giter [Library changed]
Running 1 test suites...
Test suite giter-test: RUNNING...
Tests
  Events.hs
    handleSpecialKey_CommitOpen:   OK
    handleSpecialKey_CommitClosed: OK
    booleanDecision_True:          OK
    booleanDecision_False:         OK
  MainList.hs
    mainList_getUnstagedSlices:    OK
      +++ OK, passed 100 tests.
    mainList_getStagedSlices:      OK
      +++ OK, passed 100 tests.
    mainList_getTreeSlices:        OK (0.78s)
      +++ OK, passed 100 tests.

All 7 tests passed (0.81s)
Test suite giter-test: PASS
Test suite logged to:
/home/refaelsh/repos/giter/dist-newstyle/build/x86_64-linux/ghc-9.4.6/giter-0.1.0/test/giter-0.1.0-giter-test.log
Writing: hpc_index.html
Writing: hpc_index_fun.html
Writing: hpc_index_alt.html
Writing: hpc_index_exp.html
Test coverage report written to
/home/refaelsh/repos/giter/dist-newstyle/build/x86_64-linux/ghc-9.4.6/giter-0.1.0/hpc/vanilla/html/giter-test/hpc_index.html
1 of 1 test suites (1 of 1 test cases) passed.
Writing: hpc_index.html
Writing: hpc_index_fun.html
Writing: hpc_index_alt.html
Writing: hpc_index_exp.html
Package coverage report written to
/home/refaelsh/repos/giter/dist-newstyle/build/x86_64-linux/ghc-9.4.6/giter-0.1.0/hpc/vanilla/html/giter-0.1.0/hpc_index.html
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 两个 HTML 文件都是空的,这是其中之一(它们看起来都一样):
module  Top Level Definitions   Alternatives    Expressions
%   covered / total %   covered / total %   covered / total
  Program Coverage Total    -   0/0     -   0/0     -   0/0  
Run Code Online (Sandbox Code Playgroud)

为什么它们是空的?

  1. 为什么有 2 个 HTML 文件?我期待着一个,只是用于该项目的图书馆部分。

Pet*_*man 3

两个 HTML 文件都是空的,这是其中之一

...

为什么它们是空的?

较短的答案:

测试套件other-modules列表中的模块不包括在报告中。

简单的解决方案:

other-modules在您的存储库中,只需从测试套件中删除所有内容即可。看起来是这样的:

test-suite giter-test
    type:             exitcode-stdio-1.0
    hs-source-dirs:   src/lib
    main-is:          UnitTests/UnitTests.hs
    default-language: Haskell2010

    build-depends:
        , base
        , directory
        , HUnit
        , QuickCheck
        , split
        , tasty
        , tasty-hunit
        , tasty-quickcheck
Run Code Online (Sandbox Code Playgroud)

较长的答案:

在单元测试中,当您在 中定义的主模块中导入 ModuleA 或 ModuleB 时src/lib/UnitTests/UnitTests.hs,这些模块的导入方式不会被 Haskell Program Coverage (由该标志启用的工具--enable-coverage)跟踪。

当您运行时cabal new-test --enable-coverage,ModuleA 未编译为支持 Haskell 程序覆盖率。这是因为 ModuleA 是other-modules. 为了编译支持 Haskell 程序覆盖率的 ModuleA,您必须从other-modules测试套件列表中删除 ModuleA。

这允许您编译支持 Haskell 程序覆盖率的 ModuleA。

现在,您应该在生成的报告中看到 ModuleA 的一些内容。您可以对 ModuleB 执行相同的操作。

我希望这有帮助 :)


为什么有 2 个 HTML 文件?我期待着一个,只是用于该项目的图书馆部分。

.../giter-test/hpc_index.html文件是test-suite giter-test. 它报告有多少代码经过了test-suite giter-test.

另一方面,该.../giter-0.1.0/hpc_index.html文件是所有测试套件的测试覆盖率的报告。如果您添加另一个涵盖代码不同部分的测试套件,那么您将看到该新测试套件的第三个报告,并且该.../giter-0.1.0/hpc_index.html文件将包含所有测试套件的报告的聚合。