Assertion testing may be Boolean expression at a selected point during a program which can be true until and unless there’s a bug within the program. A test assertion is defined as an expression which encapsulates some testable logic specified about a few target is under test.
The benefits and limitations of Assertion testing:
The advantages are:
- It is mainly used to detect subtle errors which will go unnoticed.
- It is used to detect errors sooner after occurring.
- It makes a statement about the effects of the code that is guaranteed to be true.
Limitations of Assertion are:
The Assertion may themselves contain a errors. It can lead to the following problems.
- Failing to report a bug that exists.
- Reporting an error when it does not exists.
- It can lead to other side effects.
- It take time to execute if it contains errors and occupies memory as well.
Types of assertions:
There are two types of Assertions
- Hard assertion
- Soft assertion
Hard Assertion- when our assertion is failing the execution will stop and it will not continue till the further steps.
Soft Assertion- If there is any failure in our assertion but the execution will continue to the next sequence of steps.
In hard assertion, it holds the execution with an exception whereas in soft assertion it continues to execute without any disturbance.
Let us consider an example of Amazon online shopping:
Let us go with a first test case that is verifying an title for Amazon home page. The expected results should come as online shopping for electronics Apparel, computers, books, DVDs and more. Now this Assertion, we will apply hard assertion technique.
Create a Testing class and initiate the driver:
We have lot of methods but one thing is common that is actual datatype and expected datatype parameters. When the actual title and expected title parameter is passed and the assert function is called it automatically asserts the both actual and expected title by comparing and judging whether it is a pass/failure.
Here the actual result title and the expected result is same so the assert test got passed.
Types of Asserts in Testing:
When we automate web application using selenium webdriver, we have to feature a validation in our automation test script to verify that our test is pass or fail.
There are different Assertion methods:
For validation purpose, Testing provides many assertion methods, out of which we have few methods which are commonly used.
- assertEqual (String actual, String expected)- It always takes two string arguments and checks whether both are equal or it fails the test.
- assertEqual (String actual,String expected,String message)- It takes three string arguments, and checks whether both are equal. If not it will fail and throw with a message, asserEquals (Booleanactual, Boolean expected) which takes two Boolean arguments and checks whether both are equal, if not it’ll fail.
- assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message)- Takes two collection objects and verifies both collections contain the same elements and with the same order. If it fails the test with the given message.
- Assert.assertTrue (condition)- It takes one Boolean argument and also verifies the condition is true, if it is not an AssertionError is thrown.
- Assert.assertFalse(condition)- It takes one boolean argument and checks that the condition is false.If the condition is not false an assertionError is thrown.