Open In App

How to Install Golang Mockery on Windows?

Last Updated : 12 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Mockery is a Golang plugin that is used to perform unit tests since it has the ability to easily generate mocks for Golang interfaces using the stretch/testify/mock package. This article covers four different ways of installing and getting started with Golang mockery. These include:

  • Installing through Homebrew
  • Using a docker image
  • Using go install
  • Using pre-built GitHub release binaries

Using Homebrew for Installation

Homebrew is a complementary package runtime manager to macOS and Linux systems as it installs everything needed that your system failed to install. To use homebrew, you will first need to install it using the command:

  “`/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)” “`

After successful installation, you need to follow the recommended next steps provided by homebrew as follows:

Use command to install mockery

 

After completion, you are now ready to install mockery using the command:   

“`brew install mockery “`   

You can also check if the installation was successful with 

“mockery –version“

check if the installation was successful

mockery installation and version

If you wish to upgrade the version, the command is   

“`brew upgrade mockery“`

Using a Docker Image

Another method of installing mockery is by pulling the mockery docker image. We use the docker pull shorthand to download the image locally. To pull the image run the command  

“`docker pull vektra/mockery “`

 The latest tag is added by default when pulling an image since none was provided. You can also confirm if the pulled image is downloaded successfully by running docker images to view a list of images.

Installing mockery is by pulling the mockery docker image.

docker image download

To generate all the mocks for your project, run :

 “` docker run -v “$PWD”:/src -w /src vektra/mockery –all “`  

If you are using Windows PowerShell, change “$PWD” to ${PWD} 

you are using Windows PowerShell, change "$PWD" to ${PWD}

 

Your container is now running. You can also confirm this on Docker Desktop as follows:

container is now running

running containers

Using Go Install

Go install is a method used in go to build and install packages in module mode. This method allows you to install a specific version of a package. Ensure you have the latest version of go before installing mockery.    

To install mockery run:  

“`go install github.com/vektra/mockery/v2@latest  “`

NB: This method should only be used for development and not for production because the server does not track any of the code in the mockery package.

Download a GitHub Release

Alternatively, you can check out the list of github releases and download one of the pre-built binaries.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads