TestNG Assertions:
While using Selenium for automated testing of web
applications, we need to add validations in our tests to report them as pass or
fail. And assertions can let us do that within a single line of code.
Here, you’ll learn how to use TestNG assertions and know
the different methods to assert conditions.
Assertions give you a
way, other than If-Else blocks, to test conditions. They are not only easy to
use but also eliminate the chances of making any error in forming test
conditions. Hence, it’s always beneficial to use them in Selenium WebDriver
projects.
There are two types
of assertions in Selenium that we can place in our test scripts using TestNG:
1. Hard Assertion.
2. Soft Assertion.
1- Hard Assertion.
It is the default
assert mechanism built into TestNG’s <org.testng.assert> package. We use it when a test
has to stop immediately after the assertion fails.
Please refer
below two scenarios to understand the concept.
Hard
Assertion – Scenario(1)
Follow the
below code which includes one assert call, which get passed and so the
test case.
package Assertion;
import org.testng.Assert;
import
org.testng.annotations.Test;
public class HardAssertion {
String empName = "Sujoy";
@Test
public void HardAssertion_TC001() {
Assert.assertEquals(empName, "Sujoy");
System.out.println("Successfully passed!");
}
}
Output:
Successfully
passed!
[Utils]
Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default
test.xml
[Utils] Directory
D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
PASSED:
HardAssertion_TC001
You can see the
message “Successfully passed!”
getting appeared in the execution output.
Hard Assertion – Scenario(2)
Follow the
below code which includes one assert call, which got failed and so it
leads to the end of the test case.
package Assertion;
import org.testng.Assert;
import
org.testng.annotations.Test;
public class HardAssertion {
String empName = "Sujoy";
@Test
public void HardAssertion_TC001() {
Assert.assertEquals(empName, "SujoyK");
System.out.println("Successfully passed!");
}
}
Output:
FAILED:
HardAssertion_TC001
java.lang.AssertionError:
expected [SujoyK] but found [Sujoy]
After
the expected string didn’t match the actual string, the execution of test
case stopped immediately.
2- Soft Assertion.
It is a
custom assert mechanism supported by TestNG’s <org.testng.asserts.Softassert> package. We use it when a test
has to continue execution even after an assertion fails in the sequence.
Follow the
below code which creates a Soft assertion object. Then, it includes
multiple assert methods followed by a <assertAll()> call. The first assert fails but
that doesn’t interrupt the execution. It is because of the <assertAll()> method which lets the other
assert calls to complete.
package Assertion;
import org.testng.annotations.Test;
import
org.testng.asserts.SoftAssert;
public class SoftAssertion {
SoftAssert softAssert = new SoftAssert();
String empName = "Sujoy";
String companyName = "TestingBar";
@Test
public void SoftAssertion_TC001() {
softAssert.assertEquals(companyName, " MyTestingBar");
softAssert.assertEquals(empName, "Sujoy");
System.out.println("Last statement gets executed!");
softAssert.assertAll();
}
}
Output:
...
Last
statement gets executed!
[Utils]
Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default
test.xml
[Utils] Directory
D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
FAILED:
SoftAssertion_TC001
java.lang.AssertionError:
The following asserts failed:
expected [ MyTestingBar] but found
[TestingBar]
You
can cross-check from the output above, that the message <The Last statement gets executed!>
appeared there even after one of the assert calls failed.
The Assert Methods Available In TestNG.
Let’s now
quickly outline the methods that you can call from Selenium code for using
TestNG assertions.
Methods
|
Description
|
1- assertEquals(String
actual,String expected);
|
1- It accepts two string
arguments.
2- It’ll check whether both are equal, if not it’ll fail the test. |
2- assertEquals(String
actual, String expected,String message);
|
1- It accepts three string
arguments.
2- It’ll test whether both are same, if not it’ll fail the test. 3- Also, it’ll throw a message that we provide. |
3- assertEquals(boolean
actual, boolean expected);
|
1- It assumes two boolean
arguments.
2- It tests them for equality, if not it’ll fail the test. |
4-
assertEquals(java.util.Collection actual,java.util.Collection expected,
java.lang.String message); |
1- It accepts two collection type
objects.
2- It checks whether they hold same elements and with the same order. 3- It’ll fail the test if the above condition doesn’t meet. 4- Also, you’ll see the message appear in the report. |
5- assertTrue(<condition>);
|
1- It accepts one boolean
argument.
2- It tests that the given condition is true. 3- If it fails, then an <AssertionError> would occur. |
6- assertTrue(<condition>,message);
|
1- It assumes one boolean
argument and a message.
2- It asserts that the given condition is true. 3- If it fails, then an <AssertionError> would occur with the message you passed. |
7- assertFalse(<condition>);
|
1- It accepts one boolean
argument.
2- It checks that the given condition is false. 3- If it doesn’t pass, then an <AssertionError> would occur. |
8- assertFalse(<condition>,message);
|
1- It assumes one boolean
argument and a message.
2- It asserts that the given condition is false. 3- If it fails, then an <AssertionError> would occur with the message you passed. |
No comments:
Post a Comment