duanfangbunao36970 2013-10-05 16:50
浏览 70
已采纳

子目录中的Golang测试

I want to create a package in Go with tests and examples for the package as subdirectories to keep the workspace cleaner. Is this possible and if so how?

All the documentation always puts the testing code in the same place as the other code, is this better in some way or just convention?

  • 写回答

4条回答 默认 最新

  • duansha7453 2014-02-12 10:43
    关注

    Note that you can run go test "recursively": you need to list all the packages you want to test.

    If you are are the root folder of your Go project, type:

    go test ./...
    

    The './...' notation is described in the section "Description of package lists" of the "command go":

    An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes.

    Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.

    As a special case, x/... matches x as well as x's subdirectories.
    For example, net/... expands to net and packages in its subdirectories.


    If you keep your _test.go files in a subfolder, the 'go test ./...' command will be able to pick them up.
    But:

    • you will need to prefix your exported variables and functions (used in your tests) with the name of your package, in order for the test file to be able to access the package exported content.
    • you wouldn't access non-exported content.

    That being said, I would still prefer keep the _test.go file right beside the main source file: it is easier to find.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分