Skip to content

Instantly share code, notes, and snippets.

@msakamoto-sf
Last active March 13, 2018 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msakamoto-sf/4528710 to your computer and use it in GitHub Desktop.
Save msakamoto-sf/4528710 to your computer and use it in GitHub Desktop.
Nested Test Cases with TestNG (nearly equals JUnit's "Enclosed" TestRunner)
#!/bin/sh -x
# (root)/
# build.sh
# testng-6.8/testng-6.8.jar
# testng.xml
# t1/
# TestFoo.java
CLASSPATH=.:./testng-6.8/testng-6.8.jar
javac -classpath ${CLASSPATH} ./t1/TestFoo.java
java -classpath ${CLASSPATH} org.testng.TestNG testng.xml
# MacOSX 10.7.5
# java version "1.7.0_06-ea"
# Java(TM) SE Runtime Environment (build 1.7.0_06-ea-b09)
# Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
$ ./build.sh
+ CLASSPATH=.:./testng-6.8/testng-6.8.jar
+ javac -classpath .:./testng-6.8/testng-6.8.jar ./t1/TestFoo.java
+ java -classpath .:./testng-6.8/testng-6.8.jar org.testng.TestNG testng.xml
...
... TestNG 6.8beta 20120825_1010 by C�dric Beust (cedric@beust.com)
...
[TestNG] Running:
/work/dev/java/testng01/testng.xml
Thread[main,5,main] - class t1.TestFoo$TestBar - beforeSuite()
Thread[main,5,main] - class t1.TestFoo - beforeSuite()
Thread[main,5,main] - class t1.TestFoo$TestBar - beforeTest()
Thread[main,5,main] - class t1.TestFoo - beforeTest()
Thread[main,5,main] - class t1.TestFoo$TestBar - beforeClass()
Thread[main,5,main] - class t1.TestFoo$TestBar - beforeMethod()
testBar1
Thread[main,5,main] - class t1.TestFoo$TestBar - afterMethod()
Thread[main,5,main] - class t1.TestFoo$TestBar - beforeMethod()
testBar2
Thread[main,5,main] - class t1.TestFoo$TestBar - afterMethod()
Thread[main,5,main] - class t1.TestFoo$TestBar - afterClass()
testBaz1
testBaz2
Thread[main,5,main] - class t1.TestFoo - beforeClass()
Thread[main,5,main] - class t1.TestFoo - beforeMethod()
testFoo1
Thread[main,5,main] - class t1.TestFoo - afterMethod()
Thread[main,5,main] - class t1.TestFoo - beforeMethod()
testFoo2
Thread[main,5,main] - class t1.TestFoo - afterMethod()
Thread[main,5,main] - class t1.TestFoo - afterClass()
Thread[main,5,main] - class t1.TestFoo$TestBar - afterTest()
Thread[main,5,main] - class t1.TestFoo - afterTest()
PASSED: testBar1
PASSED: testBar2
PASSED: testBaz1
PASSED: testBaz2
PASSED: testFoo1
PASSED: testFoo2
===============================================
t1
Tests run: 6, Failures: 0, Skips: 0
===============================================
Thread[main,5,main] - class t1.TestFoo$TestBar - afterSuite()
Thread[main,5,main] - class t1.TestFoo - afterSuite()
===============================================
Suite1
Total tests run: 6, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@389d0b87: 49 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@4c520758: 13 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@722bc102: 30 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter@77cf08b7: 6 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@23451fb5: 50 ms
// "(root)/t1/TestFoo.java"
package t1;
import static org.testng.Assert.*;
import org.testng.annotations.*;
public class TestFoo {
static void log(Class k, String mes) {
System.out.println(Thread.currentThread() + " - " + k.toString() + " - " + mes);
}
@BeforeSuite
public void beforeSuite() {
log(TestFoo.class, "beforeSuite()");
}
@AfterSuite
public void afterSuite() {
log(TestFoo.class, "afterSuite()");
}
@BeforeTest
public void beforeTest() {
log(TestFoo.class, "beforeTest()");
}
@AfterTest
public void afterTest() {
log(TestFoo.class, "afterTest()");
}
@BeforeClass
public void beforeClass() {
log(TestFoo.class, "beforeClass()");
}
@AfterClass
public void afterClass() {
log(TestFoo.class, "afterClass()");
}
@BeforeMethod
public void beforeMethod() {
log(TestFoo.class, "beforeMethod()");
}
@AfterMethod
public void afterMethod() {
log(TestFoo.class, "afterMethod()");
}
@Test
public void testFoo1() {
int expected = 3;
assertEquals(1 + 2, expected);
System.out.println("testFoo1");
}
@Test
public void testFoo2() {
int expected = 3;
assertEquals(1 + 2, expected);
System.out.println("testFoo2");
}
public class TestBar {
@BeforeSuite
public void beforeSuite() {
TestFoo.log(TestBar.class, "beforeSuite()");
}
@AfterSuite
public void afterSuite() {
TestFoo.log(TestBar.class, "afterSuite()");
}
@BeforeTest
public void beforeTest() {
TestFoo.log(TestBar.class, "beforeTest()");
}
@AfterTest
public void afterTest() {
TestFoo.log(TestBar.class, "afterTest()");
}
@BeforeClass
public void beforeClass() {
TestFoo.log(TestBar.class, "beforeClass()");
}
@AfterClass
public void afterClass() {
TestFoo.log(TestBar.class, "afterClass()");
}
@BeforeMethod
public void beforeMethod() {
TestFoo.log(TestBar.class, "beforeMethod()");
}
@AfterMethod
public void afterMethod() {
TestFoo.log(TestBar.class, "afterMethod()");
}
@Test
public void testBar1() {
int expected = 4;
assertEquals(2 + 2, expected);
System.out.println("testBar1");
}
@Test
public void testBar2() {
int expected = 4;
assertEquals(2 + 2, expected);
System.out.println("testBar2");
}
}
public class TestBaz {
@Test
public void testBaz1() {
int expected = 6;
assertEquals(2 * 3, expected);
System.out.println("testBaz1");
}
@Test
public void testBaz2() {
int expected = 6;
assertEquals(2 * 3, expected);
System.out.println("testBaz2");
}
}
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- (root)/testng.xml -->
<suite name="Suite1" verbose="2" >
<test name="t1">
<packages>
<package name="t1" />
</packages>
</test>
</suite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment