4

I have started using gomock to create mock objects for unit testing. Gomock requires that that I run the mockgen command with certain argument in order to generate code for the mock. This needs to be done again every time the interfaces that is being mocked changes. I therefore thought it might make sense to have go build run mockgen with the appropriate arguments.

Is there a way to have go build to run a script or shell command before building a package?

If not, how do you generate your mocks and keep them up to date?

Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88

1 Answers1

1

I don't think there exist any hooks into go build that would make this possible.

One solution would be to use make. Your Makefile might resemble this:

.PHONY: build test

build:
    go build

test:
    mockgen ...
    go test
Caleb Spare
  • 497
  • 2
  • 11