API Reference

go-sqlmock

A mock library for Go database/sql that lets tests simulate SQL queries without a real database.

Official Documentation ↗
CompatibilityThis documentation covers version v1.5. Check the official docs for the latest API surface.

Installation

gogo get github.com/DATA-DOG/go-sqlmock

Methods

go-sqlmock returns a database handle whose expectations are declared with regex matchers.

Key Features

  • Regex or exact query matching
  • Argument and result expectations
  • Ordered and unordered expectations

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The official API reference declares the following error types:

API reference evidence

Fetched from official sources on 2026-11-08T16:02:45+08:00 via WebFetch pkg.go.dev + PowerShell Invoke-WebRequest raw.githubusercontent.com (DATA-DOG/go-sqlmock v1.5.2) + 模块 zip (proxy.golang.org v1.5.2).

FAQ

How do I assert expectations were met?
Call mock.ExpectationsWereMet() in teardown to fail when a declared expectation was never called.
Why does my query not match?
The matcher is a regex by default. Escape special characters or use regexp.QuoteMeta for literal queries.
Can I test transactions?
Yes. ExpectBegin, ExpectCommit, and ExpectRollback declare transaction lifecycle expectations.