Difference between Assert and Verify

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.

Coming to the difference between Assert and Verify commands. In the case of the “Assert” command, as soon as the validation fails the execution of that particular test method is stopped. Following that the test method is marked as failed.

Whereas, in the 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 the Soft Assertions or using SoftAssert class.

Now, let’s get deeper into Assert and Verify (or soft assert).

Content

Assert

We use Assert when we have to validate critical functionality. If the feature fails then this 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 to prevent further statements 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 the case of SoftAssert, all the statements in the test method are executed (including multiple assertions). Once, all the statements are executed, the test results are collated based on the assertion results. And then the tests are 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.

That’s all we have in this post on the difference between assert and verify commands. Comment below if you have any queries. Also, don’t forget to check our complete Selenium tutorial here.

Selenium with Java – Complete Tutorial

От QA genius

Adblock
detector