<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Talal Yousif</title>
    <description>The latest articles on DEV Community by Talal Yousif (@talalyousif).</description>
    <link>https://dev.to/talalyousif</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F863279%2Ff90ce26e-9cd6-4d38-b8a8-0a164a99a19d.png</url>
      <title>DEV Community: Talal Yousif</title>
      <link>https://dev.to/talalyousif</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/talalyousif"/>
    <language>en</language>
    <item>
      <title>Excluding files from code coverage in GO</title>
      <dc:creator>Talal Yousif</dc:creator>
      <pubDate>Tue, 17 May 2022 09:47:11 +0000</pubDate>
      <link>https://dev.to/talalyousif/excluding-files-from-code-coverage-in-go-291f</link>
      <guid>https://dev.to/talalyousif/excluding-files-from-code-coverage-in-go-291f</guid>
      <description>&lt;p&gt;It's not uncommon to find yourself in a situation where you would like to make an exception for some code from being included in code coverage; perhaps it's auto-generated code or perhaps it's a wrapper of a third party API. Unfortunately, unlike many other languages, there is no built-in way to do this in Go (&lt;a href="https://github.com/golang/go/issues/31280" rel="noopener noreferrer"&gt;Hopefully not for too long&lt;/a&gt;). In this article, we go over a simple workaround to achieve this goal.&lt;/p&gt;

&lt;p&gt;First things first, a quick recap. Code coverage can be generated using the &lt;code&gt;go cover&lt;/code&gt; tool. From the docs, usage of the tool is described as: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Usage of 'go tool cover':&lt;br&gt;
Given a coverage profile produced by 'go test':&lt;br&gt;
        go test -coverprofile=c.out&lt;br&gt;
Open a web browser displaying annotated source code:&lt;br&gt;
        go tool cover -html=c.out&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As such, we first run our tests using:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

go test ./... -coverprofile=coverage.out


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Subsequently, we generate code coverage using:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

go tool cover -html=coverage.out


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I like to run the commands one after the other in one call, like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

go test ./... -coverprofile=coverage.out &amp;amp;&amp;amp; go tool cover -html=coverage.out


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  The workaround
&lt;/h2&gt;

&lt;p&gt;It's simple. Code coverage is generated based on the &lt;code&gt;coverprofile&lt;/code&gt; which is no more than a plain text file annotating which lines of each code file are covered and which are not. Remove all lines about a file from the &lt;code&gt;coverprofile&lt;/code&gt; and it won't be included in the code coverage report. As simple as that. &lt;/p&gt;

&lt;p&gt;Here is what a &lt;code&gt;coverprofile&lt;/code&gt; looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy3uqab14j2ls2ts87olg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy3uqab14j2ls2ts87olg.png" alt="Screenshot of a coverprofile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It wouldn't be ideal if we have to manually manipulate the &lt;code&gt;coverprofile&lt;/code&gt; every time we want to generate code coverage. A better way would be to automate this process. For that, We need to do 2 things. The first is a file that holds paths to files to be excluded from code coverage. File paths in this file should include the full package name. I created a file named "exclude-from-code-coverage.txt". Here is a snippet of what it looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubjq3xggvhyykk02hdjd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubjq3xggvhyykk02hdjd.png" alt="Screenshot of exclude-from-code-coverage.txt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second is a script that removes files in exclude-from-code-coverage.txt from the &lt;code&gt;coverprofile&lt;/code&gt;. This simple script (named exclude-from-code-coverage.sh) does just that: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#!/bin/sh
while read p || [ -n "$p" ] 
do  
sed -i '' "/${p//\//\\/}/d" ./coverage.out 
done &amp;lt; ./exclude-from-code-coverage.txt


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And finally, tying it all together:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

go test ./... -coverprofile=coverage.out &amp;amp;&amp;amp; ./exclude-from-code-coverage.sh &amp;amp;&amp;amp; go tool cover -html=coverage.out


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's break it down: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first command, &lt;code&gt;go test ./... -coverprofile=coverage.out&lt;/code&gt; runs the tests and generates the &lt;code&gt;coverprofile&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The second command, &lt;code&gt;./exclude-from-code-coverage.sh&lt;/code&gt;, removes files we want to exclude from code coverage from the &lt;code&gt;coverprofile&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The third command, &lt;code&gt;go tool cover -html=coverage.out&lt;/code&gt;, generated the code coverage report.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
      <category>testing</category>
      <category>codecover</category>
    </item>
  </channel>
</rss>
