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.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | matryer/moq |
| Owner | matryer |
| Primary language | Go |
| License | MIT — OSI-approved |
| Stars | 2.2k |
| Forks | 135 |
| Open issues | 30 |
| Latest release | v0.7.1 (2026-03-20) |
| Last updated | 2026-03-20 |
| Source | https://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()`.
Get the moq source
Clone the repository and explore it locally.
git clone https://github.com/matryer/moq.gitcd moq# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
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.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Adequate |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Strong |
| Assessment confidence | High |
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.
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.coRelated 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?
Can I use Moq mocks outside their generated package?
What happens if I call a mock method whose function field is nil?
How do I track which arguments were passed to a mocked method?
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.