Soft Assert


Soft Assert


Matching expected and actual result using- Assert, soft assert. Assert is used to compare expected and actual result. If assert is true then the program will move forward, but if assert fail, program will stop. To overcome with this, soft assert is introduced. In case of soft assert program will continue and will not stop in case if condition fails.

Example- Assertion Failed.

In below example, expected and actual results are not matching still it will execute all the flow and display the result.



String actual="My name is Yogen";
 String expected="My name is Yogen";
 String expected1="My name is Deepak";
 String expected2=null;
 @Test
 public void Testassertion()
 {
  SoftAssert assertion= new SoftAssert();
  assertion.assertNotEquals(actual, expected, "assertNotEquals :-"+"\n"+"Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
  assertion.assertEquals(actual, expected1, "assertEquals :-"+"\n"+"Atual is:-"+actual+"\n"+"Expected is:-"+expected1+"\n"+"It is not Matching");
  assertion.assertTrue(actual.contains(expected1),"assertTrue :-"+"\n"+ "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
  assertion.assertFalse(actual.contains(expected),"assertFalse :-"+"\n"+ "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching"); 
  assertion.assertNull(expected, "assertNull :-"+"\n"+"This is not null value");
  assertion.assertNotNull(expected2,"assertNotNull :-"+"\n"+ "This has some value");
  assertion.assertAll();
 }

Output-





Example- Assertion Passed

In below example, expect and actual results are matching, based on that it shows the result.

String actual="My name is Yogen";
String expected="My name is Yogen";
String expected1="My name is Deepak";
String expected2=null;
@Test
public void Testassertion()
{
SoftAssert assertion= new SoftAssert();
assertion.assertNotEquals(actual, expected1, "assertNotEquals :-"+"\n"+"Atual is:-"+actual+"\n"+"Expected is:-"+expected1+"\n"+"It is not Matching");
assertion.assertEquals(actual, expected, "assertEquals :-"+"\n"+"Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
assertion.assertTrue(actual.contains(expected),"assertTrue :-"+"\n"+ "Atual is:-"+actual+"\n"+"Expected is:-"+expected+"\n"+"It is not Matching");
assertion.assertFalse(actual.contains(expected1),"assertFalse :-"+"\n"+ "Atual is:-"+actual+"\n"+"Expected is:-"+expected1+"\n"+"It is not Matching");
assertion.assertNull(expected2, "assertNull :-"+"\n"+"This is not null value");
assertion.assertNotNull(expected,"assertNotNull :-"+"\n"+ "This has some value");
assertion.assertAll();
}

Output-







No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...