go-sqlmock
SQL mock driver for Go's database/sql package. Implements the database/sql driver interface to intercept SQL queries in tests without a real database. Define expected queries with ExpectQuery/ExpectExec, set return rows, verify all expectations were met. Tests run in-process without a database server. The standard Go tool for unit testing code that uses database/sql, GORM, or sqlx.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Test library — no network exposure. In-memory mock with no credential handling. DATA-DOG maintains.
⚡ Reliability
Best When
You need fast, isolated unit tests for Go code that uses database/sql and want to verify exact SQL queries and test error scenarios without a real database.
Avoid When
You need to verify actual database behavior, dialect-specific features, or migration correctness — use testcontainers-go for real database testing.
Use Cases
- • Unit test agent database access code without a real PostgreSQL/MySQL database — tests run in milliseconds with no external dependencies
- • Verify exact SQL queries generated by agent ORM (GORM, sqlx) with sqlmock's query expectation matching
- • Test agent error handling for database failures — sqlmock.ExpectQuery().WillReturnError() simulates DB connection failures
- • Test agent transaction logic with sqlmock's ExpectBegin/ExpectCommit/ExpectRollback transaction expectation API
- • Use sqlmock in CI pipelines to run agent database tests without database infrastructure setup
Not For
- • Integration tests that validate actual SQL behavior — use testcontainers-go for real database integration tests
- • Tests requiring specific database dialect behavior (PostgreSQL-specific functions) — sqlmock is agnostic and doesn't validate dialect-specific syntax
- • Testing raw SQL queries without a Go wrapper — sqlmock requires database/sql driver interface; use testcontainers for pure SQL testing
Interface
Authentication
Test library — no authentication.
Pricing
Community-maintained open source. MIT license.
Agent Metadata
Known Gotchas
- ⚠ sqlmock query matching is exact by default — even whitespace differences cause 'call to Query was unexpected' errors; use sqlmock.QueryMatcherRegexp or sqlmock.MatchExpectationsInOrder(false) for flexibility
- ⚠ sqlmock.New() returns both *sql.DB and sqlmock — must close the *sql.DB in defer and call mock.ExpectationsWereMet() at end of test; forgetting either masks test failures
- ⚠ Prepared statements require ExpectPrepare() — GORM and sqlx use prepared statements internally; missing ExpectPrepare() causes 'unexpected call to Prepare' even if you only mocked Query
- ⚠ Row scanning errors: Rows.Close() must be called after scanning — not calling Close() on sqlmock rows causes 'rows are closed' errors in subsequent queries in the same test
- ⚠ Named parameter queries (:name) must be converted to positional parameters ($1) or use sqlmock.QueryMatcherRegexp — named params syntax varies by driver and sqlmock's exact matcher may reject them
- ⚠ sqlmock validates argument order for ExpectExec/ExpectQuery — if GORM reorders query arguments for optimization, sqlmock expectations fail; use AnyArg() for flexible argument matching
Alternatives
Full Evaluation Report
Comprehensive deep-dive: security analysis, reliability audit, agent experience review, cost modeling, competitive positioning, and improvement roadmap for go-sqlmock.
AI-powered analysis · PDF + markdown · Delivered within 30 minutes
Package Brief
Quick verdict, integration guide, cost projections, gotchas with workarounds, and alternatives comparison.
Delivered within 10 minutes
Score Monitoring
Get alerted when this package's AF, security, or reliability scores change significantly. Stay ahead of regressions.
Continuous monitoring
Scores are editorial opinions as of 2026-03-07.