在异常情况下运行Golang中的测试用例

huangapple go评论86阅读模式
英文:

Running testcases in golang during exception

问题

我正在为不返回任何值的方法编写测试用例,例如:

func GetByNameReturnNull(serName string) {
    //逻辑
}

我的测试用例文件是myTest.go,其中有两个参数,一个是使用无效输入调用该方法,另一个是使用有效输入调用该方法。

func Test1(t *testing.T) { 
    GetByNameReturnNull("Invalid")
}

func Test2(t *testing.T) { 
    GetByNameReturnNull("valid")
}

因此,第一个测试用例将失败并抛出异常,我无法以常规方式处理它,例如:

检查从返回的方法中的错误,因为该方法根本不返回任何内容。

当我执行以下命令时:

$ go test ./... -v

第二个测试用例将不会执行,因为第一个测试用例抛出了异常。

所以,在不改变基本方法GetByNameReturnNull的逻辑来返回错误或其他内容的情况下,有没有办法在测试用例文件中处理这种情况,以便输出中显示:

1 fail 1 pass
英文:

I am writing testcases for method with doesnt return any values , for eg:

func GetByNameReturnNull(serName string)
{
 //Logic
}

My testcasefile is myTest.go which has two parameters , one calling the method with invalid input and calling the method with valid input.

func Test1(t *testing.T) { 
	GetByNameReturnNull("Invalid")
}


func Test2(t *testing.T) { 
	
	GetByNameReturnNull("valid")
}

So , the first testcase will fail and throw the exception , I cant handle it in the conventional way like ,

"check for err from the returned method because the method doesnt return anything at all.
When I execute the command,

$go test ./... -v

the second testcase will not execute because of the exception of the first.

So Without changing any logic in the base method(GetByNameReturnNull) to return err or anything , is there any way to handle this scenario in the testcase file itself to print

1 fail 1 pass in the output?

答案1

得分: 4

@VonC是正确的,没有办法自动处理它,但是你可以简单地创建一个包装器并在每个测试中调用它。

这样你就不必使用全局变量来跟踪测试了。

示例:

func logPanic(t *testing.T, f func()) {
    defer func() {
        if err := recover(); err != nil {
            t.Errorf("paniced: %v", err)
        }
    }()
    f()
}

func Test1(t *testing.T) {
    logPanic(t, func() {
        GetByNameReturnNull("invalid")
    })
    //或者如果函数不接受参数
    //logPanic(t, GetByNameReturnNull)
}

func Test2(t *testing.T) {
    logPanic(t, func() {
        GetByNameReturnNull("valid")
    })
}

希望对你有所帮助!

英文:

@VonC is correct, there's no way to automatically handle it, however you can simply make a wrapper and call it in each test.

This way you don't have to use a global variable to keep track of the tests.

Example:

func logPanic(t *testing.T, f func()) {
	defer func() {
		if err := recover(); err != nil {
			t.Errorf("paniced: %v", err)
		}
	}()
	f()
}

func Test1(t *testing.T) {
	logPanic(t, func() {
		GetByNameReturnNull("invalid")
	})
	//or if the function doesn't take arguments
	//logPanic(t, GetByNameReturnNull)
}

func Test2(t *testing.T) {
	logPanic(t, func() {
		GetByNameReturnNull("valid")
	})
}

答案2

得分: 3

你不应该看到"1 fail",如果你期望你的测试发生panic。
你应该看到两个测试都成功。

相反,你应该专门测试panic的情况,如在"Understanding Defer, Panic and Recover"中所述:

func TestPanic(t *testing.T) error {
    defer func() {
        fmt.Println("Start Panic Defer")
        if r := recover(); r != nil {
            fmt.Println("Defer Panic:", r)
        } else {
            t.Error("Should have panicked!")
        }
    }()

    fmt.Println("Start Test")
    panic("Mimic Panic")
}

如果你调用一个以panic退出的函数,那个测试将通过。

英文:

You shouldn't see "1 fail", if you expect your test to panic.
You should see both tests succeed.

Instead, you should test specifically the panic case, as described, for instance, in "Understanding Defer, Panic and Recover ":

func TestPanic(t *testing.T) error {
	defer func() {
		fmt.Println("Start Panic Defer")
		if r := recover(); r != nil {
			fmt.Println("Defer Panic:", r)
		} else {
			t.Error("Should have panicked!")
		}
	}()

	fmt.Println("Start Test")
	panic("Mimic Panic")
}

That test would pass if you call a function which exits with a panic.

huangapple
  • 本文由 发表于 2014年8月27日 18:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/25524469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定