Added option to set the tests classpath

This commit is contained in:
Erik C. Thauvin 2023-08-16 06:23:26 -07:00
parent 89a662a1c4
commit 65b9ea190b
8 changed files with 55 additions and 25 deletions

View file

@ -26,7 +26,8 @@ import java.util.List;
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
import static rife.bld.dependencies.Repository.RIFE2_RELEASES;
import static rife.bld.dependencies.Scope.*;
import static rife.bld.dependencies.Scope.compile;
import static rife.bld.dependencies.Scope.test;
import static rife.bld.operations.JavadocOptions.DocLinkOption.NO_MISSING;
public class TestNgOperationBuild extends Project {

View file

@ -52,6 +52,10 @@ public class TestNgOperation extends AbstractProcessOperation<TestNgOperation> {
* The suites to run.
*/
protected final List<String> suites = new ArrayList<>();
/**
* The classpath entries used for running tests.
*/
protected final List<String> testClasspath = new ArrayList<>();
private final List<String> args = new ArrayList<>();
private BaseProject project;
@ -118,9 +122,15 @@ public class TestNgOperation extends AbstractProcessOperation<TestNgOperation> {
args.clear();
args.add(javaTool());
args.add("-cp");
args.add(String.format("%s:%s:%s", Path.of(project.libTestDirectory().getPath(), "*"),
project.buildMainDirectory(), project.buildTestDirectory()));
if (testClasspath.isEmpty()) {
args.add(String.format("%s:%s:%s", Path.of(project.libTestDirectory().getPath(), "*"),
project.buildMainDirectory(), project.buildTestDirectory()));
} else {
args.add(String.join(":", testClasspath));
}
args.add("org.testng.TestNG");
options.forEach((k, v) -> {
@ -406,6 +416,14 @@ public class TestNgOperation extends AbstractProcessOperation<TestNgOperation> {
return this;
}
/**
* Specifies the classpath entries used to run tests.
*/
public TestNgOperation testClasspath(String... entry) {
testClasspath.addAll(Arrays.stream(entry).toList());
return this;
}
/**
* 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

View file

@ -77,32 +77,42 @@ class TestNgOperationTest {
assertThatThrownBy(() ->
new TestNgOperation().fromProject(new Project())
.testClass("rife.bld.extension.TestNgSimpleTest")
.execute()).isInstanceOf(ExitStatusException.class);
.execute())
.as("with testClass").isInstanceOf(ExitStatusException.class);
assertThatThrownBy(() ->
new TestNgOperation().fromProject(new Project())
.suites("src/test/resources/testng.xml")
.execute()).isInstanceOf(ExitStatusException.class);
.execute())
.as("with suites").isInstanceOf(ExitStatusException.class);
assertThatCode(() ->
new TestNgOperation().fromProject(new Project())
.testClass("rife.bld.extension.TestNgSimpleTest")
.methods("rife.bld.extension.TestNgSimpleTest.verifyHello")
.execute())
.doesNotThrowAnyException();
.as("with methods").doesNotThrowAnyException();
assertThatCode(() ->
new TestNgOperation().fromProject(new Project())
.suites("src/test/resources/testng2.xml")
.execute())
.doesNotThrowAnyException();
.as("suite 2").doesNotThrowAnyException();
assertThatCode(() ->
new TestNgOperation().fromProject(new Project())
.suites("src/test/resources/testng3.xml")
.log(2)
.execute())
.doesNotThrowAnyException();
.as("suite 3").doesNotThrowAnyException();
assertThatCode(() ->
new TestNgOperation().fromProject(new Project())
.suites("src/test/resources/testng3.xml")
.testClasspath("lib/test/*", "build/main", "build/test")
.log(2)
.execute())
.as("with run classpath").doesNotThrowAnyException();
}
@Test

View file

@ -30,13 +30,13 @@ class TestNgSimpleTest {
new TestNgSimpleTest().verifyHello();
}
@Test
void verifyHello() {
Assert.assertTrue(true);
}
@Test
void testFail() {
Assert.fail("failed");
}
@Test
void verifyHello() {
Assert.assertTrue(true);
}
}

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<suite name="test suite 1" verbose="2" >
<test name="simple test" >
<classes>
<class name="rife.bld.extension.TestNgSimpleTest" />
</classes>
</test>
<suite name="test suite 1" verbose="2">
<test name="simple test">
<classes>
<class name="rife.bld.extension.TestNgSimpleTest"/>
</classes>
</test>
</suite>

View file

@ -1,10 +1,10 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test suite 2" verbose="1" >
<test name="exclude fail" >
<suite name="test suite 2" verbose="1">
<test name="exclude fail">
<classes>
<class name="rife.bld.extension.TestNgSimpleTest">
<methods>
<exclude name="testFail" />
<exclude name="testFail"/>
</methods>
</class>
</classes>

View file

@ -1,10 +1,10 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="test suite 3" >
<test name="test 2 classes" >
<suite name="test suite 3">
<test name="test 2 classes">
<classes>
<class name="rife.bld.extension.TestNgSimpleTest">
<methods>
<exclude name="testFail" />
<exclude name="testFail"/>
</methods>
</class>
<class name="rife.bld.extension.TestNgSimple2Test"/>