# data-dog/go-sqlmock

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/data-dog-go-sqlmock).**

6,525 stars · 405 forks · Go · other

## Links

- GitHub: https://github.com/DATA-DOG/go-sqlmock
- awesome-repositories: https://awesome-repositories.com/repository/data-dog-go-sqlmock.md

## Topics

`database` `go` `golang` `mock` `sql` `sql-driver` `tdd` `testing`

## Description

go-sqlmock is a Go library that simulates SQL driver behavior for unit testing by intercepting database calls through the standard `database/sql/driver` interface, eliminating the need for a real database connection. It provides a comprehensive mocking framework that allows developers to define expectations for SQL operations, including queries, executions, prepared statements, and transaction lifecycles, with precise control over the results and errors returned.

The library distinguishes itself through its flexible matching and verification capabilities. It supports regex-based or exact SQL string matching, along with a custom argument matching interface that handles complex types like `time.Time` and provides wildcard matchers for flexible value comparison. Expectations can be enforced in a predefined order, and the library verifies that all registered expectations are fulfilled during the test, reporting any unmatched or missing calls. It also includes configurable delayed response simulation for testing timeout and context cancellation scenarios.

Beyond core query and exec mocking, go-sqlmock covers the full spectrum of database interaction testing. This includes simulating connection health through ping calls and sentinel errors, managing transaction begin/commit/rollback cycles with configurable errors, and building mock result sets with detailed column metadata such as names, types, nullability, and precision. The library also supports multiple result sets, named parameters, and session state reset, providing a complete toolkit for validating database interaction logic in Go tests.

## Tags

### Software Engineering & Architecture

