haskell Cabal测试覆盖率报告为空

9rnv2umw  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(149)

以下是我的项目结构:

.git
src/
  exe/
    Main.hs
  lib/
    ModuleA/
    ModuleB/
    UnitTests/
      UnitTests.hs
    MainLib.hs
giter.cabal

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

import qualified MainLib

main :: IO ()
main = MainLib.main

下面是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

下面是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

问题:
1.两个HTML文件都是空的,下面是其中一个(它们看起来都一样):

module  Top Level Definitions   Alternatives    Expressions
%   covered / total %   covered / total %   covered / total
  Program Coverage Total    -   0/0     -   0/0     -   0/0

为什么是空的?
1.为什么有两个HTMLs文件?我期待着一个,只是为图书馆的项目的一部分。

sqougxex

sqougxex1#

两个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

较长的答案:

在单元测试中,当你在src/lib/UnitTests/UnitTests.hs中定义的Main模块中导入ModuleA或ModuleB时,这些模块的导入方式会导致它们不被Haskell Program Coverage(由--enable-coverage标志启用的工具)跟踪。
运行cabal new-test --enable-coverage时,ModuleA不支持Haskell Program Coverage。这是因为ModuleA是other-modules之一。为了编译支持Haskell Program Coverage的ModuleA,您必须从测试套件的other-modules列表中删除ModuleA。
这允许您编译支持Haskell Program Coverage的ModuleA。
现在,您应该在生成的报告中看到ModuleA的一些内容。你也可以为ModuleB做同样的事情。
我希望这对你有帮助:)
为什么有两个HTMLs文件?我期待着一个,只是为图书馆的项目的一部分。
.../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文件将包含所有测试套件的报告的聚合。

相关问题