Skip to content

Commit 9d5e6d7

Browse files
committed
Parameter alwaysRun=true for before-methods forces execution of those methods
Closes testng-team#1622
1 parent f634d0a commit 9d5e6d7

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Current
22
=======
3+
Fixed: GITHUB-1622: Parameter alwaysRun=true for before-methods forces execution of those methods (Krishnan Mahadevan)
34
Fixed: GITHUB-1625: Null fields in parallel method tests (Krishnan Mahadevan)
45

56
6.13.1

src/main/java/org/testng/internal/Invoker.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ private void invokeConfigurations(IClass testClass,
195195
continue;
196196

197197
}
198-
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {
198+
boolean considerFailures =
199+
m_testContext.getSuite().getXmlSuite().getConfigFailurePolicy() != XmlSuite.FailurePolicy.CONTINUE;
200+
boolean condition = considerFailures || !alwaysRun;
201+
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && condition) {
199202
log(3, "Skipping " + Utils.detailedMethodName(tm, true));
200203
handleConfigurationSkip(tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);
201204
continue;

src/test/java/test/alwaysrun/AlwaysRunTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.testng.TestNG;
77
import org.testng.annotations.Test;
88

9+
import org.testng.xml.XmlSuite;
910
import test.SimpleBaseTest;
1011
import testhelper.OutputDirectoryPatch;
1112

@@ -18,6 +19,7 @@ public void withAlwaysRunAfter() {
1819
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
1920
testng.setTestClasses(new Class[] { AlwaysRunAfter1.class });
2021
testng.addListener(tla);
22+
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
2123
testng.run();
2224
assertTrue(AlwaysRunAfter1.success(), "afterTestMethod should have run");
2325
}
@@ -29,6 +31,7 @@ public void withAlwaysRunAfterMethod() {
2931
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
3032
testng.setTestClasses(new Class[] { AlwaysRunAfter3.class });
3133
testng.addListener(tla);
34+
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
3235
testng.run();
3336
assertTrue(AlwaysRunAfter3.success(), "afterMethod should have run");
3437
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package test.configuration.github1622;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.testng.ITestNGListener;
5+
import org.testng.TestNG;
6+
import org.testng.annotations.DataProvider;
7+
import org.testng.annotations.Test;
8+
import org.testng.xml.XmlSuite;
9+
import test.InvokedMethodNameListener;
10+
import test.SimpleBaseTest;
11+
12+
public class Issue1622Test extends SimpleBaseTest {
13+
@Test(dataProvider = "dp")
14+
public void testWithoutGroups(XmlSuite.FailurePolicy failurePolicy, String ...expected) {
15+
TestNG testNG = create(TestClassWithNoGroups.class);
16+
testNG.setConfigFailurePolicy(failurePolicy);
17+
InvokedMethodNameListener listener = new InvokedMethodNameListener();
18+
testNG.addListener((ITestNGListener) listener);
19+
testNG.run();
20+
Assertions.assertThat(listener.getInvokedMethodNames()).contains(expected);
21+
}
22+
23+
@DataProvider(name = "dp")
24+
public Object[][] getData() {
25+
return new Object[][] {
26+
{XmlSuite.FailurePolicy.SKIP, "failedBeforeSuite"},
27+
{XmlSuite.FailurePolicy.CONTINUE, "failedBeforeSuite", "beforeTest", "beforeClass", "beforeMethod"}
28+
};
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package test.configuration.github1622;
2+
3+
import org.testng.ITestNGListener;
4+
import org.testng.TestNG;
5+
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.BeforeMethod;
7+
import org.testng.annotations.BeforeSuite;
8+
import org.testng.annotations.BeforeTest;
9+
import org.testng.annotations.DataProvider;
10+
import org.testng.annotations.Test;
11+
import org.testng.xml.XmlSuite;
12+
import test.InvokedMethodNameListener;
13+
import test.SimpleBaseTest;
14+
15+
public class TestClassWithNoGroups {
16+
17+
@BeforeSuite
18+
public void failedBeforeSuite() {
19+
throw new RuntimeException();
20+
}
21+
22+
@BeforeTest(alwaysRun = true)
23+
public void beforeTest() {
24+
throw new RuntimeException();
25+
}
26+
27+
@BeforeClass(alwaysRun = true)
28+
public void beforeClass() {
29+
throw new RuntimeException();
30+
}
31+
32+
@BeforeMethod(alwaysRun = true)
33+
public void beforeMethod() {
34+
throw new RuntimeException();
35+
}
36+
37+
@Test
38+
public void testMethod() {
39+
System.out.println("I'm testMethod");
40+
}
41+
}

src/test/resources/testng.xml

+1
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@
783783
<class name="test.configuration.SingleConfigurationTest" />
784784
<class name="test.configuration.BeforeClassWithDisabledTest" />
785785
<class name="test.configuration.github1625.TestRunnerIssue1625"/>
786+
<class name="test.configuration.github1622.Issue1622Test"/>
786787
</classes>
787788
</test>
788789

0 commit comments

Comments
 (0)