From 66062ab3941e53ac02029e7c87779bc43c11d0ce Mon Sep 17 00:00:00 2001 From: Oleksandr Kulychok Date: Sat, 23 Nov 2019 05:45:49 +0200 Subject: [PATCH] Fix: alwaysRun should not force invocation of Before-methods Resolves cbeust/testng#1622 --- CHANGES.txt | 1 + .../org/testng/internal/ConfigInvoker.java | 20 ++++++++----------- .../org/testng/internal/MethodHelper.java | 16 +++++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9ea7cad900..40e2225c30 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 (Oleksandr Kulychok) Fixed: GITHUB-2180: Write scenario details for Retried tests (Devendra Raju K) Fixed: GITHUB-2124: JUnit Report should contain the output of org.testng.Reporter (Krishnan Mahadevan) Fixed: GITHUB-2171: Ability to embed attachments and make them available on TestNG XML report (Krishnan Mahadevan) diff --git a/src/main/java/org/testng/internal/ConfigInvoker.java b/src/main/java/org/testng/internal/ConfigInvoker.java index 5565d59f03..0f1aaee2fb 100644 --- a/src/main/java/org/testng/internal/ConfigInvoker.java +++ b/src/main/java/org/testng/internal/ConfigInvoker.java @@ -239,26 +239,22 @@ public void invokeConfigurations(ConfigMethodArguments arguments) { // - the test is enabled and // - the Configuration method belongs to the same class or a parent configurationAnnotation = AnnotationHelper.findConfiguration(annotationFinder(), method); - boolean alwaysRun = MethodHelper.isAlwaysRun(configurationAnnotation); - boolean canProcessMethod = - MethodHelper.isEnabled(objectClass, annotationFinder()) || alwaysRun; - if (!canProcessMethod) { - log( - 3, - "Skipping " - + Utils.detailedMethodName(tm, true) - + " because " - + objectClass.getName() - + " is not enabled"); + + if (!MethodHelper.isEnabled(objectClass, annotationFinder())) { + log(3, "Skipping " + Utils.detailedMethodName(tm, true) + " because class " + + objectClass.getName() + " is not enabled"); continue; } if (MethodHelper.isDisabled(configurationAnnotation)) { log(3, "Skipping " + Utils.detailedMethodName(tm, true) + " because it is not enabled"); continue; } + + boolean forceRun = MethodHelper.isAlwaysRun(configurationAnnotation) && + MethodHelper.isAfterMethod(configurationAnnotation); if (hasConfigurationFailureFor(arguments.getTestMethod(), tm.getGroups() , arguments.getTestClass(), - arguments.getInstance()) && !alwaysRun) { + arguments.getInstance()) && !forceRun) { log(3, "Skipping " + Utils.detailedMethodName(tm, true)); InvokedMethod invokedMethod = new InvokedMethod(arguments.getInstance(), tm, System.currentTimeMillis(), testResult); diff --git a/src/main/java/org/testng/internal/MethodHelper.java b/src/main/java/org/testng/internal/MethodHelper.java index a3f67263f2..b0aac689a7 100644 --- a/src/main/java/org/testng/internal/MethodHelper.java +++ b/src/main/java/org/testng/internal/MethodHelper.java @@ -226,6 +226,22 @@ static boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) { return alwaysRun; } + /** + * @return true when configurationAnnotation belongs to {@code @After...} method + */ + static boolean isAfterMethod(IConfigurationAnnotation configurationAnnotation) { + if (null == configurationAnnotation) { + return false; + } + + return ((configurationAnnotation.getAfterSuite() + || configurationAnnotation.getAfterTest() + || configurationAnnotation.getAfterTestClass() + || configurationAnnotation.getAfterTestMethod() + || configurationAnnotation.getAfterGroups().length != 0)); + } + + /** Extracts the unique list of ITestNGMethods. */ public static List uniqueMethodList(Collection> methods) { Set resultSet = Sets.newHashSet();