douzi5214 2018-07-29 18:07
浏览 48

如何从Go中排除Mock或Stub结构

Assume for a moment I have a Go package with numerous interfaces. For this example, I'd focus on one called Middleware defined in middleware.go:

type Middleware interface {
  // some functions
}  

I have some other parts of the system that uses it, so to better help test those parts of the system, I created a mock implementation by hand in mock_middleware.go:

type MockMiddleware struct {

}  

// implementations

When building the binary without tests, how do I exclude the mock classes from being built into the binary? I'm aware that middleware_test.go will be excluded, but I'd don't want to name all my mocks with that convention so as not to confuse actual tests with implementations to support testing.

  • 写回答

1条回答 默认 最新

  • dqftyn1717 2018-07-29 19:05
    关注

    One way could be to use Build Constraints.

    Go even supports a built in ignore tag, but I'm not really sure of its interaction with the testing/benchmark toolchain:

    To keep a file from being considered for the build:

    // +build ignore

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程