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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Go Testing by Example [video]

178 点作者 ash超过 1 年前

16 条评论

tylerneylon超过 1 年前
I like the txtar file format mentioned in this video. (Described below.) I’ve worked with teams who would have rejected this idea from a junior dev because it might sound too simple. It’s easy to forget the inherent value of simplicity, which is at odds with impressive-sounding features, or demonstrating hard-won coding sophistication.<p>This is the complete txtar file format: One or more comment lines followed by zero or more virtual files. Each virtual file begins with a line like “-- filename --“ and ends at the next such line. Specifically, these filename lines are those beginning with dash dash space and ending with space dash dash. That’s the whole format.<p>It avoids the need to have multiple real files all over the place when you want just one real file but you want your code to think in terms of multiple files. (Eg have one txtar file that combines different blocks of test data.)
评论 #38545960 未加载
评论 #38540179 未加载
评论 #38542596 未加载
peter_l_downs超过 1 年前
Agreed with most of this but I’m skeptical of the rsc.io&#x2F;script dsl approach. I’ll try it, though, because Russ is often right.<p>shameless advert: do you wish testify was implemented with generics and go-cmp, and had a more understandable surface area? Check out my small library, testy <a href="https:&#x2F;&#x2F;github.com&#x2F;peterldowns&#x2F;testy">https:&#x2F;&#x2F;github.com&#x2F;peterldowns&#x2F;testy</a><p>shameless advert: do you want to write tests against your postgres database, but each new test adds seconds to your test suite? Check out pgtestdb, the marginal cost of each test is measured in tens of milliseconds, and each test gets a unique and isolated postgres instance — with all your migrations applied. <a href="https:&#x2F;&#x2F;github.com&#x2F;peterldowns&#x2F;pgtestdb">https:&#x2F;&#x2F;github.com&#x2F;peterldowns&#x2F;pgtestdb</a>
评论 #38539787 未加载
评论 #38540236 未加载
评论 #38600065 未加载
评论 #38540909 未加载
mcint超过 1 年前
Wow! Someone brought up and demonstrated go&#x27;s &quot;example tests&quot; just yesterday to me and a group. He praised them highly for tech-adjacent fields where people don&#x27;t have familiarity with software engineering practices for modular, tested, or ready to share projects. We discuss infrastructure, and it was enlightening to be reminded of the tech-adjacent fields that could benefit greatly from a few more of the practices common in the tech industry. I suppose the Joel test and 12 Factor app would be indicative guides.
评论 #38539694 未加载
devjam超过 1 年前
A huge time-saver for me when adding table-driven test boilerplate in Go has been using gotests[0] to generate the template.<p>If you use VSCode with the Go extension it&#x27;s already available there as a command &quot;Go: Generate Unit Tests for Function&#x2F;Package&quot;.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;cweill&#x2F;gotests">https:&#x2F;&#x2F;github.com&#x2F;cweill&#x2F;gotests</a>
评论 #38540730 未加载
jeffrallen超过 1 年前
I worked on a team with a metric and associated goal of x% code coverage. So of course, even negotiating what x should be resulted in a week&#x27;s worth of meetings over a quarter. And the &quot;branch coverage versus line coverage&quot; war was long and ugly. When the metric was finally in place, it was of course immediately gamed to test getters and setters and to avoid the hard to test, sensitive integrated parts of the code.<p>This, in the same organization where another team was told by it&#x27;s VP, &quot;don&#x27;t schedule time for unit tests, we are too far behind schedule for a luxury like that&quot;.<p>All this to say, #3 &quot;coverage is not a substitute for thinking&quot; really hit me in the (grumpy) feels.
ijustlovemath超过 1 年前
Separating test data and test logic is exactly how we have built our test infrastructure to satisfy the FDA; making clear test data that you can then query and report on gives you tons of nice features for free!<p>It also really helps you to properly model your problem by breaking things out into their simplest possible components, and makes porting to a new implementation a breeze; reproduce a quick test data parser and some simple case logic, and you&#x27;re well on your way to having two totally matching, but also totally separate, implementations!<p>...all of this without reinventing Lisp, of course ;)
tptacek超过 1 年前
It&#x27;s hard to do a lively presentation like this without an audience. I&#x27;m a little jealous of how well he does it.
imran-iq超过 1 年前
See also Advanced Testing with Go by Mitchell Hashimoto[0]<p>---<p>0: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8hQG7QlcLBk" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=8hQG7QlcLBk</a>
评论 #38540437 未加载
dharmab超过 1 年前
Some similar resources I like:<p>&quot;Learn Go with Tests&quot; book: <a href="https:&#x2F;&#x2F;quii.gitbook.io&#x2F;learn-go-with-tests" rel="nofollow noreferrer">https:&#x2F;&#x2F;quii.gitbook.io&#x2F;learn-go-with-tests</a><p>&quot;Go by Example&quot;: <a href="https:&#x2F;&#x2F;gobyexample.com&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;gobyexample.com&#x2F;</a>
manlobster超过 1 年前
Test code doesn&#x27;t have tests of its own, so we must rely on inspection to establish that it is correct. This means that test code must be extremely readable.<p>It would make me uncomfortable to rely on tests like some of the examples in this talk, that have such a degree of complexity without explicit tests. Code such as to parse a text file should be in its own package. The package should expose an extremely readable API, and it should have its own suite of tests.<p>Similarly, writing formatting code for error messages and diffs inline makes the tests less readable and therefore less reliable. The golang standard library should include testing helpers such as in `testify`, which would allow tests to be concise, less buggy, and extremely readable.
ithkuil超过 1 年前
Long time Go user, recently using rust as well. One of the things I miss most in rust are subtests and t.Errorf as opposed to t.Fatal.<p>But perhaps I don&#x27;t know how to do it. In most of the rust codebases I worked with tests contain sequences of asserts (the failure of which causes the whole test to fail). Procedural macros are often employed to create many smaller tests that seem to address the use case of table driven tests + t.Error or subtests.<p>Macros in rust are ... adequate, but there is something bout being able to just express your logic in your main language instead of having to fiddle with a macro language and effectively code generation.<p>Does anybody have good tips for bringing some of the enjoyable testing patterns from Go into Rust?
评论 #38543885 未加载
ilrwbwrkhv超过 1 年前
Go tests are fantastic. Fast, easy to write and just works. Another reason we moved to Go when moving off of Common Lisp.
评论 #38539813 未加载
hknmtt超过 1 年前
I do like Go tests. Would love to have ability to find out untested parts of the code base though to increase coverage, but I do not think there is a way to measure this with available tools just yet.<p>Although I have yet to make integration tests that require live database and other dependencies. That will be fun...<p>Anyway, my main gripe with tests or TDD is that even if the code base is 95% finished, not talking libraries here, even small code changes can cause a cascade of changes that need to be addressed and tests essentially multiply the work load by a factor of 10, easy. And I am not talking about big changes. It might be a simple addition of a new struct field which suddenly breaks half of your test suite. Hence teste should be, in my experience, written as the absolutely last step before going live. Otherwise they might impose massive costs on time, and potentially money(if we&#x27;re talking actual business and not one man shot type of project).
评论 #38541807 未加载
评论 #38541775 未加载
评论 #38541817 未加载
评论 #38542956 未加载
GamerAlias超过 1 年前
Having not really written many tests before due to having worked moreso as a data scientist (a smidgen of pytest here and there), it&#x27;s so nice writing tests in Go. Definitely a considered and well implemented part of the language
评论 #38539567 未加载
earthboundkid超过 1 年前
I have a package that makes it easier to do the “use a glob of files as golden test cases” thing mentioned in the middle of the talk: <a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;github.com&#x2F;carlmjohnson&#x2F;be@v0.23.2&#x2F;testfile#Run" rel="nofollow noreferrer">https:&#x2F;&#x2F;pkg.go.dev&#x2F;github.com&#x2F;carlmjohnson&#x2F;be@v0.23.2&#x2F;testfi...</a>
jimmyed超过 1 年前
Russ Cox has fully imbibed his $DAYJOB into his life to the extent it feels unhealthy. Golang Shirt; multiple golang wall paintings; countless golang soft toys in the background and on his side. I am sure he has a few golang tattoos as well!
评论 #38540204 未加载
评论 #38548362 未加载
评论 #38540141 未加载