<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Varun Jalandery on Medium]]></title>
        <description><![CDATA[Stories by Varun Jalandery on Medium]]></description>
        <link>https://medium.com/@jvary?source=rss-3aefcc967877------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FHZvw5Z8Cb0Ko7EbEyQMUQ.png</url>
            <title>Stories by Varun Jalandery on Medium</title>
            <link>https://medium.com/@jvary?source=rss-3aefcc967877------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:12:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jvary/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Golang Unit Tests — the right way — Part 1]]></title>
            <link>https://jvary.medium.com/golang-unit-tests-the-right-way-part-1-b77c4f5e1999?source=rss-3aefcc967877------2</link>
            <guid isPermaLink="false">https://medium.com/p/b77c4f5e1999</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Varun Jalandery]]></dc:creator>
            <pubDate>Thu, 19 Nov 2020 03:06:10 GMT</pubDate>
            <atom:updated>2020-11-19T03:59:12.083Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang Unit Tests — the right way — Part 1</h3><blockquote>Golang has the right ways of doing things.</blockquote><p>This article has two parts,<br> — <strong>In part 1</strong>, we will understand how to write a simple unit test in Golang.<br> By simple unit test, it means a unit test for a function that has <strong>no external dependencies </strong>such as a database, API, etc.<br> — what is a unit test?<br> — how to write a simple unit test in Golang?</p><p>— <strong>In part 2</strong>, we will learn how to write a unit test for a function that has <strong>external dependencies</strong> such as database, API, etc.<br>We will also learn how to mock the dependencies (the right way) by using <a href="https://github.com/golang/mock"><em>mockgen</em></a>.<br>We will also learn why it is important to create and use interfaces for making our code testable. This means one should <a href="https://medium.com/better-programming/code-against-interfaces-not-implementations-37b30e7ab992"><em>program to interfaces and not implementations</em>.</a></p><h3><strong>What is a unit test?</strong></h3><p>Unit testing is the smallest possible test for software. It is much easier to fix a code for a failing unit test because of its simplicity. Many production bugs can be prevented “embarrassingly :-)” by unit tests.</p><p>A unit test typically tests the truth table of a method or sample inputs/outputs.<br>e.g. consider a method which tells whether the number is “even or not”</p><p><strong>Unit test for a simple method with one parameter that returns a boolean value</strong></p><blockquote>func IsEven (number int) bool</blockquote><p>Below is the truth table for the above function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*n2o7CtPiMJdRSsg9Gm4GTQ.jpeg" /><figcaption>fig 1: Truth table for func IsEven(number int) bool</figcaption></figure><p>So a unit test should test this function for the expected inputs and outputs as per the table(fig 1) shown above.</p><p>And below is the unit test written in Golang</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daf6c973a87ee02e72ece4c9b8a95eec/href">https://medium.com/media/daf6c973a87ee02e72ece4c9b8a95eec/href</a></iframe><p><strong>Unit test for a function with multiple parameters that returns a desired value</strong></p><p>Consider another function(as below), which adds two numbers and returns the sum,</p><blockquote>func Add(a, b int) int</blockquote><p>Below are the sample inputs/outputs for the above function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*HMDjKmfwhENLJZPbqXVCBw.jpeg" /><figcaption>fig 3: Truth table for func Add(a, b int) int</figcaption></figure><p>And below is the unit test written in Golang</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f54802b81500cbf98b8fc886c2d192e1/href">https://medium.com/media/f54802b81500cbf98b8fc886c2d192e1/href</a></iframe><p>Thank you for reading so far. I will write <strong>part 2</strong> of this blog sooner and will post the link here.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b77c4f5e1999" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>