登录
首页 >  Golang >  Go问答

未明确定的标志:-test.v

来源:stackoverflow

时间:2024-03-10 15:06:24 266浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《未明确定的标志:-test.v》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

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

这是我的主要代码:

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 工具 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”标志告诉包其余部分您想要使用模型。 进程已完成,退出代码为 1


解决方案


这看起来与 golang/go issue 33869 类似,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 的包可能会导致测试失败。

正如 the comments 中的 Sachin Raut 所指出的:

问题似乎出在外部库 (github.com/mercadolibre/golang-restclient/rest) 上。
截至目前(2020 年 7 月),此“golang-restclient/rest”库不支持最新版本的 Go。
它与 GO 版本 <= 1.12 配合良好

今天关于《未明确定的标志:-test.v》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>