패닉을 테스트하는 방법?
현재 주어진 코드가 당황했는지 확인하는 테스트를 작성하는 방법을 고민하고 있습니다. Go가 recover패닉을 잡기 위해 사용한다는 것을 알고 있지만 Java 코드와 달리 패닉이 발생했을 때 건너 뛸 코드 나 무엇을 가지고 있는지 실제로 지정할 수는 없습니다. 그래서 내가 기능이 있다면 :
func f(t *testing.T) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
OtherFunctionThatPanics()
t.Errorf("The code did not panic")
}
나는 OtherFunctionThatPanics당황하고 우리가 회복 했는지 , 또는 기능이 전혀 당황하지 않았 는지 정말로 말할 수 없습니다 . 패닉이없는 경우 건너 뛸 코드와 패닉이있는 경우 실행할 코드를 어떻게 지정합니까? 회복 된 패닉이 있었는지 어떻게 확인할 수 있습니까?
testing"성공"의 개념이 아니라 실패뿐입니다. 따라서 위의 코드는 거의 맞습니다. 이 스타일이 약간 더 명확하다는 것을 알 수 있지만 기본적으로는 동일합니다.
func TestPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
// The following is the code under test
OtherFunctionThatPanics()
}
나는 일반적으로 testing상당히 약하다는 것을 알게 됩니다. Ginkgo 와 같은 더 강력한 테스트 엔진에 관심이있을 수 있습니다 . 전체 Ginkgo 시스템을 원하지 않더라도 .NET 과 함께 사용할 수있는 일치 라이브러리 인 Gomega 만 사용할 수 있습니다 testing. Gomega에는 다음과 같은 매 처가 포함됩니다.
Expect(OtherFunctionThatPanics).To(Panic())
패닉 체크를 간단한 기능으로 마무리 할 수도 있습니다.
func TestPanic(t *testing.T) {
assertPanic(t, OtherFunctionThatPanics)
}
func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
f()
}
testify / assert 를 사용 하는 경우 한 줄짜리입니다.
func TestOtherFunctionThatPanics(t *testing.T) {
assert.Panics(t, OtherFunctionThatPanics, "The code did not panic")
}
또는 다음 OtherFunctionThatPanics이외의 서명이있는 경우 func():
func TestOtherFunctionThatPanics(t *testing.T) {
assert.Panics(t, func() { OtherFunctionThatPanics(arg) }, "The code did not panic")
}
아직 testify를 시도하지 않았다면 testify / mock을 확인하십시오 . 매우 간단한 주장과 모의.
패닉의 내용을 확인해야 할 때 복구 된 값을 typecast 할 수 있습니다.
func TestIsAheadComparedToPanicsWithDifferingStreams(t *testing.T) {
defer func() {
err := recover().(error)
if err.Error() != "Cursor: cannot compare cursors from different streams" {
t.Fatalf("Wrong panic message: %s", err.Error())
}
}()
c1 := CursorFromserializedMust("/foo:0:0")
c2 := CursorFromserializedMust("/bar:0:0")
// must panic
c1.IsAheadComparedTo(c2)
}
테스트중인 코드가 당황하지 않거나 오류와 함께 당황하거나 예상 한 오류 메시지와 함께 당황하면 테스트가 실패합니다 (원하는대로).
여러 테스트 케이스를 반복 할 때 다음과 같이 할 것입니다.
package main
import (
"reflect"
"testing"
)
func TestYourFunc(t *testing.T) {
type args struct {
arg1 int
arg2 int
arg3 int
}
tests := []struct {
name string
args args
want []int
wantErr bool
wantPanic bool
}{
//TODO: write test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("SequenceInt() recover = %v, wantPanic = %v", r, tt.wantPanic)
}
}()
got, err := YourFunc(tt.args.arg1, tt.args.arg2, tt.args.arg3)
if (err != nil) != tt.wantErr {
t.Errorf("YourFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("YourFunc() = %v, want %v", got, tt.want)
}
})
}
}
패닉에 입력을 제공하여 패닉 된 기능을 테스트 할 수 있습니다.
package main
import "fmt"
func explode() {
// Cause a panic.
panic("WRONG")
}
func explode1() {
// Cause a panic.
panic("WRONG1")
}
func main() {
// Handle errors in defer func with recover.
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok := r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
fmt.Println(err)
}
}
}()
// These causes an error. change between these
explode()
//explode1()
fmt.Println("Everything fine")
}
http://play.golang.org/p/ORWBqmPSVA
참고 URL : https://stackoverflow.com/questions/31595791/how-to-test-panics
'Program Club' 카테고리의 다른 글
| 파이썬 셀레늄 클릭 버튼 (0) | 2020.11.21 |
|---|---|
| user.email 및 user.name을 설정하지 않고 커밋 (0) | 2020.11.21 |
| React.createElement : 유형이 잘못되었습니다. 문자열이 필요합니다. (0) | 2020.11.21 |
| Angular 4+ ngOnDestroy () 서비스 중-Observable 제거 (0) | 2020.11.21 |
| C ++ 17 : 튜플 압축을 풀 때 일부 멤버 만 유지 (0) | 2020.11.21 |