I created jar when I run on command prompt I am able to see result, what I want is "I need to run failed test cases if there is any"

 public class DemoProjectClass {
    public static void main(String[]args){
            TestListenerAdapter tla = new TestListenerAdapter();
            TestNG testng = new TestNG();
            testng.setTestClasses(new Class[] { DemoProjectClass.class });


What i tried is:

java -jar myjar.jar -cp  testng-failed.xml
  • What does it do now?
    – user246
    Mar 25, 2016 at 13:04
  • @user246 when i click on jar starts executing test classes and genrates test out put containing "testng-failed.xml " i want to use that using command prompt against jar file Apr 1, 2016 at 12:05

3 Answers 3


There may be many reasons for a Test case getting failed, may be due to element not found or time out exception or stale element exception etc. Normally in automation after executing scripts/tests, we will check for the results and if the test fails because of above reasons we will re-run then again.

Instead of that we can ask testNG to execute the failed test cases again for number of times and check for the updated results.

To achieve this we need to implement TestNG IRetryAnalyzer. Below is the simple code:

package com.pack.test;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class Retry implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 1;

// Below method returns 'true' if the test method has to be retried else 'false' 
//and it takes the 'Result' as parameter of the test method that just ran
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            System.out.println("Retrying test " + result.getName() + " with status "
                    + getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s).");
            return true;
        return false;

    public String getResultStatusName(int status) {
        String resultName = null;
            resultName = "SUCCESS";
            resultName = "FAILURE";
            resultName = "SKIP";
        return resultName;

Create an other class 'RetryListener' by implementing 'IAnnotationTransformer'. We need to setRetryAnalyzer for iTestAnnotation. In the example below, add the above class name.

package com.pack.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;

public class RetryListener implements IAnnotationTransformer {

    public void transform(ITestAnnotation testannotation, Class testClass,
            Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = testannotation.getRetryAnalyzer();

        if (retry == null)  {


After this, We need to add the Listener to testng.xml file. Below is syntax to add listener for RetryListener.

        <listener class-name="com.pack.test.RetryListener"/>

TestNG itself provides a way to re-test the failed test cases. The answer to your question is quite lengthy, But I have found the good details on this blog link.

Hope it helps you.


You are trying to run your tests programmatically so that's great..Once your tests are run testng will generate a failed.xml file with tests that have failed. You can then check if the failed.xml exists or not if yes then run that xml. Some details could be found here: how-do-i-run-a-custom-testng

  • i didn't used xml to run my test cases, i used .class file to run text cases,if failed.xml exists how to use it as if iam running from runnable jar file through command prompt Mar 28, 2016 at 4:57
  • can you add a sample project with relevant source code on github and i will fix it and show you how it can be done!!! Mar 28, 2016 at 8:11
  • testng.setTestClasses(new Class[] { DemoProjectClass.class }); Mar 28, 2016 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.