Skip to content

Keep a failed test's database on request - #6

Merged
bashhack merged 1 commit into
mainfrom
keep-on-failure
Oct 22, 2026
Merged

bashhack merged 1 commit into
mainfrom
keep-on-failure

Conversation

@bashhack

Copy link
Copy Markdown
Owner

By default every test database is dropped, pass or fail. This adds a switch to keep a failed test's database so its data can be inspected:

TESTDB_KEEP_ON_FAILURE=1 go test ./... -run TestCheckout
    checkout_test.go:42: expected 2 orders, got 1
    testdb: kept database test_1761100000_a1b2c3d4 for inspection:
        psql "postgres://postgres:postgres@localhost:5432/test_1761100000_a1b2c3d4?sslmode=disable"
  • testdb.WithKeepOnFailure() is the in-code form; TESTDB_KEEP_ON_FAILURE (1/true/yes/on) turns it on for a run without touching code.
  • Passing tests still drop their databases. The policy is applied in postgres's cleanup when t.Failed().
  • TestDatabase.Keep() is the mechanism, exposed for the low-level testdb.New path where you decide yourself.
  • Off by default on purpose: pgtestdb keeps on failure unconditionally, which piles databases up in CI where nobody looks. Here the default stays clean and the switch is one env var away when you are debugging locally.

Tests cover: kept on failure (option and env), dropped on success even with the option, dropped on failure without it, the log line's content, and that Keep still releases the provider's resources.

Off by default, every test database is dropped. With
testdb.WithKeepOnFailure(), or TESTDB_KEEP_ON_FAILURE=1 for a whole run,
a failed test's database is left in place and the test log gives a psql
command for it; passing tests still clean up. TestDatabase.Keep exposes
the mechanism for the low-level API.

Kept in the test's log rather than defaulting to keep (as pgtestdb does)
so CI never accumulates databases nobody will look at.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@bashhack
bashhack merged commit faba971 into main Oct 22, 2026
1 check passed
@bashhack
bashhack deleted the keep-on-failure branch October 22, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants