# Nim Code Coverage

DevFeed: [Nim Code Coverage](<https://devfeed.tech/articles/nim-code-coverage-30829.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/nim-code-coverage/>)

Published: 2016-10-31T23:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [code-coverage](<https://devfeed.tech/tags/code-coverage.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [nim](<https://devfeed.tech/tags/nim.md>)

## AI overview

A tutorial on generating Nim code coverage reports with gcov and lcov. It shows how to enable Nim line information and compiler coverage flags, while noting that optimization and dead-code elimination can exclude impossible code and uncovered functions from coverage results.

## Source excerpt

Creating code coverage reports with Nim is surprisingly easy. You can simply use the good old gcov and lcov tools. Nim can be told to insert its own line information with the --debugger:native command line parameter. Here's the small example program we're looking at: var x = 0 if x > 1: echo "foo" echo "bar" Note that if we change the condition to if false: the Nim compiler optimizes the impossible code away and it will not count as uncovered. The same thing can happen with entire uncovered functions when optimizations and dead code elimination are enabled. The file is saved as x.nim. Here's the script I use to create the code coverage report: #!/bin/sh rm -rf *.info html nimcache nim --debugger:native --passC:--coverage --passL:--coverage c x lcov --base-directory . --directory . --zerocounters -q ./x lcov --base-directory . --directory . -c -o x.info lcov --remove x.info "lib/*" -o x.info # remove Nim system libs from coverage genhtml -o html x.info You can look at the final html report generated.