How to include passed assertions in the Test NG test result report?



  • I use Selenium webdriver 2 with Test-NG. Test-NG gives a nice report when an assertion fails. The customer wishes to see the same report in the case where an assertion passes. Is it possible to do with the default Test-NG reporter?



  • TestNG knows nothing about assertions per se. It knows only that the test threw an exception or did not. All of the information that TestNG gets from the assertion comes from the exception thrown by the assertion failure. If an assertion passes, TestNG never knows about it.

    On the other hand, you can write your assertion methods that know how to log information, or to write information through TestNG's Reporter object. There may be other ways that an assertion can send information to TestNG, but each of them involves using some form of assertion that knows when and how to report the information.

    One possibility is to write your own assertThat() method, like the one that comes with the Hamcrest library. The assertThat() method takes two parameters, a subject and a matcher. The matcher knows how to describe itself. You could write your assertThat() so that when the match succeeds, it describes the subject and the matcher to TestNG somehow (e.g. through the Reporter).

    One possible implementation:

    package my.package.name.MyAssertion;
    
    public class MyAssertion {
    
        public static  boolean assertThat(T subject, Matcher matcher) {
            MatcherAssert.assertThat(subject, matcher); // Calls the Hamcrest asserThat
    
            // If we get here, the Hamcrest assertion passed. Log it to TestNG.
            // You'll have to figure out how to write this method.
            reportMatchToTestNG(subject, matcher);
        }
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 4
  • 2
  • 2