Code coverage is enabled by passing the
--coverage flag to
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:
1) Textual coverage reports in the build output.
2) A unified textual and HTML report, considering the coverage on all local libraries, based on all of the tests that were run.
3) An index of all generated HTML reports, at
"stack hpc report" command¶
stack hpc report command generates a report for a selection of targets and
.tix files. For example, if we have 3 different packages with test-suites,
C, the default unified report will have coverage from
all 3. If we want a unified report with just two, we can instead run:
$ stack hpc report A B
This will output a textual report for the combined coverage from
test-suites, along with a path to the HTML for the report. To further
streamline this process, you can pass the
--open option, to open the report in
This command also supports taking extra
.tix files. If you've also built an
executable, against exactly the same library versions of
you could do the following:
$ stack exec -- an-exe $ stack hpc report A B C an-exe.tix
This report will consider all test results as well as the newly generated
an-exe.tix file. Since this is a common use-case, there is a convenient flag
to use all stored results -
stack hpc report --all an-exe.tix.
During the execution of the build, you can place additional tix files in
$(stack path --local-hpc-root)/extra-tix-files/ 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
$(stack path --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.
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 #1940.
Before the build runs with
--coverage, the contents of
stack path --local-hpc-rootgets 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 folder.
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
$(stack path --local-hpc-root)/pkg-name/test-name/test-name.tix).
.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 #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 at
$(stack path --local-hpc-root)/pkg-name/test-name/index.html
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
$(stack path --local-hpc-root)/combined/all/index.html
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
$(stack path --local-hpc-root)/index.html