A Go tool for generating type-safe fake implementations of interfaces to simplify unit testing.
Counterfeiter is a Go tool that automatically generates fake implementations of interfaces for use in unit testing. It solves the problem of manually writing test doubles by creating type-safe, self-contained fakes that record method calls and allow stubbing of return values. This makes it easier to test objects that depend on other interfaces without writing tedious boilerplate code.
Go developers writing unit tests for applications with complex dependencies, particularly those who need to mock or stub interfaces in their test suites. It's especially useful for teams practicing test-driven development or maintaining large codebases with many interfaces.
Developers choose Counterfeiter because it generates reliable, type-safe fakes automatically, reducing manual effort and potential errors. Unlike runtime mocking approaches, it produces compile-time safe code that integrates seamlessly with Go's toolchain and follows Go conventions.
A tool for generating self-contained, type-safe test doubles in go
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
With a single go:generate directive, counterfeiter creates complete fake implementations for any interface, eliminating tedious manual coding, as shown in the README examples.
Generated fakes maintain exact interface signatures, ensuring errors are caught during compilation rather than at test runtime, enhancing reliability.
Works natively with go generate to keep fakes synchronized with interface changes, reducing maintenance overhead and manual updates.
Can generate fakes for interfaces from external packages using package-qualified names, extending utility beyond internal codebases.
Requires adding counterfeiter as a tool dependency and managing go:generate directives, which can be cumbersome for small or fast-moving projects.
Produces separate fake files that must be committed to version control, potentially cluttering the codebase and complicating reviews.
Lacks built-in support for argument matchers or dynamic stubbing based on call conditions, often requiring additional custom logic in tests.