3

I use TestNG for assertion, for example with this assertTrue:

Assert.assertTrue(boolean b, String s)

If the assert is failed, then the String s will be printed out, and if assertion is passed, then no message is printed. I want to print a message in case the assert is passed anyway, so I write some thing like this:

// verify if additional info is saved
// do something
    Assert.assertTrue(webDriver.findElement(By.name("CreatePostbox")).isDisplayed(), "Additional Information is not saved");
    System.out.println("Additional information is saved");
    ...

So basically, if the assert is failed, then the message will be printed out as intended and test is aborted. But when the assert is passed, then the message in println will be printed out.

My question: is it a clean way to print message when the assert is passed? or is there a better way?

  • Where do you want to print on console or testNG html report itself? – Vishal Aggarwal Mar 14 '18 at 6:55
  • I want to print on console, both when pass or fail (I don't fail message on console as well with assertTrue). But testNG html would be nice as well. – Ragnarsson Mar 14 '18 at 10:31
3

I don't know that there's necessarily a better or cleaner way than your example, but I can offer at least one alternative:

Boolean saved = webDriver.findElement(By.name("CreatePostbox")).isDisplayed();

System.OutPrintln("Additional information is " + (saved ? "" : "NOT ") + "saved.");
Assert.AssertTrue(saved);

There could be many variations on this theme, including assigning the result to a string and passing the string to both the console and the assertion, for example, depending on your needs.

  • Thank you, your idea is more or less like mine, but I like the way you structured it :) – Ragnarsson Mar 9 '18 at 13:05
0

I would suggest integrate a logging api(Apache log4j) to your tests. Use TestNG listener to log the events(pass/fail). This could be a clean and robust way of doing this. This will help you analyze the test results better.

  • Thanks. I used log4j maven dependency before, but it made the logfile really long and hard to read or maybe I used it wrong way. It could be helpful if you have an example or best practice how to integrate log4j and TestNG listener. I am interested. Thanks – Ragnarsson Mar 13 '18 at 16:36
  • Dont worry... log files are meant to be large. Reading log files is an art. log files should be treated as reference only. Just override the listener methods from TestNG: @AfterMethod public void after_method(ITestResult _result) throws IOException { if (_result.getStatus() == ITestResult.FAILURE){ } else if (_result.getStatus() == ITestResult.SUCCESS){ test.log(Status.PASS, "Test Passed") ; } else { test.log(Status.SKIP, "Test Skipped") ; test.skip(_result.getThrowable()) ; } } – Kshetra Mohan Prusty Mar 14 '18 at 5:33
0

Dont worry... log files are meant to be large. Reading log files is an art. log files should be treated as reference only.

@AfterMethod
public void after_method(ITestResult _result) throws IOException {

    if (_result.getStatus() == ITestResult.FAILURE){test.log(Status.FAIL, "Test Failed") ;
    }

    else if (_result.getStatus() == ITestResult.SUCCESS){
        test.log(Status.PASS, "Test Passed") ;
    }

    else {
        test.log(Status.SKIP, "Test Skipped") ;
        test.skip(_result.getThrowable()) ;
    }
}

Something like this

0

I will use testNG's Reporter API.

If I need to log messages I will make sure they appear in the generated HTML report,so I will use the class org.testng.Reporter:

Reporter.log("Additional information is saved");

Source: testNG documentation

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.