From 0e2ca3e9815fd89a9d759dcd64d060adc3188f65 Mon Sep 17 00:00:00 2001 From: Gregory Haskins Date: Wed, 3 Feb 2016 23:55:45 -0500 Subject: [PATCH] Updated docs for focused specs --- README.md | 63 +++++++++++++++---- .../{ExampleSpec.java => ExampleSpecs.java} | 20 +++--- src/test/java/specs/FocusedSpecs.java | 63 +++++++++++++++++++ 3 files changed, 125 insertions(+), 21 deletions(-) rename src/test/java/specs/{ExampleSpec.java => ExampleSpecs.java} (89%) diff --git a/README.md b/README.md index a8055bf..988f233 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ Spectrum ## Example -> see also [ExampleSpec.java](src/test/java/specs/ExampleSpec.java) +> from [ExampleSpecs.java](src/test/java/specs/ExampleSpecs.java) ```java @RunWith(Spectrum.class) -public class ExampleSpec {{ +public class ExampleSpecs {{ describe("A spec", () -> { @@ -52,9 +52,9 @@ public class ExampleSpec {{ }); - describe("A spec using beforeEach and afterEach", () -> { + describe("A suite using beforeEach and afterEach", () -> { - final List items = new ArrayList(); + final List items = new ArrayList<>(); beforeEach(() -> { items.add("foo"); @@ -73,12 +73,12 @@ public class ExampleSpec {{ items.add("bogus"); }); - it("runs them before every test", () -> { + it("runs them before every spec", () -> { assertThat(items, contains("foo", "bar")); items.add("bogus"); }); - it("runs afterEach after every test", () -> { + it("runs afterEach after every spec", () -> { assertThat(items, not(contains("bogus"))); }); @@ -125,15 +125,15 @@ public class ExampleSpec {{ }); - describe("A spec using beforeAll", () -> { + describe("A suite using beforeAll", () -> { - final List numbers = new ArrayList(); + final List numbers = new ArrayList<>(); beforeAll(() -> { numbers.add(1); }); - it("sets the initial state before any tests run", () -> { + it("sets the initial state before any specs run", () -> { assertThat(numbers, contains(1)); numbers.add(2); }); @@ -149,12 +149,12 @@ public class ExampleSpec {{ numbers.add(3); }); - it("so proceed with caution; this *will* leak shared state across tests", () -> { + it("so proceed with caution; this *will* leak shared state across specs", () -> { assertThat(numbers, contains(1, 2, 3)); }); }); - it("cleans up after running all tests in the describe block", () -> { + it("cleans up after running all specs in the describe block", () -> { assertThat(numbers, is(empty())); }); @@ -163,6 +163,47 @@ public class ExampleSpec {{ }} ``` +### Focused Specs + +You can focus the runner on particular spec with `fit` or a suite with `fdescribe` so that only those specs get executed. + +> from [FocusedSpecs.java](src/test/java/specs/FocusedSpecs.java) + +```java +describe("Focused specs", () -> { + + fit("is focused and will run", () -> { + assertThat(true, is(true)); + }); + + it("is not focused and will not run", () -> { + throw new Exception(); + }); + + fdescribe("a focused suite", () -> { + + it("will run", () -> { + assertThat(true, is(true)); + }); + + it("all its specs", () -> { + assertThat(true, is(true)); + }); + }); + + fdescribe("another focused suite, with focused and unfocused specs", () -> { + + fit("will run focused specs", () -> { + assertThat(true, is(true)); + }); + + it("ignores unfocused specs", () -> { + throw new Exception(); + }); + }); +}); +``` + ## Supported Features Spectrum moving toward a `1.0` release with close alignment to Jasmine's test declaration API. The library already supports a nice subset of those features: diff --git a/src/test/java/specs/ExampleSpec.java b/src/test/java/specs/ExampleSpecs.java similarity index 89% rename from src/test/java/specs/ExampleSpec.java rename to src/test/java/specs/ExampleSpecs.java index 6722190..c9bb454 100644 --- a/src/test/java/specs/ExampleSpec.java +++ b/src/test/java/specs/ExampleSpecs.java @@ -27,7 +27,7 @@ @RunWith(Spectrum.class) -public class ExampleSpec {{ +public class ExampleSpecs {{ describe("A spec", () -> { @@ -65,9 +65,9 @@ public void run() throws Throwable { }); - describe("A spec using beforeEach and afterEach", () -> { + describe("A suite using beforeEach and afterEach", () -> { - final List items = new ArrayList(); + final List items = new ArrayList<>(); beforeEach(() -> { items.add("foo"); @@ -86,12 +86,12 @@ public void run() throws Throwable { items.add("bogus"); }); - it("runs them before every test", () -> { + it("runs them before every spec", () -> { assertThat(items, contains("foo", "bar")); items.add("bogus"); }); - it("runs afterEach after every test", () -> { + it("runs afterEach after every spec", () -> { assertThat(items, not(contains("bogus"))); }); @@ -138,15 +138,15 @@ public void run() throws Throwable { }); - describe("A spec using beforeAll", () -> { + describe("A suite using beforeAll", () -> { - final List numbers = new ArrayList(); + final List numbers = new ArrayList<>(); beforeAll(() -> { numbers.add(1); }); - it("sets the initial state before any tests run", () -> { + it("sets the initial state before any specs run", () -> { assertThat(numbers, contains(1)); numbers.add(2); }); @@ -162,12 +162,12 @@ public void run() throws Throwable { numbers.add(3); }); - it("so proceed with caution; this *will* leak shared state across tests", () -> { + it("so proceed with caution; this *will* leak shared state across specs", () -> { assertThat(numbers, contains(1, 2, 3)); }); }); - it("cleans up after running all tests in the describe block", () -> { + it("cleans up after running all specs in the describe block", () -> { assertThat(numbers, is(empty())); }); diff --git a/src/test/java/specs/FocusedSpecs.java b/src/test/java/specs/FocusedSpecs.java index 6af5578..726ad73 100644 --- a/src/test/java/specs/FocusedSpecs.java +++ b/src/test/java/specs/FocusedSpecs.java @@ -1,9 +1,11 @@ package specs; +import static com.greghaskins.spectrum.Spectrum.beforeEach; import static com.greghaskins.spectrum.Spectrum.describe; import static com.greghaskins.spectrum.Spectrum.fdescribe; import static com.greghaskins.spectrum.Spectrum.fit; import static com.greghaskins.spectrum.Spectrum.it; +import static com.greghaskins.spectrum.Spectrum.value; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -11,6 +13,7 @@ import org.junit.runner.RunWith; import com.greghaskins.spectrum.Spectrum; +import com.greghaskins.spectrum.Spectrum.Value; import helpers.SpectrumRunner; @@ -62,6 +65,24 @@ public class FocusedSpecs {{ }); + describe("Focused specs example", () -> { + + final Value result = value(Result.class); + + beforeEach(() -> { + result.value = SpectrumRunner.run(getFocusedSpecsExample()); + }); + + it("has two ignored specs", () -> { + assertThat(result.value.getIgnoreCount(), is(2)); + }); + + it("does not run unfocused specs", () -> { + assertThat(result.value.getFailureCount(), is(0)); + }); + + }); + } private static Class getSuiteWithFocusedSpecs() { class Suite {{ @@ -82,6 +103,8 @@ class Suite {{ return Suite.class; } + + private static Class getSuiteWithNestedFocusedSpecs() { class Suite {{ @@ -155,4 +178,44 @@ class Suite {{ return Suite.class; } +private static Class getFocusedSpecsExample() { + class FocusedSpecsExample {{ + + describe("Focused specs", () -> { + + fit("is focused and will run", () -> { + assertThat(true, is(true)); + }); + + it("is not focused and will not run", () -> { + throw new Exception(); + }); + + fdescribe("a focused suite", () -> { + + it("will run", () -> { + assertThat(true, is(true)); + }); + + it("all its specs", () -> { + assertThat(true, is(true)); + }); + }); + + fdescribe("another focused suite, with focused and unfocused specs", () -> { + + fit("will run focused specs", () -> { + assertThat(true, is(true)); + }); + + it("ignores unfocused specs", () -> { + throw new Exception(); + }); + }); + }); + + }} + return FocusedSpecsExample.class; +} + }