Skip to content

Commit 91a003a

Browse files
committed
FirstTimeOnlyConfig methods + Listener invocations
Closes #2961
1 parent 194d604 commit 91a003a

File tree

8 files changed

+108
-2
lines changed

8 files changed

+108
-2
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-2961: "Unexpected value: 16" error when multiple beforeMethod config methods with firstTimeOnly property run before a test (Krishnan Mahadevan)
23
Fixed: GITHUB-2904: Add location of docs Github to readme and contributions page (Mohsin Sackeer)
34
Fixed: GITHUB-2934: Parallel Dataproviders & retries causes test result count to be skewed (Krishnan Mahadevan)
45
Fixed: GITHUB-2925: Issue in ITestcontext.getAllTestMethods() with annotation @BeforeSuite (Krishnan Mahadevan)

testng-core/src/main/java/org/testng/internal/invokers/ConfigInvoker.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,18 @@ public void invokeConfigurations(ConfigMethodArguments arguments) {
317317
runConfigurationListeners(testResult, arguments.getTestMethod(), true /* before */);
318318

319319
Object newInstance = computeInstance(arguments.getInstance(), inst, tm);
320-
if (isConfigMethodEligibleForScrutiny(tm)) {
320+
boolean isFirstTimeOnlyConfigMethod = isConfigMethodEligibleForScrutiny(tm);
321+
if (isFirstTimeOnlyConfigMethod) {
321322
if (m_executedConfigMethods.add(arguments.getTestMethod())) {
322323
invokeConfigurationMethod(newInstance, tm, parameters, testResult);
323324
}
324325
} else {
325326
invokeConfigurationMethod(newInstance, tm, parameters, testResult);
326327
}
327328
copyAttributesFromNativelyInjectedTestResult(parameters, arguments.getTestMethodResult());
328-
runConfigurationListeners(testResult, arguments.getTestMethod(), false /* after */);
329+
if (!isFirstTimeOnlyConfigMethod) {
330+
runConfigurationListeners(testResult, arguments.getTestMethod(), false /* after */);
331+
}
329332
if (testResult.getStatus() == ITestResult.SKIP) {
330333
Throwable t = testResult.getThrowable();
331334
if (t != null) {

testng-core/src/test/java/test/configuration/ConfigurationTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Arrays;
66
import org.testng.Assert;
77
import org.testng.TestNG;
8+
import org.testng.annotations.DataProvider;
89
import org.testng.annotations.Test;
910
import org.testng.xml.XmlSuite;
1011
import test.configuration.issue2726.TestClassSample;
@@ -13,6 +14,8 @@
1314
import test.configuration.sample.ExternalConfigurationClassSample;
1415
import test.configuration.sample.MethodCallOrderTestSample;
1516
import test.configuration.sample.SuiteTestSample;
17+
import test.listeners.issue2961.OnlyOnceConfigurationThatFailsTestSample;
18+
import test.listeners.issue2961.OnlyOnceConfigurationThatPassesTestSample;
1619

1720
/**
1821
* Test @Configuration
@@ -53,4 +56,20 @@ public void testAfterClassCalledOnlyOnceForParallelTestMethods() {
5356
assertThat(TestClassSample.beforeLogs).hasSize(1);
5457
assertThat(TestClassSample.afterLogs).hasSize(1);
5558
}
59+
60+
@Test(description = "GITHUB-2961", dataProvider = "produceTestClasses")
61+
public void ensureFirstTimeOnlyConfigsHaveProperTestStatuses(Class<?> clazz) {
62+
TestNG testng = create(clazz);
63+
testng.setVerbose(2);
64+
testng.run();
65+
assertThat(testng.getStatus()).isZero();
66+
}
67+
68+
@DataProvider(name = "produceTestClasses")
69+
public Object[][] produceTestClasses() {
70+
return new Object[][] {
71+
{OnlyOnceConfigurationThatFailsTestSample.class},
72+
{OnlyOnceConfigurationThatPassesTestSample.class}
73+
};
74+
}
5675
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.configuration.issue2961;
2+
3+
import java.lang.reflect.Method;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
6+
import org.testng.annotations.Test;
7+
8+
public class OnlyOnceConfigurationTest extends ParentTestClass {
9+
10+
@BeforeMethod(firstTimeOnly = true)
11+
public void beforeMethodFirstTimeOnlyTestClass(Method method) {
12+
System.out.println("First Time Only Test Class " + method);
13+
}
14+
15+
@Test
16+
public void test() {
17+
System.out.println("Test passed");
18+
}
19+
20+
@AfterMethod(lastTimeOnly = true)
21+
public void afterMethodLastTimeOnly() {
22+
System.out.println("Last Time Only");
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test.configuration.issue2961;
2+
3+
import java.lang.reflect.Method;
4+
import org.testng.annotations.BeforeMethod;
5+
6+
public class ParentTestClass {
7+
@BeforeMethod(firstTimeOnly = true)
8+
public void beforeMethodFirstTimeOnlyParent(Method method) {
9+
System.out.println("First Time Only Parent " + method);
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.listeners.issue2961;
2+
3+
import java.lang.reflect.Method;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
6+
import org.testng.annotations.Test;
7+
8+
public class OnlyOnceConfigurationThatFailsTestSample extends ParentTestClassSample {
9+
10+
@BeforeMethod(firstTimeOnly = true)
11+
public void beforeMethodFirstTimeOnlyTestClass(Method method) {
12+
throw new RuntimeException("Example");
13+
}
14+
15+
@Test
16+
public void test() {}
17+
18+
@AfterMethod(lastTimeOnly = true)
19+
public void afterMethodLastTimeOnly() {}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.listeners.issue2961;
2+
3+
import java.lang.reflect.Method;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
6+
import org.testng.annotations.Test;
7+
8+
public class OnlyOnceConfigurationThatPassesTestSample extends ParentTestClassSample {
9+
10+
@BeforeMethod(firstTimeOnly = true)
11+
public void beforeMethodFirstTimeOnlyTestClass(Method method) {}
12+
13+
@Test
14+
public void test() {}
15+
16+
@AfterMethod(lastTimeOnly = true)
17+
public void afterMethodLastTimeOnly() {}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test.listeners.issue2961;
2+
3+
import java.lang.reflect.Method;
4+
import org.testng.annotations.BeforeMethod;
5+
6+
public class ParentTestClassSample {
7+
8+
@BeforeMethod(firstTimeOnly = true)
9+
public void beforeMethodFirstTimeOnlyParent(Method method) {}
10+
}

0 commit comments

Comments
 (0)