Skip to content

Commit d2aa85c

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

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-1622: Parameter alwaysRun=true for before-methods forces execution of those methods (Krishnan Mahadevan)
23
Fixed: GITHUB-1893: Streamline invocation of "init" method within TestResult to be private (Krishnan Mahadevan)
34
Fixed: GITHUB-1892: Configurable InvokedMethodListener (Krishnan Mahadevan)
45
Fixed: GITHUB-435: Apply <packages> at suite level to all tests (Siegmar Alber)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ private void invokeConfigurations(
201201
log(3, "Skipping " + Utils.detailedMethodName(tm, true) + " because it is not enabled");
202202
continue;
203203
}
204-
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {
204+
boolean considerFailures =
205+
m_testContext.getSuite().getXmlSuite().getConfigFailurePolicy() != XmlSuite.FailurePolicy.CONTINUE;
206+
boolean condition = considerFailures || !alwaysRun;
207+
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && condition) {
205208
log(3, "Skipping " + Utils.detailedMethodName(tm, true));
206209
InvokedMethod invokedMethod =
207210
new InvokedMethod(instance, tm, System.currentTimeMillis(), testResult);

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

Lines changed: 3 additions & 0 deletions
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
}
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 41 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
<class name="test.factory.github328.GitHub328Test" />
184184
<class name="test.factory.issue1745.Github1745Test"/>
185185
<class name="test.objectfactory.github1827.GitHub1827Test"/>
186-
<class name="test.github1490.VerifyDataProviderListener"/>
186+
<class name="test.github1490.VerifyDataProviderListener"/>
187187
<class name="test.methodselection.MethodSelectionTest"/>
188188
<class name="test.beforegroups.BeforeGroupsTest"/>
189189
<class name="test.aftergroups.issue1880.IssueTest"/>
@@ -785,7 +785,7 @@
785785
<class name="test.sanitycheck.CheckSuiteNamesTest" />
786786
</classes>
787787
</test>
788-
788+
789789
<test name="TestNames">
790790
<classes>
791791
<class name="org.testng.xml.internal.TestNamesMatcherTest"/>
@@ -812,6 +812,7 @@
812812
<class name="test.configuration.BeforeClassWithDisabledTest" />
813813
<class name="test.configuration.github1625.TestRunnerIssue1625"/>
814814
<class name="test.configuration.issue1753.IssueTest"/>
815+
<class name="test.configuration.github1622.Issue1622Test"/>
815816
</classes>
816817
</test>
817818

0 commit comments

Comments
 (0)