stack hpc commands¶
Code coverage is a measure of the degree to which the source code of a program
is executed when a test suite is run.
Haskell Program Coverage (HPC) is a
code coverage tool for Haskell that is provided with GHC. Code coverage is
enabled by passing the flag
stack hpc provides commands specific to HPC. Command
stack hpc for the
The following refers to the local HPC root directory. Its location can be obtained by command:
stack hpc report command¶
stack hpc report command generates a report for a selection of targets and
Pass the flag
--all for a report that uses all stored results.
Pass the flag --open` to open the HTML report in your browser.
During the execution of the build, you can place additional tix files in the
extra-tix-files subdirectory in the local HPC root directory, in order for
them to be included in the unified report. A couple caveats:
These tix files must be generated by executables that are built against the exact same library versions. Also note that, on subsequent builds with coverage, the local HPC root directory will be recursively deleted. It just stores the most recent coverage data.
These tix files will not be considered by
stack hpc reportunless listed explicitly by file name.
If we have three different packages with test suites, packages
C, the default unified report will have coverage from all three. If we want a
unified report with just two, we can instead command:
This will output a textual report for the combined coverage from
test suites, along with a path to the HTML for the report.
This command also supports taking extra
.tix files. If you've also built an
executable, against exactly the same library versions of
you could command the following:
This report will consider all test results as well as the newly generated
stack test --coverage is quite streamlined for the following use-case:
You have test suites which exercise your local packages.
These test suites link against your library, rather than building the library directly. Coverage information is only given for libraries, ignoring the modules which get compiled directly into your executable. A common case where this doesn't happen is when your test suite and library both have something like
hs-source-dirs: src/. In this case, when building your test suite you may also be compiling your library, instead of just linking against it.
When your project has these properties, you will get the following:
Textual coverage reports in the build output.
A unified textual and HTML report, considering the coverage on all local libraries, based on all of the tests that were run.
An index of all generated HTML reports, in
index.htmlin the local HPC root directory.
Most users can get away with just understanding the above documentation.
However, advanced users may want to understand exactly how
- The GHC option
-fhpcgets passed to all local packages. This tells GHC to output executables that track coverage information and output them to
the-exe-name.tixfiles will get written to the working directory of the executable.
When switching on this flag, it will usually cause all local packages to be rebuilt (see issue #1940).
Before the build runs with
--coverage, the contents of the local HPC root directory gets deleted. This prevents old reports from getting mixed with new reports. If you want to preserve report information from multiple runs, copy the contents of this path to a new directory.
Before a test run, if a
test-name.tixfile exists in the package directory, it will be deleted.
After a test run, it will expect a
test-name.tixfile to exist. This file will then get loaded, modified, and outputted to
pkg-name/test-name/test-name.tixin the local HPC root directory.
.tix file gets modified to remove coverage file that isn't associated
with a library. So, this means that you won't get coverage information for
the modules compiled in the
test-suite stanza of your Cabal
file. This makes it possible to directly union multiple
*.tix files from
different executables (assuming they are using the exact same versions of the
If there is enough popular demand, it may be possible in the future to give coverage information for modules that are compiled directly into the executable. See issue #1359.
Once we have a
.tixfile for a test, we also generate a textual and HTML report for it. The textual report is sent to the terminal. The index of the test-specific HTML report is available
pkg-name/test-name/index.htmlin the local HPC root directory.
After the build completes, if there are multiple output
*.tixfiles, they get combined into a unified report. The index of this report will be available at
combined/all/index.htmlin the local HPC root directory.
Finally, an index of the resulting coverage reports is generated. It links to the individual coverage reports (one for each test-suite), as well as the unified report. This index is available at
index.htmlin the local HPC root directory.