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 {
Test
name string
next *step
skip bool
}
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)
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 {
if len(t.children) == 0 {
// this is a leaf node.
s := &step{Test: t.Test}
s := &step{Test: t.Test, name: parseName(t.Test)}
for t.parent != nil {
t = t.parent
s = &step{Test: t.Test, next: s}
s = &step{Test: t.Test, name: parseName(t.Test), next: s}
}
return []step{*s}
}

@ -1,32 +1,46 @@
package tea
import "testing"
import (
// "reflect"
"testing"
)
// Run runs a tree of tests, starting from its root.
func Run(t *testing.T, tree *Tree) {
plan := tree.plan()
t.Logf("steps in plan: %d", len(plan))
for _, start := range plan {
t.Logf("start test %T: %#v", start.Test, start.Test)
start.run(t)
for _, step := range plan {
t.Run(step.name, step.run)
}
}
func New(test Test) *Tree {
return &Tree{Test: test}
return &Tree{
Test: test,
name: parseName(test),
}
}
type Tree struct {
Test
name string
parent *Tree
children []*Tree
}
func (t *Tree) Child(test Test) *Tree {
child := &Tree{
Test: test,
parent: t,
}
child := New(test)
child.parent = t
t.children = append(t.children, 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) {
t.Log("Running testThingSetup")
test.thing = new(Thing)
}
@ -27,11 +26,10 @@ type setKey struct {
}
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) {
t.Logf("Running setKey key: %q value: %q bad?: %t", test.key, test.value, test.bad)
thing := new(Thing)
err := thing.Set(test.key, test.value)
@ -45,9 +43,24 @@ func (test *setKey) Run(t *testing.T) {
func TestThing(t *testing.T) {
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: "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)
}

Loading…
Cancel
Save