Environment Setup

In this chapter we will set up the environment for working with TestNG. Let's go step by step.

For command line execution

Step 1 : Installing and setting up Java environment

TestNG is a framework for java, hence you must have JDK installed in your system before starting with testng configuration.

You must have JDK 1.7 or above.

To verify java installation in your system, run the following command.

java -version
If you don't have java installed in your system, you can follow this blog link for installation and configuration of java.

Step 2 : Setting up Maven environment

To setup maven environment you can follow this blog link How to setup maven environment.

Step 3 : TestNG project setup

Go to the project created in previous step and open the POM file and remove the dependency for JUnit or add the dependency for TestNG.
XML
Copy
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
    <scope>test</scope>
</dependency>
Now lets add TestNG test by changing the code in AppTest.java in the following project path.

testng-test/src/test/java/com/example/testngtest/AppTest.java

Update the file with the following code.
Java
Copy
package com.example.testngtest;

import org.testng.annotations.Test;
import org.testng.Assert;

public class AppTest 
{
	@Test
	public void testHelloMethod() {
	  String helloString = "Hello TestNG";
	  Assert.assertEquals("Hello TestNG", helloString);
	}
}

Step 4 : Run tests

After making the above update, change directory to the project directory and run the mvn test command.

cd testng-test
mvn test
If you are able to see the following at the end of the console log, You have successfully configured TestNG.

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS


Note: Sometimes you see Compilation failure. To fix this issue, you need to change the target and source properties to the java version installed in your system.

For IntelliJ IDEA

TestNG environment setup for IntelliJ IDEA is quite straight forward. You can follow below steps:-

  1. Download and install IntelliJ IDEA. Community version is more than enough.
  2. Create new project
  3. Under Generators, select Maven Archeteype
  4. Enter name of project
  5. Select project location
  6. Select JDK : Leave as default
  7. Select Catalog : Leave as default
  8. Select Archetype: maven-archetype-quickstart
  9. Click on Create
Once the project is created open the POM file and remove the dependency for JUnit or add the dependency for TestNG.
XML
Copy
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
    <scope>test</scope>
</dependency>
After making the above update on pom.xml reload the maven dependencies by pressing CTRL + SHIFT + A in windows or COMMAND + SHIFT + A on Mac.
Now lets add TestNG test by changing the code in AppTest.java in the project.



Update the file with the following code.
Java
Copy
import org.testng.annotations.Test;
import org.testng.Assert;

public class AppTest 
{
	@Test
	public void testHelloMethod() {
	  String helloString = "Hello TestNG";
	  Assert.assertEquals("Hello TestNG", helloString);
	}
}
Now right click on testAdd() method or anywhere on the AppTest.java file and click on Run.

If you see the following result, you have successfully configured TestNG on IntelliJ IDEA.
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

For Eclipse

To setup TestNG environment on Eclipse you can follow below steps:-
  1. Open eclipse
  2. Click on Help Menu > Eclipse Marketplace
  3. Type testng in find textbox and click on Go
  4. You may see TestNG for Eclipse
  5. Click on Install
  6. It will ask for Confirm Selected Features
  7. Click on Confirm
  8. This will take bit time to install depending on your internet speed.
Once the plugin is installed, create a new maven project. The steps to create the project is as follows:-
  1. Click on File menu > New > Others
  2. Select Maven Project from Wizard
  3. Click on Next
  4. Click on maven-archetype-quickstart and click on Next
  5. Enter the following for Archtype parameters
    • groupId : com.example.simpletest
    • artifactId : simpletest
    • version : 1.0.0
    • package : com.example.simpletest
  6. Click on Finish
With the above steps you have successfully created the maven project. Now we need to add the POM dependencies. For that open pom.xml and add the following.
XML
Copy
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
    <scope>test</scope>
</dependency>
Now add a java file AppTest.java under src/tests and update the file with following code.
Java
Copy
import org.testng.annotations.Test;
import org.testng.Assert;

public class AppTest 
{
	@Test
	public void testHelloMethod() {
	  String helloString = "Hello TestNG";
	  Assert.assertEquals("Hello TestNG", helloString);
	}
}
Now right click on testAdd() method or anywhere on the AppTest.java file and click on Run As > and click on TestNG Test.

If you see the following result, you have successfully configured TestNG on Eclipse.
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================