- [Mock Driver Implementations](https://awesome-repositories.com/f/software-engineering-architecture/sql-driver-interfaces/mock-driver-implementations.md) — Intercepts database calls by implementing the database/sql/driver interfaces to simulate SQL driver behavior.
- [Database Type Converters](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-validation/data-type-validation/value-type-conversions/database-type-converters.md) — Ships a custom type conversion system for mapping Go values to database-compatible types in mock operations. ([source](https://godoc.org/database/sql/driver))
- [Database Argument Matchers](https://awesome-repositories.com/f/software-engineering-architecture/static-type-checkers/validation-logic/cli-argument-validators/test-argument-validators/database-argument-matchers.md) — Provides custom argument matchers for comparing complex types like time.Time in database query expectations. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [SQL Query String Matchers](https://awesome-repositories.com/f/software-engineering-architecture/string-matching-algorithms/partial-string-matching/sql-query-string-matchers.md) — Validates incoming SQL queries using regular expressions, exact equality, or custom matchers for flexible assertion control. ([source](https://cdn.jsdelivr.net/gh/data-dog/go-sqlmock@master/README.md))
- [Column Metadata Retrieval](https://awesome-repositories.com/f/software-engineering-architecture/type-safe-binding-registries/database-column-bindings/column-metadata-retrieval.md) — Reports column names, types, nullability, length, precision, and scale for query result sets. ([source](https://godoc.org/database/sql/driver))
- [Mock Column Metadata Builders](https://awesome-repositories.com/f/software-engineering-architecture/type-safe-binding-registries/database-column-bindings/column-metadata-retrieval/mock-column-metadata-builders.md) — Builds mock column metadata with type, nullability, and precision details for simulating query results. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))

### Testing & Quality Assurance

- [Database Interaction Mocking](https://awesome-repositories.com/f/testing-quality-assurance/general-testing-utilities/test-utilities-assertions/test-data-management/test-mock-management-tools/database-interaction-mocking.md) — Simulates SQL driver behavior in tests by intercepting database calls through the standard database/sql interface. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [SQL Mocking Libraries](https://awesome-repositories.com/f/testing-quality-assurance/go-testing-frameworks/sql-mocking-libraries.md) — Simulate any SQL driver behavior in tests by intercepting database calls through the database/sql/driver interface without requiring a real database connection. ([source](https://cdn.jsdelivr.net/gh/data-dog/go-sqlmock@master/README.md))
- [Interaction Expectations](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations.md) — Check that every registered expectation was fulfilled during the test and report any unmatched or missing calls. ([source](http://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [SQL Statement Expectation Registries](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/sql-statement-expectation-registries.md) — Registers expected SQL INSERT, UPDATE, or DELETE statements with argument matching and configurable results. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Query Statement Expectations](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/sql-statement-expectation-registries/query-statement-expectations.md) — Define expected Query calls with argument matching, returning custom rows or errors to test select operations. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Mocked SQL Results](https://awesome-repositories.com/f/testing-quality-assurance/result-returning-tests/empty-result-returns/mocked-sql-results.md) — Returns predefined results or errors for SQL operations, including row sets, affected row counts, last insert IDs, and custom errors. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Mock Response Delays](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/dependency-mocking/timer-mocks/mock-response-delays.md) — Introduces configurable time delays before returning results for any expected database operation. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [SQL Query Verifications](https://awesome-repositories.com/f/testing-quality-assurance/validation-verification/input-validation/compile-time-validators/sql-query-validators/sql-query-verifications.md) — Validating that SQL queries, arguments, and execution order match expected patterns during automated testing.
- [Ordered](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/ordered.md) — Enforces that database operations occur in a predefined sequence by tracking expectation order.
- [Response Simulators](https://awesome-repositories.com/f/testing-quality-assurance/network-resilience-simulators/response-simulators.md) — Adds configurable delays to any expected database operation to test timeout and context cancellation behavior. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))

### Part of an Awesome List

- [SQL Pattern Matchers](https://awesome-repositories.com/f/awesome-lists/devtools/regex-and-pattern-matching/regex-pattern-matching/sql-pattern-matchers.md) — Validates SQL query strings and arguments using regex, equality, or custom matchers in tests.
- [Equality SQL Matchers](https://awesome-repositories.com/f/awesome-lists/devtools/regex-and-pattern-matching/regex-pattern-matching/sql-pattern-matchers/equality-sql-matchers.md) — Validates incoming SQL strings against expected patterns using either regular expression or exact case-sensitive matching. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Customizable SQL Matchers](https://awesome-repositories.com/f/awesome-lists/devtools/regex-and-pattern-matching/regex-pattern-matching/sql-pattern-matchers/customizable-sql-matchers.md) — Replace default regex-based SQL matching with equality matching or a fully custom query matcher for sophisticated validation. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Database Operation Delays](https://awesome-repositories.com/f/awesome-lists/devtools/timing-and-delays/database-operation-delays.md) — Delays the response of SQL operations by a specified duration to test timeouts and context cancellation behavior. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))

### Data & Databases

- [Mock Transaction Lifecycles](https://awesome-repositories.com/f/data-databases/atomic-commit-logs/transaction-lifecycle-control/mock-transaction-lifecycles.md) — Set expectations for Begin, Commit, and Rollback calls, optionally returning errors or delays to test transaction handling. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Mock Result Set Builders](https://awesome-repositories.com/f/data-databases/column-mappings/result-set-labeling/mock-result-set-builders.md) — Creates mock result objects with configurable LastInsertId and RowsAffected values or error returns. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Custom Result Returns](https://awesome-repositories.com/f/data-databases/column-mappings/result-set-labeling/mock-result-set-builders/custom-result-returns.md) — Configure mocked queries and execs to return specific row sets, last insert IDs, affected row counts, or errors. ([source](http://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Error Simulators](https://awesome-repositories.com/f/data-databases/database-error-handling/error-simulators.md) — Configures specific errors to be returned from any database operation to test error handling paths. ([source](http://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [SQL Query Execution](https://awesome-repositories.com/f/data-databases/database-management-systems/database-systems-management/database-operations/sql-query-execution.md) — Executes SQL statements that return no rows and reports the number of affected rows. ([source](https://godoc.org/database/sql/driver))
- [Mock Row Queries](https://awesome-repositories.com/f/data-databases/database-schema-mapping/row-mapping-logic/row-record-access/tabular-row-storage/mock-row-queries.md) — Runs statements that return rows and provides an iterator over the result set for testing. ([source](https://godoc.org/database/sql/driver))
- [Exec Statement Mocking](https://awesome-repositories.com/f/data-databases/database-schema-mapping/row-mapping-logic/row-record-access/tabular-row-storage/mock-row-queries/exec-statement-mocking.md) — Expects Exec or ExecContext calls, returning configurable results or errors, with optional argument matching. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Query Statement Mocking](https://awesome-repositories.com/f/data-databases/database-schema-mapping/row-mapping-logic/row-record-access/tabular-row-storage/mock-row-queries/query-statement-mocking.md) — Expects Query, QueryRow, or their context variants, returning configurable rows or errors, with optional argument matching. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Database Transaction Management](https://awesome-repositories.com/f/data-databases/database-transaction-management.md) — Provides a mocking framework for programmatic control of database transaction boundaries in tests. ([source](https://godoc.org/database/sql/driver))
- [Prepared Statements](https://awesome-repositories.com/f/data-databases/prepared-statements.md) — Parses and stores query strings for repeated execution with different arguments. ([source](https://godoc.org/database/sql/driver))
- [Mocking Frameworks](https://awesome-repositories.com/f/data-databases/prepared-statements/mocking-frameworks.md) — Expect Prepare calls and define subsequent Exec or Query expectations on the prepared statement, optionally simulating close errors. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Mock Execution Mode Handlers](https://awesome-repositories.com/f/data-databases/result-set-search/multi-result-set-fetching/mock-execution-mode-handlers.md) — Returns predefined results or errors when matching INSERT/UPDATE/DELETE statements are executed with argument validation. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))
- [Statement Executions](https://awesome-repositories.com/f/data-databases/sql-statement-parsing/statement-executions.md) — Runs SQL statements that do not return rows, such as INSERT or UPDATE operations. ([source](https://godoc.org/database/sql/driver))
- [Transaction Management](https://awesome-repositories.com/f/data-databases/transaction-management.md) — Starts, commits, or rolls back transactions on a database connection. ([source](https://godoc.org/database/sql/driver))
- [Health Validation](https://awesome-repositories.com/f/data-databases/connection-pooling/health-validation.md) — Signal whether a connection is still valid before it is returned to or reused from the connection pool. ([source](https://godoc.org/database/sql/driver))
- [Bad Connection Signaling](https://awesome-repositories.com/f/data-databases/connection-pools/bad-connection-signaling.md) — Return a sentinel error to indicate a connection is invalid so the pool can discard and retry it. ([source](https://godoc.org/database/sql/driver))
- [Connection Health Simulators](https://awesome-repositories.com/f/data-databases/database-connection-health-monitoring/connection-health-simulators.md) — Simulates database connection health scenarios including pings, errors, and session resets for pool testing.

### Programming Languages & Runtimes

- [Mock Argument Matchers](https://awesome-repositories.com/f/programming-languages-runtimes/programming-utilities/function-utilities/argument-collectors/splat-argument-validations/argument-constraint-matchers/mock-argument-matchers.md) — Match query or exec arguments using custom logic, including a wildcard matcher for any value or type-specific matchers like time.Time. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))

### System Administration & Monitoring

- [Database Ping Simulators](https://awesome-repositories.com/f/system-administration-monitoring/custom-ping-identifiers/database-ping-simulators.md) — Mocks database ping calls to verify connectivity checks without a real database. ([source](http://godoc.org/github.com/DATA-DOG/go-sqlmock))

### User Interface & Experience

- [Database Connection Close Mocks](https://awesome-repositories.com/f/user-interface-experience/ui-components/feedback-overlay-components/close-buttons/database-connection-close-mocks.md) — Expects Close calls on the database connection and returns configurable errors. ([source](https://godoc.org/github.com/DATA-DOG/go-sqlmock))

### Networking & Communication

- [Ping Output Normalization](https://awesome-repositories.com/f/networking-communication/cross-platform-networking/ping-output-normalization.md) — Observes and mocks database ping calls to verify connection health checks in tests. ([source](http://godoc.org/github.com/DATA-DOG/go-sqlmock))
