Benefits of Customised Assertions
Customised Assertions in your test automation framework :
- For complex and repetitive test scenarios, it can be useful to create custom assertions where logic is plugged away from the tests and placed into separate class
- Customised assertions help us dive into the intent of the tests
- Plug in various features like attaching the responses , logging and so on which other wise gets complex using assertions straight away in tests
Example Custom assertion class :
public abstract class Verify extends BaseClass {
public static void verifyTrue(boolean condition, String message, Object... response) {
try {
Assert.assertTrue(condition, message);
} catch (AssertionError e) {
// Perform actions like attaching or logging
Assert.fail(message);
}
}
public static void verifyNotNull(Object object, String message,Object response) {
try {
Assert.assertNotNull(object, message);
} catch (AssertionError e) {
// Perform actions like attaching or logging
Assert.fail(message);
}
}
public static void verifyTrue(boolean condition) {
Assert.assertTrue(condition);
}
public static void verifyNotNull(Object object, String message) {
Assert.assertNotNull(object, message);
}
public static void verifyFalse(boolean condition, String message, Object... response) {
try {
Assert.assertFalse(condition, message);
} catch (AssertionError e) {
// Perform actions like attaching or logging
Assert.fail(message);
}
}
}
Example Test Class :
public class extends BaseTest {
@Test(description = "Test Discription", groups = "smoke")
public void TestXXXX() {
//Test steps go here
Verify.verifyTrue(testCondition.size() > 0, "Test Failed", response);
}
}
Comments
Post a Comment