-
Notifications
You must be signed in to change notification settings - Fork 3
/
SmartDirtiesSuiteListener.java
62 lines (55 loc) · 2.71 KB
/
SmartDirtiesSuiteListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.github.seregamorph.testsmartcontext.testng;
import com.github.seregamorph.testsmartcontext.SmartDirtiesContextTestExecutionListener;
import com.github.seregamorph.testsmartcontext.SmartDirtiesTestsHolder;
import com.github.seregamorph.testsmartcontext.SmartDirtiesTestsSorter;
import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.internal.RuntimeBehavior;
import org.testng.xml.XmlSuite;
/**
* See description in {@link SmartDirtiesContextTestExecutionListener}.
* <p>
* Reorders TestNG test classes in suite grouping ITs with the same context configuration to minimize number of parallel
* existing contexts.
* <p>
* For Jupiter test classes - see {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer}, for
* JUnit 4 test classes - see {@link com.github.seregamorph.testsmartcontext.SmartDirtiesPostDiscoveryFilter}.
*/
@SuppressWarnings("CodeBlock2Expr")
public class SmartDirtiesSuiteListener extends SmartDirtiesTestsHolder
implements IAlterSuiteListener, IMethodInterceptor {
@Override
public void alter(List<XmlSuite> suites) {
// dryRun is only true when called via junit5 testng-engine on discovery phase, there will be subsequent
// call of this method with dryRun=false on execution phase
if (RuntimeBehavior.isDryRun()) {
// the list of test classes is wrong, listener is called per each class as single in suite
return;
}
// TestNG needs it (otherwise reorders back to default alphabetical order)
suites.forEach(suite -> {
suite.getTests().forEach(xmlTest -> {
xmlTest.setPreserveOrder(false);
});
});
}
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
// dryRun is only true when called via junit5 testng-engine on discovery phase, there will be subsequent
// call of this method with dryRun=false on execution phase
if (RuntimeBehavior.isDryRun()) {
// the list of test classes is wrong, listener is called per each class as single in suite
return methods;
}
// this intercept method is executed by TestNG before running the suite (both IDEA and maven)
SmartDirtiesTestsSorter sorter = SmartDirtiesTestsSorter.getInstance();
List<List<Class<?>>> testClassesLists = sorter.sort(methods, methodInstance -> {
return methodInstance.getMethod().getTestClass().getRealClass();
});
SmartDirtiesTestsHolder.setTestClassesLists(testClassesLists);
return methods;
}
}