Skip to content

Commit

Permalink
Allow @BeforeMethod when whole test suite single-threaded
Browse files Browse the repository at this point in the history
Allow `@BeforeMethod` and `@AfterMethod` on non-single-threaded test
classes when the whole suite is marked as such.
  • Loading branch information
findepi committed Oct 6, 2020
1 parent b14fc99 commit 6740ece
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite.ParallelMode;
import org.testng.xml.XmlTest;

import java.lang.reflect.Method;

Expand All @@ -33,6 +35,10 @@ public class ReportMultiThreadedBeforeOrAfterMethod
public void onBeforeClass(ITestClass testClass)
{
try {
if (!isParallel(testClass.getXmlTest())) {
return;
}

reportMultiThreadedBeforeOrAfterMethod(testClass.getRealClass());
}
catch (RuntimeException | Error e) {
Expand All @@ -44,6 +50,16 @@ public void onBeforeClass(ITestClass testClass)
}
}

private boolean isParallel(XmlTest xmlTest)
{
if (xmlTest.getThreadCount() == 1) {
return false;
}

ParallelMode parallel = xmlTest.getParallel();
return parallel.isParallel();
}

@VisibleForTesting
static void reportMultiThreadedBeforeOrAfterMethod(Class<?> testClass)
{
Expand Down

0 comments on commit 6740ece

Please sign in to comment.