Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is TestNG?
History
sUnit for Smalltalk (~1998, Kent Beck)
JUnit (~2000, Kent Beck & Erich Gamma)
Latest 3.x: 3.8.2 (March 2006)
TestNG (~2004)
Latest: 5.8 (March 2008)
TestNG philosophy
Use more Java and OO features
Feature-rich (JUnit: simplicity is valued)
Support testing integrated classes (e.g.,
by default, dont create a new test class
instance for every test method).
Separate compile-time test code from runtime configuration/data info.
5
Test class
// JUnit 4
// TestNG
TestNG Assertions
import static org.testng.Assert.*;
import org.testng.annotations.Test;
public class MyTest {
@Test
public void myTestMethod() {
// ... Possibly some setup
// ... Call production method(s)
assertTrue(boolExpression);
// ... more assertions
}
}
10
Expected Exceptions
JUnit 4 (can simplify test code)
@Test(expected = AThrowable.class)
12
TestNG Groups
13
15
Setup/Teardown (TestNG)
@BeforeMethod
@BeforeClass (no need to be static)
@BeforeGroups ({group1, })
@BeforeTest
@BeforeSuite
And @After*
16
Hierarchy of suite/test/class/method.
A TestNG run executes 1 or more suites.
Can be transparent, e.g., run 1 test method via Eclipse.
17
testng.xml
Controls test runs
Optional
File can have any name (.xml suffix advised)
TestNG creates testng-failed.xml, for easy re-run.
18
testng.xml (continued)
Which potential test methods to run: suite/test: list of
java package name patterns to include/exclude; test: list
of fully qualified class names to include; within a class,
optional (default = include all) list of method names to
include/exclude; test: list of group name patterns to
include/exclude (and can define new groups for this file
with a list of existing group names).
Parameter name/value pairs: Suite/test: (primitive
values) for @Parameters (test values override suite
values).
JUnit semantics: test-level flag (default: false);
migration aid.
19
Disable Tests
TestNG
@Test(enabled = false)
Add to a group which is excluded
Exclude in other ways in testng.xml
Test Timeouts
TestNG
@Test(timeout = 1000)
testng.xml <suite|test> time-out attribute
JUnit 4
@Test(timeout = 1000)
21
Integration
If the units work independently, will they work
together?
25
TestNG:
@Test(dependsOnMethods = {m1, m2, ...})
@Test(dependsOnGroups = {g1, g2, ...})
E.g.: configuration dependencies; production lifecycle
testng.xml:
<suite|test
parallel
=tests|methods|none
thread-count=# (5); 1 if not parallel
time-out
=# ms (default: 0=none)
...>
Reference
[TestNG] Next Generation JavaTM Testing;
TestNG and Advanced Concepts; Cdric Beust and Hani
Suleiman; Addison-Wesley; 2008
34