From f8783afde530d147edbb90baa8dd9f735dff02ec Mon Sep 17 00:00:00 2001 From: bj-9527 Date: Fri, 18 Feb 2022 10:41:42 +0800 Subject: [PATCH] beforeConfiguration() listener method should be invoked for skipped configurations as well Fixes #2729 --- CHANGES.txt | 1 + .../internal/invokers/ConfigInvoker.java | 1 + .../ConfigurationListenerTest.java | 9 +++++++++ .../issue2729/BeforeConfigSampleListener.java | 16 +++++++++++++++ .../issue2729/BeforeConfigTestSample.java | 20 +++++++++++++++++++ .../listeners/ConfigurationListenerTest.java | 2 +- ...ListenerInvocationDefaultBehaviorTest.java | 1 + ...istenerInvocationDisabledBehaviorTest.java | 1 + 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 testng-core/src/test/java/test/configuration/issue2729/BeforeConfigSampleListener.java create mode 100644 testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java diff --git a/CHANGES.txt b/CHANGES.txt index b3377aea74..4556cc3a23 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ Current 7.6.0 +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) Fixed: GITHUB-2704: IHookable and IConfigurable callback discrepancy (Krishnan Mahadevan) diff --git a/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java b/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java index 52411e8a3d..a778b2111d 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java @@ -277,6 +277,7 @@ public void invokeConfigurations(ConfigMethodArguments arguments) { && !alwaysRun) { log(3, "Skipping " + Utils.detailedMethodName(tm, true)); InvokedMethod invokedMethod = new InvokedMethod(System.currentTimeMillis(), testResult); + runConfigurationListeners(testResult, arguments.getTestMethod(), true /* before */); runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult); testResult.setEndMillis(testResult.getStartMillis()); testResult.setStatus(ITestResult.SKIP); diff --git a/testng-core/src/test/java/test/configuration/ConfigurationListenerTest.java b/testng-core/src/test/java/test/configuration/ConfigurationListenerTest.java index ffca620db7..0274975297 100644 --- a/testng-core/src/test/java/test/configuration/ConfigurationListenerTest.java +++ b/testng-core/src/test/java/test/configuration/ConfigurationListenerTest.java @@ -4,6 +4,8 @@ import org.testng.TestNG; import org.testng.annotations.Test; import test.SimpleBaseTest; +import test.configuration.issue2729.BeforeConfigSampleListener; +import test.configuration.issue2729.BeforeConfigTestSample; public class ConfigurationListenerTest extends SimpleBaseTest { @@ -14,4 +16,11 @@ public void listenerShouldBeCalled() { tng.run(); Assert.assertTrue(ConfigurationListenerSampleTest.m_passed); } + + @Test(description = "github 2729") + public void beforeConfigShouldExecutedForSkippedConfig() { + TestNG tng = create(BeforeConfigTestSample.class); + tng.run(); + Assert.assertEquals(BeforeConfigSampleListener.count, 2); + } } diff --git a/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigSampleListener.java b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigSampleListener.java new file mode 100644 index 0000000000..bfa4018330 --- /dev/null +++ b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigSampleListener.java @@ -0,0 +1,16 @@ +package test.configuration.issue2729; + +import org.testng.IConfigurationListener; +import org.testng.IInvokedMethodListener; +import org.testng.ITestListener; +import org.testng.ITestResult; + +public class BeforeConfigSampleListener + implements ITestListener, IInvokedMethodListener, IConfigurationListener { + public static int count = 0; + + @Override + public void beforeConfiguration(ITestResult testResult) { + count++; + } +} diff --git a/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java new file mode 100644 index 0000000000..ad73df8199 --- /dev/null +++ b/testng-core/src/test/java/test/configuration/issue2729/BeforeConfigTestSample.java @@ -0,0 +1,20 @@ +package test.configuration.issue2729; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners({BeforeConfigSampleListener.class}) +public class BeforeConfigTestSample { + @BeforeClass + public void beforeClass() { + int i = 5 / 0; + } + + @BeforeMethod + public void beforeMethod() {} + + @Test + public void sampleTest() {} +} diff --git a/testng-core/src/test/java/test/listeners/ConfigurationListenerTest.java b/testng-core/src/test/java/test/listeners/ConfigurationListenerTest.java index 4cdb958565..5a6a180608 100644 --- a/testng-core/src/test/java/test/listeners/ConfigurationListenerTest.java +++ b/testng-core/src/test/java/test/listeners/ConfigurationListenerTest.java @@ -56,6 +56,6 @@ public void shouldFail() { @Test public void shouldSkip() { - runTest(ConfigurationListenerSkipSampleTest.class, 1 + 5 + 7); // fail + skip + runTest(ConfigurationListenerSkipSampleTest.class, 1 + 1 + 5 + 7); // fail + skip } } diff --git a/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationDefaultBehaviorTest.java b/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationDefaultBehaviorTest.java index 49dda3add3..bf62523722 100644 --- a/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationDefaultBehaviorTest.java +++ b/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationDefaultBehaviorTest.java @@ -129,6 +129,7 @@ public void testOrderHasOnlyFailedAndSkippedConfigAndSkippedTestMethod() { IINVOKEDMETHODLISTENER_AFTER_INVOCATION, IINVOKEDMETHODLISTENER_AFTER_INVOCATION_WITH_CONTEXT, ICONFIGURATIONLISTENER_ON_CONFIGURATION_FAILURE, + ICONFIGURATIONLISTENER_BEFORE_CONFIGURATION, IINVOKEDMETHODLISTENER_BEFORE_INVOCATION, IINVOKEDMETHODLISTENER_BEFORE_INVOCATION_WITH_CONTEXT, IINVOKEDMETHODLISTENER_AFTER_INVOCATION, diff --git a/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationListenerInvocationDisabledBehaviorTest.java b/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationListenerInvocationDisabledBehaviorTest.java index e67b83c93c..73c9b0c171 100644 --- a/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationListenerInvocationDisabledBehaviorTest.java +++ b/testng-core/src/test/java/test/listeners/ordering/ListenerInvocationListenerInvocationDisabledBehaviorTest.java @@ -126,6 +126,7 @@ public void testOrderHasOnlyFailedAndSkippedConfigAndSkippedTestMethod() { IINVOKEDMETHODLISTENER_AFTER_INVOCATION, IINVOKEDMETHODLISTENER_AFTER_INVOCATION_WITH_CONTEXT, ICONFIGURATIONLISTENER_ON_CONFIGURATION_FAILURE, + ICONFIGURATIONLISTENER_BEFORE_CONFIGURATION, IINVOKEDMETHODLISTENER_BEFORE_INVOCATION, IINVOKEDMETHODLISTENER_BEFORE_INVOCATION_WITH_CONTEXT, IINVOKEDMETHODLISTENER_AFTER_INVOCATION,