DEV.co
Open-Source Testing · matryer

moq

Moq is a Go code generation tool that automatically creates mock implementations from interface definitions, streamlining unit test setup. It integrates with Go's built-in code generation workflow and eliminates boilerplate mock struct writing.

Source: GitHub — github.com/matryer/moq
2.2k
GitHub stars
135
Forks
Go
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorymatryer/moq
Ownermatryer
Primary languageGo
LicenseMIT — OSI-approved
Stars2.2k
Forks135
Open issues30
Latest releasev0.7.1 (2026-03-20)
Last updated2026-03-20
Sourcehttps://github.com/matryer/moq

What moq is

Moq parses Go interface definitions and generates struct types with function fields for each method, enabling runtime mock behavior injection during testing. Generated mocks implement the original interface and provide call tracking via `.MethodCalls()` and `.ResetCalls()`.

Quickstart

Get the moq source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/matryer/moq.gitcd moq# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit Testing with Interface Dependencies

Mock external service clients, database layers, or file I/O interfaces to isolate units under test and control behavior deterministically.

Test-Driven Development (TDD)

Quickly define interfaces and auto-generate mocks to support rapid test writing before implementation, reducing test boilerplate iteration time.

Integration Test Harnesses

Create test doubles for middleware, configuration providers, or logging interfaces to build deterministic end-to-end test scenarios.

Implementation considerations

  • Use `go:generate` directives in test files to keep mock generation automated and reproducible across team and CI environments.
  • Only mock interface methods actively used in tests; unused mock fields add unnecessary noise and maintenance burden.
  • Capture call details via closure variables inside test functions rather than relying solely on `.MethodCalls()` for complex assertions.
  • Consider the `-skip-ensure` flag if mocks are generated in separate packages to avoid import cycles with the tested package.
  • Use `-stub` flag to suppress panics on nil function calls when partial mocking is acceptable in your test strategy.

When to avoid it — and what to weigh

  • Projects Not Using Go Interfaces — Moq works only on interface types. Codebases that rely heavily on concrete types or struct embedding may not benefit.
  • Go Versions Below 1.18 — Building from source requires Go 1.18+. Legacy projects on older Go versions must use pre-built binaries, limiting version flexibility.
  • Highly Dynamic Mock Behavior — Complex state machines or context-dependent mocking beyond simple function field callbacks may require custom mock logic outside Moq's scope.
  • Nil-Safety Requirements — Moq panics if a function field is nil and called. Projects requiring strict nil-safety patterns may need defensive wrapper logic.

License & commercial use

MIT License. Permissive OSI-approved license permitting commercial use, modification, and distribution with attribution.

MIT is a permissive OSI license explicitly allowing commercial use without royalty, warranty, or liability. No commercial restrictions apply. However, inclusion of copyright and license notices is required in derivative works.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

Moq parses Go source files and generates code; no external network, data persistence, or cryptographic operations occur. Generated mocks are test-only. Review generated code for correctness but no inherent security posture assessment available. Standard Go build and tooling security practices apply.

Alternatives to consider

GoMock (github.com/golang/mock)

Reflection-based mock generation supporting both interfaces and concrete types; alternative generics approach with different call assertion patterns.

Testify/mock

Manual assertion library with fluent API for building mock expectations; no code generation, useful for lightweight mocking without build-time setup.

Manual Mocks

Explicit hand-written mock structs provide full control and zero dependencies but require more boilerplate and ongoing maintenance.

Software development agency

Build on moq with DEV.co software developers

Moq eliminates mock boilerplate and integrates seamlessly with go generate. Start generating mocks in minutes—no external dependencies or complex setup required.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

moq FAQ

Does Moq work with generic interfaces?
Requires Go 1.18+ for source builds. Generics support status not clearly stated in provided documentation; review release notes or test against your generic interface patterns.
Can I use Moq mocks outside their generated package?
Yes, if the mock struct is exported (capitalized). Use `-pkg` to generate in a different package and `-skip-ensure` if importing creates cycles.
What happens if I call a mock method whose function field is nil?
Moq panics by default. Use `-stub` flag to return zero values instead; this suppresses panics for unimplemented methods.
How do I track which arguments were passed to a mocked method?
Use the auto-generated `.MethodCalls()` function to retrieve call history, or capture arguments via closure variables in the test function.

Custom software development services

Need help beyond evaluating moq? DEV.co is a software development agency offering software development services and web development for teams of every size. Our software developers and web developers build custom software, web applications, APIs, and open-source testing integrations — and maintain them long-term.

Ready to streamline your Go test suite?

Moq eliminates mock boilerplate and integrates seamlessly with go generate. Start generating mocks in minutes—no external dependencies or complex setup required.