go-testing
How Go tests are written in these repos: assertions, tables, test doubles, helpers, async waits, golden files, fuzzing, benchmarks, and integration tests against a real Postgres via the bundled pgtest helper (one shared testcontainer, a fresh database per test). TRIGGER — load BEFORE writing, editing or reviewing any *_test.go file, testdata/ golden file, fuzz target or benchmark, or when the task says go test, test helper, fake, mock, stub, table test, testcontainers, TestMain, test database, flaky test or coverage in a Go repo. Plain requests count: "write the tests", "add a test for this", "fix the failing test", or a feature task whose last step is testing it. Load alongside go-idiomatic and tdd, not instead of them. SKIP when no Go test code is written or judged.
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- go-testing
- description
- How Go tests are written in these repos: assertions, tables, test doubles, helpers, async waits, golden files, fuzzing, benchmarks, and integration tests against a real Postgres via the bundled pgtest helper (one shared testcontainer, a fresh database per test). TRIGGER — load BEFORE writing, editing or reviewing any *_test.go file, testdata/ golden file, fuzz target or benchmark, or when the task says go test, test helper, fake, mock, stub, table test, testcontainers, TestMain, test database, flaky test or coverage in a Go repo. Plain requests count: "write the tests", "add a test for this", "fix the failing test", or a feature task whose last step is testing it. Load alongside go-idiomatic and tdd, not instead of them. SKIP when no Go test code is written or judged.
- allowed-tools
- Read, Edit, Write, Glob, Grep, Bash(go:*), Bash(golangci-lint:*), Bash(docker:*), Bash(git:*)
02 资源文件 8
- 资产文件assets/golangci-depguard.yml356 B
- 脚本assets/pgtest/pgtest_test.go3.1 KB
- 脚本assets/pgtest/pgtest.go8.2 KB
- 脚本assets/pgtest/settings.go289 B
- 脚本assets/pgtest/sweep_test.go2.0 KB
- 参考文档evals/evals.json10.4 KB
- 参考文档references/examples.md18.7 KB
- 参考文档references/integration-db.md8.7 KB
03 SKILL.md 正文
Go Testing
How a Go test is written, in every repo. What to test is the repo's testing-policy when it
ships one: the kinds of test it uses, what each is worth, which a feature needs, and the reusable
patterns to copy as the codebase grows. It never restates how a test is written, so the two do not
overlap. A repo without one gets it from testing-policy-creator. For the red-green-refactor loop, use tdd.
Your defaults that are wrong here
These are the habits that show up unprompted. Check your test against this table before writing it, and again before finishing, because they creep back in late in a long task.
| You reach for | Do this instead |
|---|---|
testify assert/require, any assertion library |
Plain if plus t.Errorf; cmp.Diff for anything not a scalar |
reflect.DeepEqual |
cmp.Diff(want, got), reported as (-want +got) |
A table with wantErr bool and an if tt.wantErr |
Two tables under one test: success, and errors checked with errors.Is |
gomock, mockery, EXPECT().Times(1) |
A stateful in-memory fake; assert on the state it ends in |
sqlmock or a mocked database/sql driver |
A real Postgres from internal/pgtest |
TestMain starting a container per package |
pgtest.New(t): one container per repo, a database per test |
TRUNCATE between tests, shared rows |
A fresh database per test, so t.Parallel() is safe |
time.Sleep to wait for a goroutine |
A channel the code signals, a deadline poll, or testing/synctest |
strings.Contains(err.Error(), "...") |
errors.Is / errors.As against an exported sentinel or type |
| A new test dependency | Only github.com/google/go-cmp; nothing else without asking |
Rules
- Test behaviour through the exported API, in the black-box
package foo_test. Usepackage fooonly when the unit is unexported by design, andexport_test.goas a last resort. - Assert on outputs and state, not on which calls were made. A test that breaks on a refactor that changes no behaviour is a change-detector: delete it.
- Pick the most faithful double: real, then fake, then stub, then mock. Mock only a state-changing call across a system boundary (an email sent, a card charged), and then a hand-written recorder, not a mocking library.
- A fake is stateful and honest: the same sentinels, the same not-found behaviour. One contract suite runs against the fake and the real implementation. Force an error path with a one-method stub that embeds the interface, never with failure knobs on the fake.
- Test SQL against a real Postgres through
internal/pgtest. If the repo has nointernal/pgtest, install it from this skill's assets: loadreferences/integration-db.md. - Compare with
cmp.Diff(want, got)and report(-want +got). Ifgo.modlacksgithub.com/google/go-cmp, add it withgo geton the first test you touch, and fix any lint rule or doc in the repo that contradicts this in the same change. When the user or the repo has explicitly ruled out new dependencies ("stdlib only"), compare scalars with==, slices and maps withslices.Equal/maps.Equal, structs field by field. Neverreflect.DeepEqualeither way. - Failure messages read
Func(in) = got, want want. Match errors witherrors.Is/errors.As; a caller that needs to tell errors apart gets an exported sentinel or type. - One outer test per function or method under test (
TestFunc,TestType_Method); every case runs int.Run. Where cases are checked the same way, use a table: a slice of structs with anamefield run witht.Run(tt.name, ...). Where they aren't, write named subtests inside the same outer test. A branch on a case field means named subtests, not one table. Never a second top-levelTestnamed after a scenario. t.Errorto keep going,t.Fatalwhen the rest can't run. Nevert.Fatalfrom a goroutine the test didn't start on; send the error back on a channel.- Helpers take
t, callt.Helper(), fail on setup errors and registert.Cleanup. They set up; they don't assert. No genericassertEqual. - No
time.Sleepto wait. Wait on a channel, poll a condition with a deadline, or run the test insynctest.Test(Go 1.25+) when it involves timers. t.Parallel()only for tests that share no state: no globals,t.Setenv,t.Chdiror shared rows. Apgtestdatabase is never shared, so DB tests can be parallel.- Compare JSON by decoding it, never as bytes. Normalise golden output before comparing; a
human reviews every
-updatediff. - Fuzz parsers of untrusted input with a property (no panic, round trip); commit
testdata/fuzz/. - Test HTTP handlers through the real router and middleware, built the way
mainbuilds them.
Running
Plain go test ./... runs everything, database tests included: no build tag, no -short skip,
no make target. Build tags are only for end-to-end suites. Without Docker and without
TEST_DATABASE_URL, pgtest fails with a message naming both; don't paper over it with
t.Skip. Run go test -race ./... and golangci-lint run before finishing.
Where to look
| If you're writing… | Load |
|---|---|
A test that touches Postgres, or a repo with no internal/pgtest yet |
references/integration-db.md |
CI for database tests (TEST_DATABASE_URL, service containers) |
references/integration-db.md |
| A unit, table, fake plus contract suite, HTTP, async, golden, fuzz or benchmark test | references/examples.md |
assets/pgtest/ holds the helper and its own tests; assets/golangci-depguard.yml holds the
lint rule that keeps testcontainers imports inside internal/pgtest.
See also
| Skill | When |
|---|---|
go-idiomatic |
Everything about the code under test |
tdd |
Writing the test first, one behaviour at a time |
testing-policy |
What to test: test kinds, their value, reusable patterns (per repo) |
04 同仓库更多 Skills
obsidian-develop
Build, prototype, extend, debug and review Obsidian community plugins against a live vault: inspect the running app's UI, DOM, console and plugin state, prototype in the runtime, then verify each change with a build, reload and check loop. Use whenever the user is working on an Obsidian plugin, even when they just say "add a command", "make a sidebar view", "my settings tab is blank", "why isn't my view updating" or "get this ready for the community directory"; when they ask about the plugin API (obsidian.d.ts, Workspace, leaves, Vault, MetadataCache, FileManager, views, modals, settings tabs, CodeMirror 6 editor extensions, markdown post-processors, events); or when they want a running Obsidian instance's DOM, CSS, console or plugins inspected. Not for general note-taking, Obsidian Markdown syntax, themes without a plugin, or managing vault content (use ingest, to-vault or vault-recall for those).
vault-recall
Answer the user's conceptual or technical question using their Obsidian vault as well as general knowledge. Use when the user asks a question (not a task) whose topic appears in the vault MOC digest from session start. Not for task instructions, not mid-task, and not for writing to the vault (that is to-vault).
ingest
Ingest one external source (an article, course lesson, doc, PDF or URL) into the user's Obsidian vault: save the original to raw/, then build wiki/ pages per the vault's § Ingest. Use when the user types /ingest, or says "ingest this", "add this source to my wiki", "put this article/lesson in the vault as a wiki page". Works from any repo. Routing rule: someone else's words go to wiki/ (this skill); the user's own learnings and decisions go to notes/ (/to-vault).
to-tickets
Breaks a source into independently-grabbable GitHub tickets. For a code feature the source is the plan (`docs/plans/<feature>.md`, from /to-plan) and every new or changed symbol lands in exactly one ticket, its signatures copied into the ticket body; a non-code feature is cut from the PRD's slices. Cuts tickets for one feature per run. If a code feature has no approved plan, offers /to-plan instead of cutting from prose. In a repo that runs command-centre, also emits `[[task]]` blocks into its config; in a plain treepad repo, emits a Batch Manifest instead so the tickets materialise as stacked worktrees.
to-seams
Records an agreed-but-unlanded cross-repo interface as a seam: the file a consumer's agent gets pasted into its prompt as ground truth, plus the `[[seam]]` block command-centre reads for the retirement pointer. Use when two repos need to build against an interface neither has landed yet — "record a seam for X", "we agreed on this schema, seam it", "add a seam so Y can build against Z's new field". Halts for approval before writing either file.
to-roadmap
Prioritises settled features into a Now / Next / Later kanban board, rendered as a self-contained ./ideas/roadmap.html. Each card is a feature, not a ticket — one card later becomes several tickets via /to-prd -> /to-plan -> /to-tickets. Boards the features in ./docs/approach.md, or the "## Accepted ideas" in ./ideas/CONTEXT.md when there is no approach doc. The approach doc is settled truth: this assigns tiers and reasons about ordering, it never re-litigates what was decided.