How can I generate HTML code coverage reports with new cabal?

471 views Asked by At

Running stack test --coverage generates a nice HTML report showing what lines your test suite covers. How can I achieve the same thing using cabal new-test?

I'm able to pass --enable-coverage to generate a .tix file but I'm not sure what to run on the .tix file to generate the HTML report. I'm pretty sure it involves hpc but I haven't been able to work out the right command.

I have the standard Cabal configuration of my application being a library, with a test-suite for that library.

2

There are 2 answers

2
Peter Becich On

Cabal 3.6 should be able to generate the HPC report. There is one caveat; this error may appear:

Error:
Internal libraries only supported with per-component builds.
Per-component builds were disabled because program coverage is enabled

https://github.com/haskell/cabal/issues/6440

To avoid the error, add to cabal.project:

package *
  coverage: True
  library-coverage: True

then cabal test (without --enable-coverage). The report should be somewhere in dist-newstyle.

0
MaxGabriel On

It appears it's as easy as passing --enable-coverage to cabal new-test. I had previously been running tests with cabal new-run test:test to workaround some limitations of new-test (e.g. lacking streaming and colors), so the fix is to use new-test instead of new-run.