01: package test.simple;
02:
03: import java.util.Collection;
04: import java.util.List;
05:
06: import org.testng.Assert;
07: import org.testng.IReporter;
08: import org.testng.ISuite;
09: import org.testng.ITestNGMethod;
10: import org.testng.TestNG;
11: import org.testng.annotations.BeforeMethod;
12: import org.testng.annotations.Test;
13: import org.testng.xml.XmlSuite;
14:
15: import testhelper.OutputDirectoryPatch;
16:
17: public class IncludedExcludedTest {
18:
19: private TestNG m_tng;
20:
21: @BeforeMethod
22: public void init() {
23: m_tng = new TestNG();
24: m_tng.setOutputDirectory(OutputDirectoryPatch
25: .getOutputDirectory());
26: m_tng.setVerbose(0);
27: m_tng.setUseDefaultListeners(false);
28: }
29:
30: @Test(description="First test method")
31: public void verifyIncludedExcludedCount1() {
32: m_tng
33: .setTestClasses(new Class[] { IncludedExcludedSampleTest.class });
34: m_tng.setGroups("a");
35: m_tng.addListener(new MyReporter(new String[] { "test3" },
36: new String[] { "test1", "test2" }));
37: m_tng.run();
38: }
39:
40: @Test(description="Second test method")
41: public void verifyIncludedExcludedCount2() {
42: m_tng
43: .setTestClasses(new Class[] { IncludedExcludedSampleTest.class });
44: m_tng.addListener(new MyReporter(new String[] { "beforeSuite",
45: "beforeTest", "beforeTestClass", "beforeTestMethod",
46: "test1", "beforeTestMethod", "test3" },
47: new String[] { "test2" }));
48: m_tng.run();
49: }
50:
51: }
52:
53: class MyReporter implements IReporter {
54:
55: private String[] m_included;
56: private String[] m_excluded;
57:
58: public MyReporter(String[] included, String[] excluded) {
59: m_included = included;
60: m_excluded = excluded;
61: }
62:
63: public void generateReport(List<XmlSuite> xmlSuites,
64: List<ISuite> suites, String outputDirectory) {
65: Assert.assertEquals(suites.size(), 1);
66: ISuite suite = suites.get(0);
67:
68: {
69: Collection<ITestNGMethod> invoked = suite
70: .getInvokedMethods();
71: Assert.assertEquals(invoked.size(), m_included.length);
72: for (String s : m_included) {
73: Assert.assertTrue(containsMethod(invoked, s));
74: }
75: }
76:
77: {
78: Collection<ITestNGMethod> excluded = suite
79: .getExcludedMethods();
80: Assert.assertEquals(excluded.size(), m_excluded.length);
81: for (String s : m_excluded) {
82: Assert.assertTrue(containsMethod(excluded, s));
83: }
84: }
85: }
86:
87: private boolean containsMethod(Collection<ITestNGMethod> invoked,
88: String string) {
89: for (ITestNGMethod m : invoked) {
90: if (m.getMethodName().equals(string))
91: return true;
92: }
93:
94: return false;
95: }
96:
97: }
|