Assertion is a way to check whether expected and actual results match.
assert.method(actual Result, expected Result)
Assertions help us by telling whether the test is passing or failing based on validation.
- Helps us to check if assumptions made during development are correct or not.
- Helps us in verifying if the code block is reachable or not.
- Helps us to validate whether method invocations are correct or not.
- Last but not least, it provides us with the best possible way to detect and correct errors.
- Download Eclipse IDE
- Create a maven project in Eclipse
- Add various dependencies like selenium, TestNG, WebDriver Manager
- Download and install TestNG from the Eclipse marketplace
Note: Before we move ahead, we need to understand the difference between assert and verify.
|When assert fails, further any test will not be executed and thus aborted.
|When a Verify command fails, the Test will continue to execute and thus log all the failures.
|Assert is best used when checking if the given input value has passed the execution phase or not. Login/Signup is the best example.
|Verify is better used to check for non-critical things.
Example: Presence of headline.
In the First step, we would be creating a maven project inside Eclipse. We would fill in basic details like name, group id, description, etc.
In the Second step, we would be adding a package and then create an execution file.
After creating the package, we would be creating different classes each performing different sets of actions as shown below.
And lastly, we would create a class file as shown below.
In our given code we would perform assertion by searching ‘Neova solutions’ and thus getting the title for the same. The code is shown below:
In the above code, we can observe that we are navigating to Google and searching for Neova solutions and lastly asserting for the title.
- The code at line number 20 tells us what our expected title should be.
- The code at line number 21 gets us the actual title.
- Lastly, in line number 22 we assert for both to check if actual results and expected results match or not.
Commonly used TestNG Assert Methods:
We will be looking at a detailed explanation of all these static methods below with a code illustration.
Assert Not Equals is the exact opposite of assertEquals methods. In this method, we are validating for false conditions. If the given condition does not match the actual results, then our test Passes or else it won’t.
AssertNotEquals(unaccepted Results, Actual results);
In the above code, it can be observed that I have purposefully given the wrong title. Instead of validating for the title GitHub, I’m checking for githubbb.
The assert not equal methods validate and returns pass in the test suite.
Similarly, let’s try out other methods.
Assert True is a Boolean condition that returns either true or false depending upon the validation or assertion. It can be denoted as follows:
AssertTrue(string message, Boolean condition);
The code Test is shown below:
Assert False is a Boolean condition that returns either true or false depending upon the validation or assertion. It can be denoted as follows:
AssertFalse(string message, Boolean condition);
The code Test is shown below.
In this condition, it returns pass.
In the above output window on the top, we see that it returns expected false but found true. Hence the code stops its execution.
Types of Assertions:
1. Hard Assertion:
This is a type of assertion in which the flow of execution halts if there is any failure of the test case. Immediately after seizing the execution, it throws Assert Exception.
In the code below, we will understand in detail:
In the above code, we need to navigate to ‘facebook.com’ and thus perform assertion for 3 major conditions.
- URL assertion
- Title assertion
- Text assertion
Failure of any condition in between will result in immediate seizure of further execution.
If we change the expected title in Title assertion to something else as shown below, we would notice that Text Assertion won’t happen.
We can see that I have purposefully changed the expected title in line no. 34 of the code. So, if there are changes in the actual title and expected title, the process immediately terminates.
We notice there is an error in the title mismatch. Thus, the next phase of Execution is not made.
2. Soft Assertion:
Soft assert is the exact opposite of hard assert, instead of throwing Assert Exception when a given test case fails, it continues the flow of execution and thus this approach is more advantageous compared to Hard Assertion.
We will understand this with an example code shown below.
We are using the same piece of code as we had used in Hard Assertion. But the only change we would make is in line no. 21 where we import the Soft Assert class from TestNG and thus make an object of the same.
We are further using the object of soft assert wherever we perform the actual assertion.
We notice that, Even Though we have an error in Title Assertion, the flow of execution never halts instead it continues with other Test Cases. We also see that unlike Hard Assertion; Soft Assertion does not return Failure.