Introducing Mockery's New Packages Feature
vektra/mockery is a Go package that auto-generates mock implementations of interfaces in your project. The latest update to the code introduces a feature called packages, which offers significant benefits to developers.
Previously, developers had to use go:generate directives in their code to tell mockery to generate mock implementations. This approach had several issues, including performance and inflexibility. However, with the new packages feature, the configuration model has been improved to align with Go's abstract syntax tree parsing. This change provides more flexibility and control over how mocks are generated.
Using the packages feature, developers can specify the configurations in the configuration file, resulting in faster performance. In fact, the mockery project itself has seen a 5x increase in speed with the usage of packages. Additionally, the configuration options allow developers to have precise control over the generation of mocks.
To demonstrate the usage of packages, let's consider an example with the Stringer interface. By specifying the configuration in the config file, developers can easily generate a mock for the Stringer interface. The generated mock file will be stored in a separate mocks folder.
The packages feature in mockery is a game-changer for developers looking for faster and more flexible mock generation. It outperforms other mock generator packages, including the officially supported gomock project, in terms of speed and control. Developers can explore advanced examples and testimonials on the project's GitHub page.
Useful links: