Why am I getting "no tests to run" despite having a test function?

Viewed 4576

Go newbie here, I have written a simple main_test.go file to run some test cases for main.go, when I run go test it says testing: warning: no tests to run PASS ok Solution 0.878s

my main.go:

package main

func normalizePhoneNum(phoneNumber string) string {
    return ""
}

func main() {

}

main_test.go:

package main

import (
    "testing"
)

func testNormalizePhoneNum(t *testing.T) {
    testCase := []struct {
        input  string
        output string
    }{
        {"1234567890", "1234567890"},
        {"123 456 7891", "123 456 7891"},
        {"(123) 456 7892", "(123) 456 7892"},
        {"(123) 456-7893", "(123) 456-7893"},
        {"123-456-7894", "123-456-7894"},
        {"123-456-7890", "123-456-7890"},
        {"1234567892", "1234567892"},
        {"(123)456-7892", "(123)456-7892"},
    }
    for _, tc := range testCase {
        t.Run(tc.input, func(t *testing.T) {
            actual := normalizePhoneNum(tc.input)
            if actual != tc.output {
                t.Errorf("for %s: got %s, expected %s", tc.input, actual, tc.output)
            }
        })
    }
}

Can anyone please tell, why it's not running the test cases?

1 Answers

Elementary! See the documentation for the go test command:

A test function is one named TestXxx (where Xxx does not start with a lower case letter) and should have the signature,

func TestXxx(t *testing.T) { ... }

Note that the first letter must be an uppercase T. You must respect this naming convention for test functions or the testing tool will simply ignore them.

Rename your test function to TestNormalizePhoneNum and try running go test again.


Alternatively—and although that is most likely not what you want here—you can force the testing tool to run a "test function" that doesn't adhere to the naming convention by specifying its name (or, more generally, a regular expression that its name matches) in the -run flag:

go test -run=testNormalizePhoneNum
Related