Write first test using TestNG framework
Learn TestNG from the scratch
If you’re a beginner and want to learn the TestNG framework from the scratch then you’re on the right article!. So, without wasting your time let’s start writing the first program on the TestNG framework.
Prerequisites:
Before writing the test script, let's verify if TestNG has been successfully installed in Eclipse or not.
Steps:
- Go to the window option then click on preferences. Then you will see a preferences dialog box.
2. Remove “type filter text” and type “TestNG” there.
If you will get TestNG in the list like in the above picture then it means you have successfully installed TestNG in Eclipse IDE.
Now, Let's get started with the TestNG framework. Follow all the steps mentioned below.
#1) Create a new Java Project on Eclipse
Follow the steps below to create a new java project on Eclipse IDE.
1) Go to option File → New → Java Project. Then a “New Java Project” dialog box will appear. Give any name to your project like I gave “TestNGProject” then click on “Next” as shown in the picture.
2) Now, you will get one more dialog box of “New module-info.java” then click on “Don’t Create” because for the time we don't need any module.
Finally, you will see your created project in the left panel of Eclipse IDE. As “TestNG” has shown in the list.
#2) Create a New Package for the Java Project
Now, let’s create a new package for the java project that we have created in the above step.
1) Go to your java project “TestNGProject” on the left panel of Eclipse as shown in the above step, extend this project. Now, you will have the “src” folder in it.
2) Right-click on this “src” folder, go to a new → package, then you will see a dialog box of “New Java Package”. Give any name to the package then click on “finish”.
Now, you will see your created package under the “src” folder.
3) Right-click on “testcase” package, go to New → Class, then you will get a dialog box. Give any name to the class then click on finish.
#3) Write your first test in Class
1) Write this code in demo.java class
This is a very basic code with the TestNG framework. Let me explain to you.
Here you see @ Test is one of the annotations in the TestNG library, which will recognize the method followed by this annotation as a test case.
Optional case:
If you get a “red line” error under the word @Test then do the following steps:
- Move your cursor on @Test annotation you will get these options, Select “Add TestNG library”.
2. Now, move your cursor again on @Test annotation and select the first option “Import ‘Test’ (org.testng.annotations).
In this way, you can resolve the “red line” error under @Test annotation.
2) Click on the option Run → Run as → TestNG Test.
3)After running this test you will get the output on your console window.
Here, you can see the test report on the console that shows a number of test cases that pass, fail, and skips.
Congratulations !! you have done your first basic test using TestNG. Hence, this is very basic of TestNG framework, you can run your test with an XML file that I will explain to you in another article.
If you find this post useful, please tap 👏 button below 😊
Best of Luck 👍