Recent Posts

Saturday, 9 December 2017

Creating Java Project with TestNG


     Before we write our first TestNG test, we have to create a Java project in Eclipse and add our TestNG test dependencies.

1. Go to File and then New  A window with multiple options will be shown.



2) Select Java Project as shown in the following screenshot and click on Next.


3. On the next screen, enter a Project name for a Java project, let’s say AshokTestNG, as shown in the following screenshot, and click on Finish


This will create a new Java project in Eclipse.

4. Now go to Project --> Properties. Select Java Build Path on the left-hand side on the Properties window as shown in the following screenshot. This will display the build path for the newly created project.


5. Click on the Libraries tab and click on the Add Library option. Now select TestNG on the Add Library window as shown in the following screenshot and click on Next.


6. Click on Finish on your next window.


7. This will add the TestNG library to your Eclipse project.


     Now we have successfully created a new Java project in Eclipse and added a TestNG library to the build path of the project.

Creating your first TestNG class
1. Go to File --> New --> Other. This will open a new Add wizard window in Eclipse.


2. Now select TestNG class from the Add wizard window and click on Next.


3. On the next window click on the Browse button and select the Java project where you need to add your class.


4. Now enter the Package name and the Class name and click on Finish.


5. This window also gives you an option to select different annotations while creating a new TestNG class. If selected, the plugin will generate dummy methods for these annotations while generating the class. This will add a new TestNG class to your project.

package com.ashok.testng;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class AshokTestNG {
  @Test
  public void f() {
  }
  @BeforeTest
  public void beforeTest() {
  }

  @AfterTest
  public void afterTest() {
  }
}

     We have successfully added a new TestNG test class to the newly created Java project in Eclipse. Feel free to modify the code as needed. Now let’s run the newly created test class through Eclipse.

Running TestNG test

1. Select the Java project in the Eclipse and go to Run --> Run Configuration.


2. Select TestNG in the given options and click on the New button to create a new configuration.


3. Please notice that TestNG plugin provides multiple options for running your test cases as follows

  • Class: Using this option you can provide the class name along with the package to run only the said specific test class.
  • Method: Using this you can run only a specific method in a test class.
  • Groups: In case you would like to run specific test methods belonging to a particular TestNG group, you can enter those here for executing them.
  • Package: If you would like to execute all the tests inside a package, you can specify these in this box.
  • Suite: In case you have suite files in the form of testing.xml files, you can select those here for execution.
Let’s enter the configuration name as AshokTestNGRunConfig and select the newly created class under the Class section and click on Apply.


4. Now if you would like to run the newly created configuration, just click on Run after clicking on Apply. This will compile and run the TestNG test class that we have written. The result of the test execution is displayed in the Console and Results windows of Eclipse as shown in the following screenshot.





Next Tutorial  : TestNG Annotations

Previous Tutorial  : Testng Setup

No comments:

Post a Comment