标签: go-testing

去assert.Equal一些带有time.Time对象的接口

我正在尝试检查返回的数据是否等于期望

这是我的功能:

func extractData(payload string) (interface{}, time.Time, error) {

    eventTime := gjson.Get(payload, "data.eventDateTime").String()

    dateTime, err := time.Parse("2006-01-02T15:04:05-07:00", eventTime)
    if err != nil {
        return nil, time.Time{}, fmt.Errorf("Date Time Error: %w", err)
    }

    body := data.Body{
        EventTime: dateTime,
    }

    return body, dateTime, nil
}
Run Code Online (Sandbox Code Playgroud)

这是我为其编写的单元测试:

func TestExtractData(t *testing.T) {

    tests := []struct {
        Name           string
        Payload        string
        ExpectedOutput interface{}
    }{
        {
            Name:    "test-2",
            Payload: "{\"data\":\"2020-11-02T10:44:48+01:00\"}",
            ExpectedOutput: data.Body{
                EventTime: time.Date(2020, 11, 2, 10, 44, 48, 0, time.FixedZone("CET", 3600)),
            },
        },
    }

    for …
Run Code Online (Sandbox Code Playgroud)

unit-testing go testify go-testing

2
推荐指数
1
解决办法
1万
查看次数

go测试用例中如何处理json语法错误?

json.Unmarshall我正在测试失败并返回的场景

&json.SyntaxError{msg:"unexpected end of JSON input", Offset:0}
Run Code Online (Sandbox Code Playgroud)

代码是这样的:

err = json.Unmarshal(input, &data)

if err != nil {
    return nil, err
}
Run Code Online (Sandbox Code Playgroud)

测试用例预计会出现这种类型的错误:

{
...
errorType: &json.SyntaxError{},
...
}
Run Code Online (Sandbox Code Playgroud)

断言是这样的:

assert.Equal(t, tt.errorType, err)
Run Code Online (Sandbox Code Playgroud)

这是失败的,因为错误消息不同:

expected: &json.SyntaxError{msg:"", Offset:0}
actual  : &json.SyntaxError{msg:"unexpected end of JSON input", Offset:0}
Run Code Online (Sandbox Code Playgroud)

我该如何处理这个问题?也许利用Error()

go go-testing

2
推荐指数
1
解决办法
1881
查看次数

为什么我仅在测试中收到“errors.As should not be *error”构建错误的第二个参数?

考虑以下测试:

import (
    "errors"
    "fmt"
    "testing"
)

func TestError(t *testing.T) {
    err := &MyError{}
    var target error
    fmt.Println(errors.As(err, &target))
}

type MyError struct{}

func (err *MyError) Error() string {
    return "oops!"
}
Run Code Online (Sandbox Code Playgroud)

运行此测试会返回构建错误second argument to errors.As should not be *error

去游乐场

但是,当在 中运行完全相同的代码时main,程序运行没有问题:

package main

import (
    "errors"
    "fmt"
)

func main() {
    err := &MyError{}
    var target error
    fmt.Println(errors.As(err, &target))
}

type MyError struct{}

func (err *MyError) Error() string {
    return "oops!"
}
Run Code Online (Sandbox Code Playgroud)

去游乐场

我在 …

go go-testing

2
推荐指数
1
解决办法
1192
查看次数

为什么 t.Fail() 不接受字符串参数?

我正在努力改进我的 Golang 测试。我正在读这个:https://ieftimov.com/post/testing-in-go-failing-tests/

我使用了t.Fatal("message")很多,而我应该使用以下组合:

t.Fail()
t.Logf()
Run Code Online (Sandbox Code Playgroud)

那么到底为什么没有一个调用可以使测试失败并记录原因呢?有没有办法让我将这样的方法添加到 test.Testing 实例中?我只想做:

t.FailWithReason("the reason the test failed")
Run Code Online (Sandbox Code Playgroud)

这是否存在,如果不存在我可以以某种方式添加它吗?

go go-testing

1
推荐指数
1
解决办法
3006
查看次数

如何在golang中初始化基准测试的测试数据?

当我为我的算法编写基准测试时,我对一个问题感到困惑!

我的测试代码详细信息已推送到 github,我将其复制到此处并添加一些注释。

https://github.com/hidstarshine/Algorithm/blob/master/leet/problem24_test.go

var TDBenchmarkSwapPairs1 *leet.ListNode

// This function may be not good, it should be init()?
func FTDBenchmarkSwapPairs1() {
    TDBenchmarkSwapPairs1 = &leet.ListNode{
        Val:  0,
        Next: nil,
    }
    changeNode := TDBenchmarkSwapPairs1
    for i := 1; i < 100; i++ {
        changeNode.Next = &leet.ListNode{
            Val:  i,
            Next: nil,
        }
        changeNode = changeNode.Next
    }
}

func BenchmarkSwapPairs1(b *testing.B) {
    FTDBenchmarkSwapPairs1() // problem is here
    for i := 0; i < b.N; i++ {
        leet.SwapPairs1(TDBenchmarkSwapPairs1)
    }
}
Run Code Online (Sandbox Code Playgroud)

在问题行中,我调用FTDBenchmarkSwapPairs1(FTD表示填充测试数据)来初始化数据。

然后令人惊奇的事情发生了,BenchmarkSwapPairs1 似乎在许多 goroutine 中运行。 …

go go-testing

0
推荐指数
1
解决办法
2563
查看次数

如何跳过失败的测试

在 Go 中,你可以跳过已经失败的测试吗?

语境:

我有一个heisenbug,目前无法确定其原因。它有时会导致某些测试失败。通过检查各种日志,我可以识别故障模式。我想做这样的事情:

if t.Failed() {
    if strings.Contains(string(suite.Stdout), "connection reset by peer") {
        t.Skip("Skip Test failed ")
    }
}
Run Code Online (Sandbox Code Playgroud)

这些测试非常有价值,尽管有 heisenbug,我还是想在 CI 中运行它们,所以这只是一个临时的解决方法。

这是行不通的。如果测试失败,是否有办法追溯跳过测试?

go go-testing

-1
推荐指数
1
解决办法
2762
查看次数

golang 测试在 test.v 的标志解析上失败

在我的 go 程序中,main 方法执行以下操作:

port := flag.Int("port", 8080, "Port number to start service on")
flag.Parse()
Run Code Online (Sandbox Code Playgroud)

我有一个虚拟测试,如下所示:

func TestName(t *testing.T) {
    fmt.Println("Hello there")
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试(从 goland 或命令行)时,出现以下错误:

/usr/local/go/bin/go tool test2json -t /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools....test -test.v -test.paniconexit0 -test.run ^\QTestName\E$
flag provided but not defined: -test.v
Usage of /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools.....test:
  -port int
        Port number to start service on (default 8080)
Run Code Online (Sandbox Code Playgroud)

当我删除主文件中的标志行时,测试正常执行 请知道如何解决此问题?

提前致谢

testing flags go goland go-testing

-2
推荐指数
1
解决办法
3268
查看次数

标签 统计

go ×7

go-testing ×7

flags ×1

goland ×1

testify ×1

testing ×1

unit-testing ×1