Bind a method with time-out event in TestNG
In a TestNG project, I added a time-out in suite tag in xml file.
<suite name="Acceptance test suite" verbose="1" time-out="5400000" parallel="instances" thread-count="15">
This timeout is working fine.
I want to bind a method that will execute whenever an individual test will timeout. Actually, I want to log that this specific test is terminated by TestNG
framework and couple of other tasks by using the information of test that just terminated.
I have checked the Annotation @AfterTest
but it execute when all tests are completed. I want to log at individual test level.
How can I achieve it?
testng
add a comment |
In a TestNG project, I added a time-out in suite tag in xml file.
<suite name="Acceptance test suite" verbose="1" time-out="5400000" parallel="instances" thread-count="15">
This timeout is working fine.
I want to bind a method that will execute whenever an individual test will timeout. Actually, I want to log that this specific test is terminated by TestNG
framework and couple of other tasks by using the information of test that just terminated.
I have checked the Annotation @AfterTest
but it execute when all tests are completed. I want to log at individual test level.
How can I achieve it?
testng
add a comment |
In a TestNG project, I added a time-out in suite tag in xml file.
<suite name="Acceptance test suite" verbose="1" time-out="5400000" parallel="instances" thread-count="15">
This timeout is working fine.
I want to bind a method that will execute whenever an individual test will timeout. Actually, I want to log that this specific test is terminated by TestNG
framework and couple of other tasks by using the information of test that just terminated.
I have checked the Annotation @AfterTest
but it execute when all tests are completed. I want to log at individual test level.
How can I achieve it?
testng
In a TestNG project, I added a time-out in suite tag in xml file.
<suite name="Acceptance test suite" verbose="1" time-out="5400000" parallel="instances" thread-count="15">
This timeout is working fine.
I want to bind a method that will execute whenever an individual test will timeout. Actually, I want to log that this specific test is terminated by TestNG
framework and couple of other tasks by using the information of test that just terminated.
I have checked the Annotation @AfterTest
but it execute when all tests are completed. I want to log at individual test level.
How can I achieve it?
testng
testng
edited Nov 14 '18 at 6:31
Al Imran
627416
627416
asked Nov 13 '18 at 12:05
Waqar HaiderWaqar Haider
297
297
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You should be able to do it with TestNG as of today. You just need to extract out the Throwable
object from the ITestResult
of a particular test method, check if it is a timeout exception and if yes, log it separately.
As of today there's no straight forward way of doing this via a TestNG listener. I have logged a new issue for tracking this (https://github.com/cbeust/testng/issues/1952) I will try and get to fixing this at the earliest and have it available in the next upcoming major release of TestNG. Please feel free to watch this issue for updates.
Here's a sample that shows how to do it.
import com.rationaleemotions.stackoverflow.qn53280681.TimeoutListener;
import java.util.concurrent.TimeUnit;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.internal.thread.ThreadTimeoutException;
@Listeners(TimeoutListener.class)
public class qn53280681 {
@Test(timeOut = 500)
public void timedMethod() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}
public static class TimeoutListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Throwable t = result.getThrowable();
if (t instanceof ThreadTimeoutException) {
System.err.println("The test timedout and caused a failure");
}
}
}
}
Here's the output
The test timedout and caused a failure
org.testng.internal.thread.ThreadTimeoutException: Method com.rationaleemotions.stackoverflow.qn53280681.timedMethod() didn't finish within the time-out 500
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1150)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280681%2fbind-a-method-with-time-out-event-in-testng%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should be able to do it with TestNG as of today. You just need to extract out the Throwable
object from the ITestResult
of a particular test method, check if it is a timeout exception and if yes, log it separately.
As of today there's no straight forward way of doing this via a TestNG listener. I have logged a new issue for tracking this (https://github.com/cbeust/testng/issues/1952) I will try and get to fixing this at the earliest and have it available in the next upcoming major release of TestNG. Please feel free to watch this issue for updates.
Here's a sample that shows how to do it.
import com.rationaleemotions.stackoverflow.qn53280681.TimeoutListener;
import java.util.concurrent.TimeUnit;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.internal.thread.ThreadTimeoutException;
@Listeners(TimeoutListener.class)
public class qn53280681 {
@Test(timeOut = 500)
public void timedMethod() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}
public static class TimeoutListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Throwable t = result.getThrowable();
if (t instanceof ThreadTimeoutException) {
System.err.println("The test timedout and caused a failure");
}
}
}
}
Here's the output
The test timedout and caused a failure
org.testng.internal.thread.ThreadTimeoutException: Method com.rationaleemotions.stackoverflow.qn53280681.timedMethod() didn't finish within the time-out 500
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1150)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
add a comment |
You should be able to do it with TestNG as of today. You just need to extract out the Throwable
object from the ITestResult
of a particular test method, check if it is a timeout exception and if yes, log it separately.
As of today there's no straight forward way of doing this via a TestNG listener. I have logged a new issue for tracking this (https://github.com/cbeust/testng/issues/1952) I will try and get to fixing this at the earliest and have it available in the next upcoming major release of TestNG. Please feel free to watch this issue for updates.
Here's a sample that shows how to do it.
import com.rationaleemotions.stackoverflow.qn53280681.TimeoutListener;
import java.util.concurrent.TimeUnit;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.internal.thread.ThreadTimeoutException;
@Listeners(TimeoutListener.class)
public class qn53280681 {
@Test(timeOut = 500)
public void timedMethod() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}
public static class TimeoutListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Throwable t = result.getThrowable();
if (t instanceof ThreadTimeoutException) {
System.err.println("The test timedout and caused a failure");
}
}
}
}
Here's the output
The test timedout and caused a failure
org.testng.internal.thread.ThreadTimeoutException: Method com.rationaleemotions.stackoverflow.qn53280681.timedMethod() didn't finish within the time-out 500
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1150)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
add a comment |
You should be able to do it with TestNG as of today. You just need to extract out the Throwable
object from the ITestResult
of a particular test method, check if it is a timeout exception and if yes, log it separately.
As of today there's no straight forward way of doing this via a TestNG listener. I have logged a new issue for tracking this (https://github.com/cbeust/testng/issues/1952) I will try and get to fixing this at the earliest and have it available in the next upcoming major release of TestNG. Please feel free to watch this issue for updates.
Here's a sample that shows how to do it.
import com.rationaleemotions.stackoverflow.qn53280681.TimeoutListener;
import java.util.concurrent.TimeUnit;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.internal.thread.ThreadTimeoutException;
@Listeners(TimeoutListener.class)
public class qn53280681 {
@Test(timeOut = 500)
public void timedMethod() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}
public static class TimeoutListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Throwable t = result.getThrowable();
if (t instanceof ThreadTimeoutException) {
System.err.println("The test timedout and caused a failure");
}
}
}
}
Here's the output
The test timedout and caused a failure
org.testng.internal.thread.ThreadTimeoutException: Method com.rationaleemotions.stackoverflow.qn53280681.timedMethod() didn't finish within the time-out 500
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1150)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
You should be able to do it with TestNG as of today. You just need to extract out the Throwable
object from the ITestResult
of a particular test method, check if it is a timeout exception and if yes, log it separately.
As of today there's no straight forward way of doing this via a TestNG listener. I have logged a new issue for tracking this (https://github.com/cbeust/testng/issues/1952) I will try and get to fixing this at the earliest and have it available in the next upcoming major release of TestNG. Please feel free to watch this issue for updates.
Here's a sample that shows how to do it.
import com.rationaleemotions.stackoverflow.qn53280681.TimeoutListener;
import java.util.concurrent.TimeUnit;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.internal.thread.ThreadTimeoutException;
@Listeners(TimeoutListener.class)
public class qn53280681 {
@Test(timeOut = 500)
public void timedMethod() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}
public static class TimeoutListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Throwable t = result.getThrowable();
if (t instanceof ThreadTimeoutException) {
System.err.println("The test timedout and caused a failure");
}
}
}
}
Here's the output
The test timedout and caused a failure
org.testng.internal.thread.ThreadTimeoutException: Method com.rationaleemotions.stackoverflow.qn53280681.timedMethod() didn't finish within the time-out 500
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1150)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
answered Nov 14 '18 at 2:48
Krishnan MahadevanKrishnan Mahadevan
8,35831541
8,35831541
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280681%2fbind-a-method-with-time-out-event-in-testng%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown