英文:
How to run mockgen during build?
问题
我开始使用gomock来创建用于单元测试的模拟对象。Gomock要求我使用特定的参数运行mockgen
命令,以便为模拟生成代码。每当被模拟的接口发生更改时,都需要再次运行该命令来更新代码。因此,我认为在运行go build
之前,让它执行mockgen
命令并传递适当的参数可能是有意义的。
是否有办法让go build
在构建包之前运行脚本或shell命令?
如果没有,你是如何生成你的模拟对象并保持其更新的?
英文:
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?
答案1
得分: 1
我不认为有任何钩子可以将此功能嵌入到go build
中。
一种解决方案是使用make。你的Makefile
可能如下所示:
.PHONY: build test
build:
go build
test:
mockgen ...
go test
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论