<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Go Testing Masterclass on Atharva Pandey</title><link>https://atharva.page/series/go-testing-masterclass/</link><description>Recent content in Go Testing Masterclass on Atharva Pandey</description><generator>Hugo</generator><language>en-us</language><copyright>Copyright ©</copyright><lastBuildDate>Tue, 25 Mar 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://atharva.page/series/go-testing-masterclass/index.xml" rel="self" type="application/rss+xml"/><item><title>Lesson 10: Test Architecture — Tests that survive refactoring</title><link>https://atharva.page/post/go/go-testing-architecture/</link><pubDate>Tue, 25 Mar 2025 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-architecture/</guid><description>&lt;p&gt;Every test suite starts clean. Then the codebase grows, the team grows, deadlines hit, and gradually the tests become the thing you dread touching. You add a feature and a hundred tests break — not because the feature is wrong, but because the tests were coupled to implementation details. You rename a method and spend more time updating tests than writing the feature. Sound familiar? The problem isn&amp;rsquo;t the tests themselves. It&amp;rsquo;s the architecture — the structure, the layering, and the principles that determine whether your test suite stays an asset or becomes a liability.&lt;/p&gt;</description></item><item><title>Lesson 9: Flaky Test Control — A flaky test is worse than no test</title><link>https://atharva.page/post/go/go-testing-flaky/</link><pubDate>Sat, 15 Feb 2025 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-flaky/</guid><description>&lt;p&gt;A flaky test is a test that sometimes passes and sometimes fails with no code change in between. It sounds like a minor annoyance. It&amp;rsquo;s actually corrosive. Once your team learns that the test suite sometimes fails for &amp;ldquo;no reason,&amp;rdquo; they start merging on red. They start dismissing failures. They lose trust in the test suite as a signal. A single reliably-failing test is informative; a dozen flaky tests teach your team to ignore failures. I&amp;rsquo;ve seen this destroy test suite culture on multiple teams.&lt;/p&gt;</description></item><item><title>Lesson 8: Race-Aware Tests — If it passes without -race, it hasn't passed</title><link>https://atharva.page/post/go/go-testing-race-aware/</link><pubDate>Mon, 20 Jan 2025 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-race-aware/</guid><description>&lt;p&gt;A test suite that passes without &lt;code&gt;-race&lt;/code&gt; is not a clean bill of health. It&amp;rsquo;s a test suite that hasn&amp;rsquo;t checked one of the most insidious categories of bugs in Go: data races. I&amp;rsquo;ve shipped code that passed every test, passed code review, and then caused memory corruption in production because two goroutines were reading and writing a map concurrently. The race detector would have found it in under a second. We just never ran it.&lt;/p&gt;</description></item><item><title>Lesson 7: Golden File Testing — Store expected output, compare on run</title><link>https://atharva.page/post/go/go-testing-golden-files/</link><pubDate>Thu, 05 Dec 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-golden-files/</guid><description>&lt;p&gt;Some functions produce output that&amp;rsquo;s too large or too structured to assert inline. A template renderer, a code generator, a JSON serializer for a deeply nested type, a CLI help text formatter — any of these can produce hundreds of lines of output that you need to verify is correct. Hardcoding that expected output inside the test function creates a wall of string literals. The golden file pattern solves this by storing the expected output in a file, comparing against it at test time, and offering a flag to regenerate it when the output intentionally changes.&lt;/p&gt;</description></item><item><title>Lesson 6: Mocking Alternatives — Interfaces over mocks, always</title><link>https://atharva.page/post/go/go-testing-mocking/</link><pubDate>Tue, 05 Nov 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-mocking/</guid><description>&lt;p&gt;Mocking has a bad reputation in the Go community, and some of it is deserved. Not because mocks are inherently wrong, but because the way most people use them — generating verbose stubs from interfaces, asserting on call counts, verifying argument order — produces tests that are coupled to implementation details rather than behaviour. Refactor the internals of a function without changing its public contract, and suddenly half your mocks break. That&amp;rsquo;s not a test problem. That&amp;rsquo;s a mock-as-test-double problem.&lt;/p&gt;</description></item><item><title>Lesson 5: Database Testing with Testcontainers — Mock the DB and you mock the truth</title><link>https://atharva.page/post/go/go-testing-database/</link><pubDate>Thu, 10 Oct 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-database/</guid><description>&lt;p&gt;I used to mock databases. I had a clean &lt;code&gt;Store&lt;/code&gt; interface, a &lt;code&gt;MockStore&lt;/code&gt; implementation for tests, and 100% coverage on my service layer. I felt good about it. Then we migrated from MySQL to PostgreSQL and discovered that a dozen subtle behaviours we&amp;rsquo;d been mocking around were wrong — &lt;code&gt;UPSERT&lt;/code&gt; semantics, NULL handling in &lt;code&gt;GROUP BY&lt;/code&gt;, timestamp precision, transaction isolation differences. The mock had been lying to us for months. Testcontainers fixed that.&lt;/p&gt;</description></item><item><title>Lesson 4: HTTP Handler Testing — httptest is your best friend</title><link>https://atharva.page/post/go/go-testing-http-handlers/</link><pubDate>Sun, 01 Sep 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-http-handlers/</guid><description>&lt;p&gt;Every Go HTTP handler is just a function that takes a &lt;code&gt;ResponseWriter&lt;/code&gt; and a &lt;code&gt;*Request&lt;/code&gt;. That&amp;rsquo;s the entire contract. The fact that the standard library ships with a testing package that lets you call that function with a fake writer and a real request — without starting a server, without binding a port — is a gift that too many people overlook. I spent months spinning up actual servers in tests before I discovered &lt;code&gt;net/http/httptest&lt;/code&gt;. I will not let you make the same mistake.&lt;/p&gt;</description></item><item><title>Lesson 3: Integration Tests — Unit tests lie, integration tests prove</title><link>https://atharva.page/post/go/go-testing-integration/</link><pubDate>Thu, 01 Aug 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-integration/</guid><description>&lt;p&gt;Unit tests feel productive. You write a function, you write a test, everything goes green, and you merge. But unit tests exist in a bubble — a bubble where every dependency returns exactly what you told it to return. The real world doesn&amp;rsquo;t do that. Your database serializes a &lt;code&gt;time.Time&lt;/code&gt; differently than you expect. Your HTTP client follows a redirect your mock never mentioned. Your message queue drops a message under backpressure that your fake queue cheerfully delivered. I&amp;rsquo;ve had unit test suites where every test passed and the feature didn&amp;rsquo;t work in staging. Integration tests are what close that gap.&lt;/p&gt;</description></item><item><title>Lesson 2: Fuzzing in Go — Let the machine find your edge cases</title><link>https://atharva.page/post/go/go-testing-fuzzing/</link><pubDate>Wed, 10 Jul 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-fuzzing/</guid><description>&lt;p&gt;There&amp;rsquo;s a class of bugs that you will never find by thinking about edge cases. You&amp;rsquo;ll think about empty strings, about zero values, about negative numbers. But will you think about the string that&amp;rsquo;s exactly 65,536 bytes? Or the UTF-8 sequence that&amp;rsquo;s technically valid but trips up a specific parser codepath? Or the floating point value that serializes and then fails to deserialize because of a precision edge in your JSON handling? You won&amp;rsquo;t. But a fuzzer will find it in under a minute.&lt;/p&gt;</description></item><item><title>Lesson 1: Subtests and t.Run — Name your test cases or debug blind</title><link>https://atharva.page/post/go/go-testing-subtests/</link><pubDate>Sat, 15 Jun 2024 00:00:00 +0000</pubDate><guid>https://atharva.page/post/go/go-testing-subtests/</guid><description>&lt;p&gt;I used to write tests the way I wrote the first functions I ever wrote in Go — flat, repetitive, and with names so generic that when they failed, I had no idea which case broke. &amp;ldquo;TestParseDate failed&amp;rdquo; is not information. It&amp;rsquo;s a dare. Go figure out which of the twelve implicit cases in the body is responsible. I wasted hours doing exactly that before I committed to subtests.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;t.Run&lt;/code&gt; is one of those language features that looks like a small convenience and turns out to be load-bearing for any serious test suite. Once you start using it, you wonder how you ever shipped anything without it.&lt;/p&gt;</description></item></channel></rss>