A Go interface mocking tool that generates fake implementations for testing, with detailed call tracking.
Charlatan is a Go interface mocking tool that generates fake implementations of Go interfaces for unit testing. It automates the creation of mock structs with hook functions and call tracking, solving the problem of writing repetitive mock code manually. By integrating with `go generate`, it streamlines test setup and ensures type safety.
Go developers writing unit tests that require mocking dependencies, particularly those working on projects with complex interfaces or seeking to reduce boilerplate in test code.
Developers choose Charlatan for its seamless integration with Go tooling, automatic generation that reduces manual errors, and detailed call tracking that enhances test assertions. Its lightweight design and focus on type safety make it a reliable alternative to hand-written mocks or heavier mocking frameworks.
Go Interface Mocking Tool
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses `go generate` to automatically create mock structs from Go interfaces, eliminating manual boilerplate and ensuring type safety, as shown in the example with the Service interface.
Records each method invocation with parameters and results in structs like QueryInvocation, enabling precise assertions about mock usage in tests without extra setup.
Allows defining custom behavior for interface methods via hook functions, supporting diverse test scenarios, such as conditional logic or error simulation in the example.
Integrates natively with Go's toolchain via `go generate` and standard testing practices, fitting smoothly into existing workflows without external dependencies.
Mocks panic when receiving calls without defined hooks, leading to abrupt test failures instead of graceful error handling or informative messaging, which can hinder debugging.
Requires running `go generate` and managing generated files like charlatan.go, adding complexity and potential for stale mocks if not regenerated regularly.
Only generates mocks for Go interfaces, making it unsuitable for mocking structs, functions, or other non-interface dependencies, restricting its use in broader testing contexts.