failure causes dependent tests to be skipped

g-counter
Jordan Orelli 4 years ago
parent 10508bceeb
commit 094ecccb5a

@ -4,24 +4,35 @@ import "testing"
type step struct { type step struct {
Test Test
name string
next *step next *step
skip bool
} }
func (s *step) run(t *testing.T) { func (s *step) run(t *testing.T) {
t.Logf("running step: %v", s.Test) if s.skip {
if s.next != nil {
s.next.skip = true
t.Run(s.next.name, s.next.run)
}
t.SkipNow()
}
s.Test.Run(t) s.Test.Run(t)
if s.next != nil { if s.next != nil {
s.next.run(t) if t.Failed() || t.Skipped() {
s.next.skip = true
}
t.Run(s.next.name, s.next.run)
} }
} }
func (t *Tree) plan() []step { func (t *Tree) plan() []step {
if len(t.children) == 0 { if len(t.children) == 0 {
// this is a leaf node. s := &step{Test: t.Test, name: parseName(t.Test)}
s := &step{Test: t.Test}
for t.parent != nil { for t.parent != nil {
t = t.parent t = t.parent
s = &step{Test: t.Test, next: s} s = &step{Test: t.Test, name: parseName(t.Test), next: s}
} }
return []step{*s} return []step{*s}
} }

@ -1,32 +1,46 @@
package tea package tea
import "testing" import (
// "reflect"
"testing"
)
// Run runs a tree of tests, starting from its root. // Run runs a tree of tests, starting from its root.
func Run(t *testing.T, tree *Tree) { func Run(t *testing.T, tree *Tree) {
plan := tree.plan() plan := tree.plan()
t.Logf("steps in plan: %d", len(plan)) for _, step := range plan {
for _, start := range plan { t.Run(step.name, step.run)
t.Logf("start test %T: %#v", start.Test, start.Test)
start.run(t)
} }
} }
func New(test Test) *Tree { func New(test Test) *Tree {
return &Tree{Test: test} return &Tree{
Test: test,
name: parseName(test),
}
} }
type Tree struct { type Tree struct {
Test Test
name string
parent *Tree parent *Tree
children []*Tree children []*Tree
} }
func (t *Tree) Child(test Test) *Tree { func (t *Tree) Child(test Test) *Tree {
child := &Tree{ child := New(test)
Test: test, child.parent = t
parent: t,
}
t.children = append(t.children, child) t.children = append(t.children, child)
return child return child
} }
func parseName(test Test) string {
if s, ok := test.(interface{ String() string }); ok {
return s.String()
}
return "???"
// T := reflect.TypeOf(test)
// for i := 0; i < T.NumField(); i++ {
// field := T.Field(i)
// }
}

@ -14,7 +14,6 @@ type testThingSetup struct {
} }
func (test *testThingSetup) Run(t *testing.T) { func (test *testThingSetup) Run(t *testing.T) {
t.Log("Running testThingSetup")
test.thing = new(Thing) test.thing = new(Thing)
} }
@ -27,11 +26,10 @@ type setKey struct {
} }
func (test setKey) String() string { func (test setKey) String() string {
return fmt.Sprintf("setKey(%q=%q)", test.key, test.value) return fmt.Sprintf("setKey:%s", test.key)
} }
func (test *setKey) Run(t *testing.T) { func (test *setKey) Run(t *testing.T) {
t.Logf("Running setKey key: %q value: %q bad?: %t", test.key, test.value, test.bad)
thing := new(Thing) thing := new(Thing)
err := thing.Set(test.key, test.value) err := thing.Set(test.key, test.value)
@ -45,9 +43,24 @@ func (test *setKey) Run(t *testing.T) {
func TestThing(t *testing.T) { func TestThing(t *testing.T) {
root := tea.New(new(testThingSetup)) root := tea.New(new(testThingSetup))
root.Child(&setKey{key: "alice", value: "apple"})
bob := root.Child(&setKey{key: "bob", value: "banana"}) {
bob.Child(&setKey{key: "car-el", value: "candy"}) root.Child(&setKey{key: "alice", value: "apple"})
root.Child(&setKey{key: "d' oh", bad: true}) root.Child(&setKey{key: "bob", value: "banana"})
root.Child(&setKey{key: "carol", value: "cherry"})
}
{
test := root.Child(&setKey{key: "b ob", value: "banana"})
test = test.Child(&setKey{key: "car-el", value: "cherry"})
test = test.Child(&setKey{key: "dave", value: "durian"})
}
{
root.Child(&setKey{key: "al ice", bad: true})
root.Child(&setKey{key: " alice", bad: true})
root.Child(&setKey{key: "alice ", bad: true})
}
tea.Run(t, root) tea.Run(t, root)
} }

Loading…
Cancel
Save