Getting Started with JUnit Reports with Eclipse and Ant…

So a lot of people have been asking me about how to create JUnit Reports with their automated test cases. I’m going to show you how to create these reports in the easiest way possible. You will not have to download anything, add any code, etc. You will only have to use generate the HTML report using Ant with Eclipse.

The first think you have to do is generate the Ant build. You can do this by:

1. Right click on the project and
select Export –> Ant Buildfiles (this will be under General) –> Next–> Select the project that contains your JUnit tests –> Finish.

The default JUnit output directory is appropriately named “junit”.

2. The next thing you have to do is resolve your dependencies by making sure that the junit.jar is added to
Ant’s “Global Entries.”. You can do this by.

Window->Preferences->Ant->Runtime->Global Entries and Add External JARs… Navigate to your “eclipse” directory (where eclipse is installed). It is under “plugins” directory and presently the junit.jar is in a directory named “org.junit_3.8.1”. If this is missing then you will see error messages since
junit is an optional Ant task.

* If you are using a Mac, Preferences are under the the Eclipse menu.

3) Finally, right-click on the Ant build file build.xml (this file is the one you created in Step 1) and Run As->Ant Build. This will display a list of targets. All of the launch configurations you have previously configured will have a corresponding target in your Ant build file. Select the desired target(s), and also select the “junitreport” target (very important). Check the “Target execution order” text area to make sure the junitreport is run last.

As you can see I’ve specified the order in which I want my cases to run (below)

JUnit Targets

Whenever you run a test case it will generate results and will be placed in the junit output directory. These results are formatted into an HTML report by the junitreport target and stored in the junit output directory.

As you can see below my automated Test Suite has completely run and you can see which tests failed and which passed (below). I see my cases for login, registration, and account-sign up are failing. Upon further investigation, these are failing because the web service is down on our staging server.

Pass or Fail

This is a fast and easy explanation of how to create JUnit HTML reports, but hopefully it helps get you off your feet. Next time around, we’ll have some more fun when I show you how to extend this output to get more information displayed in the report. For example to add an additional column which contains link to a screenshot taken by the test.

Happy Testing!

Advertisements
Leave a comment

19 Comments

  1. Yogesh Thanvi

     /  October 15, 2012

    I am trying to run build.xml but i am not bale to see any ant build option is there any wrong in setup in plugins junit file is available though also

    Reply
  2. al

     /  February 7, 2013

    thanks for the tutorial! I have one issue though, I do not see “junitreport’ on my targets list. how do I get this to display or how do i create this?

    thanks

    Reply
    • Hi Al,
      Sorry for the late reply. I’ve been on a bit of a hiatus but will be contributing more to this blog. To answer your question, sometimes I have the problem of it the target not showing up and then I have to add this manually to the build.xml file.

      Open your build.xml and add this code manually to this file, then try running your project as an Ant build and it should be in your list of targets.

      ….

      target name=”junitreport”
      junitreport todir=”${junit.output.dir}”
      fileset dir=”${junit.output.dir}”
      include name=”TEST-*.xml”/
      /fileset
      report format=”frames” todir=”${junit.output.dir}”/
      /junitreport>
      /target>
      /project>

      * each line of code starts with .

      Let me know if that works.

      Earl

      Reply
      • Anurag

         /  February 18, 2014

        Thanks for the Tutorial but It is not generating the report only Build Successful message is coming.

  3. Lana

     /  March 5, 2013

    Thank you very much. I’ve been searching for century!!!

    I’m waiting for the screenshot taken link. Did you write it already?

    Reply
    • Hi Lana,
      I’ve written a new blog about this here: https://earlwillis.wordpress.com/2013/08/23/taking-screenshots-with-webdriver/

      You can use the following code if you are using Selenium 1.0. The post I’ve written above is for Webdriver as Selenium has a few limitation regarding screenshots for Internet Explorer.

      Firefox OS/X: selenium.captureEntirePageScreenshot(“/users/[username]/[folder-name]/[screenshot name].png”,””);

      Internet Explorer Windows:
      selenium.captureEntirePageScreenshotToString(“C://[folder]//[screenshot-name].png”);

      Let me know how that turns out.

      Reply
  4. You can find some common pure green coffee extract in grocery stores or big
    box stores, or over the counter drug in a safe manner.

    Reply
  5. new@java

     /  September 27, 2013

    I have recorded some test scripts using selenium ide.. exported those test scripts to Eclipse. I am able to parameterize the test scripts, compile and run it. Works fine. The problem is when I try to create a junit test report, I get a Build succesful message but the values are empty under the test report. I have imported all the required junit jar files, etc..
    I am new to Java. So please don’t be harsh 🙂

    Reply
  6. Srini

     /  February 19, 2014

    Hi Earl,

    Could you please help me how to add a column to this existing report which contains link to screen shot. It would be helpful.

    Reply
    • The junitreport task uses XSLT to produce the report from the XML files generated by the junittask.

      You can customize the output by specifying your own XSLT using the styledir attribute of the nested report element:

      For customizing the the output, one option would be to make a copy of the default XSLT and modify that. Or you could look for an alternative XSLT which is more easy to customize for your purposes.

      For small changes, it might be easiest to just import the default XSLT and override whatever templates you need to customize. For example, to add a column for each test, you would need to override the template which produces the table header and the template which produces a table row.

      !– ADDED —
      -td-
      -a href=”link/to/newcolumn/for/test/{@name}”-newcolumnname-/a-
      <-td-

      Reply
  7. Stephen Jonathan

     /  February 24, 2014

    This helped a lot. Thanks!!!

    Reply
  8. Amit Jaiswal

     /  July 18, 2014

    Found very helpful, only thing is to generate report into html we need to modify our build.xml with this.

    Anyways Thanks a lot

    Reply
  9. Earlwillis for me HTML report is not generated please help me

    Reply
  10. mohit

     /  December 16, 2014

    Hi Good morning,
    Previously my entire process was working fine,But as Today i don;t know what was changed in my project environment set up i am facing following issues:-
    The Junit folder is not getting updated and showing the same data after every execution of the script.
    Please provide the solution for this.

    Reply
  1. Getting a list of all link URL’s on a page with Selenium.. « Test-Obsessed
  2. Generate JUnit report programatically | BlogoSfera

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: