Both Assert and Verify statements are used in the test suites for adding validations to the test methods. Testing frameworks like TestNG and JUnit are used with Selenium to provide assertions.
The major difference between "Assert" and "Verify" commands is-
In case of "Assert" command, as soon as the validation fails the execution of that particular test method is stopped and the test method is marked as failed.
Whereas, in case of "Verify", the test method continues execution even after the failure of an assertion statement. Although the test method will still be marked as failed but the remaining statements of the test method will be executed normally. In TestNG, the "Verify" functionality is provided by means of "Soft Assertions" or "SoftAssert" class.
Now, let's get deeper into Assert and Verify/Soft Assert.
We use Assert when we have to validate critical functionality, failing of which makes the execution of further statements irrelevant. Hence, the test method is aborted as soon as failure occurs.
Here, we can observe that only the text "Passing 1" gets printed. The second assertion aborts the test method as it fails preventing further statement from getting executed.
At times, we might require the test method to continue execution even after the failure of the assertion statements. In TestNG, Verify is implemented using SoftAssert class.
In case of SoftAssert, all the statements in the test method are executed (including multiple assertions). Once, all the statements are executed, the test result is collated based on the assertion results and test is marked as passed or fail.
Here, we can see that even though both the test methods are bound to fail, still the test continues to execute.