Skip to content

Commit

Permalink
Implemented panic handling within test and step wrappers. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubermensch01 authored and MaximeBellou committed Dec 17, 2019
1 parent 52d4576 commit 1bc7fdc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
12 changes: 12 additions & 0 deletions example/example_panic_handling_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
14 changes: 14 additions & 0 deletions example/example_panic_in_step_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
})
}
20 changes: 15 additions & 5 deletions step.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package allure

import (
"github.com/pkg/errors"
"log"
"testing"

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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,
Expand Down

0 comments on commit 1bc7fdc

Please sign in to comment.