首页 > 解决方案 > 为什么这个 Go 代码输出 `testing: warning: no tests to run`?

问题描述

为什么这个 Go 代码输出testing: warning: no tests to run

package test

import (
    "fmt"
    "testing"
    "github.com/stretchr/testify/assert"
)

func testCode(t *testing.T) {
    arr := [2]string{"stringA", "stringB"}
    fmt.Println(arr)
    
    assert.Contains(t, arr, "stringB")
}

标签: go

解决方案


你必须导出你的测试函数,所以它必须是func TestCode(大写 T):

-func testCode(t *testing.T) {
+func TestCode(t *testing.T) {
     arr := [2]string{"stringA", "stringB"}
     fmt.Println(arr)
    
     assert.Contains(t, arr, "stringB")
 }

推荐阅读