dppb79372 2017-03-22 17:25
浏览 159
已采纳

如何运行“进行测试”并包括单元测试文件,但排除其他测试文件[重复]

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:

gopath/
|-package1.go
|-package1_unit_test.go
|-package1_e2e_test.go
|-package2/
  |-package2.go
  |-package2_unit_test.go
  |-package2_e2e_test.go

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!

</div>
  • 写回答

1条回答 默认 最新

  • dousi6701 2017-03-22 18:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效