Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configfailurepolicy=continue only works for BeforeTest when using TestNG XML file #2735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -28,15 +27,15 @@ public Object[][] getData() {
// params - confFail, confSkip, skippedTests
new Object[] {new Class[] {ClassWithFailedBeforeClassMethod.class}, 1, 1, 1},
new Object[] {new Class[] {ClassWithFailedBeforeClassMethodAndAfterClass.class}, 1, 1, 1},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}, 2, 0, 2},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}, 1, 1, 2},
new Object[] {
new Class[] {ClassWithFailedBeforeClassMethodAndBeforeMethodAfterMethodAfterClass.class},
1,
3,
1
},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleInvocations.class}, 4, 0, 4},
new Object[] {new Class[] {ExtendsClassWithFailedBeforeMethod.class}, 2, 2, 2},
new Object[] {new Class[] {ClassWithFailedBeforeMethodAndMultipleInvocations.class}, 1, 3, 4},
new Object[] {new Class[] {ExtendsClassWithFailedBeforeMethod.class}, 1, 3, 2},
new Object[] {new Class[] {ExtendsClassWithFailedBeforeClassMethod.class}, 1, 2, 2},
new Object[] {
new Class[] {
Expand All @@ -49,7 +48,7 @@ public Object[][] getData() {
new Object[] {new Class[] {ClassWithSkippingBeforeMethod.class}, 0, 1, 1},
new Object[] {new Class[] {FactoryClassWithFailedBeforeMethod.class}, 2, 0, 2},
new Object[] {
new Class[] {FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class}, 8, 0, 8
new Class[] {FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class}, 2, 6, 8
},
new Object[] {new Class[] {FactoryClassWithFailedBeforeClassMethod.class}, 2, 2, 2},
};
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I have a concern.

The documentation says this Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @Before* method fails. Default behavior is skip.

But here we are trying to move forward with trying to run the next lower level configuration even when the earlier higher order one failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @before* method fails. Default behavior is skip.

In my opinion, the default behavior is skip because of that the default config policy is set to FailurePolicy DEFAULT_CONFIG_FAILURE_POLICY = FailurePolicy.SKIP; , I'm not sure the description for Continue, will look up in document.

@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() {}
}
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