diff --git a/README.md b/README.md index d4d86de..11ae0f0 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,13 @@ func Test(t *testing.T) { g.Assert(3+1).Equal(4) }) }) + + g.Skip.Describe("Skip all tests in this block", func(){ + // Skipped + g.It("Should not run", func() { + g.Assert(2).Equal(4) + }) + }) } ``` diff --git a/describe_test.go b/describe_test.go index f009466..03665f9 100644 --- a/describe_test.go +++ b/describe_test.go @@ -171,3 +171,69 @@ func TestNestedAfter(t *testing.T) { t.Fatal("Failed") } } + +func TestSkipDescribe(t *testing.T) { + g := Goblin(t) + + g.Describe("Describe will run", func() { + + g.It("This will run", func() { + g.Assert(4).Equal(4) + }) + }) + + g.Skip.Describe("Describe will not run", func() { + + g.Before(func() { + t.Fatal("This Before() should not run") + }) + g.After(func() { + t.Fatal("This After() should not run") + }) + + g.BeforeEach(func() { + t.Fatal("This BeforeEach() should not run") + }) + g.AfterEach(func() { + t.Fatal("This AfterEach() should not run") + }) + + g.JustBeforeEach(func() { + t.Fatal("This JustBeforeEach() should not run") + }) + + g.It("This will not run", func() { + t.Fatal("Failed") + }) + + g.Describe("Describe will not run also", func() { + g.Before(func() { + t.Fatal("This Before() should not run") + }) + g.After(func() { + t.Fatal("This After() should not run") + }) + g.It("This will not run also", func() { + t.Fatal("Failed") + }) + }) + }) + + g.Describe("Last describe will run", func() { + + counter := 0 + + g.Before(func() { + counter++ + }) + g.BeforeEach(func() { + counter++ + }) + g.JustBeforeEach(func() { + counter++ + }) + g.It("This will run", func() { + g.Assert(counter).Equal(3) + }) + }) +} diff --git a/goblin.go b/goblin.go index 8585153..10fa2bf 100644 --- a/goblin.go +++ b/goblin.go @@ -26,6 +26,7 @@ func (g *G) Describe(name string, h func()) { if d.parent != nil { d.parent.children = append(d.parent.children, Runnable(d)) + d.isSkipped = d.parent.isSkipped } g.parent = d @@ -59,6 +60,7 @@ type Describe struct { justBeforeEach []func() hasTests bool parent *Describe + isSkipped bool } func (d *Describe) runBeforeEach() { @@ -95,7 +97,12 @@ func (d *Describe) runAfterEach() { func (d *Describe) run(g *G) bool { failed := false if d.hasTests { - g.reporter.BeginDescribe(d.name) + + if d.isSkipped { + g.reporter.BeginXdescribe(d.name) + } else { + g.reporter.BeginDescribe(d.name) + } for _, b := range d.befores { b() @@ -135,6 +142,11 @@ type It struct { func (it *It) run(g *G) bool { g.currentIt = it + if it.parent.isSkipped { + g.reporter.ItIsExcluded(it.name) + return false + } + if it.h == nil { g.reporter.ItIsPending(it.name) return false @@ -166,6 +178,14 @@ func (it *It) failed(msg string, stack []string) { it.failure = &Failure{Stack: stack, Message: msg, TestName: it.parent.name + " " + it.name} } +type Skip struct { + g *G +} + +func (s *Skip) Describe(name string, h func()) { + s.g.xdescribe(name, h) +} + type Xit struct { h interface{} name string @@ -206,6 +226,8 @@ func Goblin(t *testing.T, arguments ...string) *G { parseFlags() } g := &G{t: t, timeout: *timeout} + g.Skip = &Skip{g: g} + var fancy TextFancier if *isTty { fancy = &TerminalFancier{} @@ -267,6 +289,7 @@ type G struct { shouldContinue chan bool mutex sync.Mutex timer *time.Timer + Skip *Skip } func (g *G) SetReporter(r Reporter) { @@ -295,6 +318,28 @@ func (g *G) Xit(name string, h ...interface{}) { } } +func (g *G) xdescribe(name string, h func()) { + d := &Describe{name: name, h: h, parent: g.parent, isSkipped: true} + + if d.parent != nil { + d.parent.children = append(d.parent.children, Runnable(d)) + } + + g.parent = d + + h() + + g.parent = d.parent + + if g.parent == nil && d.hasTests { + g.reporter.Begin() + if d.run(g) { + g.t.Fail() + } + g.reporter.End() + } +} + func matchesRegex(value string) bool { if runRegex != nil { return runRegex.MatchString(value) @@ -310,22 +355,37 @@ func notifyParents(d *Describe) { } func (g *G) Before(h func()) { + if g.parent.isSkipped { + return + } g.parent.befores = append(g.parent.befores, h) } func (g *G) BeforeEach(h func()) { + if g.parent.isSkipped { + return + } g.parent.beforeEach = append(g.parent.beforeEach, h) } func (g *G) JustBeforeEach(h func()) { + if g.parent.isSkipped { + return + } g.parent.justBeforeEach = append(g.parent.justBeforeEach, h) } func (g *G) After(h func()) { + if g.parent.isSkipped { + return + } g.parent.afters = append(g.parent.afters, h) } func (g *G) AfterEach(h func()) { + if g.parent.isSkipped { + return + } g.parent.afterEach = append(g.parent.afterEach, h) } diff --git a/reporting.go b/reporting.go index 39708ec..4d52565 100644 --- a/reporting.go +++ b/reporting.go @@ -9,6 +9,7 @@ import ( type Reporter interface { BeginDescribe(string) + BeginXdescribe(string) EndDescribe() Begin() End() @@ -89,6 +90,12 @@ func (r *DetailedReporter) BeginDescribe(name string) { r.level++ } +func (r *DetailedReporter) BeginXdescribe(name string) { + fmt.Println("") + r.print(r.fancy.Yellow(name)) + r.level++ +} + func (r *DetailedReporter) EndDescribe() { r.level-- } diff --git a/reporting_test.go b/reporting_test.go index e13edfe..d1ab355 100644 --- a/reporting_test.go +++ b/reporting_test.go @@ -23,6 +23,10 @@ func (r *FakeReporter) BeginDescribe(name string) { r.describes = append(r.describes, name) } +func (r *FakeReporter) BeginXdescribe(name string) { + r.BeginDescribe(name) +} + func (r *FakeReporter) EndDescribe() { r.ends++ }