I am setting up a test suite using Selenium with TestNG and I’m confused about which validation method to use. I noticed that when an Assert fails, my entire test stops immediately, but I've heard that Verify allows the test to continue. What are the specific use cases for each, and how do they impact the final test report? If I have a page with ten different elements to check, is it better to use one over the other to ensure I get a full picture of the page's status?
3 answers
The primary difference lies in how they handle failure. Assert (often called a "Hard Assert") is used when the test cannot proceed if the condition isn't met—for example, if a login fails, there is no point in checking the user's profile settings. When an Assert fails, an exception is thrown and the test method stops immediately. Verify (implemented in TestNG as SoftAssert) is used for non-critical checks. If a Verify fails, the error is logged, but the execution continues to the next line. This is ideal for "sanity checks" where you want to verify multiple elements on a page, like checking if five different labels are correct, without stopping the test at the first typo.
I tried using SoftAssert for my "Verify" logic, but my test still passed even though the logs showed errors. Am I missing a step to make the test actually fail at the end?
Think of Assert as a "Critical Blocker" and Verify as a "Bug Report Collector." I always use Assert for URL checks and Verify for UI text checks.
That’s a perfect analogy, Elena. Using Hard Asserts for everything leads to "brittle" tests where you have to run the suite ten times to find ten different bugs. Soft Asserts (Verify) give you all those bugs in a single run.
Yes, Kevin! When using SoftAssert, you must call assertAll() at the very end of your test method. This "collects" all the failures that happened during the "Verify" steps and finally throws the exception to mark the test as failed in your report. If you forget assertAll(), the test will report a "Pass" even if every single verification failed. It’s the most common mistake people make when switching from hard asserts.