From 50c08c09e27620df3304b1910b8955d533446459 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sat, 16 Jan 2021 13:38:37 +0100 Subject: [PATCH] Add more test cases --- .../github1632/AfterListenerSkipSample.java | 29 +++++++++ ...ple.java => BeforeListenerSkipSample.java} | 8 +-- .../java/test/skip/github1632/IssueTest.java | 20 ++++-- .../NoConfigAfterListenerSample.java | 24 +++++++ ...java => NoConfigBeforeListenerSample.java} | 9 +-- .../java/test_result/AfterListenerSample.java | 57 +++++++++++++++++ .../test_result/BeforeListenerSample.java | 54 ++++++++++++++++ .../{github1197 => }/GitHub1197Sample.java | 2 +- .../java/test_result/ResultStatusTest.java | 62 +++++++++++++++++++ .../github1197/GitHub1197Test.java | 27 -------- src/test/resources/testng.xml | 2 +- 11 files changed, 248 insertions(+), 46 deletions(-) create mode 100644 src/test/java/test/skip/github1632/AfterListenerSkipSample.java rename src/test/java/test/skip/github1632/{SpringSample.java => BeforeListenerSkipSample.java} (79%) create mode 100644 src/test/java/test/skip/github1632/NoConfigAfterListenerSample.java rename src/test/java/test/skip/github1632/{NoConfigSpringSample.java => NoConfigBeforeListenerSample.java} (72%) create mode 100644 src/test/java/test_result/AfterListenerSample.java create mode 100644 src/test/java/test_result/BeforeListenerSample.java rename src/test/java/test_result/{github1197 => }/GitHub1197Sample.java (96%) create mode 100644 src/test/java/test_result/ResultStatusTest.java delete mode 100644 src/test/java/test_result/github1197/GitHub1197Test.java diff --git a/src/test/java/test/skip/github1632/AfterListenerSkipSample.java b/src/test/java/test/skip/github1632/AfterListenerSkipSample.java new file mode 100644 index 0000000000..f30809083c --- /dev/null +++ b/src/test/java/test/skip/github1632/AfterListenerSkipSample.java @@ -0,0 +1,29 @@ +package test.skip.github1632; + +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestResult; +import org.testng.SkipException; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(AfterListenerSkipSample.MySkipTestListener.class) +public class AfterListenerSkipSample { + + @BeforeMethod + void beforeMethod() { + } + + @Test + void shouldNotBeExecuted() { + } + + public static class MySkipTestListener implements IInvokedMethodListener { + + @Override + public void afterInvocation(IInvokedMethod method, ITestResult testResult) { + throw new SkipException("skip"); + } + } +} diff --git a/src/test/java/test/skip/github1632/SpringSample.java b/src/test/java/test/skip/github1632/BeforeListenerSkipSample.java similarity index 79% rename from src/test/java/test/skip/github1632/SpringSample.java rename to src/test/java/test/skip/github1632/BeforeListenerSkipSample.java index fde555e0c1..9aa3149550 100644 --- a/src/test/java/test/skip/github1632/SpringSample.java +++ b/src/test/java/test/skip/github1632/BeforeListenerSkipSample.java @@ -9,8 +9,8 @@ import org.testng.annotations.Test; // Sample from https://github.com/spring-projects/spring-framework/issues/26387#issuecomment-760326643 -@Listeners(SpringSample.MySkipTestListener.class) -public class SpringSample { +@Listeners(BeforeListenerSkipSample.MySkipTestListener.class) +public class BeforeListenerSkipSample { @BeforeMethod void beforeMethod() { @@ -26,9 +26,5 @@ public static class MySkipTestListener implements IInvokedMethodListener { public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { throw new SkipException("skip"); } - - @Override - public void afterInvocation(IInvokedMethod method, ITestResult testResult) { - } } } diff --git a/src/test/java/test/skip/github1632/IssueTest.java b/src/test/java/test/skip/github1632/IssueTest.java index 1b415fd869..ea52142ab9 100644 --- a/src/test/java/test/skip/github1632/IssueTest.java +++ b/src/test/java/test/skip/github1632/IssueTest.java @@ -23,14 +23,26 @@ public void runTest() { } @Test - public void runSpringSample() { - InvokedMethodNameListener listener = run(SpringSample.class); + public void runBeforeListenerSkipSample() { + InvokedMethodNameListener listener = run(BeforeListenerSkipSample.class); assertThat(listener.getResult("shouldNotBeExecuted").getStatus()).isEqualTo(ITestResult.SKIP); } @Test - public void runNoConfigSpringSample() { - InvokedMethodNameListener listener = run(NoConfigSpringSample.class); + public void runNoConfigBeforeListenerSample() { + InvokedMethodNameListener listener = run(NoConfigBeforeListenerSample.class); + assertThat(listener.getResult("shouldNotBeExecuted").getStatus()).isEqualTo(ITestResult.SKIP); + } + + @Test + public void runAfterListenerSkipSample() { + InvokedMethodNameListener listener = run(AfterListenerSkipSample.class); + assertThat(listener.getResult("shouldNotBeExecuted").getStatus()).isEqualTo(ITestResult.SKIP); + } + + @Test + public void runNoConfigAfterListenerSample() { + InvokedMethodNameListener listener = run(NoConfigAfterListenerSample.class); assertThat(listener.getResult("shouldNotBeExecuted").getStatus()).isEqualTo(ITestResult.SKIP); } } diff --git a/src/test/java/test/skip/github1632/NoConfigAfterListenerSample.java b/src/test/java/test/skip/github1632/NoConfigAfterListenerSample.java new file mode 100644 index 0000000000..51b9b16880 --- /dev/null +++ b/src/test/java/test/skip/github1632/NoConfigAfterListenerSample.java @@ -0,0 +1,24 @@ +package test.skip.github1632; + +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestResult; +import org.testng.SkipException; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(NoConfigAfterListenerSample.MySkipTestListener.class) +public class NoConfigAfterListenerSample { + + @Test + void shouldNotBeExecuted() { + } + + public static class MySkipTestListener implements IInvokedMethodListener { + + @Override + public void afterInvocation(IInvokedMethod method, ITestResult testResult) { + throw new SkipException("skip"); + } + } +} diff --git a/src/test/java/test/skip/github1632/NoConfigSpringSample.java b/src/test/java/test/skip/github1632/NoConfigBeforeListenerSample.java similarity index 72% rename from src/test/java/test/skip/github1632/NoConfigSpringSample.java rename to src/test/java/test/skip/github1632/NoConfigBeforeListenerSample.java index 321cf5bd75..828fd4866b 100644 --- a/src/test/java/test/skip/github1632/NoConfigSpringSample.java +++ b/src/test/java/test/skip/github1632/NoConfigBeforeListenerSample.java @@ -4,13 +4,12 @@ import org.testng.IInvokedMethodListener; import org.testng.ITestResult; import org.testng.SkipException; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Listeners; import org.testng.annotations.Test; // Sample from https://github.com/spring-projects/spring-framework/issues/26387#issuecomment-760326643 -@Listeners(NoConfigSpringSample.MySkipTestListener.class) -public class NoConfigSpringSample { +@Listeners(NoConfigBeforeListenerSample.MySkipTestListener.class) +public class NoConfigBeforeListenerSample { @Test void shouldNotBeExecuted() { @@ -22,9 +21,5 @@ public static class MySkipTestListener implements IInvokedMethodListener { public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) { throw new SkipException("skip"); } - - @Override - public void afterInvocation(IInvokedMethod method, ITestResult testResult) { - } } } diff --git a/src/test/java/test_result/AfterListenerSample.java b/src/test/java/test_result/AfterListenerSample.java new file mode 100644 index 0000000000..bc7a409056 --- /dev/null +++ b/src/test/java/test_result/AfterListenerSample.java @@ -0,0 +1,57 @@ +package test_result; + +import org.testng.Assert; +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestResult; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(AfterListenerSample.MySkipTestListener.class) +public class AfterListenerSample { + + @Test + public void failedTest() { + } + + @Test(expectedExceptions = IllegalStateException.class, enabled = false) + public void failedTest2() { + throw new IllegalStateException(); + } + + @Test + public void skippedTest() { + } + + @Test + public void succeedTest() { + Assert.fail(); + } + + @Test + public void succeedTest2() { + throw new IllegalStateException(); + } + + public static class MySkipTestListener implements IInvokedMethodListener { + + @Override + public void afterInvocation(IInvokedMethod method, ITestResult result) { + switch (method.getTestMethod().getMethodName()) { + case "failedTest": + case "failedTest2": + result.setStatus(ITestResult.FAILURE); + break; + case "skippedTest": + result.setStatus(ITestResult.SKIP); + break; + case "succeedTest": + case "succeedTest2": + result.setStatus(ITestResult.SUCCESS); + break; + default: + throw new RuntimeException("Unknown method name: " + method.getTestMethod().getMethodName()); + } + } + } +} diff --git a/src/test/java/test_result/BeforeListenerSample.java b/src/test/java/test_result/BeforeListenerSample.java new file mode 100644 index 0000000000..1f79df73c5 --- /dev/null +++ b/src/test/java/test_result/BeforeListenerSample.java @@ -0,0 +1,54 @@ +package test_result; + +import org.testng.*; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(BeforeListenerSample.MySkipTestListener.class) +public class BeforeListenerSample { + + @Test + public void failedTest() { + } + + @Test(expectedExceptions = IllegalStateException.class, enabled = false) + public void failedTest2() { + throw new IllegalStateException(); + } + + @Test + public void skippedTest() { + } + + @Test + public void succeedTest() { + Assert.fail(); + } + + @Test + public void succeedTest2() { + throw new IllegalStateException(); + } + + public static class MySkipTestListener implements IInvokedMethodListener { + + @Override + public void beforeInvocation(IInvokedMethod method, ITestResult result) { + switch (method.getTestMethod().getMethodName()) { + case "failedTest": + case "failedTest2": + result.setStatus(ITestResult.FAILURE); + break; + case "skippedTest": + result.setStatus(ITestResult.SKIP); + break; + case "succeedTest": + case "succeedTest2": + result.setStatus(ITestResult.SUCCESS); + break; + default: + throw new RuntimeException("Unknown method name: " + method.getTestMethod().getMethodName()); + } + } + } +} diff --git a/src/test/java/test_result/github1197/GitHub1197Sample.java b/src/test/java/test_result/GitHub1197Sample.java similarity index 96% rename from src/test/java/test_result/github1197/GitHub1197Sample.java rename to src/test/java/test_result/GitHub1197Sample.java index 1c8e4b8c27..5cc8c8b5e8 100644 --- a/src/test/java/test_result/github1197/GitHub1197Sample.java +++ b/src/test/java/test_result/GitHub1197Sample.java @@ -1,4 +1,4 @@ -package test_result.github1197; +package test_result; import org.testng.Assert; import org.testng.ITestResult; diff --git a/src/test/java/test_result/ResultStatusTest.java b/src/test/java/test_result/ResultStatusTest.java new file mode 100644 index 0000000000..6e23d0a2e7 --- /dev/null +++ b/src/test/java/test_result/ResultStatusTest.java @@ -0,0 +1,62 @@ +package test_result; + +import org.testng.TestNG; +import org.testng.annotations.Test; +import test.InvokedMethodNameListener; +import test.SimpleBaseTest; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ResultStatusTest extends SimpleBaseTest { + + @Test + public void testGitHub1197() { + TestNG tng = create(GitHub1197Sample.class); + + // A skipped method is not supposed to be run but, here, it's the goal of the feature + InvokedMethodNameListener listener = new InvokedMethodNameListener(true, true); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSucceedMethodNames()).containsExactly("succeedTest", "succeedTest2"); + assertThat(listener.getFailedBeforeInvocationMethodNames()).isEmpty(); + assertThat(listener.getFailedMethodNames()).containsExactly("failedTest"); // , "failedTest2"); + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getSkippedAfterInvocationMethodNames()).containsExactly("skippedTest"); + } + + @Test + public void testBeforeListener() { + TestNG tng = create(BeforeListenerSample.class); + + // A skipped method is not supposed to be run but, here, it's the goal of the feature + InvokedMethodNameListener listener = new InvokedMethodNameListener(true, true); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSucceedMethodNames()).containsExactly("succeedTest", "succeedTest2"); + assertThat(listener.getFailedBeforeInvocationMethodNames()).isEmpty(); + assertThat(listener.getFailedMethodNames()).containsExactly("failedTest"); // , "failedTest2"); + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getSkippedAfterInvocationMethodNames()).containsExactly("skippedTest"); + } + + @Test + public void testAfterListener() { + TestNG tng = create(AfterListenerSample.class); + + // A skipped method is not supposed to be run but, here, it's the goal of the feature + InvokedMethodNameListener listener = new InvokedMethodNameListener(true, true); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSucceedMethodNames()).containsExactly("succeedTest", "succeedTest2"); + assertThat(listener.getFailedBeforeInvocationMethodNames()).isEmpty(); + assertThat(listener.getFailedMethodNames()).containsExactly("failedTest"); // , "failedTest2"); + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getSkippedAfterInvocationMethodNames()).containsExactly("skippedTest"); + } +} diff --git a/src/test/java/test_result/github1197/GitHub1197Test.java b/src/test/java/test_result/github1197/GitHub1197Test.java deleted file mode 100644 index af33747bb3..0000000000 --- a/src/test/java/test_result/github1197/GitHub1197Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package test_result.github1197; - -import org.testng.ITestNGListener; -import org.testng.TestNG; -import org.testng.annotations.Test; -import test.InvokedMethodNameListener; -import test.SimpleBaseTest; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GitHub1197Test extends SimpleBaseTest { - - @Test - public void testGitHub1197() { - TestNG tng = create(GitHub1197Sample.class); - - // A skipped method is not supposed to be run but, here, it's the goal of the feature - InvokedMethodNameListener listener = new InvokedMethodNameListener(true, true); - tng.addListener((ITestNGListener) listener); - - tng.run(); - - assertThat(listener.getSucceedMethodNames()).containsExactly("succeedTest", "succeedTest2"); - assertThat(listener.getFailedMethodNames()).containsExactly("failedTest"); // , "failedTest2"); - assertThat(listener.getSkippedAfterInvocationMethodNames()).containsExactly("skippedTest"); - } -} diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index 6b7cb0388e..15057bfad4 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -189,7 +189,7 @@ - +