Skip to content

Commit

Permalink
Parameter alwaysRun=true for before-methods forces execution of those…
Browse files Browse the repository at this point in the history
… methods

Closes testng-team#1622
  • Loading branch information
krmahadevan committed Nov 30, 2017
1 parent f634d0a commit 9d5e6d7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Current
=======
Fixed: GITHUB-1622: Parameter alwaysRun=true for before-methods forces execution of those methods (Krishnan Mahadevan)
Fixed: GITHUB-1625: Null fields in parallel method tests (Krishnan Mahadevan)

6.13.1
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/testng/internal/Invoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ private void invokeConfigurations(IClass testClass,
continue;

}
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {
boolean considerFailures =
m_testContext.getSuite().getXmlSuite().getConfigFailurePolicy() != XmlSuite.FailurePolicy.CONTINUE;
boolean condition = considerFailures || !alwaysRun;
if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && condition) {
log(3, "Skipping " + Utils.detailedMethodName(tm, true));
handleConfigurationSkip(tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/test/alwaysrun/AlwaysRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.testng.TestNG;
import org.testng.annotations.Test;

import org.testng.xml.XmlSuite;
import test.SimpleBaseTest;
import testhelper.OutputDirectoryPatch;

Expand All @@ -18,6 +19,7 @@ public void withAlwaysRunAfter() {
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(new Class[] { AlwaysRunAfter1.class });
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.run();
assertTrue(AlwaysRunAfter1.success(), "afterTestMethod should have run");
}
Expand All @@ -29,6 +31,7 @@ public void withAlwaysRunAfterMethod() {
testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());
testng.setTestClasses(new Class[] { AlwaysRunAfter3.class });
testng.addListener(tla);
testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
testng.run();
assertTrue(AlwaysRunAfter3.success(), "afterMethod should have run");
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/test/configuration/github1622/Issue1622Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test.configuration.github1622;

import org.assertj.core.api.Assertions;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import test.InvokedMethodNameListener;
import test.SimpleBaseTest;

public class Issue1622Test extends SimpleBaseTest {
@Test(dataProvider = "dp")
public void testWithoutGroups(XmlSuite.FailurePolicy failurePolicy, String ...expected) {
TestNG testNG = create(TestClassWithNoGroups.class);
testNG.setConfigFailurePolicy(failurePolicy);
InvokedMethodNameListener listener = new InvokedMethodNameListener();
testNG.addListener((ITestNGListener) listener);
testNG.run();
Assertions.assertThat(listener.getInvokedMethodNames()).contains(expected);
}

@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][] {
{XmlSuite.FailurePolicy.SKIP, "failedBeforeSuite"},
{XmlSuite.FailurePolicy.CONTINUE, "failedBeforeSuite", "beforeTest", "beforeClass", "beforeMethod"}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package test.configuration.github1622;

import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import test.InvokedMethodNameListener;
import test.SimpleBaseTest;

public class TestClassWithNoGroups {

@BeforeSuite
public void failedBeforeSuite() {
throw new RuntimeException();
}

@BeforeTest(alwaysRun = true)
public void beforeTest() {
throw new RuntimeException();
}

@BeforeClass(alwaysRun = true)
public void beforeClass() {
throw new RuntimeException();
}

@BeforeMethod(alwaysRun = true)
public void beforeMethod() {
throw new RuntimeException();
}

@Test
public void testMethod() {
System.out.println("I'm testMethod");
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@
<class name="test.configuration.SingleConfigurationTest" />
<class name="test.configuration.BeforeClassWithDisabledTest" />
<class name="test.configuration.github1625.TestRunnerIssue1625"/>
<class name="test.configuration.github1622.Issue1622Test"/>
</classes>
</test>

Expand Down

0 comments on commit 9d5e6d7

Please sign in to comment.