Last active
January 11, 2016 12:22
-
-
Save dzieciou/1addcc19e0d465afa3e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.testing.listeners; | |
import org.testng.IInvokedMethod; | |
import org.testng.IInvokedMethodListener; | |
import org.testng.ITestResult; | |
import java.util.Optional; | |
public class MyListener implements IInvokedMethodListener { | |
public static final String KNOWN_BUGS_FLAG = "knownBugs"; | |
public enum Strategy { | |
SKIP("skip"), FAIL("fail"), PASS("pass"), EXECUTE("execute"); | |
private final String value; | |
Strategy(String value) { | |
this.value = value; | |
} | |
static Strategy fromValue(String value) { | |
for (Strategy strategy : values()) { | |
if (strategy.value.equals(value)) { | |
return strategy; | |
} | |
} | |
return null; | |
} | |
} | |
@Override | |
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { | |
Optional<Issue> knownBug = checkKnownBug(method, testResult); | |
if (!knownBug.isPresent()) { | |
return; | |
} | |
Strategy knownBugsStrategy = getKnownBugsStrategy(); | |
switch (knownBugsStrategy) { | |
case EXECUTE: // Do not interrupt, just execute the test | |
return; | |
default: | |
throw new IllegalArgumentException("Uknown strategy: " + knownBugsStrategy.toString()); | |
} | |
} | |
private Strategy getKnownBugsStrategy() { | |
return readKnownBugsStrategy().orElse(Strategy.EXECUTE); | |
} | |
private static Optional<Issue> checkKnownBug(IInvokedMethod method, ITestResult testResult) { | |
if (!method.isTestMethod()) { | |
return Optional.empty(); | |
} | |
Optional<Issue> issue = getIssueAnnotation(testResult); | |
return issue.map(i -> i.type() == Issue.Type.BUG ? i : null); | |
} | |
private static Optional<Strategy> readKnownBugsStrategy() { | |
return Optional.ofNullable(Strategy.fromValue(System.getProperty(KNOWN_BUGS_FLAG))); | |
} | |
private static Optional<Issue> getIssueAnnotation(ITestResult testResult) { | |
return Optional.ofNullable(testResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Issue.class)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.testing.listeners; | |
import org.testng.TestListenerAdapter; | |
import org.testng.TestNG; | |
import org.testng.annotations.Test; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.equalTo; | |
import static org.testng.Assert.fail; | |
public class MyListenerTest { | |
@Test | |
public void testListener() { | |
System.setProperty("knownBugs", "execute"); | |
TestListenerAdapter tla = new TestListenerAdapter(); | |
TestNG testng = new TestNG(); | |
testng.setTestClasses(new Class[]{AlwaysFailingTest.class}); | |
testng.addListener(tla); | |
testng.addListener(new KnownBugsHandler()); | |
testng.run(); | |
assertThat(tla.getSkippedTests().size(), equalTo(0)); | |
assertThat(tla.getFailedTests().size(), equalTo(1)); | |
assertThat(tla.getPassedTests().size(), equalTo(0)); | |
} | |
public static class AlwaysFailingTest { | |
@Test | |
@Issue("BUG-509") | |
public void shouldAlwaysFail() { | |
fail("Failing as always"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment