-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define A TestSuite In A Class And Not XML #2331
Comments
@opticyclic - This is an interesting proposition :) Essentially both these are accomplishing the same ask (of course with testng doing more by letting you define groups or other method selectors via a bean shell, letting your define listeners, letting you control your parallelism strategy etc.,) |
Basically the same reasons why Spring added the ability to define Beans in a class instead of only in xml. Their competitor (Guice) had it and people don't like editing xml as much. Just to be clear, I'm not asking to get rid of the xml. |
@opticyclic - While we wait to hear back from @cbeust and @juherr can you please check if something like this would work for you ?
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({TYPE})
public @interface SuiteClass {
String name();
Class<?>[] classes();
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class LocalSuiteAlterer implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
Optional<? extends Class<?>> found = suites.stream()
.flatMap(xmlSuite -> xmlSuite.getTests().stream())
.flatMap(xmlTest -> xmlTest.getXmlClasses().stream())
.map(XmlClass::getSupportClass)
.filter(cls -> cls.getAnnotation(SuiteClass.class) != null)
.findAny();
if (found.isPresent()) {
SuiteClass suiteClass = found.get().getAnnotation(SuiteClass.class);
Class<?>[] classes = suiteClass.classes();
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName(suiteClass.name() + "_suite");
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName(suiteClass.name() + "_test");
List<XmlClass> xmlClasses = Arrays.stream(classes).map(XmlClass::new).collect(Collectors.toList());
xmlTest.setXmlClasses(xmlClasses);
suites.clear();
suites.add(xmlSuite);
}
}
}
@SuiteClass(name = "CodingSuite", classes = {One.class, Two.class, Three.class})
public class RunnerTest {
} All the sample test classes ( import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class One {
@Test
public void method() {
ITestResult result = Reporter.getCurrentTestResult();
System.err.println("Executing " + result.getMethod().getQualifiedName() + "()");
}
} Caveats: |
That probably would work in the interim and is likely to be useful in projects where I can't update to a newer version. 👍 Hopefully, this means that it shouldn't be too hard to add it to core TestNG. After all @cbeust we don't want a comparison table with JUnit having a tick in a box and TestNG having a cross! :) |
The idea is good but I don't know if the 3rd party support will follow quickly. IMO, this feature is a very good excuse to rework the model around I have just some doubts about the naming because |
@opticyclic - I spent some time and built a full fledged library that now supports this outside of TestNG. Take a look at https://github.com/RationaleEmotions/sangrahah |
@juherr - Do you think we can close this issue given the fact that there are alternatives and now a full fledged library that I built which can solve this issue ? |
Feature implemented in a 3rd party |
TestNG Version
7.1.0
Expected behavior
TestNG should be able to specify the test classes with annotations just like JUnit can:
https://github.com/junit-team/junit4/wiki/aggregating-tests-in-suites
Actual behavior
Currently you have to define your suite in XML and can't use annotations in a class.
The text was updated successfully, but these errors were encountered: