Home >Backend Development >Golang >Why Does 'go test' Show 'no tests to run' Even With a Test Function?

Why Does 'go test' Show 'no tests to run' Even With a Test Function?

Barbara Streisand
Barbara StreisandOriginal
2024-12-05 22:52:10472browse

Why Does

Why "no tests to run" Despite Having a Test Function

Go testing requires a specific naming convention for test functions. The function name must begin with an uppercase letter "T" and follow the format TestXxx(t *testing.T).

In the provided code snippet, the test function is named testNormalizePhoneNum, which violates this convention. The testing tool ignores this function, resulting in the "no tests to run" message.

Solution:

To fix this issue, rename the test function to TestNormalizePhoneNum with an uppercase "T":

package main

import (
    "testing"
)

func TestNormalizePhoneNum(t *testing.T) {
    // ...
}

Alternative Solution:

Alternatively, you can force the testing tool to run the function by using the -run flag:

go test -run=testNormalizePhoneNum

However, this approach is not recommended as it bypasses the standard naming convention.

The above is the detailed content of Why Does 'go test' Show 'no tests to run' Even With a Test Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn