In this tutorial my goal is to share knowledge regarding Test Next Generation (TestNG)
TestNG is an open-source test automation framework for Java. It is develope on the same lines of JUnit and NUnit , Few advanced and useful features provided by TestNG makes it more robust framework compared to it’s peers.
TestNG framework eliminates the limitations of the older framework by providing more powerful and flexible test cases with help of easy annotations, grouping, sequencing and parametrizing.
TestNG is not a part of xUnit but was influence by XUnit, JUnit, NUnit and PyUnit are some of the frameworks within the XUnit family. JUnit is the de facto standard for Java while NUnit is the framework for C# and PyUnit is the framework for Python. However, in the beginning there were limitations with the xUnit family.
For example JUnit 3 had the following constraints :
1) Could not return values
2) Could not have parameters
3) Test Method names had to start with test
As a result of the limitations TestNG was create and added more features. In return JUnit 4 embraced TestNG’s features then added some of those same features. Hopefully you can learn and apply a new feature whether you are using TestNG or another Test Framework. By the end of this tutorial you will know:
1) Importance of Test Framework
2) TestNG Installation and Setup
3) TestNG Annotations
4) TestNG Assertions
Importance of Test Framework
let’s clarify what a test automation framework actually is. a test automation framework is a platform that is a combination of programs, compilers, features, tools, etc. It provides an environment where you can execute automated test scripts.
Basic functions of Test framework are:
1.Create and Execute Test Scripts
2.Generate A Test Report
4.Read and Write Test Data
TestNG Installation and Setup
We can install TestNG using an IDE (Eclipse, NetBeans, IntelliJ), Build Tool (Maven, Gradle, Ant), Command Line, or Download the TestNG jars. For scalability, a Build Tool is the preferred way to install TestNG. However, Eclipse Marketplace is one of the fastest ways to install TestNG. Here are the steps using Eclipse IDE.
Step 1– Go to help and navigate to the ‘Eclipse Marketplace’. The referenced snapshot is below:
Step 2 – Click on ‘Eclipse Marketplace’. You will be directed to the marketplace modal. Type TestNG in the search keyword and hit ‘Go’. The referenced snapshot is below:
Step 3– If TestNG is not install in your Eclipse, rather than the ‘Installed’ button you would see ‘install’. Click on install and your TestNG framework will be install in your Eclipse.
Step 4-Post-restarting your Eclipse, re-verify whether you can see options for creating a TestNG class or not as below:
An annotation is a tag that provides information about the method, class, and suite. It helps to define the execution approach of your test cases and the different features associated with it. They are written above their respective method and prefixed with an at “@” symbol. We can place an Annotation anywhere on the editor because it’s automatically connected to the method.
Below have the major annotations used:
@Test: Marks a class or a method as a part of the test.
@BeforeMethod: A method which is mark with this annotation will be execute before every @test annotated method.
@AfterMethod: A method which is mark with this annotation will be execute after every @test annotated method.
@BeforeClass: A method which is mark with this annotation will be execute before first @Test method execution. It runs only once per class.
@AfterClass: A method which is mark with this annotation will be execute after all the test methods in the current class have run.
@BeforeTest: A method which is mark with this annotation will be execute before first @Test annotated method.
@AfterTest: A method which is marked with this annotation will be executed when all @Test annotated methods complete the execution of those classes which are inside <test> tag in testng.xml file.
@BeforeSuite & @AfterSuite: These annotations run before and after any test annotated method in a class respectively. These annotations start the beginning of a test and the end of it, for all the classes in a suite.
Talking about the execution order of these annotations, they execute in the below order:
@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod -> @AfterClass -> @AfterCTest -> @AfterSuite
The key point to remember is apart from @BeforeMethod and @AfterMethod, all other annotations run only once, whereas the @BeforeMethod and @AfterMethod run post every @Test method.
Assertions are used to perform various kinds of validations in the test cases, which in turn helps us to decide whether the test case has passed or failed. We consider a test as successful if it runs without any exception.
Here, we would discuss two types of assertions:
Hard Assertions: We call general assert as Hard Assert, A hard assertion does not continue with execution until the assertion condition is meet.Hard assertions usually throw an Assertion Error whenever an assertion condition has not been met. The test case will be immediately mark as Failed when a hard assertion condition fails.
The disadvantage of Hard Assert – It marks the method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.
Soft Assertion: A soft assertion continue with the next step of the test execution even if the assertion condition is not meet. Soft Assertion is the type of assertion that does not throw an exception automatically when an assertion fail unless it is asked for. This is useful if you are doing multiple validations in a form, out of which only a few validations directly have an impact on deciding the test case status.
If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.
When To Use Hard And Soft Assertion?
If you need to execute all the steps of a test case to be execute even after an assertion fails, and you also want to report assertion exception, then opt for using Soft Assertions. Using Soft Assertions in your test scripts is a good practice and an effective way of handling your test execution
If you want your test case execution to proceed only after an assertion is passed (For Example, To Verify valid login and only then execute the other steps), then use Hard Assertions.
Example of Hard Assertion in Selenium:
Let’s try to understand the TestNG assertions better with a simple example of verifying the title of a page.
In the above example, we have two string variables, the expected title, and the actual title whose values are “Google” and “Oogle” respectively. In the actual scenario, we will have to find the actual title using the WebDriver code. It is just a dummy example to understand assertions. Next, we compare the two values using assertions using the method assertEquals. The “assertEquals” is one of the methods in Assert class. The statement, “Assert.assertEquals(actual, expected)” checks if the two values are equal and throw an exception if they are not.
Let’s check the result. In the result below, you can see that the test has failed. It shows an Assertion error: expected [Google] but found [Oogle].
Example of Soft Assertion in Selenium:
Let’s apply the Soft Assertion in the example that we have discussed so far.
For this, we first create the object of SoftAssert class at the class level. Next, we use the SoftAssert class object to call the method assertEquals using the statement, “softAssert.assertEquals(actual, expected)”. The statement compares the actual and expected title. Let’s check the result of running the above piece of code.