提供但未定义的标志:-test.v

flag provided but not defined: -test.v

以下代码的单元测试失败:

这是我的主要代码:

package locprovider

import (
    "encoding/json"
    "fmt"
    "net/http"

    "api/domain/location"
    "api/utils/error"
    "github.com/mercadolibre/golang-restclient/rest"
)

const (
    getCountryUrl = "https://api.mercadolibre.com/countries/%s"
)

func GetCountry(countryId string) (*location.Country, *error.ApiError) {

    response := rest.Get(fmt.Sprintf(getCountryUrl, countryId))
    if response == nil || response.Response == nil {
        return nil, &error.ApiError {
            Status: http.StatusInternalServerError,
            Message: fmt.Sprintf("invalid restclient response when trying to get country %s", countryId),
        }
    }

    if response.StatusCode > 299 {
        var apiError error.ApiError
        if err := json.Unmarshal(response.Bytes(), &apiError); err != nil {
            return nil, &error.ApiError {
                Status: http.StatusInternalServerError,
                Message: fmt.Sprintf("invalid error response when getting country %s", countryId),
            }
        }
        return nil, &apiError
    }

    var result location.Country
    if err := json.Unmarshal(response.Bytes(), &result); err != nil {
        return nil, &error.ApiError {
            Status: http.StatusInternalServerError,
            Message: fmt.Sprintf("error when trying to unmarshal country data for %s", countryId),
        }
    }

    return &result, nil
}

这是我的单元测试代码:

package locprovider

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestGetCountryRestClientError(t *testing.T) {
    // Execution:
    country, err := GetCountry("AR")

    // Validation:
    assert.Nil(t, country)
    assert.NotNil(t, err)
    assert.EqualValues(t, 500, err.Status)
    assert.EqualValues(t, "invalid restclient response when trying to get country AR", err.Message)
}

当我执行这个单元测试时,我 运行 进入以下错误,我不确定为什么会这样。

我几乎没有来自其他 go 包的其他单元测试,它们工作正常,没有这个错误。

GOROOT=/usr/local/Cellar/go/1.13.3/libexec #gosetup GOPATH=/Users/abc/go-testing #gosetup /usr/local/Cellar/go/1.13.3/libexec/bin/go test -c -o /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider api/provider/locprovider #gosetup /usr/local/Cellar/go/1.13.3/libexec/bin/go tool test2json -t /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider -test.v -test.run ^TestGetCountryRestClientError$ #gosetup flag provided but not defined: -test.v Usage of /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider: -mock Use 'mock' flag to tell package rest that you would like to use mockups. Process finished with exit code 1

这似乎类似于 golang/go issue 33869, which in turn pointed to go/issue 31859 "testing: panic in Init if flag.Parse has already been called " and this Go 1.13 instruction

Testing flags are now registered in the new Init function, which is invoked by the generated main function for the test.
As a result, testing flags are now only registered when running a test binary, and packages that call flag.Parse during package initialization may cause tests to fail.

Sachin Raut in 所述:

The problem seems to be with the external library (github.com/mercadolibre/golang-restclient/rest).
As of now (July 2020), this "golang-restclient/rest" library does not support the latest versions of Go.
It works well with GO VERSIONS <= 1.12