Sunday, April 12, 2015

First steps with TestNG, Maven, and Eclipse

Environment
– OS X Yosemite
– Eclipse Luna
– Java 1.7.0_60

Steps:
– Open up Eclipse Luna and create a new Maven Project
– Install the TestNG Eclipse plugin
– Help -> Install New Software.  Add the TestNG site with URL http://beust.com/eclipse (For eclipse 3.4 and above).  Make sure TestNG dependency is enabled, click “Next”.  Accept license agreement.  Finish.
– Open up the pom.xml file, and add the following testng dependency:
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.21</version>
  <scope>test</scope>
</dependency>
– Navigate to src/test/java, create a New -> TestNG class.  TestNG runs entirely on annotations specified in the class, so choose the annotations you want the plugin to auto-generate for you.  The XML Suite file is not required, we will skip that.  NOTE: You can certainly put TestNG annotations on any class (i.e. src/main/java), but following maven’s standard directory layout, we’ll place them in src/main/test.

Sample TestNG class
We’ll start with two annotations without any additional attributes.  @BeforeMethod indicates this method will be run before each @Test method.  @Test indicates that the method is a test method.
package com.cherryshoe.testng;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class FirstTest
{
private String status;
@BeforeMethod
public void setUp() throws Exception {
status = “setup”;
System.out.println(“status[” + status + “]”);
}
@Test
public void firstTest() throws Exception {
status = “firstTest”;
System.out.println(“status[” + status + “]”);
}
@Test
public void secondTest() throws Exception {
status = “secondTest”;
System.out.println(“status[” + status + “]”);
}
}

How to run tests:
The XML Suite file is not required for this example.  There are two options since we are using.
1 – Since we are using Eclipse with the TestNG plugin installed.  We can right click on the .java class file itself, the java class name, or the java class method to
2 – Since we are using maven, we can run “mvn test” to run the test build phase.  You can see that the maven-surefire-plugin is called to run TestNG tests.
[jhsu@MacBook-Pro ~/workspace/cherryshoe/testng]$ mvn test
[INFO] Scanning for projects…
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ————————————————————————
[INFO] Building testng 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ testng —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jhsu/workspace/cherryshoe/testng/src/main/resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ testng —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ testng —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jhsu/workspace/cherryshoe/testng/src/test/resources
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ testng —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ testng —
[INFO] Surefire report directory: /Users/jhsu/workspace/cherryshoe/testng/target/surefire-reports
——————————————————-
T E S T S
——————————————————-
Running com.cherryshoe.testng.FirstTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@28278a47
status[setup]
status[firstTest]
status[setup]
status[secondTest]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.248 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 1.341 s
[INFO] Finished at: 2015-04-12T09:59:52-05:00
[INFO] Final Memory: 7M/245M
[INFO] ————————————————————————

No comments:

Post a Comment

I appreciate your time in leaving a comment!