1 package test.configurationfailurepolicy; 2 3 import static org.testng.Assert.assertEquals; 4 5 import org.testng.ITestContext; 6 import org.testng.TestListenerAdapter; 7 import org.testng.TestNG; 8 import org.testng.annotations.BeforeClass; 9 import org.testng.annotations.DataProvider; 10 import org.testng.annotations.Test; 11 import org.testng.xml.XmlSuite; 12 13 import testhelper.OutputDirectoryPatch; 14 15 public class FailurePolicyTest { 16 17 // only if this is run from an xml file that sets this on the suite 18 @BeforeClass(enabled=false) setupClass( ITestContext testContext)19 public void setupClass( ITestContext testContext) { 20 assertEquals(testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), XmlSuite.CONTINUE); 21 } 22 23 @DataProvider( name="dp" ) getData()24 public Object[][] getData() { 25 Object[][] data = new Object[][] { 26 // params - confFail, confSkip, skipedTests 27 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 }, 28 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleTests.class }, 2, 0, 2 }, 29 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleInvocations.class }, 4, 0, 4 }, 30 new Object[] { new Class[] { ExtendsClassWithFailedBeforeMethod.class }, 2, 2, 2 }, 31 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 }, 32 new Object[] { new Class[] { ExtendsClassWithFailedBeforeClassMethod.class }, 1, 2, 2 }, 33 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class }, 2, 3, 3 }, 34 new Object[] { new Class[] { ClassWithSkippingBeforeMethod.class }, 0, 1, 1 }, 35 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethod.class }, 2, 0, 2 }, 36 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class }, 8, 0, 8 }, 37 new Object[] { new Class[] { FactoryClassWithFailedBeforeClassMethod.class }, 2, 2, 2 }, 38 }; 39 return data; 40 } 41 42 @Test( dataProvider = "dp" ) confFailureTest(Class[] classesUnderTest, int configurationFailures, int configurationSkips, int skippedTests)43 public void confFailureTest(Class[] classesUnderTest, int configurationFailures, int configurationSkips, int skippedTests) { 44 45 TestListenerAdapter tla = new TestListenerAdapter(); 46 TestNG testng = new TestNG(); 47 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); 48 testng.setTestClasses(classesUnderTest); 49 testng.addListener(tla); 50 testng.setVerbose(0); 51 testng.setConfigFailurePolicy(XmlSuite.CONTINUE); 52 testng.run(); 53 54 verify(tla, configurationFailures, configurationSkips, skippedTests); 55 } 56 57 @Test commandLineTest_policyAsSkip()58 public void commandLineTest_policyAsSkip() { 59 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), 60 "-configfailurepolicy", "skip", 61 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" }; 62 TestListenerAdapter tla = new TestListenerAdapter(); 63 TestNG.privateMain(argv, tla); 64 65 verify(tla, 1, 1, 2); 66 } 67 68 @Test commandLineTest_policyAsContinue()69 public void commandLineTest_policyAsContinue() { 70 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), 71 "-configfailurepolicy", "continue", 72 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" }; 73 TestListenerAdapter tla = new TestListenerAdapter(); 74 TestNG.privateMain(argv, tla); 75 76 verify(tla, 2, 0, 2); 77 } 78 79 @Test commandLineTestWithXMLFile_policyAsSkip()80 public void commandLineTestWithXMLFile_policyAsSkip() { 81 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), 82 "-configfailurepolicy", "skip", "src/test/resources/testng-configfailure.xml" }; 83 TestListenerAdapter tla = new TestListenerAdapter(); 84 TestNG.privateMain(argv, tla); 85 86 verify(tla, 1, 1, 2); 87 } 88 89 @Test commandLineTestWithXMLFile_policyAsContinue()90 public void commandLineTestWithXMLFile_policyAsContinue() { 91 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), 92 "-configfailurepolicy", "continue", "src/test/resources/testng-configfailure.xml" }; 93 TestListenerAdapter tla = new TestListenerAdapter(); 94 TestNG.privateMain(argv, tla); 95 96 verify(tla, 2, 0, 2); 97 } 98 verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests )99 private void verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests ) { 100 assertEquals(tla.getConfigurationFailures().size(), configurationFailures, "wrong number of configuration failures"); 101 assertEquals(tla.getConfigurationSkips().size(), configurationSkips, "wrong number of configuration skips"); 102 assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests"); 103 } 104 105 } 106