diff --git a/example/example_panic_handling_test.go b/example/example_panic_handling_test.go new file mode 100644 index 0000000..e9ae21d --- /dev/null +++ b/example/example_panic_handling_test.go @@ -0,0 +1,12 @@ +package example + +import ( + "github.com/dailymotion/allure-go" + "testing" +) + +func TestPanicBasic(t *testing.T) { + allure.Test(t, "Panic handling test", func() { + panic("throwing a panic") + }) +} diff --git a/example/example_panic_in_step_test.go b/example/example_panic_in_step_test.go new file mode 100644 index 0000000..4d3c05b --- /dev/null +++ b/example/example_panic_in_step_test.go @@ -0,0 +1,14 @@ +package example + +import ( + "github.com/dailymotion/allure-go" + "testing" +) + +func TestPanicInStep(t *testing.T) { + allure.Test(t, "Panic handling test", func() { + allure.Step("step that will panic", func() { + panic("throwing a panic") + }) + }) +} diff --git a/step.go b/step.go index ebc3877..0f723fe 100644 --- a/step.go +++ b/step.go @@ -1,6 +1,7 @@ package allure import ( + "github.com/pkg/errors" "log" "testing" @@ -64,27 +65,36 @@ func StepWithParameter(description string, parameters map[string]interface{}, ac } defer func() { + panicObject := recover() step.Stop = getTimestampMs() manipulateOnObjectFromCtx( testInstanceKey, func(testInstance interface{}) { - if testInstance.(*testing.T).Failed() { + if panicObject != nil { + Break(errors.Errorf("%+v", panicObject)) + } + if testInstance.(*testing.T).Failed() || + panicObject != nil { if step.Status == "" { step.Status = "failed" } } }) + step.Stage = "finished" + if step.Status == "" { + step.Status = "passed" + } manipulateOnObjectFromCtx(nodeKey, func(currentStepObj interface{}) { currentStep := currentStepObj.(hasSteps) currentStep.AddStep(*step) }) + + if panicObject != nil { + panic(panicObject) + } }() ctxMgr.SetValues(gls.Values{nodeKey: step}, action) - step.Stage = "finished" - if step.Status == "" { - step.Status = "passed" - } } // SkipStepWithParameter doesn't execute the action and marks the step as skipped in report diff --git a/test.go b/test.go index 5483b79..4392111 100644 --- a/test.go +++ b/test.go @@ -1,10 +1,12 @@ package allure import ( + "fmt" "github.com/dailymotion/allure-go/severity" "github.com/fatih/camelcase" "github.com/jtolds/gls" "log" + "runtime/debug" "strings" "testing" ) @@ -41,7 +43,16 @@ func TestWithParameters(t *testing.T, description string, parameters map[string] } defer func() { + panicObject := recover() r.Stop = getTimestampMs() + if panicObject != nil { + t.Fail() + r.StatusDetails = &statusDetails{ + Message: fmt.Sprintf("%+v", panicObject), + Trace: filterStackTrace(debug.Stack()), + } + r.Status = broken + } if r.Status == "" { r.Status = getTestStatus(t) } @@ -51,6 +62,10 @@ func TestWithParameters(t *testing.T, description string, parameters map[string] if err != nil { log.Println("Failed to write content of result to json file", err) } + + if panicObject != nil { + panic(panicObject) + } }() ctxMgr.SetValues(gls.Values{ testResultKey: r,