Skip to content

Commit

Permalink
configfailurepolicy=continue only works for BeforeTest when using Tes…
Browse files Browse the repository at this point in the history
…tNG XML file

Fixes #2731
  • Loading branch information
bj-9527 committed Mar 1, 2022
1 parent 0438740 commit 86d924d
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Current
7.6.0
Fixed: GITHUB-2731: configfailurepolicy=continue only works for BeforeTest when using TestNG XML file (Nan Liang)
Fixed: GITHUB-2729: beforeConfiguration() listener method should be invoked for skipped configurations as well(Nan Liang)
Fixed: assertEqualsNoOrder for Collection and Iterators size check was missing (Adam Kaczmarek)
Fixed: GITHUB-2709: Testnames not working together with suites in suite (Martin Aldrin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ public void invokeConfigurations(ConfigMethodArguments arguments) {
tm.getGroups(),
arguments.getTestClass(),
arguments.getInstance())
&& !alwaysRun) {
&& !alwaysRun
&& !m_continueOnFailedConfiguration) {
log(3, "Skipping " + Utils.detailedMethodName(tm, true));
InvokedMethod invokedMethod = new InvokedMethod(System.currentTimeMillis(), testResult);
runConfigurationListeners(testResult, arguments.getTestMethod(), true /* before */);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ private ITestResult invokeMethod(
arguments.getTestMethod(),
arguments.getTestMethod().getGroups(),
arguments.getTestClass(),
arguments.getInstance())) {
arguments.getInstance())
&& suite.getConfigFailurePolicy() == XmlSuite.FailurePolicy.SKIP) {
Throwable exception =
ExceptionUtils.getExceptionDetails(m_testContext, arguments.getInstance());
ITestResult result =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package test.configurationfailurepolicy;

import static org.testng.Assert.assertEquals;
import static test.SimpleBaseTest.getPathToResource;

import org.testng.ITestContext;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import test.configurationfailurepolicy.issue2731.ConfigFailTestSample;
import testhelper.OutputDirectoryPatch;

public class FailureContinuePolicyTest {
// only if this is run from an xml file that sets this on the suite
@BeforeClass(enabled = false)
public void setupClass(ITestContext testContext) {
assertEquals(
testContext.getSuite().getXmlSuite().getConfigFailurePolicy(),
XmlSuite.FailurePolicy.CONTINUE);
}

@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][] {
// params - confFail, confSkip, skippedTests
new Object[] {new Class[] {ClassWithFailedBeforeClassMethod.class}, 1, 0, 0},
new Object[] {new Class[] {ClassWithFailedBeforeClassMethodAndAfterClass.class}, 1, 0, 0},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}, 2, 0, 0},
new Object[] {
new Class[] {ClassWithFailedBeforeClassMethodAndBeforeMethodAfterMethodAfterClass.class},
1,
0,
0
},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleInvocations.class}, 4, 0, 0},
new Object[] {new Class[] {ExtendsClassWithFailedBeforeMethod.class}, 2, 0, 0},
new Object[] {new Class[] {ExtendsClassWithFailedBeforeClassMethod.class}, 1, 0, 0},
new Object[] {
new Class[] {
ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class
},
2,
0,
0
},
new Object[] {new Class[] {ClassWithSkippingBeforeMethod.class}, 0, 1, 0},
new Object[] {new Class[] {FactoryClassWithFailedBeforeMethod.class}, 2, 0, 0},
new Object[] {
new Class[] {FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class}, 8, 0, 0
},
new Object[] {new Class[] {FactoryClassWithFailedBeforeClassMethod.class}, 2, 0, 0},
new Object[] {new Class[] {ConfigFailTestSample.class}, 4, 0, 0}
};
}

@Test(dataProvider = "dp")
public void confFailureTest(
Class[] classesUnderTest,
int configurationFailures,
int configurationSkips,
int skippedTests) {

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(classesUnderTest);
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.run();

verify(tla, configurationFailures, configurationSkips, skippedTests);
}

@Test
public void confFailureTestInvolvingGroups() {
Class[] classesUnderTest =
new Class[] {ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class};

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(classesUnderTest);
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.setGroups("group1");
testng.run();
verify(tla, 1, 0, 0);
}

@Test
public void commandLineTest_policyAsSkip() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"skip",
"-testclass",
ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName()
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 1, 1, 2);
}

@Test
public void commandLineTest_policyAsContinue() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"continue",
"-testclass",
ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName()
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 2, 0, 0);
}

@Test
public void commandLineTestWithXMLFile_policyAsSkip() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"skip",
getPathToResource("testng-configfailure.xml")
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 1, 1, 2);
}

@Test
public void commandLineTestWithXMLFile_policyAsContinue() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"continue",
getPathToResource("testng-configfailure.xml")
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 2, 0, 0);
}

