Skip to content

Commit

Permalink
Added an alternative mechanism for ignoring tests/suites.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Frieze committed Nov 26, 2016
1 parent 900e7b9 commit 73184fb
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.java text eol=lf
*.gradle text eol=lf
*.md text eol=lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.junit.runner.notification.RunListener;

/**
* A listener to detect test failure.
* A listener to detect test failure. This is used by {@link Suite} to
* detect whether to stop running its children if the children represent
* a singular path of testing.
*/
class FailureDetectingRunListener extends RunListener {
private boolean hasFailedYet = false;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Parent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ interface Parent {

boolean isIgnored();

Parent getParent();

default boolean isParentIgnored() {
return getParent().isIgnored() || getParent().isParentIgnored();
}

Parent NONE = new Parent() {
@Override
public void focus(final Child child) {}
Expand All @@ -14,5 +20,15 @@ public void focus(final Child child) {}
public boolean isIgnored() {
return false;
}

@Override
public Parent getParent() {
return null;
}

@Override
public boolean isParentIgnored() {
return false;
}
};
}
15 changes: 15 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Spectrum.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ public static void xit(final String behavior, final com.greghaskins.spectrum.Blo
it(behavior);
}

/**
* Makes the next item ignored.
*/
public static void ignore() {
getCurrentSuiteBeingDeclared().ignoreNext();
}

/**
* Makes the next item ignored. Allows a reason to be provided. This is not used.
* @param reason a description of why the item is being ignored.
*/
public static void ignore(final String reason) {
ignore();
}

/**
* Declare a {@link com.greghaskins.spectrum.Block} to be run before each spec in the suite.
*
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/greghaskins/spectrum/Suite.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Suite implements Parent, Child {
private final Description description;
private final Parent parent;
private boolean ignored;

private boolean ignoreNext;

/**
* The strategy for running the children within the suite.
Expand Down Expand Up @@ -105,6 +105,12 @@ private Spec createSpec(final String name, final Block block) {

private void addChild(final Child child) {
this.children.add(child);
if (this.ignoreNext || isParentIgnored()) {
child.ignore();
}

// after adding, ignore next does not apply anymore
this.ignoreNext = false;
}

void beforeAll(final Block block) {
Expand All @@ -123,6 +129,10 @@ void afterEach(final Block block) {
this.afterEach.addBlock(block);
}

void ignoreNext() {
this.ignoreNext = true;
}

@Override
public void focus(final Child child) {
this.focusedChildren.add(child);
Expand All @@ -148,6 +158,11 @@ public boolean isIgnored() {
return this.ignored;
}

@Override
public Parent getParent() {
return parent;
}

@Override
public void run(final RunNotifier notifier) {
if (testCount() == 0) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/specs/ExampleSpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static com.greghaskins.spectrum.Spectrum.beforeAll;
import static com.greghaskins.spectrum.Spectrum.beforeEach;
import static com.greghaskins.spectrum.Spectrum.describe;
import static com.greghaskins.spectrum.Spectrum.ignore;
import static com.greghaskins.spectrum.Spectrum.it;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
Expand Down Expand Up @@ -133,5 +135,39 @@ public class ExampleSpecs {

});

describe("A suite using the ignore function", () -> {
ignore();
describe("An ignored sub-suite", () -> {
it("has a child, but that's ignored too", () -> {
assertTrue(true);
});
});

describe("The next suite does not get ignored by default", () -> {
it("is able to run", () -> {
assertTrue(true);
});
});

describe("A suite can have individually ignored specs", () -> {
it("has an non ignored one", () -> {
assertTrue(true);
});

ignore();
it("has an ignored one", () -> {
assertTrue(true);
});

it("has an executed one afterwards", () -> {
assertTrue(true);
});

ignore("Ignoring this for demonstration");
it("has another ignored one", () -> {
assertTrue(true);
});
});
});
}
}
26 changes: 26 additions & 0 deletions src/test/java/specs/IgnoredSpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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.ignore;
import static com.greghaskins.spectrum.Spectrum.it;
import static com.greghaskins.spectrum.Spectrum.let;
import static com.greghaskins.spectrum.Spectrum.xdescribe;
import static com.greghaskins.spectrum.Spectrum.xit;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -82,6 +84,15 @@ public class IgnoredSpecs {
assertThat(result.get().getFailureCount(), is(0));
});
});

describe("Ignoring by method", () -> {
describe("A single suite", () -> {
it("is not executed", () -> {
Result result = SpectrumHelper.run(getSingleIgnoredSuite());
assertThat(result.getIgnoreCount(), is(1));
});
});
});
}

private static Class<?> getSuiteWithIgnoredSpecs() {
Expand Down Expand Up @@ -247,4 +258,19 @@ class FocusedSpecsExample {

return FocusedSpecsExample.class;
}

private static Class<?> getSingleIgnoredSuite() {
class SingleIgnoredExample {
{
ignore();
describe("This suite is ignored", () -> {
it("should not be run", () -> {
assertTrue(true);
});
});
}
}

return SingleIgnoredExample.class;
}
}

0 comments on commit 73184fb

Please sign in to comment.