-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated Status in afterInvocation() not considered #1611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
import test.SimpleBaseTest; | ||
import test.retryAnalyzer.github1519.MyListener; | ||
import test.retryAnalyzer.github1519.TestClassSample; | ||
import test.retryAnalyzer.github1600.Github1600Listener; | ||
import test.retryAnalyzer.github1600.Github1600TestSample; | ||
|
||
public class RetryAnalyzerTest extends SimpleBaseTest { | ||
@Test | ||
|
@@ -41,4 +43,16 @@ public void testIfRetryIsInvokedBeforeListener() { | |
tng.run(); | ||
assertThat(TestClassSample.messages).containsExactly("afterInvocation", "retry", "afterInvocation"); | ||
} | ||
|
||
@Test(description = "GITHUB-1600") | ||
public void testIfRetryIsInvokedBeforeListenerButHasToConsiderFailures() { | ||
TestNG tng = create(Github1600TestSample.class); | ||
Github1600Listener listener = new Github1600Listener(); | ||
TestListenerAdapter tla = new TestListenerAdapter(); | ||
tng.addListener((ITestNGListener) tla); | ||
tng.addListener((ITestNGListener) listener); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just thinking a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its use would be limited to just us for writing our unit tests, and to those that use the TestNG APIs directly (perhaps the IDE plugins and the build tool plugins). I dont see much of a use for this in other places. But yes, I agree, it would be simple change. |
||
tng.run(); | ||
assertThat(tla.getFailedTests()).hasSize(1); | ||
assertThat(tla.getSkippedTests()).hasSize(1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package test.retryAnalyzer.github1600; | ||
|
||
import org.testng.IRetryAnalyzer; | ||
import org.testng.ITestResult; | ||
|
||
public class Github1600Analyzer implements IRetryAnalyzer { | ||
|
||
static final String RETRY = "RETRY"; | ||
public static final String NO = "NO"; | ||
static final String YES = "YES"; | ||
private static int retryCount = 0; | ||
private static final int MAX_RETRY_COUNT = 10; | ||
|
||
@Override | ||
public boolean retry(ITestResult iTestResult) { | ||
String attribute = (String) iTestResult.getAttribute(RETRY); | ||
if (NO.equalsIgnoreCase(attribute)) { | ||
return false; | ||
} else if (YES.equalsIgnoreCase(attribute) || retryCount < MAX_RETRY_COUNT) { | ||
retryCount++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test.retryAnalyzer.github1600; | ||
|
||
import org.testng.IAnnotationTransformer; | ||
import org.testng.IInvokedMethod; | ||
import org.testng.IInvokedMethodListener; | ||
import org.testng.IRetryAnalyzer; | ||
import org.testng.ITestResult; | ||
import org.testng.annotations.ITestAnnotation; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
public class Github1600Listener implements IInvokedMethodListener, IAnnotationTransformer { | ||
@Override | ||
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { | ||
if (iInvokedMethod.isTestMethod()) { | ||
String attribute = Github1600Analyzer.NO; | ||
if (iTestResult.getStatus() == ITestResult.SUCCESS) { | ||
iTestResult.setStatus(ITestResult.FAILURE); | ||
attribute = Github1600Analyzer.YES; | ||
} | ||
iTestResult.setAttribute(Github1600Analyzer.RETRY, attribute); | ||
} | ||
} | ||
|
||
@Override | ||
public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) { | ||
IRetryAnalyzer retry = iTestAnnotation.getRetryAnalyzer(); | ||
if (retry == null) | ||
iTestAnnotation.setRetryAnalyzer(Github1600Analyzer.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.retryAnalyzer.github1600; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class Github1600TestSample { | ||
private static int a = 2; | ||
|
||
@Test | ||
public void test1() { | ||
Assert.assertEquals(a, 2); | ||
a++; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand well. What are the value of
holder.status
andtestResult.getStatus()
before and after handleInvocationResults, and before/after your fix?Maybe
testResult.getStatus() == holder.status
can replace wasResultUnaltered?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No that wouldn't work.
holder.status
represents the computed status by us, taking into account theexpectedExceptions
attribute of an@Test
annotation. So for an e.g., if an@Test
method threw aorg.testng.SkipException
then its status would beFAILURE
(because it threw an exception) but our computed status forholder.status
would beSKIP
(since we have a special meaning forSkipException
).So if we merely stuck to evaluating
testResult.getStatus() == holder.status
we end up usingtestResult.getStatus()
which is incorrect.The
testResult.getStatus()
value should be considered only if it was altered by the user definedIInvokedMethodListener
implementations (which is what #1600 is asking for).In all other cases, we should only consider
holder.status