TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How I write unit tests in Go

67 pointsby badrequestabout 1 year ago

6 comments

kbolinoabout 1 year ago
For table-based testing, I have been using map[string]struct{...} for a while now instead of including the test name as a struct field. I have found it improves readability slightly and makes it harder to misname test cases (empty strings stand out more and duplicates won&#x27;t compile).<p>Also, the shadowing of t in t.Run is intentional. You should not try to work around it. There&#x27;s no risk of confusion either because you will always use the right one in the right place.
ashconnorabout 1 year ago
I&#x27;m far too lazy to write mocks by hand in go. You can generate a mock for a given interface with mockery <a href="https:&#x2F;&#x2F;github.com&#x2F;vektra&#x2F;mockery">https:&#x2F;&#x2F;github.com&#x2F;vektra&#x2F;mockery</a>
评论 #40003642 未加载
coffeebeqnabout 1 year ago
So unless I pepper every test and subtest with t.Parallel() it will always run sequentially ? I gotta try that. Also I wish I could just declare that once
评论 #39998126 未加载
评论 #39997786 未加载
评论 #40001673 未加载
tommiegannertabout 1 year ago
Nice showdown of a bunch of scenarios.<p>I&#x27;d add using testing.T.Cleanup for tearing down the testcontainer (or use a TestMain and a deferred if the container is slow and concurrency-safe.)
count_choculaabout 1 year ago
i am terrified of clicking on that website link
jozvolskyefabout 1 year ago
I would vote this down if I could for the following reasons:<p>- I prefer state-based TDD as opposed to interaction-based (see <a href="https:&#x2F;&#x2F;martinfowler.com&#x2F;articles&#x2F;mocksArentStubs.html" rel="nofollow">https:&#x2F;&#x2F;martinfowler.com&#x2F;articles&#x2F;mocksArentStubs.html</a>).<p>- I&#x27;ve used both testcontainers and dockertest, and from my experience dockertest is more robust.<p>- The capital T for the outer argument comes across as being hypercorrect. Why would one consider the shadowing of this argument bad?
评论 #39996484 未加载