Difference between Assert and Verify

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.

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.
Example-

@Test
public void assertionTest(){
   
   //Assertion Passing
   Assert.assertTrue(1+2 == 3);
   System.out.println("Passing 1");
   
   //Assertion failing
   Assert.fail("Failing second assertion");
   System.out.println("Failing 2");
}


Output-

Passing 1
FAILED: assertionTest
java.lang.AssertionError: Failing second assertion

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.

Verify

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.
Example-

@Test
public void softAssertionTest(){
   
   //Creating softAssert object
   SoftAssert softAssert = new SoftAssert();
   
   //Assertion failing
   softAssert.fail("Failing first assertion");
   System.out.println("Failing 1");
   
   //Assertion failing
   softAssert.fail("Failing second assertion");
   System.out.println("Failing 2");

   //Collates the assertion results and marks test as pass or fail
   softAssert.assertAll();
}


Output-

Failing 1
Failing 2
FAILED: softAssertionTest
java.lang.AssertionError: The following asserts failed:
	Failing first assertion,
	Failing second assertion

Here, we can see that even though both the test methods are bound to fail, still the test continues to execute.