【问题标题】:flag provided but not defined: -test.v提供但未定义的标志:-test.v
【发布时间】:2020-07-28 22:58:34
【问题描述】:

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

这是我的主要代码:

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=/用户/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 提供但未定义的标志:-test.v /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider的用法: -嘲笑 使用 'mock' 标志告诉 package rest 你想使用模型。 进程以退出代码 1 结束

【问题讨论】:

  • 你试过运行go test -mock吗?
  • 使用 -mock 选项仍然会出现同样的错误。

标签: unit-testing go goland


【解决方案1】:

这似乎与golang/go issue 33869 相似,后者又指向go/issue 31859 "testing: panic in Init if flag.Parse has already been called " 和此Go 1.13 instruction

测试标志现在注册在新的Init 函数中,该函数由生成的测试主函数调用。
因此,现在仅在运行测试二进制文件时注册测试标志,在包初始化期间调用 flag.Parse 的包可能会导致测试失败。

正如Sachin Rautthe comments 中所指出的:

问题似乎出在外部库 (github.com/mercadolibre/golang-restclient/rest)。
截至目前(2020 年 7 月),这个“golang-restclient/rest”库不支持最新版本的 Go。
它适用于 GO VERSIONS

【讨论】:

  • 我在 darwin/amd64 平台上使用 go1.13.3。我必须降级我的 go 版本来解决这个问题吗?解决此问题的替代方法是什么?
  • @AnilJ 您确实可以测试降级是否足够。再次,用于测试。
  • 将 go 编译器降级到 1.12 确实有帮助。我从这里按照编译器降级说明进行操作。 blog.notmyhostna.me/downgrade-go-installed-with-homebrew
  • @AnilJ 这很令人担忧,github.com/golang/go/issues/31859#issuecomment-577988090 确实适用:“Go 已经走了这么久(直到 1.13)告诉人们将 flag.Parse() 放入 init() 中,然后突然行为被默默打破”
  • @SachinRaut 感谢您的反馈。我已将您的评论包含在答案中以提高知名度。
猜你喜欢
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多