Home > Backend Development > Golang > Why Does `go test` Show 'no tests to run' Despite Defined Functions?

Why Does `go test` Show 'no tests to run' Despite Defined Functions?

DDD
Release: 2024-12-18 05:47:13
Original
400 people have browsed it

Why Does `go test` Show

Mistaken Test Function Naming

Despite having defined a test function, you are encountering the "no tests to run" message while executing go test. Let's investigate the cause of this issue.

The Go testing package expects test functions to adhere to a specific naming convention. As per the official documentation:

func TestXxx(t *testing.T) { ... }
Copy after login

In your case, the test function is named testNormalizePhoneNum, which does not conform to the above convention. The first letter of the function name must be uppercase ("T").

Solution:

To resolve this issue, simply rename your test function to TestNormalizePhoneNum (capitalizing the "T"). Once you have made this modification, running go test should execute your test cases successfully.

Alternative Method:

Alternatively, you can force the testing package to run your non-conventionally named test function using the -run flag:

go test -run=testNormalizePhoneNum
Copy after login

This flag allows you to specify the name of the test function (or a regular expression matching its name) that you want to execute. However, this approach is generally not recommended for routine testing purposes.

The above is the detailed content of Why Does `go test` Show 'no tests to run' Despite Defined Functions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template