-1

In my project I have multiple packages and sub-directories. In top directory, I can run go test ./... and it runs all the tests in all sub-directories. I have been reading for quite some time on how test only tests matching a given pattern or test name but unable to find a solution. By going through these (1 2 3 and godocs) I thought I could use this:

go test ./... -run mypattern

to run tests matching mypattern but for me it runs all the tests. For now I am using a combination of cd, find and grep to run tests matching my criteria.

You can try this:

git clone https://github.com/grafana/grafana-plugin-sdk-go.git  
> go test -run TestJSON  
=== RUN   TestJSONNotice
=== RUN   TestJSONNotice/notice_with_severity_and_text
--- PASS: TestJSONNotice (0.00s)
    --- PASS: TestJSONNotice/notice_with_severity_and_text (0.00s)
=== RUN   TestJSON
=== RUN   TestJSON/json.Unmarshal_and_json.Marshal
=== RUN   TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error
=== RUN   TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data
--- PASS: TestJSON (0.00s)
    --- PASS: TestJSON/json.Unmarshal_and_json.Marshal (0.00s)
        --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error (0.00s)
        --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data (0.00s)
PASS
ok      github.com/grafana/grafana-plugin-sdk-go/data   0.016s

> cd data

> go test -run *TestJSON*  
  zsh: no matches found: *TestJSON*  

> go test -run ^TestJSON*  
  zsh: no matches found: ^TestJSON*
    
> go test -v -cover --short -race  ./... -run ^TestFloatAt*  
  zsh: no matches found: ^TestFloatAt*

Can someone please tell me how test only tests matching a given pattern or test name?

Raaka
  • 304
  • 1
  • 3
  • 15
  • 1
    It does work (though I would suggest not putting a flag after the arguments, since most go programs do not parse them). Please show a [mre] which demonstrates the problem you are having. – JimB Aug 06 '21 at 14:18
  • Passing tests do not print anything, are you really certain they are _not_ being run? – JimB Aug 06 '21 at 14:22
  • It seems to work fine on my machine. Can you share the exact value for "mypattern", and can you reproduce this in a public GitHub repo? – Deleplace Aug 06 '21 at 14:57
  • 3
    @Raaka you need to put the expression in quotes, your shell is trying to parse it as a file wildcard, which you can tell because you're getting a shell error and not a go test error. – Adrian Aug 06 '21 at 15:06
  • Thanks @Adrian that's how I started my day today: 10094 8/6/2021 09:07 go test -run "^TestLoad$" It was a combination of not using -v flag and quotes at some later point. The output from go didn't help either, it says "[no tests to run]" for the test cases left out. As I am new to the repo that I am working on I thought it was due to missing or no test cases present in the repo. Coming from node didn't help either. – Raaka Aug 06 '21 at 17:04

1 Answers1

3

Passing regular expression that fits name of your test should run matched tests...

for examples:

go test -v -cover --short -race  ./... -run ^TestError*

runs different tests that's start with TestError in the project...

?       github.com/user/project/internal/rabbitmq       [no test files]
=== RUN   TestErrorMiddleware
    printer.go:54: GET http://localhost:45678/surprise
⇨ http server started on [::]:45678
{"error":{"code":"EXPECTED","status":418},"level":"warning","msg":"expected: here's tea","time":"2021-08-06T17:25:58+03:00"}
--- PASS: TestErrorMiddleware (0.01s)
PASS
coverage: 54.8% of statements
ok      ithub.com/user/project/internal/datadog        3.725s  coverage: 70.6% of statements
?       github.com/user/project/internal/ftp    [no test files]
?       github.com/user/project/internal/mongo  [no test files]
testing: warning: no tests to run
PASS
coverage: 0.0% of statements
ok      github.com/user/project/postgres       3.704s  coverage: 0.0% of statements [no tests to run]
ok      github.com/user/project/escrow  7.784s  coverage: 0.0% of statements [no tests to run]
=== RUN   TestError
--- PASS: TestError (0.00s)
=== RUN   TestErrorHandling
--- PASS: TestErrorHandling (0.00s)
PASS
coverage: 70.6% of statements
...

what you targeting is a name of the test (function name that starts with Test)

Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33