|
| 1 | +package test.configurationfailurepolicy; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | +import static test.SimpleBaseTest.getPathToResource; |
| 5 | + |
| 6 | +import org.testng.ITestContext; |
| 7 | +import org.testng.TestListenerAdapter; |
| 8 | +import org.testng.TestNG; |
| 9 | +import org.testng.annotations.BeforeClass; |
| 10 | +import org.testng.annotations.DataProvider; |
| 11 | +import org.testng.annotations.Test; |
| 12 | +import org.testng.xml.XmlSuite; |
| 13 | +import test.configurationfailurepolicy.issue2731.ConfigFailTestSample; |
| 14 | +import testhelper.OutputDirectoryPatch; |
| 15 | + |
| 16 | +public class FailureContinuePolicyTest { |
| 17 | + // only if this is run from an xml file that sets this on the suite |
| 18 | + @BeforeClass(enabled = false) |
| 19 | + public void setupClass(ITestContext testContext) { |
| 20 | + assertEquals( |
| 21 | + testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), |
| 22 | + XmlSuite.FailurePolicy.CONTINUE); |
| 23 | + } |
| 24 | + |
| 25 | + @DataProvider(name = "dp") |
| 26 | + public Object[][] getData() { |
| 27 | + return new Object[][] { |
| 28 | + // params - confFail, confSkip, skippedTests |
| 29 | + new Object[] {new Class[] {ClassWithFailedBeforeClassMethod.class}, 1, 0, 0}, |
| 30 | + new Object[] {new Class[] {ClassWithFailedBeforeClassMethodAndAfterClass.class}, 1, 0, 0}, |
| 31 | + new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}, 2, 0, 0}, |
| 32 | + new Object[] { |
| 33 | + new Class[] {ClassWithFailedBeforeClassMethodAndBeforeMethodAfterMethodAfterClass.class}, |
| 34 | + 1, |
| 35 | + 0, |
| 36 | + 0 |
| 37 | + }, |
| 38 | + new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleInvocations.class}, 4, 0, 0}, |
| 39 | + new Object[] {new Class[] {ExtendsClassWithFailedBeforeMethod.class}, 2, 0, 0}, |
| 40 | + new Object[] {new Class[] {ExtendsClassWithFailedBeforeClassMethod.class}, 1, 0, 0}, |
| 41 | + new Object[] { |
| 42 | + new Class[] { |
| 43 | + ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class |
| 44 | + }, |
| 45 | + 2, |
| 46 | + 0, |
| 47 | + 0 |
| 48 | + }, |
| 49 | + new Object[] {new Class[] {ClassWithSkippingBeforeMethod.class}, 0, 1, 0}, |
| 50 | + new Object[] {new Class[] {FactoryClassWithFailedBeforeMethod.class}, 2, 0, 0}, |
| 51 | + new Object[] { |
| 52 | + new Class[] {FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class}, 8, 0, 0 |
| 53 | + }, |
| 54 | + new Object[] {new Class[] {FactoryClassWithFailedBeforeClassMethod.class}, 2, 0, 0}, |
| 55 | + new Object[] {new Class[] {ConfigFailTestSample.class}, 4, 0, 0} |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + @Test(dataProvider = "dp") |
| 60 | + public void confFailureTest( |
| 61 | + Class[] classesUnderTest, |
| 62 | + int configurationFailures, |
| 63 | + int configurationSkips, |
| 64 | + int skippedTests) { |
| 65 | + |
| 66 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 67 | + TestNG testng = new TestNG(); |
| 68 | + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); |
| 69 | + testng.setTestClasses(classesUnderTest); |
| 70 | + testng.addListener(tla); |
| 71 | + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); |
| 72 | + testng.run(); |
| 73 | + |
| 74 | + verify(tla, configurationFailures, configurationSkips, skippedTests); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void confFailureTestInvolvingGroups() { |
| 79 | + Class[] classesUnderTest = |
| 80 | + new Class[] {ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class}; |
| 81 | + |
| 82 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 83 | + TestNG testng = new TestNG(); |
| 84 | + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); |
| 85 | + testng.setTestClasses(classesUnderTest); |
| 86 | + testng.addListener(tla); |
| 87 | + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); |
| 88 | + testng.setGroups("group1"); |
| 89 | + testng.run(); |
| 90 | + verify(tla, 1, 0, 0); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void commandLineTest_policyAsSkip() { |
| 95 | + String[] argv = |
| 96 | + new String[] { |
| 97 | + "-log", |
| 98 | + "0", |
| 99 | + "-d", |
| 100 | + OutputDirectoryPatch.getOutputDirectory(), |
| 101 | + "-configfailurepolicy", |
| 102 | + "skip", |
| 103 | + "-testclass", |
| 104 | + ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() |
| 105 | + }; |
| 106 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 107 | + TestNG.privateMain(argv, tla); |
| 108 | + |
| 109 | + verify(tla, 1, 1, 2); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void commandLineTest_policyAsContinue() { |
| 114 | + String[] argv = |
| 115 | + new String[] { |
| 116 | + "-log", |
| 117 | + "0", |
| 118 | + "-d", |
| 119 | + OutputDirectoryPatch.getOutputDirectory(), |
| 120 | + "-configfailurepolicy", |
| 121 | + "continue", |
| 122 | + "-testclass", |
| 123 | + ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() |
| 124 | + }; |
| 125 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 126 | + TestNG.privateMain(argv, tla); |
| 127 | + |
| 128 | + verify(tla, 2, 0, 0); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void commandLineTestWithXMLFile_policyAsSkip() { |
| 133 | + String[] argv = |
| 134 | + new String[] { |
| 135 | + "-log", |
| 136 | + "0", |
| 137 | + "-d", |
| 138 | + OutputDirectoryPatch.getOutputDirectory(), |
| 139 | + "-configfailurepolicy", |
| 140 | + "skip", |
| 141 | + getPathToResource("testng-configfailure.xml") |
| 142 | + }; |
| 143 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 144 | + TestNG.privateMain(argv, tla); |
| 145 | + |
| 146 | + verify(tla, 1, 1, 2); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void commandLineTestWithXMLFile_policyAsContinue() { |
| 151 | + String[] argv = |
| 152 | + new String[] { |
| 153 | + "-log", |
| 154 | + "0", |
| 155 | + "-d", |
| 156 | + OutputDirectoryPatch.getOutputDirectory(), |
| 157 | + "-configfailurepolicy", |
| 158 | + "continue", |
| 159 | + getPathToResource("testng-configfailure.xml") |
| 160 | + }; |
| 161 | + TestListenerAdapter tla = new TestListenerAdapter(); |
| 162 | + TestNG.privateMain(argv, tla); |
| 163 | + |
| 164 | + verify(tla, 2, 0, 0); |
| 165 | + } |
| 166 | + |
| 167 | + private void verify( |
| 168 | + TestListenerAdapter tla, |
| 169 | + int configurationFailures, |
| 170 | + int configurationSkips, |
| 171 | + int skippedTests) { |
| 172 | + assertEquals( |
| 173 | + tla.getConfigurationFailures().size(), |
| 174 | + configurationFailures, |
| 175 | + "wrong number of configuration failures"); |
| 176 | + assertEquals( |
| 177 | + tla.getConfigurationSkips().size(), |
| 178 | + configurationSkips, |
| 179 | + "wrong number of configuration skips"); |
| 180 | + assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests"); |
| 181 | + } |
| 182 | +} |
0 commit comments