methods() {
return methods_;
}
/**
* Mixed mode autodetects the type of current test and run it with appropriate runner.
*
* Default is {@code false}
*
* @param isMixed {@code true} or {@code false}
* @return this operation instance
*/
public TestNgOperation mixed(Boolean isMixed) {
options_.put("-mixed", String.valueOf(isMixed));
return this;
}
/**
* The list of {@code .class} files or class names implementing {@code ITestRunnerFactory}.
*
* A fully qualified class name that implements {@code org.testng.ITestObjectFactory} which can be used to create
* test class and listener instances.
*
* @param factory one or more factory
* @return this operation instance
* @see #objectFactory(Collection) #objectFactory(Collection)
*/
public TestNgOperation objectFactory(String... factory) {
return objectFactory(List.of(factory));
}
/**
* The list of {@code .class} files or class names implementing {@code ITestRunnerFactory}.
*
* A fully qualified class name that implements {@code org.testng.ITestObjectFactory} which can be used to create
* test class and listener instances.
*
* @param factory the list of factories
* @return this operation instance
* @see #objectFactory(String...) #objectFactory(String...)
*/
public TestNgOperation objectFactory(Collection factory) {
options_.put("-objectfactory", String.join(",", factory.stream().filter(this::isNotBlank).toList()));
return this;
}
/**
* Returns the run options.
*
* @return the map of run options
*/
public Map options() {
return options_;
}
/**
* The list of fully qualified class names of listeners that should be skipped from being wired in via
* Service Loaders.
*
* @param method one or more methods
* @return this operation instance
* @see #overrideIncludedMethods(Collection) #overrideIncludedMethods(Collection)
*/
public TestNgOperation overrideIncludedMethods(String... method) {
return overrideIncludedMethods(List.of(method));
}
/**
* The list of fully qualified class names of listeners that should be skipped from being wired in via
* Service Loaders.
*
* @param method the list of methods
* @return this operation instance
* @see #overrideIncludedMethods(String...) #overrideIncludedMethods(String...)
*/
public TestNgOperation overrideIncludedMethods(Collection method) {
options_.put("-overrideincludedmethods", String.join(",", method.stream().filter(this::isNotBlank).toList()));
return this;
}
/**
* The list of packages to include in this test.
* If the package name ends with .* then subpackages are included too.
* Required if no {@link #suites(String... suites)} specified.
*
* For example: {@code "com.example", "test.sample.*"}
*
* @param name one or more names
* @return this operation instance
* @see #packages(Collection) #packages(Collection)
*/
public TestNgOperation packages(String... name) {
return packages(List.of(name));
}
/**
* The list of packages to include in this test.
* If the package name ends with .* then subpackages are included too.
* Required if no {@link #suites(String... suites)} specified.
*
* For example: {@code "com.example", "test.sample.*"}
*
* @param name the list of names
* @return this operation instance
* @see #packages(String...) #packages(String...)
*/
public TestNgOperation packages(Collection name) {
packages_.addAll(name.stream().filter(this::isNotBlank).toList());
return this;
}
/**
* Returns the suite packages to run.
*
* @return the set of packages
*/
public Set packages() {
return packages_;
}
/**
* If specified, sets the default mechanism used to determine how to use parallel threads when running tests.
* If not set, default mechanism is not to use parallel threads at all.
* This can be overridden in the suite definition.
*
* @param mechanism the mechanism
* @return this operation instance
* @see Parallel
*/
public TestNgOperation parallel(Parallel mechanism) {
options_.put("-parallel", mechanism.name().toLowerCase(Locale.getDefault()));
return this;
}
/**
* Specifies the port number.
*
* @param port the port
* @return this operation instance
*/
public TestNgOperation port(int port) {
if (port >= 1) {
options_.put("-port", String.valueOf(port));
}
return this;
}
/**
* Should TestNG consider failures in Data Providers as test failures.
*
* Default is {@code false}
*
* @param isPropagateDataProviderFailure {@code true} or {@code false}
* @return this operation instance
*/
public TestNgOperation propagateDataProviderFailureAsTestFailure(Boolean isPropagateDataProviderFailure) {
options_.put("-propagateDataProviderFailureAsTestFailure", String.valueOf(isPropagateDataProviderFailure));
return this;
}
/**
* Specifies the extended configuration for custom report listener.
*
* @param reporter the reporter
* @return this operation instance
*/
public TestNgOperation reporter(String reporter) {
if (isNotBlank(reporter)) {
options_.put("-reporter", reporter);
}
return this;
}
/**
* Should TestNG use a global Shared ThreadPool (At suite level) for running data providers.
*
* @param shareThreadPoolForDataProviders {@code true} or {@code false}
* @return this operation instance
*/
public TestNgOperation shareThreadPoolForDataProviders(boolean shareThreadPoolForDataProviders) {
if (shareThreadPoolForDataProviders) {
options_.put("-shareThreadPoolForDataProviders", "true");
}
return this;
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory one or more directories
* @return this operation instance
* @see #sourceDir(Collection)
*/
public TestNgOperation sourceDir(String... directory) {
return sourceDir(List.of(directory));
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory one or more directories
* @return this operation instance
* @see #sourceDirFiles(Collection)
*/
public TestNgOperation sourceDir(File... directory) {
return sourceDirFiles(List.of(directory));
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory one or more directories
* @return this operation instance
* @see #sourceDirPaths(Collection)
*/
public TestNgOperation sourceDir(Path... directory) {
return sourceDirPaths(List.of(directory));
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory the list of directories
* @return this operation instance
* @see #sourceDir(String...)
*/
public TestNgOperation sourceDir(Collection directory) {
options_.put("-sourcedir", String.join(";", directory.stream().filter(this::isNotBlank).toList()));
return this;
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory the list of directories
* @return this operation instance
* @see #sourceDir(File...)
*/
public TestNgOperation sourceDirFiles(Collection directory) {
return sourceDir(directory.stream().map(File::getAbsolutePath).toList());
}
/**
* The directories where your javadoc annotated test sources are. This option is only necessary
* if you are using javadoc type annotations. (e.g. {@code "src/test"} or
* {@code "src/test/org/testng/eclipse-plugin", "src/test/org/testng/testng"}).
*
* @param directory the list of directories
* @return this operation instance
* @see #sourceDir(Path...)
*/
public TestNgOperation sourceDirPaths(Collection directory) {
return sourceDirFiles(directory.stream().map(Path::toFile).toList());
}
/**
* Specifies the List of fully qualified class names of listeners that should be skipped from being wired in via
* Service Loaders.
*
* @param listenerToSkip the listeners to skip
* @return this operation instance
* @see #spiListenersToSkip(Collection) #spiListenersToSkip(Collection)
*/
public TestNgOperation spiListenersToSkip(String... listenerToSkip) {
return spiListenersToSkip(List.of(listenerToSkip));
}
/**
* Specifies the List of fully qualified class names of listeners that should be skipped from being wired in via
* Service Loaders.
*
* @param listenerToSkip the listeners to skip
* @return this operation instance
* @see #spiListenersToSkip(String...) #spiListenersToSkip(String...)
*/
public TestNgOperation spiListenersToSkip(Collection listenerToSkip) {
options_.put("-spilistenerstoskip",
String.join(",", listenerToSkip.stream().filter(this::isNotBlank).toList()));
return this;
}
/**
* This specifies the default name of the test suite, if not specified in the suite definition file or source code.
* This option is ignored if the {@code suite.xml} file or the source code specifies a different suite name.
*
* @param name the name
* @return this operation instance
*/
public TestNgOperation suiteName(String name) {
if (isNotBlank(name)) {
options_.put("-suitename", '"' + name + '"');
}
return this;
}
/**
* Specifies the size of the thread pool to use to run suites.
* Required if no {@link #packages(String...)} specified.
*
* @param poolSize the pool size
* @return this operation instance
*/
public TestNgOperation suiteThreadPoolSize(int poolSize) {
if (poolSize >= 0) {
options_.put("-suitethreadpoolsize", String.valueOf(poolSize));
}
return this;
}
/**
* Specifies the suites to run.
*
* For example: {@code "testng.xml", "testng2.xml"}
*
* @param suite one or more suites
* @return this operation instance
* @see #suites(Collection) #suites(Collection)
*/
public TestNgOperation suites(String... suite) {
return suites(List.of(suite));
}
/**
* Specifies the suites to run.
*
* For example: {@code "testng.xml", "testng2.xml"}
*
* @param suite the list of suites
* @return this operation instance
* @see #suites(String...) #suites(String...)
*/
public TestNgOperation suites(Collection suite) {
suites_.addAll(suite.stream().filter(this::isNotBlank).toList());
return this;
}
/**
* Returns the suites to run.
*
* @return the set of suites
*/
public Set suites() {
return suites_;
}
/**
* Create a test file and delete it on exit.
*
* @return this operation instance
*/
private File tempFile() throws IOException {
var temp = File.createTempFile("testng", ".xml");
temp.deleteOnExit();
return temp;
}
/**
* Specifies the list of class files.
*
* For example: {@code "org.foo.Test1","org.foo.test2"}
*
* @param aClass one or more classes
* @return this operation instance
* @see #testClass(Collection) #testClass(Collection)
*/
public TestNgOperation testClass(String... aClass) {
return testClass(List.of(aClass));
}
/**
* Specifies the list of class files.
*
* For example: {@code "org.foo.Test1","org.foo.test2"}
*
* @param aClass the list of classes
* @return this operation instance
* @see #testClass(String...) #testClass(String...)
*/
public TestNgOperation testClass(Collection aClass) {
options_.put("-testclass", String.join(",", aClass.stream().filter(this::isNotBlank).toList()));
return this;
}
/**
* Specifies the classpath entries used to run tests.
*
* @param entry one or more entries
* @return this operation instance
* @see #testClasspath(String...) #testClasspath(String...)
*/
public TestNgOperation testClasspath(String... entry) {
return testClasspath(List.of(entry));
}
/**
* Specifies the classpath entries used to run tests.
*
* @param entry the list of entries
* @return this operation instance
* @see #testClasspath(String...) #testClasspath(String...)
*/
public TestNgOperation testClasspath(Collection entry) {
testClasspath_.addAll(entry.stream().filter(this::isNotBlank).toList());
return this;
}
/**
* Returns the classpath entries used for running tests.
*
* @return the set of test classpath
*/
public Set testClasspath() {
return testClasspath_;
}
/**
* Specifies a jar file that contains test classes. If a {@code testng.xml} file is found at the root of that
* jar file, it will be used, otherwise, all the test classes found in this jar file will be considered test
* classes.
*
* @param jar the jar
* @return this operation instance
*/
public TestNgOperation testJar(String jar) {
if (isNotBlank(jar)) {
options_.put("-testjar", jar);
}
return this;
}
/**
* This specifies the default name of test, if not specified in the suite definition file or source code.
* This option is ignored if the {@code suite.xml} file or the source code specifies a different test name.
*
* @param name the name
* @return this operation instance
*/
public TestNgOperation testName(String name) {
if (isNotBlank(name)) {
options_.put("-testname", '"' + name + '"');
}
return this;
}
/**
* Only tests defined in a {@code } tag matching one of these names will be run.
*
* @param name one or more names
* @return this operation instance
* @see #testNames(Collection) #testNames(Collection)
*/
public TestNgOperation testNames(String... name) {
return testNames(List.of(name));
}
/**
* Only tests defined in a {@code } tag matching one of these names will be run.
*
* @param name the list of names
* @return this operation instance
* @see #testName(String) #testName(String)
*/
public TestNgOperation testNames(Collection name) {
options_.put("-testnames",
name.stream().filter(this::isNotBlank).map(s -> '"' + s + '"').collect(Collectors.joining(",")));
return this;
}
/**
* Specifies the factory used to create tests.
*
* @param factory the factory
* @return this operation instance
*/
public TestNgOperation testRunFactory(String factory) {
if (isNotBlank(factory)) {
options_.put("-testrunfactory", factory);
}
return this;
}
/**
* This sets the default maximum number of threads to use for running tests in parallel. It will only take effect
* if the parallel mode has been selected (for example, with the {@link #parallel(Parallel) parallel} option).
* This can be overridden in the suite definition.
*
* @param count the count
* @return this operation instance
*/
public TestNgOperation threadCount(int count) {
if (count >= 0) {
options_.put("-threadcount", String.valueOf(count));
}
return this;
}
/**
* Specifies the thread pool executor factory implementation that TestNG should use.
*
* @param factoryClass the factory class
* @return this operation instance
*/
public TestNgOperation threadPoolFactoryClass(String factoryClass) {
if (isNotBlank(factoryClass)) {
options_.put("-threadpoolfactoryclass", factoryClass);
}
return this;
}
/**
* Whether to use the default listeners
*
* Default is {@code true}
*
* @param isDefaultListener {@code true} or {@code false}
* @return this operation instance
*/
public TestNgOperation useDefaultListeners(Boolean isDefaultListener) {
options_.put("-usedefaultlisteners", String.valueOf(isDefaultListener));
return this;
}
/**
* Should TestNG use a global Shared ThreadPool (At suite level) for running regular and data driven tests.
*
* @param useGlobalThreadPool {@code true} or {@code false}
* @return this operation instance
*/
public TestNgOperation useGlobalThreadPool(boolean useGlobalThreadPool) {
if (useGlobalThreadPool) {
options_.put("-useGlobalThreadPool", "true");
}
return this;
}
/**
* Set the Level of verbosity.
*
* @param level the level
* @return this operation instance
* @see #log(int) #log(int)
*/
public TestNgOperation verbose(int level) {
if (level >= 0) {
options_.put("-verbose", String.valueOf(level));
}
return this;
}
private File writeDefaultSuite() throws IOException {
var temp = tempFile();
try (var bufWriter = Files.newBufferedWriter(Paths.get(temp.getPath()))) {
bufWriter.write("" +
"" +
"" +
"" +
"");
for (var p : packages_) {
bufWriter.write(String.format("", p));
}
bufWriter.write("");
}
return temp;
}
/**
* This attribute should contain the path to a valid XML file inside the test jar
* (e.g. {@code "resources/testng.xml"}). The default is {@code testng.xml}, which means a file called
* {@code testng.xml} at the root of the jar file. This option will be ignored unless a test jar is specified.
*
* @param path the path
* @return this operation instance
*/
public TestNgOperation xmlPathInJar(String path) {
if (isNotBlank(path)) {
options_.put("-xmlpathinjar", path);
}
return this;
}
/**
* This attribute should contain the path to a valid XML file inside the test jar
* (e.g. {@code "resources/testng.xml"}). The default is {@code testng.xml}, which means a file called
* {@code testng.xml} at the root of the jar file. This option will be ignored unless a test jar is specified.
*
* @param path the path
* @return this operation instance
*/
public TestNgOperation xmlPathInJar(File path) {
return xmlPathInJar(path.getAbsolutePath());
}
/**
* This attribute should contain the path to a valid XML file inside the test jar
* (e.g. {@code "resources/testng.xml"}). The default is {@code testng.xml}, which means a file called
* {@code testng.xml} at the root of the jar file. This option will be ignored unless a test jar is specified.
*
* @param path the path
* @return this operation instance
*/
public TestNgOperation xmlPathInJar(Path path) {
return xmlPathInJar(path.toFile());
}
/**
* Parallel Mechanisms
*/
public enum Parallel {
/**
* Methods mechanism.
*/
METHODS,
/**
* Tests mechanism.
*/
TESTS,
/**
* Classes mechanism.
*/
CLASSES
}
/**
* Failure Policies
*/
public enum FailurePolicy {
/**
* Skip failure policy.
*/
SKIP,
/**
* Continue failure policy.
*/
CONTINUE
}
}