I've been persuaded to have a bash at TestNG as a replacement for JUnit and am struggling to nicely reproduce some common soft assertion functionality I had managed in JUnit to apply automatically to all of my test methods.
I have a base test class that all my other test classes extend. This lets me define a single setup()@beforeMethod to set up a selenium driver instance (in the base class) for all of the child test classes to use.
Similarly, there's a teardown()@afterMethod that tears down the selenium driver.
I also want all of my tests to be able to easily use soft asserts. (
http://beust.com/weblog/2012/07/29/reinventing-assertions/)
The easy way to do this seems to be to create a SoftAssert instance at the same time as I set up the Selenium Driver in @beforeMethod, and then call assertAll() in the @afterMethod.
I found the configfailurepolicy="continue" option to make all tests run even if an @afterMethod 'fails', and it kind of works, however the test method itself isn't marked as failed, just the teardown()@afterMethod. This makes reporting not as clean as I'd like. I want to see the names of the tests marked as failed, not the name of the teardown() method. From the various comments I've seen, @afterMethod is designed to be 'not really part of the test', but I want it to be! I want a failure there to fail the test it is associated with (as it does in Junit).
I've seen some stuff about @Verify for mock objects, which looks like it could possibly do what I'm looking for, but that means all my test methods need to specify @Verify in addition to @Test, and if another tester forgets the @Verify, then their tests could accidentally pass as the soft assertion will never be checked. (
http://beust.com/weblog/2010/03/23/better-mock-testing-with-testng/)
The descriptions of using softAssertions seem to require that each
individual test call softAssert.assertAll() at the end of every method,
which seems like boilerplate that could easily be abstracted away using similar functionality to @beforeMethod.
Can anyone think of a good way of having the @afterMethod (or something similar) common functionality for all of my tests which can make the test fail, rather than being reported as the @afterMethod method failing?
Hope someone can help.
Martin.