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.

Evaluated Mar 07, 2026 (0d ago) v2.x
Homepage ↗ Repo ↗ Developer Tools go sql testing mock database sqldb unit-testing driver
⚙ Agent Friendliness
65
/ 100
Can an agent use this?
🔒 Security
83
/ 100
Is it safe for agents?
⚡ Reliability
84
/ 100
Does it work consistently?

Score Breakdown

⚙ Agent Friendliness

MCP Quality
--
Documentation
83
Error Messages
80
Auth Simplicity
95
Rate Limits
95

🔒 Security

TLS Enforcement
85
Auth Strength
82
Scope Granularity
80
Dep. Hygiene
85
Secret Handling
84

Test library — no network exposure. In-memory mock with no credential handling. DATA-DOG maintains.

⚡ Reliability

Uptime/SLA
86
Version Stability
84
Breaking Changes
80
Error Recovery
86
AF Security 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

REST API
No
GraphQL
No
gRPC
No
MCP Server
No
SDK
Yes
Webhooks
No

Authentication

Methods: none
OAuth: No Scopes: No

Test library — no authentication.

Pricing

Model: open_source
Free tier: Yes
Requires CC: No

Community-maintained open source. MIT license.

Agent Metadata

Pagination
none
Idempotent
Full
Retry Guidance
Not documented

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

$99

Package Brief

Quick verdict, integration guide, cost projections, gotchas with workarounds, and alternatives comparison.

Delivered within 10 minutes

$3

Score Monitoring

Get alerted when this package's AF, security, or reliability scores change significantly. Stay ahead of regressions.

Continuous monitoring

$3/mo

Scores are editorial opinions as of 2026-03-07.

6470
Packages Evaluated
26150
Need Evaluation
173
Need Re-evaluation
Community Powered