1 package test.failedreporter; 2 3 import org.testng.Assert; 4 import org.testng.TestNG; 5 import org.testng.annotations.AfterMethod; 6 import org.testng.annotations.BeforeMethod; 7 import org.testng.annotations.Test; 8 import org.testng.collections.Lists; 9 10 import test.BaseTest; 11 12 import java.io.File; 13 import java.io.IOException; 14 import java.util.List; 15 16 public class FailedReporterTest extends BaseTest { 17 private File mTempDirectory; 18 19 @BeforeMethod setUp()20 public void setUp() { 21 File slashTmpDir = new File(System.getProperty("java.io.tmpdir")); 22 mTempDirectory = new File(slashTmpDir, "testng-tmp-" + System.currentTimeMillis() % 1000); 23 mTempDirectory.mkdirs(); 24 mTempDirectory.deleteOnExit(); 25 } 26 27 @AfterMethod tearDown()28 public void tearDown() { 29 deleteDir(mTempDirectory); 30 } 31 32 @Test failedAndSkippedMethodsShouldBeIncluded()33 public void failedAndSkippedMethodsShouldBeIncluded() throws IOException { 34 testFailedReporter(FailedReporterSampleTest.class, new String[] { "f1", "f2" }, 35 "<include name=\"%s\"" + "\"/>"); } 36 37 @Test failedMethodWithDataProviderShouldHaveInvocationNumbers()38 public void failedMethodWithDataProviderShouldHaveInvocationNumbers() throws IOException { 39 testFailedReporter(FailedReporter2SampleTest.class, new String[] { "f1" }, 40 "<include invocationNumbers=\"1\" name=\"%s\"" + "\"/>"); 41 } 42 testFailedReporter(Class<?> cls, String[] expectedMethods, String expectedLine)43 private void testFailedReporter(Class<?> cls, String[] expectedMethods, String expectedLine) { 44 TestNG tng = new TestNG(); 45 tng.setVerbose(0); 46 tng.setTestClasses(new Class[] { cls }); 47 tng.setOutputDirectory(mTempDirectory.getAbsolutePath()); 48 tng.run(); 49 50 File failed = new File(mTempDirectory, "testng-failed.xml"); 51 for (String s : expectedMethods) { 52 List<String> resultLines = Lists.newArrayList(); 53 grep(failed, expectedLine.format(s), resultLines); 54 Assert.assertEquals(1, resultLines.size()); 55 } 56 57 } 58 } 59