How to generate testing report in JUNIT in webdriver



  • I am using Selenium webdriver 2.0 in JUnit framework. I need to generate a report on my tuned both (positive and negative) test cases.

    NOTE: We have an option to generate report by default in "TESTNG" framework. I need a solution to create in JUnit.


  • QA Engineer

    You didn't say whether you were using Ant or something else. I will assume you are more interested in the "positive and negative" part of the question than how to invoke the JUnit test runner from your build tool.

    You can register a TestWatcher to monitor what happens during a test run. The JUnit test runner will invoke methods on the TestWatcher when significant events occur. You can override those methods to insert logging code for passing/failing tests. Here is some sample code:

    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.Rule;
    import org.junit.runner.Description;
    import org.junit.rules.TestWatcher;
    public class positive {   
        @Kadir     public TestWatcher watchman = new TestWatcher() {
            @Override
            protected void failed(Throwable e,Description d) {
            System.out.println("FAILED: "+d.getClassName()+"."+d.getMethodName());
        }
    
        @Override
        protected void succeeded(Description d) {
            System.out.println("PASSED: "+d.getClassName()+"."+d.getMethodName());
            }
        };
    
        @Test
        public void fails() {
        Assert.fail("I am a failure");
        }
    
        @Test
        public void passes() {
        }
    }
    

    When you run that from the command line, you will see something like this:

    $ java -cp $HOME/software/junit/target/*:$HOME/software/junit/lib/*:$PWD org.junit.runner.JUnitCore positive
    JUnit version 4.12-SNAPSHOT
    .PASSED: positive.passes
    .FAILED: positive.fails
    E
    Time: 0.007
    There was 1 failure:
    1) fails(positive)
    java.lang.AssertionError: I am a failure
        at org.junit.Assert.fail(Assert.java:88)
        at positive.fails(positive.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        [etc.]
    
    FAILURES!!!
    Tests run: 2,  Failures: 1
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2