Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
juherr authored and krmahadevan committed Jan 20, 2021
1 parent ac9d977 commit 50c08c0
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 46 deletions.
29 changes: 29 additions & 0 deletions src/test/java/test/skip/github1632/AfterListenerSkipSample.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
}
}
}
20 changes: 16 additions & 4 deletions src/test/java/test/skip/github1632/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
}
}
}
57 changes: 57 additions & 0 deletions src/test/java/test_result/AfterListenerSample.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
54 changes: 54 additions & 0 deletions src/test/java/test_result/BeforeListenerSample.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test_result.github1197;
package test_result;

import org.testng.Assert;
import org.testng.ITestResult;
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/test_result/ResultStatusTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
27 changes: 0 additions & 27 deletions src/test/java/test_result/github1197/GitHub1197Test.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<class name="test.skip.github1632.IssueTest"/>
<class name="test.listeners.issue2220.IssueTest"/>
<class name="test.listeners.issue2328.IssueTest"/>
<class name="test_result.github1197.GitHub1197Test"/>
<class name="test_result.ResultStatusTest"/>
<class name="test_result.issue1590.IssueTest"/>
<class name="test.listeners.github1319.TestResultInstanceCheckTest"/>
<class name="test.testng1396.ParallelByInstancesInterceptorTest"/>
Expand Down

0 comments on commit 50c08c0

Please sign in to comment.