down123321123 2014-06-04 05:58
浏览 1218
已采纳

通过go test跳过一些测试

Is it possible to skip/exclude some tests from being run with go test?

I have a fairly large amount of integration type tests which call a rest service written as standard go tests, and run with go test. When a new feature is developed its sometimes useful to be able to skip some of the tests, for example if the new feature is not yet deployed on the testing server and I still want to run all the existing tests (except those new ones which tests the new feature).

I know about -run, but I dont want to specify all tests I want to run, that would be a long list. At the same time I have not been able to write a regex for excluding tests.

Another option would be to not commit the tests which dont run in the same branch, but it would be easier if I could just specify what to exclude.

  • 写回答

2条回答 默认 最新

  • duanpa1898 2014-06-04 11:31
    关注

    Like VonC said, you can use +build tags

    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ ls
    a_test.go  b_test.go  c_test.go
    

    a_test.go :

    package tags
    
    import "testing"
    
    func TestA(t *testing.T) {}
    

    b_test.go :

    // +build !feature1
    
    package tags
    
    import "testing"
    
    func TestB(t *testing.T) {}
    

    c_test.go :

    // +build !feature1
    // +build !feature2
    
    package tags
    
    import "testing"
    
    func TestC(t *testing.T) {}
    

    Then run the test with the -tags parameter :

    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v . | grep PASS:
    --- PASS: TestA (0.00 seconds)
    --- PASS: TestB (0.00 seconds)
    --- PASS: TestC (0.00 seconds)
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v -tags feature1 . | grep PASS:
    --- PASS: TestA (0.00 seconds)
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v -tags feature2 . | grep PASS:
    --- PASS: TestA (0.00 seconds)
    --- PASS: TestB (0.00 seconds)
    

    // Update : different logic :

    a_test.go:

    // +build all
    
    package tags
    
    import "testing"
    
    func TestA(t *testing.T) {}
    

    b_test.go:

    // +build all feature1
    
    package tags
    
    import "testing"
    
    func TestB(t *testing.T) {}
    

    c_test.go:

    // +build all feature2
    
    package tags
    
    import "testing"
    
    func TestC(t *testing.T) {}
    
    
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v -tags all | grep PASS:
    --- PASS: TestA (0.00 seconds)
    --- PASS: TestB (0.00 seconds)
    --- PASS: TestC (0.00 seconds)
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v -tags feature1 | grep PASS:
    --- PASS: TestB (0.00 seconds)
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -v -tags="feature1 feature2" | grep PASS:
    --- PASS: TestB (0.00 seconds)
    --- PASS: TestC (0.00 seconds)
    

    Or you call specific tests by name like :

    d_test.go:

    package tags
    
    import "testing"
    
    func TestA1(t *testing.T) {}
    func TestB1(t *testing.T) {}
    func TestC1(t *testing.T) {}
    func TestD1(t *testing.T) {}
    

    Output:

    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -run="(A|B)1" -v | grep PASS:
    --- PASS: TestA1 (0.00 seconds)
    --- PASS: TestB1 (0.00 seconds)
    ┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
    └──➜ go test -run="D1" -v | grep PASS:
    --- PASS: TestD1 (0.00 seconds)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端