Skip to content

Commit

Permalink
Merge pull request #27 from dailymotion/handling_before_test_failure
Browse files Browse the repository at this point in the history
Implemented a fix to handle panics in allure.BeforeTest statement whi…
  • Loading branch information
ubermensch01 committed Mar 6, 2020
2 parents 656b250 + 1164daf commit 3ce7241
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
36 changes: 36 additions & 0 deletions before.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package allure

import (
"fmt"
"github.com/fatih/camelcase"
"github.com/jtolds/gls"
"log"
"runtime/debug"
"strings"
"testing"
)

Expand All @@ -24,10 +28,42 @@ func BeforeTest(t *testing.T, testOptions ...Option) {
}

defer func() {
panicObject := recover()

beforeSubContainer.Stop = getTimestampMs()
beforeSubContainer.Status = getTestStatus(t)
beforeSubContainer.Stage = "finished"

if panicObject != nil {
t.Fail()
beforeSubContainer.StatusDetails = &statusDetails{
Message: fmt.Sprintf("%+v", panicObject),
Trace: filterStackTrace(debug.Stack()),
}
beforeSubContainer.Status = broken

r := newResult()
r.Stop = getTimestampMs()
r.Name = strings.Join(camelcase.Split(t.Name())[1:], " ")
r.Description = t.Name()
r.setDefaultLabels(t)
r.Status = skipped

err := r.writeResultsFile()
if err != nil {
log.Println("Failed to write content of result to json file", err)
}

setups := getCurrentTestPhaseObject(t).Befores
for _, setup := range setups {
setup.Children = append(setup.Children, r.UUID)
err := setup.writeResultsFile()
if err != nil {
log.Println("Failed to write content of result to json file", err)
}
}
panic(panicObject)
}
}()
ctxMgr.SetValues(gls.Values{
testResultKey: beforeSubContainer,
Expand Down
14 changes: 14 additions & 0 deletions example/example_before_panic_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 TestBeforePanic(t *testing.T) {
allure.BeforeTest(t, allure.Action(func() {
panic("panic at the before statement! (disco)")
}))

allure.Test(t, allure.Action(func() {}))
}

0 comments on commit 3ce7241

Please sign in to comment.