开发者

Customizing jUnit AssertFailure Error messages - combined with slf4j logging

I am new to jUnit and Selenium. Currently, we are doing a POC in our project for evaluating the tools' suitability.

One of the key requirements for us would be use friendly messages in case of assertion failures and verify Failures. The objective is to have those analyzed by manual testers by keeping those user intuitive.

I am trying out S开发者_如何学PythonLF4J for logging INFO messages for user friendly messages.

The challenge I am facing is that - when assertions fail, how to pass a custom message instead of jUnit default message?

For example, I want to get rid of the following default assertion failure message

expected "Health A-Z" to match glob "Health A-ZZ" (had transformed the glob into regexp "Health A-ZZ"

and frame it as

The title of the Menu Item doesn't match the expected value "Health A-ZZ". The actual value seen is "Health A-Z"

Question1 - Is there a way I override the default message with what I want? Question2 - Can I pass variable parameters to the message and make it a dynamic message?

Sample Code:

try {
    assertEquals("Health A-ZZ", selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
    logger.info("SUCCESS: Menu Item {} title verification Passed. EXPECTED : A-Z and Actual: {}",mainCounter, selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
}

catch (AssertionError e) {
    logger.error("FAILURE: Menu Item {} title verification Failed. EXPECTED : A-ZZ and Actual: {}",mainCounter, selenium.getText("//div[@id='topnav']/ul/li[2]/a"));
}

I get the assertFailure message printed out twice.. One with the error message in the catch block above and the other, the default junit assertfailre message like the one I mentioned above I wanted to get rid of.

Thanks in Advance.

Regards, Prakash


You can use the Junit assert function. assertEquals(message, expected, actual)

This will print the message that has been given in the message String in case of failure. You can also pass String variable to the "message".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