<?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 Mark St. Godard on Medium]]></title>
        <description><![CDATA[Stories by Mark St. Godard on Medium]]></description>
        <link>https://medium.com/@markst.godard?source=rss-1fdad33c950a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hqWdMablijq68Exm.jpg</url>
            <title>Stories by Mark St. Godard on Medium</title>
            <link>https://medium.com/@markst.godard?source=rss-1fdad33c950a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:50:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@markst.godard/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[Unit testing in Go with Ginkgo: Part 1]]></title>
            <link>https://medium.com/boldly-going/unit-testing-in-go-with-ginkgo-part-1-ce6ff06eb17f?source=rss-1fdad33c950a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce6ff06eb17f</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Mark St. Godard]]></dc:creator>
            <pubDate>Wed, 03 Apr 2019 16:55:10 GMT</pubDate>
            <atom:updated>2019-04-03T16:55:10.896Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oPbfQSI6E09FT1WtQszb3Q.jpeg" /></figure><p>At <a href="https://boldcommerce.com/">Bold</a>, many of our back-end services are written in Go. On any given day, over 80,000 eCommerce websites use our software to power their online sales. Any downtime or severe bugs could result in lost sales and revenue for these merchants, which is why it’s important that we focus on delivering high-quality production code.</p><p>Test-driven development (<a href="https://en.wikipedia.org/wiki/Test-driven_development">TDD</a>) is an important practice in our software development process. It helps give us confidence that the changes we’re introducing won’t cause any unintended side effects.</p><p>Go has a fairly capable built-in testing package, however it does have some limitations. This article will explain (with some simple examples) how we use <a href="https://github.com/onsi/ginkgo">Ginkgo</a> and <a href="https://github.com/onsi/gomega">Gomega</a> at Bold to write more expressive and structured unit tests in Go.</p><h3>Background</h3><p>Let’s first take a look at Go’s testing package and toolchain:</p><p>For example, I have a package adder which provides an <em>Add </em>function that sums two integers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6e05f85ea483109aec33025164a371c/href">https://medium.com/media/a6e05f85ea483109aec33025164a371c/href</a></iframe><p>Using the testing package, a test could be written like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4465b898e4b59188db8e670e62a3408c/href">https://medium.com/media/4465b898e4b59188db8e670e62a3408c/href</a></iframe><p>Run the test in your terminal with go test.</p><pre><strong>$ go test </strong></pre><pre><strong>PASS</strong> <strong>ok</strong>      <br>github.com/markstgodard/adder   0.006s</pre><p>Great, a passing unit test, but what if my function has a bug and subtracts instead of adds? What would the output of a go test look like?</p><pre><strong>$ go test </strong><br>--- FAIL: TestAdd (0.00s)     <br>    adder_test.go:8: Sum, got: -1, want: 5. <br><strong>FAIL</strong> <br>exit status 1 <br><strong>FAIL</strong>    github.com/markstgodard/adder   0.006s</pre><p>This is fine for basic use, but the built-in Go testing package has two major limitations:</p><ul><li>It only provides a simple <a href="https://en.wikipedia.org/wiki/XUnit">XUnit</a> style of tests</li><li>It does not provide a library for assertions</li></ul><h3>Enter Ginkgo 🎉</h3><p><a href="https://github.com/onsi/ginkgo">Ginkgo</a> is a Go unit testing framework, created by <a href="https://twitter.com/onsijoe">Onsi Fakhouri</a>, that provides for a <a href="https://en.wikipedia.org/wiki/Behavior-driven_development">BDD</a> style of unit tests (think <a href="http://rspec.info/">RSpec</a>, but for Go).</p><h3>Getting started</h3><p>To get started with <strong>Ginkgo</strong>, follow these steps.</p><h4>Download Ginkgo</h4><p>Install the latest version of ginkgo with go get.</p><pre><strong>$ </strong>go get -u github.com/onsi/ginkgo/ginkgo <br><strong>$</strong> go get -u github.com/onsi/gomega/...</pre><h4>Create a Suite</h4><p>Ginkgo requires a test suite for each package that your program uses.</p><pre><strong>$ mkdir adder </strong>&amp;&amp;<strong> cd adder<br>$ ginkgo bootstrap </strong><br>Generating ginkgo test suite bootstrap for adder in:                          <br>    adder_suite_test.go</pre><p>This will create a test suite to bootstrap running your tests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/812f5844816149c147b06013a3a3f1f0/href">https://medium.com/media/812f5844816149c147b06013a3a3f1f0/href</a></iframe><h4>Create a new unit test</h4><p>Now that we have a suite, let’s create our first Ginkgo unit test!</p><pre><strong>$ ginkgo generate </strong><br>Generating ginkgo test for Adder in:   <br>   adder_test.go</pre><p>This will generate an empty test file, into which you can start adding individual unit tests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdc80130a564917d6b6c34e88971d6be/href">https://medium.com/media/cdc80130a564917d6b6c34e88971d6be/href</a></iframe><p>Now, let’s update and add our first test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdeafc8c810dd77016b33489b3128ae4/href">https://medium.com/media/fdeafc8c810dd77016b33489b3128ae4/href</a></iframe><h4>Running unit tests</h4><p>To run our unit tests, we run Ginkgo.</p><pre><strong>$ ginkgo </strong><br>Running Suite: Adder Suite <br>========================== <br>Random Seed: 1552860025 <br>Will run 1 of 1 specs  </pre><pre><strong>• </strong><br>Ran 1 of 1 Specs in 0.000 seconds <br>SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped <br>PASS  </pre><pre>Ginkgo ran 1 suite in 1.549965067s <br>Test Suite Passed</pre><p>Now, let’s say our requirements change and we’re only allowed to add positive numbers. Using the TDD process, let’s start by adding a failing test.</p><p>We can use <strong>Context</strong> to indicate specific pre-conditions, such as <em>when</em> our inputs are valid or not valid:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a2d78dedd2ead6938d20a6032703e44/href">https://medium.com/media/6a2d78dedd2ead6938d20a6032703e44/href</a></iframe><p>Now, we’ll re-run our tests.</p><pre><strong>$ ginkgo</strong><br>Running Suite: Adder Suite<br>==========================<br>Random Seed: 1553135447<br>Will run 2 of 2 specs</pre><pre><strong>•</strong><br>------------------------------<br><strong>•</strong> <strong>Failure</strong> [0.001 seconds]<br><strong>Adder</strong><br>/Users/mark/go/src/github.com/markstgodard/adder/adder_test.go:10<br>  <strong>Add</strong><br>  /Users/mark/go/src/github.com/markstgodard/adder/adder_test.go:12<br>    <strong>when summand is negative</strong><br>    /Users//go/src/github.com/markstgodard/adder/adder_test.go:24<br>      <strong>returns an err</strong> [It]<br>      /Users/mark/go/src/github.com/markstgodard/adder/adder_test.go:26</pre><pre><strong>Expected an error to have occurred</strong>.  Got:<br>          &lt;nil&gt;: <strong>nil</strong></pre><pre>/Users/mark/go/src/github.com/markstgodard/adder/adder_test.go:28<br>------------------------------</pre><pre>Summarizing 1 Failure:</pre><pre>[Fail] Adder Add when summand is negative [It] returns an err<br>/Users/mark/go/src/github.com/markstgodard/adder/adder_test.go:28</pre><pre>Ran 2 of 2 Specs in 0.001 seconds<br>FAIL! -- <strong>1 Passed</strong> | <strong>1 Failed</strong> | 0 Pending | 0 Skipped<br>--- FAIL: TestAdder (0.00s)<br>FAIL</pre><pre>Ginkgo ran 1 suite in 1.658112523s<br>Test Suite Failed</pre><p>Okay, so we have a failing test. Time to update our function to get the test to pass.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/198df51d8f6170e2f8d14e91076a2182/href">https://medium.com/media/198df51d8f6170e2f8d14e91076a2182/href</a></iframe><p>Now, we’ll re-run them so that we should have two passing tests.</p><pre><strong>$ ginkgo</strong><br>Running Suite: Adder Suite<br>==========================<br>Random Seed: 1553135928<br>Will run 2 of 2 specs</pre><pre>••<br>Ran 2 of 2 Specs in 0.000 seconds<br>SUCCESS! -- 2 Passed | 0 Failed | 0 Pending | 0 Skipped<br>PASS</pre><pre>Ginkgo ran 1 suite in 1.789141688s<br>Test Suite Passed</pre><blockquote><strong><em>Tip: </em></strong><em>Using </em><strong><em>-v </em></strong><em>provides more of a human-readable description of your unit tests.</em></blockquote><h3>Summary</h3><p>Ginkgo is a powerful unit testing framework, providing a BDD-style DSL that runs on top of the standard Go testing infrastructure.</p><p>Ginkgo provides the DSL that allows developers to compose tests in a more expressive way.</p><p><strong>Describe </strong>blocks define the thing you’re testing.</p><p><strong>Context </strong>blocks define the <em>when</em> of the test (i.e. “when invalid request is received,” “when service is unavailable”).</p><p><strong>It</strong> blocks run the code to test and state what you expect to happen.</p><p><strong>BeforeEach</strong> blocks run before each unit test ( It blocks).</p><p><strong>AfterEach </strong>blocks run after each unit test.</p><h3>Next steps 🚀</h3><p>Ginkgo helps you write more expressive unit tests in Go (like the one we did today) to make your program safer and more dependable.</p><p>Our next post will take a deeper dive into Ginkgo, including:</p><ul><li>More advanced assertion matching using <a href="https://github.com/onsi/gomega">Gomega</a></li><li>How to generate “fakes” to mock your dependencies</li><li>How to write tests with concurrent code</li><li>Tips and tricks we’ve learned along the way</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce6ff06eb17f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/boldly-going/unit-testing-in-go-with-ginkgo-part-1-ce6ff06eb17f">Unit testing in Go with Ginkgo: Part 1</a> was originally published in <a href="https://medium.com/boldly-going">Boldly Going</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>