testing.go 616 B

12345678910111213141516171819202122232425
  1. package missinggo
  2. import (
  3. "regexp"
  4. "runtime"
  5. )
  6. // It will be the one and only identifier after a package specifier.
  7. var testNameRegexp = regexp.MustCompile(`\.(Test[\p{L}_\p{N}]*)`)
  8. // Returns the name of the test function from the call stack. See
  9. // http://stackoverflow.com/q/35535635/149482 for another method.
  10. func GetTestName() string {
  11. pc := make([]uintptr, 32)
  12. n := runtime.Callers(0, pc)
  13. for i := 0; i < n; i++ {
  14. name := runtime.FuncForPC(pc[i]).Name()
  15. ms := testNameRegexp.FindStringSubmatch(name)
  16. if ms == nil {
  17. continue
  18. }
  19. return ms[1]
  20. }
  21. panic("test name could not be recovered")
  22. }