Skip to content

Commit

Permalink
Updated docs for focused specs
Browse files Browse the repository at this point in the history
  • Loading branch information
greghaskins committed Feb 4, 2016
1 parent 8f5c991 commit 0e2ca3e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 21 deletions.
63 changes: 52 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", () -> {

Expand Down Expand Up @@ -52,9 +52,9 @@ public class ExampleSpec {{

});

describe("A spec using beforeEach and afterEach", () -> {
describe("A suite using beforeEach and afterEach", () -> {

final List<String> items = new ArrayList<String>();
final List<String> items = new ArrayList<>();

beforeEach(() -> {
items.add("foo");
Expand All @@ -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")));
});

Expand Down Expand Up @@ -125,15 +125,15 @@ public class ExampleSpec {{

});

describe("A spec using beforeAll", () -> {
describe("A suite using beforeAll", () -> {

final List<Integer> numbers = new ArrayList<Integer>();
final List<Integer> 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);
});
Expand All @@ -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()));
});

Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


@RunWith(Spectrum.class)
public class ExampleSpec {{
public class ExampleSpecs {{

describe("A spec", () -> {

Expand Down Expand Up @@ -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<String> items = new ArrayList<String>();
final List<String> items = new ArrayList<>();

beforeEach(() -> {
items.add("foo");
Expand All @@ -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")));
});

Expand Down Expand Up @@ -138,15 +138,15 @@ public void run() throws Throwable {

});

describe("A spec using beforeAll", () -> {
describe("A suite using beforeAll", () -> {

final List<Integer> numbers = new ArrayList<Integer>();
final List<Integer> 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);
});
Expand All @@ -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()));
});

Expand Down
63 changes: 63 additions & 0 deletions src/test/java/specs/FocusedSpecs.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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;

import org.junit.runner.Result;
import org.junit.runner.RunWith;

import com.greghaskins.spectrum.Spectrum;
import com.greghaskins.spectrum.Spectrum.Value;

import helpers.SpectrumRunner;

Expand Down Expand Up @@ -62,6 +65,24 @@ public class FocusedSpecs {{

});

describe("Focused specs example", () -> {

final Value<Result> 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 {{
Expand All @@ -82,6 +103,8 @@ class Suite {{
return Suite.class;
}



private static Class<?> getSuiteWithNestedFocusedSpecs() {
class Suite {{

Expand Down Expand Up @@ -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;
}

}

0 comments on commit 0e2ca3e

Please sign in to comment.