sqlmock simulates SQL driver behavior in Go tests so code can be checked without a live database connection.
Overview
go-sqlmock implements the sql/driver interface so unit tests can open a mock database connection and assert expected queries, arguments, transactions, and results. It exists solely to mimic driver behavior in tests while keeping a test-driven workflow intact.
Expectations can cover prepares, executes, queries, transactions, and result rows, with strict ordering by default. The library supports concurrency, multiple connections, context-related features, and named parameters without changing application source code.
Custom QueryMatcher implementations allow regexp, equality, or bespoke SQL matching when drivers use nonstandard syntax. An Argument interface enables richer matching for values such as timestamps.
The project describes itself as stable with few new changes. Users who need real transactional integration tests are pointed toward separate functional test databases.
Key features
- Mock sql/driver implementation
- Expected query and exec matching
- Custom SQL query matchers
- Rows and result simulation
- Context and named parameters
- Concurrent connection support
- Strict expectation ordering
Use cases
- Go database layer unit tests
- TDD without live DB
- Testing error and edge SQL paths
- CI pipelines without database service