简体   繁体   中英

How to run mockgen during build?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM