doufu5401 2018-04-02 04:52
浏览 152
已采纳

在一个软件包下运行go测试时,如何在测试以及覆盖率报告中跳过go文件

I want to skip a particular go file under a package, while running the go test and coverage. Please find the below command, I have used to run under my service package :

cd src/service && go test --coverprofile coverageService.out --coverpkg ./.
  • 写回答

1条回答 默认 最新

  • doulao2916 2018-04-02 05:29
    关注

    Unfortunately there's no simple way to skip tests based solely on the file name, however, here are three simple options which may work:

    1. Specify tests using a regular expression

    The go test command supports the -run runtime option which only runs tests satisfying a regular expression:

    -run regexp

    Run only those tests and examples matching the regular expression. For tests, the regular expression is split by unbracketed slash (/) characters into a sequence of regular expressions, and each part of a test's identifier must match the corresponding element in the sequence, if any. Note that possible parents of matches are run too, so that -run=X/Y matches and runs and reports the result of all tests matching X, even those without sub-tests matching Y, because it must run them to look for those sub-tests.

    In order for this to work, you will need to name all of the tests in the file in question in such a way that you can exclude them via a regular expression (for example, make all of them start with TestExcludable..., such as TestExcludableA, TestExludableB, etc). Then run using a slightly hacky negative regular expression, such as -run "Test[^E][^x][^c][^l][^u][^d][^a][^b][^l][^e].*"

    2. Don't compile certain tests using build flags

    As explained in detail in this stackoverflow post, you can use build flags to prevent a particular file from building as part of the test suite. In a nutshell,

    skip_test.go

    // +build !skipset1
    package name
    
    import testing
    
    // Rest of test file
    

    Then when you invoke go test us the -tags parameter and specify the tests you would like to skip, such as -tags skipset1.

    3. Rename the offending file during the build

    As specified in the go test docs,

    Files whose names begin with "_" (including "_test.go") or "." are ignored.

    Simply surround your go test command with a rename so that the file to skip is not actually compiled into the test executable:

    mv src/skip_test.go src/_skip_test.go && go test ... || mv src/_skip_test.go src/skip_test.go

    (note the use of an || and in the second move, to cause it to run even if the go test fails).

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向