From d2aa85c48ed565955ead2a3168fe4aec4770b61e Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Thu, 30 Nov 2017 09:36:40 +0530 Subject: [PATCH] Parameter alwaysRun=true for before-methods forces execution of those methods Closes #1622 --- CHANGES.txt | 1 + .../java/org/testng/internal/Invoker.java | 5 ++- .../java/test/alwaysrun/AlwaysRunTest.java | 3 ++ .../github1622/Issue1622Test.java | 30 ++++++++++++++ .../github1622/TestClassWithNoGroups.java | 41 +++++++++++++++++++ src/test/resources/testng.xml | 5 ++- 6 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/test/java/test/configuration/github1622/Issue1622Test.java create mode 100644 src/test/java/test/configuration/github1622/TestClassWithNoGroups.java diff --git a/CHANGES.txt b/CHANGES.txt index c57f047c79..0229bc33ec 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-1622: Parameter alwaysRun=true for before-methods forces execution of those methods (Krishnan Mahadevan) Fixed: GITHUB-1893: Streamline invocation of "init" method within TestResult to be private (Krishnan Mahadevan) Fixed: GITHUB-1892: Configurable InvokedMethodListener (Krishnan Mahadevan) Fixed: GITHUB-435: Apply at suite level to all tests (Siegmar Alber) diff --git a/src/main/java/org/testng/internal/Invoker.java b/src/main/java/org/testng/internal/Invoker.java index df8d3183d8..a897a35c07 100644 --- a/src/main/java/org/testng/internal/Invoker.java +++ b/src/main/java/org/testng/internal/Invoker.java @@ -201,7 +201,10 @@ private void invokeConfigurations( log(3, "Skipping " + Utils.detailedMethodName(tm, true) + " because it is not enabled"); 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)); InvokedMethod invokedMethod = new InvokedMethod(instance, tm, System.currentTimeMillis(), testResult); diff --git a/src/test/java/test/alwaysrun/AlwaysRunTest.java b/src/test/java/test/alwaysrun/AlwaysRunTest.java index ca631dbb30..50f0ddacd8 100644 --- a/src/test/java/test/alwaysrun/AlwaysRunTest.java +++ b/src/test/java/test/alwaysrun/AlwaysRunTest.java @@ -6,6 +6,7 @@ import org.testng.TestNG; import org.testng.annotations.Test; +import org.testng.xml.XmlSuite; import test.SimpleBaseTest; import testhelper.OutputDirectoryPatch; @@ -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"); } @@ -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"); } diff --git a/src/test/java/test/configuration/github1622/Issue1622Test.java b/src/test/java/test/configuration/github1622/Issue1622Test.java new file mode 100644 index 0000000000..0a56fd773a --- /dev/null +++ b/src/test/java/test/configuration/github1622/Issue1622Test.java @@ -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"} + }; + } +} diff --git a/src/test/java/test/configuration/github1622/TestClassWithNoGroups.java b/src/test/java/test/configuration/github1622/TestClassWithNoGroups.java new file mode 100644 index 0000000000..f3df2e8c27 --- /dev/null +++ b/src/test/java/test/configuration/github1622/TestClassWithNoGroups.java @@ -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"); + } +} diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index b9b3922f86..a16837328c 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -183,7 +183,7 @@ - + @@ -785,7 +785,7 @@ - + @@ -812,6 +812,7 @@ +