-1

I learn from the go flags from Testing flags

And I write some test files in my project.

This is my service folder:

  • service
    • family_limit_settings.go
    • family_limit_settings_test.go
    • xxxxxx.go (others go source files)
    • xxxxxx_test.go (others go test source files)

and my family_limit_settings_test.go content is as follows:

func TestSaveSettings(t *testing.T) {
    // todo
}

func TestListByFamilyId(t *testing.T) {
    // todo
}

func TestFamilyLimitVerify(t *testing.T) {
    // todo
}

func BenchmarkFamilyListByFamilyId(b *testing.B) {
    // todo
}

func BenchmarkFamilySaveSettings(b *testing.B) {
    // todo
}

func BenchmarkFamilyLimitVerify(b *testing.B) {
    // todo
}

my first question

I cd this service file and run command follow:

 go test -v -bench=.

But I find it run other test function those are not benchmark function.(I know it because something wrong occurs in other common test function)

my second question

go test -v -bench=. -run=BenchmarkFamilyListByFamilyId

I want to execute the benchmark function with the name BenchmarkFamilyListByFamilyId but I found it execute all the benchmark functions.

Linuxea
  • 319
  • 1
  • 4
  • 15
  • 3
    1. `family_limit_settings-test.go` probably should be `family_limit_settings_test.go`: Its a _underscore_ that separates "test". 2. If you do not want to run test you have to make sure none get run e.g. via `-run x`. – Volker Sep 17 '21 at 04:50
  • 1. I just write wrong here, I edit it. – Linuxea Sep 17 '21 at 05:45
  • 2. -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. – Linuxea Sep 17 '21 at 05:46

1 Answers1

2

The -run argument matches tests and examples only, -bench matches benchmarks. And . is a regular expression that matches every name, so -bench=. executes all benchmarks.

To execute only the BenchmarkFamilyListByFamilyId benchmark and none of the tests, run something like

go test -v -bench=ByFamilyId -run=XXX
Peter
  • 29,454
  • 5
  • 48
  • 60
  • -bench regexp Run only those benchmarks matching a regular expression. -run regexp Run only those tests and examples matching the regular expression. I know now but that is a really strange design. both bench and run flag execute that only match the regular expression, which is clear now. But don't specify bench flag would execute nothing benchmark, don't specify run flag however run any test and example. – Linuxea Sep 17 '21 at 07:54
  • 1
    Making the most common use-case -- testing a package -- require no options isn't strange to me at all. – Peter Sep 17 '21 at 08:24