Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Go
  3. moq

moq

MITGov0.7.1

A Go interface mocking tool that generates mock structs from interfaces for unit testing using go generate.

Visit WebsiteGitHubGitHub
2.2k stars135 forks0 contributors

What is moq?

Moq is a Go code generation tool that creates mock implementations of interfaces for unit testing. It automatically generates structs that implement target interfaces, allowing developers to easily control behavior and verify interactions in tests. It integrates with the standard `go generate` workflow to reduce boilerplate code.

Target Audience

Go developers writing unit tests that require mocking dependencies, particularly those working in projects that use interfaces and want to avoid manual mock implementation.

Value Proposition

Developers choose Moq for its simplicity and seamless integration with Go's native tooling, specifically `go generate`, which automates mock creation and minimizes repetitive code. Its pragmatic approach provides essential mocking features like call tracking and reset capabilities without unnecessary complexity.

Overview

Interface mocking tool for go generate

Use Cases

Best For

  • Automatically generating mock structs from Go interfaces to avoid writing boilerplate mock code manually.
  • Integrating mock generation into a Go project's build process using `//go:generate` directives.
  • Unit testing Go code where you need to control the behavior of interface dependencies and verify method calls.
  • Tracking and resetting calls to mocked methods within test contexts for precise test isolation.
  • Projects requiring mocks that panic when nil function fields are called, ensuring test failures for unexpected calls.
  • Debugging generated mock code by using the `-fmt noop` flag to inspect raw output without formatting errors.

Not Ideal For

  • Projects that need to mock functions or concrete types without interfaces
  • Teams preferring dynamic, reflection-based mocks for runtime flexibility over generated code
  • Environments where automated code generation is prohibited or adds compliance overhead
  • Developers requiring advanced mocking features like argument matchers or automatic expectation verification

Pros & Cons

Pros

Automatic Interface Mocking

Moq generates mock structs from any Go interface using go generate, eliminating manual boilerplate code, as demonstrated in the usage examples where interfaces like MyInterface are automatically converted to mocks.

Go Toolchain Integration

Seamlessly integrates with standard Go tools like go generate, allowing easy automation within the build process, which aligns with the project's philosophy of reducing boilerplate through native workflows.

Call Tracking and Reset

Provides .MethodCalls() to track invocations and .ResetCalls() to clear history, enabling precise verification and test isolation, as highlighted in the tips section for better test management.

Configurable Behavior

Offers flags like -stub to return zero values and -skip-ensure to avoid import cycles, giving flexibility in mock generation and usage, as detailed in the command-line options.

Cons

Limited to Interfaces

Can only generate mocks for interfaces, not for functions or other Go types, which restricts its use in projects that rely on different abstraction patterns or need to mock standalone functions.

Panic on Undefined Calls

If a mocked method is called without a defined function field, it panics by default, leading to abrupt test failures compared to libraries that offer graceful fallbacks or warnings.

Code Generation Overhead

Requires running moq or go generate to update mocks when interfaces change, adding an extra step and potential synchronization issues in fast-paced development environments.

Basic Feature Set

Lacks advanced mocking features like argument matchers or built-in expectation frameworks, which are common in alternatives like gomock, making it less suitable for complex test scenarios.

Frequently Asked Questions

Quick Stats

Stars2,204
Forks135
Contributors0
Open Issues18
Last commit8 days ago
CreatedSince 2016

Tags

#unit-testing#test-doubles#mocking#testing#golang#go-generate#go#code-generation#codegen

Built With

G
Go

Links & Resources

Website

Included in

Go169.1k
Auto-fetched 9 hours ago

Related Projects

mockerymockery

A mock code autogenerator for Go

Stars7,161
Forks459
Last commit4 days ago
go-sqlmockgo-sqlmock

Sql mock driver for golang to test database interactions

Stars6,570
Forks414
Last commit2 months ago
gomockgomock

GoMock is a mocking framework for the Go programming language.

Stars3,402
Forks178
Last commit13 days ago
hoverflyhoverfly

Lightweight service virtualization/ API simulation / API mocking tool for developers and testers

Stars2,512
Forks229
Last commit1 day ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub