Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# The Power of Go Tests — Reading Guide
## 【One-Line Pitch】
A practical, hands-on guide to writing effective tests in Go that will transform how you think about software design, correctness, and confidence in your code. Perfect for Go developers who want to move beyond basic testing and learn professional-grade testing techniques.
## 【Book Arc】
- **Opening (~0%–10%)**: Introduces the philosophy of test-driven development and why "self-testing code" is the foundation of confident programming. Sets up the core workflow: write tests first, watch them fail, then implement code to make them pass.
- **Early (~10%–32%)**: Walks through a complete test-first example (building a `ListItems` function for a text adventure game), demonstrating the full cycle: writing a failing test, implementing minimal code, using `cmp.Diff` for better failure messages, and iterating.
- **Middle (~32%–52%)**: Covers the "new behaviour, new test" rule, refactoring tests into table-driven tests, and systematically adding test cases one at a time. Emphasizes that tests are living documentation of intended behavior.
- **Middle (~52%–70%)**: Explores advanced testing topics including error handling patterns, constructing effective test inputs, property-based testing, and fuzz testing to find edge cases you didn't anticipate.
- **Late (~70%–90%)**: Introduces mutation testing to evaluate test quality, coverage analysis (as a signal, not a target), and techniques for testing "untestable" code like concurrency, CLI tools, and long-running tasks.
- **Ending (~90%–100%)**: Presents `testscript` for testing command-line interfaces and end-to-end program behavior, wrapping up with the philosophy that tests are communication tools for future developers.
## 【Key Takeaways】
- **Tests-first development forces clarity** (Early): Writing tests before implementation compels you to define function names, signatures, and expected behavior upfront—the hardest design decisions—before writing any code. This prevents vague thinking and wasted effort.
- **A test isn't real until you've seen it fail** (Early): The author emphasizes verifying that your test actually catches bugs by deliberately writing incorrect implementations first. If the test passes when it shouldn't, the test itself has problems.
- **"New behaviour, new test" is a discipline** (Middle): Every time you think of new functionality, you must write a test for it first. This rule prevents scope creep and ensures you always know when you've written enough code.
- **Table tests scale your coverage elegantly** (Middle): Structuring tests as tables of input/expected-output pairs lets you add cases one at a time, treating each as a distinct behavior to verify. This makes tests readable and maintainable.
- **Error handling is part of your API** (Middle): How your code handles errors—whether it ignores them, wraps them, or uses sentinel errors—is a design decision that should be tested deliberately. Use `errors.Is` and `errors.As` for robust error checking.
- **Fuzzing finds bugs you didn't know existed** (Middle–Late): Random input generation and property-based testing can uncover edge cases and panics that hand-written tests miss. Failing fuzz inputs should become permanent static test cases.
- **Mutation testing reveals test quality** (Late): Deliberately introducing bugs ("mutants") into your code and checking whether tests catch them exposes weak tests and untested code paths. Coverage percentage alone is misleading.
- **Tests are communication with future developers** (Throughout): Well-named tests read as documentation, capturing intent and serving as executable specifications. Test names should be sentences describing behavior, not implementation details.
## 【Reading Tips】
- **Deep-read Chapters 1–3** (the test-first workflow and test design philosophy): These chapters establish the mental model that everything else builds on. The `ListItems` example is worth following along with in your own editor.
- **Skim the praise and introduction** (~0–10%): The motivational content is nice but not essential. Jump straight to the first chapter's practical content.
- **Pay special attention to the "new behaviour, new test" section** (~42%): This is where the author addresses a common stumbling block for experienced programmers who are used to just writing code.
- **The fuzzing and mutation testing chapters** (~70–90%) are the most advanced material: If you're new to testing, consider reading these twice or returning after practicing the earlier techniques.
- **Try the exercises as you go**: The book is structured around building a text adventure game, so coding along with the examples will cement the concepts far better than passive reading.
## 【Coverage Limits】
This guide covers the book's progression from test-first fundamentals through advanced techniques like fuzzing and mutation testing, but the excerpts don't include the full details of the `testscript` chapter or the complete code listings for every example.
##
Excerpt 1
information Custom error types and errors.As Conclusions 5. Users shouldn’t do that Constructing effective test inputs User testing Crafting bespoke bug dete...
a clear idea of what it should do is simply a waste of time. It’s almost bound to be wrong in important ways. We’re also likely to end up with a design which...
ith some different error, we would know there was a problem. But it seems to be fine, so now we can go ahead and implement ListItems for real. Here’s one rou...
to insert an extra step in the process: writing a new test . Even when they’ve written a function test-first to start with, the temptation is then to start e...
+ ", and " + items [ len ( items )- 1 ] + "." } } ( Listing game/8 ) Did we break anything or change any behaviour? No, because the test ...
libraries, or do any code generation or anything like that. All you need to do is put your code in a file whose name ends with _test.go , import the testing ...
go test --- FAIL: TestRunningIsTrueWhenServiceIsRunning (0.00s) service_test.go:11: false And there we are. We’re at “red”. Now we can go ahead and fill in t...
se we need to set up some test fixture, such as a data file. Opening a file can always fail, so we need to check that error: f , err := os . Open ( "tes...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
The Power of Go Tests (John Arundel)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share to WeChat Moments
The Power of Go Tests (John Arundel)(Z-Library)
Scan the QR code with WeChat to open this book
Tap the ··· menu in the top-right corner
Choose Share to Moments
You are already in WeChat. Tap ··· in the top-right corner and choose Share to Moments.
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
The Power of Go Tests (John Arundel)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
这本书围绕 Go 语言中的测试实践展开,核心主张是测试不只是验证代码正确性的手段,更是推动设计、支撑重构和保障变更安全的基础。作者 John Arundel 以动手写代码的方式组织内容,让测试贯穿从功能构思到实现调整的整个过程,而不是把它当作写完代码之后的附加环节。
读者可以抓住的第一条线索是测试先行的写法。书中以一个老式文字冒险游戏为例,设想玩家在阁楼场景中看到物品列表的效果,由此引出函数在 Go 中如何以测试为先来编写。先明确期望的输出,再让实现逐步满足测试,这种方式让每一步改动都有明确的检验标准。
第二条线索是测试失败时的排查与修正。摘录中展示了调整 ListItems 函数以解决检测到的问题,并追问改动之后是否破坏了原有行为——答案是没有,因为测试仍然通过。这里体现的是测试作为安全网的作用:它让人能够放心修改实现,同时确认行为没有被意外改变。
第三条线索是测试环境的搭建与错误处理。书中提到有时需要准备测试夹具,例如打开一个数据文件,而打开文件总可能失败,因此必须检查返回的错误,并用 t.Fatal 在出错时终止测试。类似地,当代码调用外部包中的函数时,需要先正确导入,否则构建就会失败,书...
Digital LibraryAI Assistant
Hi — I can help you pick a book. Tell me a topic, language, or level, e.g. “a light English read”. If that's too broad, I'll ask one follow-up.
Loading comments...
Reply to Comment
Edit Comment