模拟代码减少代码覆盖率

I am working with Golang and using mockhiato to generate mocks for all interfaces. This tool generates mocked implementation in mocks.go file within the same package. I can't rename mocks.go to mocks_test.go as this mock file is consumed by other packages.

The problem is that these mocks files are counted by go coverage tool and thus reducing my code coverage percentage for the package.

I am looking a good workaround so that my code coverage will not show bad numbers.

That's how we solved it.

  1. Put interface in the consumer folders. If service is injected in the handler, then the handler will have the interface definition of service. This is because GoLang philosophy suggests that interface is to consume functionality rather than expose it.
  2. Used mockery to generate mocks.

  3. Generate mocks in a separate _mock folder.

The best thing in this case would be to move the mocks to their own dedicated package which would have no test coverage. This would remove their impact on code that you actually want coverage data on.