0

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 });
            testng.addListener(tla);
            testng.run();


}}  

What i tried is:

java -jar myjar.jar -cp  testng-failed.xml
  • What does it do now? – user246 Mar 25 '16 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 – sameer joshi Apr 1 '16 at 12:05
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).");
            retryCount++;
            return true;
        }
        return false;
    }

    public String getResultStatusName(int status) {
        String resultName = null;
        if(status==1)
            resultName = "SUCCESS";
        if(status==2)
            resultName = "FAILURE";
        if(status==3)
            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 {

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

        if (retry == null)  {
            testannotation.setRetryAnalyzer(Retry.class);
        }

    }
}

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

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

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.

0

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 – sameer joshi Mar 28 '16 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!!! – Mrunal Gosar Mar 28 '16 at 8:11
  • testng.setTestClasses(new Class[] { DemoProjectClass.class }); – sameer joshi Mar 28 '16 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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