How to run mockgen during build?

huangapple go评论100阅读模式
英文:

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

huangapple
  • 本文由 发表于 2013年10月26日 13:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/19602997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定