Conditional logic inside assertions

In Agile software development, unit testing is must and unit testing frameworks are avialble for every major language. Writing good unit tests requires skill and experience. Often we see people use conditional logic inside assertions and calling wrong API inside the unit test. The unit test may be testing the right thing, but not in the right way.

Here is one example on how to remove conditional logic from assertions.

Assert.assertTrue("Employee list shouldn't be empty", employeeList.size() > 0 );

can be written as

Assert.assertFalse("Employee list shouldn't be empty", employeeList.isEmpty() );

The latter assertion easily tells what it does because there are no conditional logic and it’s using proper API on both assertions as well as on the code to be tested.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s