<?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 Shivani Sehdev on Medium]]></title>
        <description><![CDATA[Stories by Shivani Sehdev on Medium]]></description>
        <link>https://medium.com/@shivanisehdev?source=rss-53b0583ff83f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-DMazAaRfZhe-90y1L50ZA.png</url>
            <title>Stories by Shivani Sehdev on Medium</title>
            <link>https://medium.com/@shivanisehdev?source=rss-53b0583ff83f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:31:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivanisehdev/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[How to write and run tests in Golang]]></title>
            <link>https://blog.gopherslab.com/how-to-write-and-run-tests-in-golang-bcbd71f0cf84?source=rss-53b0583ff83f------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcbd71f0cf84</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[golang-testing]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[unit-test-framework]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Shivani Sehdev]]></dc:creator>
            <pubDate>Thu, 19 Nov 2020 12:26:21 GMT</pubDate>
            <atom:updated>2020-11-19T12:26:21.578Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Xa6I3YKzsUZEsJBaW2rgg.png" /><figcaption>Testing in Golang</figcaption></figure><h3>Why write test cases.</h3><p>Since you are here to this article you already feel the need for testing your code and this explanation is no different. <br>Writing test cases for your code gives you the confidence that any change to the code you made for the upcoming release will not break the existing functionality. <br>You can run your code with different possible inputs and ensure you are getting the expected results.</p><p>In this article, we will be covering:</p><ol><li>About Golang Testing Package</li><li>How to write Unit test cases with a single input</li><li>How to write Unit test cases with multiple inputs.</li><li>Different Commands to run test cases with example</li></ol><h3><strong>About Golang Testing Package</strong></h3><p>Golang provides an in-built testing package for writing test cases. So you will not have to depend upon any external libraries for testing your functionalities.</p><p>To getting started with your first unit test follow these conventions</p><ol><li>Import the golang “testing” package</li><li>The filename where you write your test function ends with _test.go. eg : filename_test.go</li><li>Function name starts with TestFunctionName() .<br><strong>Note the first letter after the Test is uppercase.</strong></li></ol><h3><strong>How to write Unit test cases with a single input</strong></h3><p>This is a very basic form of unit testing, where you test your function against a single input and compare the output with the expected output.<br>Let’s do it practically with the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caed26dea9d2be66c8314c3ccbf8130f/href">https://medium.com/media/caed26dea9d2be66c8314c3ccbf8130f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f15609962f62e1b65701ad543bc85c05/href">https://medium.com/media/f15609962f62e1b65701ad543bc85c05/href</a></iframe><p>In the example above I have started creating a package for golang algorithms and names it <strong><em>goalgos</em></strong>.</p><p>In file <strong><em>sum_numbers.go</em></strong>, I have written a function <strong><em>SumNumbersInList()</em></strong> that returns the sum of all the numbers in the list.<br>To test this function I have created a file <strong><em>sum_numbers_test.go</em></strong>. Here I have written a test function <strong><em>TestSumNumbersInList()</em></strong> that will run the function <strong>SumNumbersInList()</strong> for provided inputs.</p><p>In a very basic test, we run the function to be tested (TestSumNumbersInList) with sample inputs and compare the output with the expected output to make sure the function runs as expected</p><blockquote><strong>Wait! : What is t.Errorf ?</strong></blockquote><blockquote>Testing package has struct type T which provides various functions to run test cases, write log and Errors.</blockquote><blockquote>For the expected failure of the function, we use Errorf. If a test case it will return the Fail status with the message provided.<br>Usage of all these logs and test cases is very well explained in the go documentation (<a href="https://golang.org/pkg/testing/#T">https://golang.org/pkg/testing/#T</a>)</blockquote><p>Here we have run a very simple test case with single input which will not be the case always we have to test our function against different inputs. So to test another <strong>SumNumbersInList()</strong> for another input what we can do is something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f8e9c2614624dfa5605a3dff10a9d6e/href">https://medium.com/media/0f8e9c2614624dfa5605a3dff10a9d6e/href</a></iframe><p>Here we can see code is duplicated where we get output and compare for the expected output. Hence we need a way to pass multiple inputs and test again each input called <strong>Table driven tests</strong></p><h3><strong>How to write Unit test cases with multiple inputs AKA Table Driven tests</strong></h3><p>For the same case, we can pass multiple inputs. And test our code. As in the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96adc71656f8b428835da56f114497f9/href">https://medium.com/media/96adc71656f8b428835da56f114497f9/href</a></iframe><p>Here we have run the test case again with different input leveraging the <strong>t.Run() </strong>from a testing package.</p><p>A new test will be run for each input and at the end of all the executions if any error occur test case will fail, otherwise, it will return the success</p><h3><strong>Different Commands to run test cases with example</strong></h3><p>Here are different commands we can use to run the test cases. I have run these command for the example above and mentioned all the results</p><pre> <strong>go test </strong> : to run all _test.go files in the package.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/0*_LSJJB1bb0bzh7Co" /><figcaption><strong>go test</strong></figcaption></figure><pre><strong>go test filename_test.go</strong> : to run all tests in the file filename_test.go</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/0*rUh_6T2Q78neRMV7" /><figcaption>go test sum_numbers.go sum_numbers_test.go</figcaption></figure><pre><strong>go test -v</strong> : will display the result of all test cases with verbose logging.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*m_kWkK-H1pNi2qkelvdL3A.png" /></figure><pre><strong>go test -run TestFunctionName</strong> : to run specific function again all inputs provided</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/0*HIYCIib4vIDbCi-7" /><figcaption>go test -run TestSumNumbersInList</figcaption></figure><pre><strong>go test -run TestFunctionName/Inputvalue= </strong>: to run the test case for specific input. <br>Input is what is passed as first argument to the t.Run(InputValue“)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/0*shiDIbYGcyTME8Zh" /><figcaption>Here I run the `TestSumNumbersInList` for only the 5th index of input.</figcaption></figure><pre><strong>go test -run /InputValue</strong> : run test cases for matching inputs</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/0*7y_nnOs1Rp1hnrIr" /><figcaption>go test -run /SumNumbers=1 -v</figcaption></figure><p>Here we have covered how to use the golang testing package, write unit test cases with different inputs, and checked different commands to run tests.<br>This is how I started testing my function hope it will be helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcbd71f0cf84" width="1" height="1" alt=""><hr><p><a href="https://blog.gopherslab.com/how-to-write-and-run-tests-in-golang-bcbd71f0cf84">How to write and run tests in Golang</a> was originally published in <a href="https://blog.gopherslab.com/">Gophers Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>