TestNG Assertions – Types Of Asserts in TestNG: When we automate any web application using selenium webdriver, we have to add validation in our automation test script to verify that our test is pass or fail. But Selenium WebDriver does not have any assertions. For using assertions, we have to use the TestNG Assertions with a single line of code, we can verify that our test case pass or fail.
One thing comes to your mind what we are using TestNg assertions where we have log4j and System.out.println() by which we got to know that a test case is passed or failed.
Let me told you if we use the log4j and System.out.println() those are print the result in the console but not in the report, but the result will not be reflected in the report. So the report will be more helpful to the end-user or customer because he can get a clear idea from that.
This post, we are going to discuss how to use the TestNG assertions and the various methods which are present in TestNG.
Types Of TestNG Assertions In Selenium
In Selenium, we have two types of assertions, those categorized based on how they behave after a condition is pass or fail. The two assertions like:
- Hard Assertion
- Soft Assertion
Hard Assertion
The Default build mechanism of assert is Hard assertion, and it’s stored in org.testng.assert package. We use this type of assertion when our requirement for the test execution should stop immediately is the assertions fail and will throw an assertion error. The test case also marked as failed when a hard condition fails.
Example:
package com.techbeamers.hardassertion; import org.testng.Assert; import org.testng.annotations.Test; public class HardAssertion { String className = “HardAssertion”; @Test public void test_UsingHardAssertion() { Assert.assertTrue(true == true); Assert.assertEquals(“HardAssert”, “HardAssertion”); Assert.assertEquals(className, “HardAssertion”); System.out.println(“Successfully passed!”); } }
Output:
... FAILED: test_UsingHardAssertion java.lang.AssertionError: expected [HardAssertion] but found [HardAssert] ...
As you see in the output when both actual and expected values are not matched on the below statement, we are getting the assertion error, and the test case also marked as failed.
Assert.assertEquals("HardAssert", "HardAssertion");
Soft Assertion
This assertion is stored in org.testng.asserts.Softassert and we need to use this type of assertion when we want to continue our execution even after the assertion got failed. Soft assertions will not be throwing an error when the assertions got failed, but it continues the execution to the next step.
For use, the Soft Assertion is your automation script. First, you have to create an instance of SoftAssert class, and by using that instance, you can call the various method of the soft assertion.
package com.techbeamers.softassertion; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; public class SoftAssertion { SoftAssert softAssert = new SoftAssert(); String className = "SoftAssertion"; @Test public void test_UsingSoftAssertion() { softAssert.assertTrue(true == true); softAssert.assertEquals("SoftAssert", "SoftAssertion"); softAssert.assertEquals(className, "SoftAssertion"); System.out.println("Last statement gets executed!"); softAssert.assertAll(); } }
You can see the output that still some of the assertions are failed, but our execution is not stopped, it completes the execution.
... The last statement gets executed! [Utils] Attempting to create C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite\Default test.xml [Utils] Directory C:\workspace\work\selenium\TestNGAssertions\test-output\Default suite exists: true FAILED: test_UsingSoftAssertion java.lang.AssertionError: The following asserts failed: expected [SoftAssertion] but found [SoftAssert] ...
The issues in using the Soft assertion
If there are multiple test methods and for all of those methods, if you are use the same SoftAssert instance, then if one tests failed, it makes other tests also fail. It happens due to the use of the same assert object, which evaluates all occurrences of assert methods despite being in different cases.
Java Class:
package com.techbeamers.softassertion; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; public class SoftAssertIssue { SoftAssert softAssert = new SoftAssert(); String className = "SoftAssertion"; @Test public void test1_UsingSoftAssertion() { softAssert.assertTrue(true == true); softAssert.assertEquals("SoftAssert", "SoftAssertion"); softAssert.assertEquals(className, "SoftAssertion"); softAssert.assertAll(); } @Test public void test2_UsingSoftAssertion() { softAssert.assertTrue(true == true); softAssert.assertEquals("SoftAssertion", "SoftAssertion"); softAssert.assertEquals(className, "SoftAssertion"); softAssert.assertAll(); } }
Output:
FAILED: test1_UsingSoftAssertion java.lang.AssertionError: The following asserts failed: expected [SoftAssertion] but found [SoftAssert] ... FAILED: test2_UsingSoftAssertion java.lang.AssertionError: The following asserts failed: expected [SoftAssertion] but found [SoftAssert] ... =============================================== Default test Tests run: 2, Failures: 2, Skips: 0 ===============================================
If you see the output, then you can find that test1 is failed because the actual and expected values are not matching, but in the test2 method, there are no issues, but that also got marked as failed because we are using the same instance for all the test methods.
The right way to use the Soft assertion
In the above example, we have seen what the issues we are going to use SoftAssert properly so that we can validate all the test methods properly are. The simple solution for this is to create separate SoftAssert for each test case or test method.
package com.techbeamers.softassertion; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; public class SoftAssertSolution { SoftAssert softAssert1 = new SoftAssert(); SoftAssert softAssert2 = new SoftAssert(); String className = "SoftAssertion"; @Test public void test1_UsingSoftAssertion() { softAssert1.assertTrue(true == true); softAssert1.assertEquals("SoftAssert", "SoftAssertion"); softAssert1.assertEquals(className, "SoftAssertion"); softAssert1.assertAll(); } @Test public void test2_UsingSoftAssertion() { softAssert2.assertTrue(true == true); softAssert2.assertEquals("SoftAssertion", "SoftAssertion"); softAssert2.assertEquals(className, "SoftAssertion"); softAssert2.assertAll(); } }
Output:
... PASSED: test2_UsingSoftAssertion FAILED: test1_UsingSoftAssertion java.lang.AssertionError: The following asserts failed: expected [SoftAssertion] but found [SoftAssert] ... =============================================== Default test Tests run: 2, Failures: 1, Skips: 0 ===============================================
Different Assertion Methods
For Validation Purpose TestNg provides so many assertions method out of which we have mentioned a few methods which are used commonly:
assertEqual(String actual, String expected):– It takes two string arguments and checks whether both are equal; if not, it will fail the test.
assertEqual(String actual, String expected, String message):– It takes three string arguments, and checks whether both are equal. If not, it will fail the test and throws the message which we provide.
assertEquals(boolean actual, boolean expected):- It takes two Boolean arguments and checks whether both are equal; if not, it will fail the test.
assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message) :- Takes two collection objects and verifies both collections contain the same elements and with the same order. if not, it will fail the test with the given message.
Assert.assertTrue(condition):– It takes one boolean argument and checks that a condition is true. If it isn’t, an AssertionError is thrown.
Assert.assertTrue(condition, message): It takes one boolean argument and String message. It Asserts that a condition is true. If it isn’t, an AssertionError, with the given message, is thrown.
Assert.assertFalse(condition): It takes one boolean argument and checks that a condition is false. If it isn’t, an AssertionError is thrown.
Assert.assertFalse(condition, message): It takes one boolean argument and String message. It Asserts that a condition is false. If it isn’t, an AssertionError, with the given message, is thrown.
Source: link
Leave a Reply