该教程介绍了TestNG支持的常见注解(annotation)
package com.fenby.testng;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGTest1 {
private Collection<String> collection;
@BeforeClass
public void oneTimeSetUp() {
// one-time 初始化代码
System.out.println("@BeforeClass - 调用oneTimeSetUp()方法");
}
@AfterClass
public void oneTimeTearDown() {
// one-time 清除代码
System.out.println("@AfterClass - 调用oneTimeTearDown()方法");
}
@BeforeMethod
public void setUp() {
collection = new ArrayList<String> ();
System.out.println("@BeforeMethod - 调用setUp()方法");
}
@AfterMethod
public void tearDown() {
collection.clear();
System.out.println("@AfterMethod - 调用tearDown()方法");
}
@Test
public void testEmptyCollection() {
Assert.assertEquals(collection.isEmpty(), true);
System.out.println("@Test - 调用testEmptyCollection()方法");
}
@Test
public void testOneItemCollection() {
collection.add("元素A");
Assert.assertEquals(collection.size(), 1);
System.out.println("@Test - 调用testOneItemCollection()方法");
}
}
运行结果:
[TestNG] Running:
/private/var/folders/43/whhl4z0j2vs4s7s6cxyvt3g40000gp/T/testng-eclipse-573975755/testng-customsuite.xml
@BeforeClass - 调用oneTimeSetUp()方法
@BeforeMethod - 调用setUp()方法
@Test - 调用testEmptyCollection()方法
@AfterMethod - 调用tearDown()方法
@BeforeMethod - 调用setUp()方法
@Test - 调用testOneItemCollection()方法
@AfterMethod - 调用tearDown()方法
@AfterClass - 调用oneTimeTearDown()方法
PASSED: testEmptyCollection
PASSED: testOneItemCollection
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@30b42a7e: 130 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@37a7481f: 11 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@72a548e4: 12 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1fdf75cc: 110 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@3d48ff04: 17 ms