Skip to content

Commit 80a0274

Browse files
committed
Added a pending mehtod with reason parameter
1 parent 5a504e9 commit 80a0274

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

src/main/java/de/tobiasroeser/lambdatest/LambdaTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public interface LambdaTest {
1010
*/
1111
void pending();
1212

13+
/**
14+
* Marks the test as pending and uses the given <code>reason</code> as
15+
* message. Instructions after <code>pending()</code> will not be executed.
16+
*/
17+
void pending(String reason);
18+
1319
/**
1420
* Intercept exceptions of type <code>exceptionType</code> and fail if no
1521
* such exception or an exception with an incompatible type was thrown.

src/main/java/de/tobiasroeser/lambdatest/junit/FreeSpec.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
@RunWith(FreeSpecRunner.class)
3939
public class FreeSpec implements LambdaTest {
4040

41+
static final String PENDING_DEFAULT_MSG = "Pending";
42+
4143
private final List<LambdaTestCase> testCases = new LinkedList<>();
4244
private boolean expectFailFast;
4345

@@ -75,11 +77,20 @@ public void test(final String name, final RunnableWithException testCase) {
7577

7678
/**
7779
* Marks the test as pending. Instructions after <code>pending()</code> will
78-
* not be executed and TestNG marks the test as skipped.
80+
* not be executed.
7981
*/
8082
@Override
8183
public void pending() {
82-
throw new AssumptionViolatedException("Pending");
84+
throw new AssumptionViolatedException(PENDING_DEFAULT_MSG);
85+
}
86+
87+
/**
88+
* Marks the test as pending and uses the given <code>reason</code> as
89+
* message. Instructions after <code>pending()</code> will not be executed.
90+
*/
91+
@Override
92+
public void pending(final String reason) {
93+
throw new AssumptionViolatedException("Pending: " + reason);
8394
}
8495

8596
/**
@@ -122,7 +133,7 @@ public <T extends Throwable> T intercept(final Class<T> exceptionType,
122133
@Override
123134
public <T extends Throwable> T intercept(final Class<T> exceptionType,
124135
final String messageRegex, final RunnableWithException throwing)
125-
throws Exception {
136+
throws Exception {
126137
return Intercept.intercept(exceptionType, messageRegex, throwing);
127138
}
128139

src/main/java/de/tobiasroeser/lambdatest/junit/FreeSpecRunner.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ protected void runChild(final LambdaTestCase testCase, final RunNotifier runNoti
101101
}
102102
out.println(ansi.fg(Color.GREEN) + "-- SUCCESS " + testName + ansi.reset());
103103
} catch (final AssumptionViolatedException e) {
104-
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + " (pending)" + ansi.reset());
104+
if (FreeSpec.PENDING_DEFAULT_MSG.equals(e.getMessage())) {
105+
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + " (pending)" + ansi.reset());
106+
} else {
107+
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + ": " + e.getMessage() + ansi.reset());
108+
}
105109
runNotifier.fireTestAssumptionFailed(new Failure(description, e));
106110
} catch (final Throwable e) {
107111
try {

src/main/java/de/tobiasroeser/lambdatest/testng/FreeSpec.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
*/
4646
public class FreeSpec implements LambdaTest {
4747

48+
private static final String PENDING_DEFAULT_MSG = "Pending";
49+
4850
private final List<LambdaTestCase> testCases = new LinkedList<>();
4951
private volatile boolean testNeverRun = true;
5052
private boolean runInParallel = false;
@@ -96,7 +98,17 @@ public void test(final String name, final RunnableWithException testCase) {
9698
*/
9799
@Override
98100
public void pending() {
99-
throw new SkipException("Pending");
101+
throw new SkipException(PENDING_DEFAULT_MSG);
102+
}
103+
104+
/**
105+
* Marks the test as pending and uses the given <code>reason</code> as
106+
* message. Instructions after <code>pending()</code> will not be executed
107+
* and TestNG marks the test as skipped.
108+
*/
109+
@Override
110+
public void pending(final String reason) {
111+
throw new SkipException("Pending: " + reason);
100112
}
101113

102114
/**
@@ -176,7 +188,8 @@ private void runTestCase(final LambdaTestCase testCase) throws Throwable {
176188
if (uncatchedTestError != null && delayedTestError != null) {
177189
throw new AssertionError(
178190
"An error occured (see root cause) after some expectations failed. Failed Expectations:\n"
179-
+ delayedTestError.getMessage(), uncatchedTestError);
191+
+ delayedTestError.getMessage(),
192+
uncatchedTestError);
180193
} else if (uncatchedTestError != null) {
181194
// if this was a SkipException, we still detect it, else some
182195
// other errors occurred before
@@ -186,7 +199,11 @@ private void runTestCase(final LambdaTestCase testCase) throws Throwable {
186199
}
187200
out.println(ansi.fg(Color.GREEN) + "-- SUCCESS " + testName + ansi.reset());
188201
} catch (final SkipException e) {
189-
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + " (pending)" + ansi.reset());
202+
if (PENDING_DEFAULT_MSG.equals(e.getMessage())) {
203+
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + " (pending)" + ansi.reset());
204+
} else {
205+
out.println(ansi.fg(Color.YELLOW) + "-- SKIPPED " + testName + ": " + e.getMessage() + ansi.reset());
206+
}
190207
throw e;
191208
} catch (final Throwable e) {
192209
try {

src/test/java/de/tobiasroeser/lambdatest/junit/RuntimeTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ public SimplePendingTest() {
3434
}
3535
}
3636

37+
public static class SimplePendingWithReasonTest extends FreeSpec {
38+
public SimplePendingWithReasonTest() {
39+
test("should be pending with reason", () -> {
40+
pending("With Reason");
41+
Assert.fail("should not be reached");
42+
});
43+
}
44+
}
45+
3746
@Test(groups = { "junit" })
3847
public void testSuccess() {
3948
final Result result = JUnitCore.runClasses(SimpleSuccessTest.class);
@@ -58,4 +67,12 @@ public void testPending() {
5867
assertEquals(result.getIgnoreCount(), 0);
5968
}
6069

70+
@Test(groups = { "junit" })
71+
public void testPendingWithReason() {
72+
final Result result = JUnitCore.runClasses(SimplePendingWithReasonTest.class);
73+
assertEquals(result.getRunCount(), 1);
74+
assertEquals(result.getFailureCount(), 0);
75+
assertEquals(result.getIgnoreCount(), 0);
76+
}
77+
6178
}

src/test/java/de/tobiasroeser/lambdatest/testng/RuntimeTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public SimplePendingTest() {
4747
}
4848
}
4949

50+
public static class SimplePendingWithReasonTest extends FreeSpec {
51+
public SimplePendingWithReasonTest() {
52+
if (runInnerTests) {
53+
test("should be pending with reason", () -> {
54+
pending("The Reason");
55+
Assert.fail("should not be reached");
56+
});
57+
}
58+
}
59+
}
60+
5061
public static class SimpleSuccessTest extends FreeSpec {
5162
public SimpleSuccessTest() {
5263
if (runInnerTests) {
@@ -140,4 +151,14 @@ public void testPendingInSubProcess() throws Exception {
140151
});
141152
}
142153

154+
@Test(groups = { "testng" }, dependsOnGroups = { "tempfile" })
155+
public void testPendingWithReasonInSubProcess() throws Exception {
156+
testInJvm(SimplePendingWithReasonTest.class.getName(), result -> {
157+
assertNotEquals(result.exitCode, 0);
158+
final Optional<String> line = Util.find(result.output, l -> l.startsWith("Total tests run"));
159+
assertTrue(line.isDefined());
160+
assertEquals(line.get(), "Total tests run: 1, Failures: 0, Skips: 1");
161+
});
162+
}
163+
143164
}

0 commit comments

Comments
 (0)