private void verify(
TestListenerAdapter tla,
int configurationFailures,
int configurationSkips,
int skippedTests) {
assertEquals(
tla.getConfigurationFailures().size(),
configurationFailures,
"wrong number of configuration failures");
assertEquals(
tla.getConfigurationSkips().size(),
configurationSkips,
"wrong number of configuration skips");
assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class FailurePolicyTest {
@BeforeClass(enabled = false)
public void setupClass(ITestContext testContext) {
assertEquals(
testContext.getSuite().getXmlSuite().getConfigFailurePolicy(),
XmlSuite.FailurePolicy.CONTINUE);
testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), XmlSuite.FailurePolicy.SKIP);
}

@DataProvider(name = "dp")
Expand Down Expand Up @@ -67,7 +66,7 @@ public void confFailureTest(
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(classesUnderTest);
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.SKIP);
testng.run();

verify(tla, configurationFailures, configurationSkips, skippedTests);
Expand All @@ -83,7 +82,7 @@ public void confFailureTestInvolvingGroups() {
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(classesUnderTest);
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.SKIP);
testng.setGroups("group1");
testng.run();
verify(tla, 1, 3, 1);
Expand All @@ -108,25 +107,6 @@ public void commandLineTest_policyAsSkip() {
verify(tla, 1, 1, 2);
}

@Test
public void commandLineTest_policyAsContinue() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"continue",
"-testclass",
ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName()
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 2, 0, 2);
}

@Test
public void commandLineTestWithXMLFile_policyAsSkip() {
String[] argv =
Expand All @@ -145,24 +125,6 @@ public void commandLineTestWithXMLFile_policyAsSkip() {
verify(tla, 1, 1, 2);
}

@Test
public void commandLineTestWithXMLFile_policyAsContinue() {
String[] argv =
new String[] {
"-log",
"0",
"-d",
OutputDirectoryPatch.getOutputDirectory(),
"-configfailurepolicy",
"continue",
getPathToResource("testng-configfailure.xml")
};
TestListenerAdapter tla = new TestListenerAdapter();
TestNG.privateMain(argv, tla);

verify(tla, 2, 0, 2);
}

private void verify(
TestListenerAdapter tla,
int configurationFailures,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test.configurationfailurepolicy.issue2731;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ConfigFailTestSample {
@BeforeSuite
public void beforeSuite() {
Assert.fail("This before suite is fail");
}

@BeforeTest
public void beforeTest() {
Assert.fail("This before test is fail");
}

@BeforeClass
public void beforeClass() {
Assert.fail("This before class is fail");
}

@BeforeMethod
public void beforeMethod() {
Assert.fail("This before method is fail");
}

@Test
public void test() {}
}
12 changes: 10 additions & 2 deletions testng-core/src/test/java/test/listeners/github1602/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,27 @@ public Object[][] getData() {
"BeforeInvocation_afterMethod_STARTED",
"AfterInvocation_afterMethod_SUCCESS");
List<String> baseList =
Arrays.asList(
"BeforeInvocation_beforeMethod_STARTED",
"AfterInvocation_beforeMethod_FAILURE",
"BeforeInvocation_testMethod_STARTED",
"AfterInvocation_testMethod_SUCCESS",
"BeforeInvocation_afterMethod_STARTED");
List<String> commonSkipList =
Arrays.asList(
"BeforeInvocation_beforeMethod_STARTED",
"AfterInvocation_beforeMethod_FAILURE",
"BeforeInvocation_testMethod_SKIP",
"AfterInvocation_testMethod_SKIP",
"BeforeInvocation_afterMethod_STARTED");
"BeforeInvocation_afterMethod_STARTED",
"AfterInvocation_afterMethod_SKIP");
List<String> skipList = Lists.newArrayList(baseList);
skipList.add("AfterInvocation_afterMethod_SKIP");
List<String> failList = Lists.newArrayList(baseList);
failList.add("AfterInvocation_afterMethod_FAILURE");
return new Object[][] {
{TestClassWithPassingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, passList},
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, skipList},
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.SKIP, commonSkipList},
{TestClassWithPassingConfigsSample.class, XmlSuite.FailurePolicy.CONTINUE, passList},
{TestClassWithFailingConfigsSample.class, XmlSuite.FailurePolicy.CONTINUE, failList}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testOnStartInvokedForSkippedTests() {
"before_test_method: test1",
"after_test_method: test1",
"after_test_method: test1",
"testSkipped_test_method: test1",
"testSuccess_test_method: test1",
"testStart_test_method: test2",
"before_test_method: test2",
"before_test_method: test2",
Expand Down
1 change: 1 addition & 0 deletions testng-core/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@
<test name="ConfigFailurePolicy">
<classes>
<class name="test.configurationfailurepolicy.FailurePolicyTest" />
<class name="test.configurationfailurepolicy.FailureContinuePolicyTest"></class>
</classes>
</test>

Expand Down

0 comments on commit 86d924d

Please sign in to comment.