2017-03-22 17:25


  • unit-testing

This question already has an answer here:

I'm trying to automate running unit tests on an existing Go project. It already has a lot of existing tests which do non-unit test things like contact external services and write to databases. I would like to exclude these test files from the automation using a naming convention. Say the structure of the project is like this:


I can run go test ./... to run all the tests. If I include 'UnitTest' in the names of my unit tests I can run go test ./... -run UnitTest but this still runs the startup code in the e2e tests. I need it to completely ignore those files. Someone suggested running go test $(go list ./... | grep -v /e2e/) but go list outputs directory names, not file names, so that doesn't work.

And yes, ideally the e2e tests should go in a different directory and not rely on the internals of the package but unfortunately that's not how my coworkers wrote them. I'm looking for a quick fix right now!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dousi6701 dousi6701 4年前

    You can use build tags to limit builds, including tests, to a certain subset of files: https://dave.cheney.net/2014/09/28/using-build-to-switch-between-debug-and-release

    For example, you could put //+build integration at the top of your integration test files, and then those would only be built and executed when you run go test -tags integration.

    点赞 评论 复制链接分享