在 Go 中执行 main 函数测试时出现问题

2024年 2月 11日 50.7k 0

在 go 中执行 main 函数测试时出现问题

php小编西瓜在进行 Go 语言的开发时,经常会遇到在执行 main 函数进行测试时出现问题的情况。这种情况可能会导致程序无法正常运行或者出现意料之外的结果。为了解决这个问题,我们需要仔细排查可能的原因,并采取相应的措施来修复代码。本文将介绍一些常见的问题和解决方法,帮助开发者更好地应对这类情况。

问题内容

我几个月前编写了这个测试,它运行成功,没有任何错误,并且从未执行过 init() 函数,因为我只从测试中调用了处理程序函数,但现在它失败并出现以下错误:

/usr/local/opt/go/libexec/bin/go tool test2json -t /private/var/folders/wt/fc27nzn51rdbvwvjzkl05hlc0000gs/T/GoLand/___TestHandleRequest_in_next_api_cmd_getAllRawTransportOrders.test -test.v -test.paniconexit0 -test.run ^QTestHandleRequestE$
{"level":"fatal","msg":"no configuration found: not set","time":"2023-08-27T22:48:03+05:30"}

Process finished with the exit code 1

登录后复制

我知道测试正在调用 init() 函数,但为什么不更早呢。我在这里缺少一些需要理解的东西吗?

init.go

//go:build !test
// +build !test

package main

import (
"crypto/tls"
"encoding/json"
log "github.com/sirupsen/logrus"
"os"

"lib/storage/ssm"

"next-api/aws/dynamodb"
"next-api/cmd/utils"
"next-api/config"
"next-api/http"
)

func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})

// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)

// Only log the warning severity or above.
log.SetLevel(log.InfoLevel)

var err error
paramsRepo := ssm.NewRepository()

configID, err := utils.GetEnv("CONFIG_ID")
if err != nil {
log.Fatal("no configuration found: ", err)
}

cfg, err = config.Load(configID, paramsRepo)
if err != nil {
log.Fatal("could not load config: ", err)
}

urls := http.URLs{}
json.Unmarshal([]byte(cfg.MABES.URLs), &urls)

// configure mTLS
cert, err := tls.X509KeyPair([]byte(cfg.MABES.Certificate), []byte(cfg.MABES.PrivateKey))
if err != nil {
log.Fatal("could not create MABES certificate: ", err)
}

MABES, err = http.NewMABESClient(urls, http.WithTLS(cert))
if err != nil {
log.Fatal("could not create sofaClient client.", err)
}

NEXT = dynamodb.NewRepository(cfg.NEXT.Table)
}

登录后复制

ma​​in.go

package main

import (
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
log "github.com/sirupsen/logrus"
gohttp "net/http"

"next-api/api"
"next-api/config"
"next-api/errors"
"next-api/http"
"next-api/http/problem"
"next-api/mabes"
"next-api/next"
)

var (
cfg config.Config
MABES mabes.Client
NEXT next.Repository
)

func main() {
lambda.Start(handler)
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
facilityID := request.PathParameters["facilityID"]
if facilityID == "" {
return events.APIGatewayProxyResponse{
StatusCode: gohttp.StatusBadRequest,
Headers: http.Headers("application/problem+json"),
Body: problem.ServerError(fmt.Errorf("missing facilityID parameter")).ToJSONResponse(),
}, nil
}

transportOrders, err := api.GetAllRawTransportOrders(facilityID, request.QueryStringParameters, MABES, NEXT)
b, err := json.Marshal(transportOrders)
if err != nil {
log.Errorln(err)

if errors.Is(errors.NEXTRead, err) {
return events.APIGatewayProxyResponse{
StatusCode: gohttp.StatusOK,
Headers: http.Headers("application/json"),
Body: fmt.Sprintf(`{"data":%s}`, string(b)),
}, nil
}

return events.APIGatewayProxyResponse{
StatusCode: gohttp.StatusInternalServerError,
Headers: http.Headers("application/problem+json"),
Body: problem.ServerError(err).ToJSONResponse(),
}, nil
}

return events.APIGatewayProxyResponse{
StatusCode: gohttp.StatusOK,
Headers: http.Headers("application/json"),
Body: fmt.Sprintf(`{"data":%s}`, string(b)),
}, nil
}

登录后复制

ma​​in_test.go

package main

import (
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
"time"

"next-api/api"
"next-api/mabes"
"next-api/next"
)

func TestHandleRequest(t *testing.T) {
t.Run("should return the TransportOrders", func(t *testing.T) {
req := events.APIGatewayProxyRequest{
PathParameters: map[string]string{
"facility": "11",
},
}
timeStamp, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
sofa1 := next.SofaTransferModel{}
sofa1.Phase = "1"
sofa1.PartKey.PartCode = "abcd"
sofa1.PartKey.DispatcherFacility = "11"
sofa1.PartKey.UsageCode = "01"
sofa1.Storage.Number = "00315"
sofa1.Shipment.RequestedArrivalDate = timeStamp.String()
transportOrders := make([]next.SofaTransferModel, 0)
transportOrders = append(transportOrders, sofa1)

getAllRawTransportOrdersCalls := 0
api.GetAllRawTransportOrders = func(facilityID string, params map[string]string, MABES mabes.Client, NEXT next.Repository) (next.SofaTransferObjects, error) {
getAllRawTransportOrdersCalls++
return transportOrders, nil
}

// when
res, err := handler(req)
// then
assert.Equal(t, 1, getAllRawTransportOrdersCalls)

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

assert.True(t, strings.Contains(res.Body, transportOrders[0].PartKey.PartCode))
assert.True(t, strings.Contains(res.Body, transportOrders[0].Storage.Number))
})

}

登录后复制

解决方法

名为 init(全部小写)的顶级函数总是,并且总是在测试之前执行。 init 现在失败的原因,即使以前没有,现在看来,是环境的变化;即在 CONFIG_ID 设置之前,但现在没有设置,至少在执行测试的 shell 中没有设置。

https://www.php.cn/link/905d8fc4ffb4275a428a84589810f8f4

如果您使用 -tags test 运行 go test 命令,则将遵守 init.go 文件顶部的构建约束,即 //go:build !test,并且该文件将从测试中省略建造。但默认情况下,没有“test”构建标签,因此如果没有 -tags test 参数,这些构建约束将被忽略,并且 init.go 文件将包含在测试构建中。 p>

以上就是在 Go 中执行 main 函数测试时出现问题的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论