TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How I write unit tests in Go

67 点作者 badrequest大约 1 年前

6 条评论

kbolino大约 1 年前
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.
ashconnor大约 1 年前
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 未加载
coffeebeqn大约 1 年前
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 未加载
tommiegannert大约 1 年前
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_chocula大约 1 年前
i am terrified of clicking on that website link
jozvolskyef大约 1 年前
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 未加载