1

I need to write the testcase Pass/Fail status in an Excel report. How do I catch the result in a simple way? In Ant XML/HTML reports status (Success/Failed) is displayed so I believe there must be some way to catch this..may be in the @After Class.
Can anyone help me out with this?

I am using Selenium Webdriver with JUnit4 and using Apache POI(oh its screwing my mind too!) for excel handling. Let me know in case you need more info.

Thanks :)

P.S: As I am asking a lot of questions it would be great if someone can change or suggest me changes to make these questions and threads helpful for others.

3
  • Are you using any Assert conditions to validate your tests? Post some code so that the community can suggest any changes to help you.Don't post the entire code just the relevant code.For this question post the code by which you determine if your test failed or passed.
    – Hari Reddy
    Jul 24, 2012 at 16:54
  • @Hari: Well they are simple Selenium JUnit codes like driver.get(url); send keys, etc. Yes I am using assertions in general but I think it will return only true/false. The JUnit Report I was talking about has 3 values - Success/Failed/Error. I would like to catch all the three for my excel report. Is there any way to do that?? Jul 25, 2012 at 5:03
  • Something like this - junit.org/junit/javadoc/3.8.1/junit/framework/TestCase.html where the result can be collected. But I am not able to use it properly. Jul 25, 2012 at 5:15

1 Answer 1

3

I got your problem and this is what I have do in all my test cases now -

In your test case if you want to check if 2 Strings are equal -

You might be using this code -

Assert.assertTrue(value1.equals(value2));

And if these values are not equal an AssertionError is generated.

Instead you can change your code like this -

String testCaseStatus = "";
if(value1.equals(value2)){
   testCaseStatus = "success";
}
else{
   testCaseStatus = "fail"; 
 }

Now you store this result in your excel sheet by passing the testCaseStatus to your code which adds a line to your excel which you have implemented using Apache POI. You can also handle the conditions of "error" if you implement a try catch block.

For example you can catch some exception and add the status as error to your excel sheet.

Edited part of answer -

I just figured out on how to use the TestResult class - This is some sample code I'm posting -

This is the test case class called ExampleTest -

public class ExampleTest implements junit.framework.Test {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test(TestResult result) {

    try{

        Assert.assertEquals("hari", "");

    }catch(AssertionFailedError e){

        result.addFailure(this, e);         
    }catch(AssertionError e){
        result.addError(this, e);
    }


}

@Override
public int countTestCases() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void run(TestResult result) {

    test(result);
}

}

I call the above test case from this code -

public class Test {

public static void main(String[] args) {

    TestResult result = new TestResult();

    TestSuite suite = new TestSuite();

    suite.addTest(new ExampleTest());

    suite.run(result);

    System.out.println(result.errorCount());

}

}

You can call many test cases by just adding them to this suite and then get the entire result using the TestResult class failures() and errors() methods.

You can read more on this from here.

1
  • Thanks! Well I am using something of this sort now. But its a bit painful as my testcases have a lot of this assertTrue situation and I have to handle each of them and calculate the final result. And as I am using a framework written in JUnit/Selenium it will be very tough to implement. And on top of that I would like to catch the Error status too. I need 3 status Pass/Fail/Error as shown in junitreport. There is a TestResult class in JUnit which stores this data. Actually I want to use it for this. Any idea how to use it? The link to javadoc is present in my comments in the question. Jul 25, 2012 at 12:20

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.