Search This Blog

Monday, August 25, 2014

Soft Assertions using TestNG

All of us write various automation tests for the application that we test in our every day work. One thing that has been debated the most is the use of soft assertions. Before we dive deep into the debate, a quick look at the two types of assertions.

Hard Assertions (or simply Assertions)

Tests immediately fail and stop executing the moment a failure occurs in the assertion. You may want to use a hard assert if you want to verify if you have logged in correctly and fail the test if you haven’t as there is no point in proceeding further if the test if the pre-condition itself fails.

Soft Assertions

Tests don’t stop running even if an assertion condition fails, but the test itself is marked as a failed test to indicate the right result. This is useful if you are doing multiple validations in a form (well.. you may actually question why you should be doing multiple validations in the first place which is probably a separate topic by itself), you may actually want to complete all the validations and fail the test once all validations are complete in case of failures.
Now.. there are multiple ways of doing soft asserts. You can write your own custom logic using the regular assert, but trap any exceptions and make the test proceed further. Or, you can use the TestNG library (for Java) which does this magic for you.
Here is how you initialize the TestNG assertions…
123456789
package automation.tests;
 
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
 
public class MyTest {
private Assertion hardAssert = new Assertion();
private SoftAssert softAssert = new SoftAssert();
}
view rawgistfile1.java hosted with ❤ by GitHub
Here is a sample test for a Hard Assertion…
1234
@Test
public void testForHardAssert() {
hardAssert.assertTrue(false);
}
view rawgistfile1.java hosted with ❤ by GitHub
Here is a sample test for Soft Assertion which does not fail the test…
1234
@Test
public void testForSoftAssertWithNoFailure() {
softAssert.assertTrue(false);
}
view rawgistfile1.java hosted with ❤ by GitHub
And here is a soft assertion that actually marks the test as a failure…
123456
@Test
public void testForSoftAssertionFailure() {
softAssert.assertTrue(false);
softAssert.assertEquals(1, 2);
softAssert.assertAll();
}
view rawgistfile1.java hosted with ❤ by GitHub
If you look at the above test, the softAssert.assertAll() does the trick. This method collates all the failures and decides whether to fail the test or not at the end. So instead of writing a custom logic, the TestNG library itself offers the facility to perform Soft Assertions in your test.

Thursday, August 14, 2014

Apache Kafka - A high throughput distributed messaging system

Apache Kafka is publish-subscribe messaging rethought as a distributed commit log.
Fast
A single Kafka broker can handle hundreds of megabytes of reads and writes per second from thousands of clients.
Scalable
Kafka is designed to allow a single cluster to serve as the central data backbone for a large organization. It can be elastically and transparently expanded without downtime. Data streams are partitioned and spread over a cluster of machines to allow data streams larger than the capability of any single machine and to allow clusters of co-ordinated consumers
Durable
Messages are persisted on disk and replicated within the cluster to prevent data loss. Each broker can handle terabytes of messages without performance impact.
Distributed by Design
Kafka has a modern cluster-centric design that offers strong durability and fault-tolerance guarantees.

My Profile

My photo
can be reached at 09916017317