diff options
author | Nikita Iashchenko <nikitai@google.com> | 2022-03-30 18:14:58 +0100 |
---|---|---|
committer | Nikita Iashchenko <nikitai@google.com> | 2022-03-31 17:53:15 +0100 |
commit | 37e7bb5768418ab7544b056e2fe0ef0bcd2e7c2a (patch) | |
tree | 6dc965a21f720593d92c92ead30943321223e9a0 /src | |
parent | fe094d44ce73aa2a81d355e1a9c7f336383eb56e (diff) | |
download | vogar-37e7bb5768418ab7544b056e2fe0ef0bcd2e7c2a.tar.gz |
Add tests for TestNG support in vogar
Add qualified name and args support; fixed help message to include
TestNG description and added basic test coverage.
Bug: 208639267
Test: java -cp ./out/host/linux-x86/framework/vogar-tests.jar \
org.junit.runner.JUnitCore vogar.target.testng.AllTestNgTests
Test: art/tools/run-libcore-tests.sh --mode host \
test.java.lang.String.IsBlank
Change-Id: I2b2e6df8d3d0a6a4585c924a4cac27236a4b7409
Diffstat (limited to 'src')
-rw-r--r-- | src/vogar/Vogar.java | 4 | ||||
-rw-r--r-- | src/vogar/target/testng/TestNgAnnotationTransformer.java | 27 | ||||
-rw-r--r-- | src/vogar/target/testng/TestNgListenerAdapter.java | 2 | ||||
-rw-r--r-- | src/vogar/target/testng/TestNgTargetRunner.java | 14 |
4 files changed, 38 insertions, 9 deletions
diff --git a/src/vogar/Vogar.java b/src/vogar/Vogar.java index 82622fc..5a51ff4 100644 --- a/src/vogar/Vogar.java +++ b/src/vogar/Vogar.java @@ -230,10 +230,10 @@ public final class Vogar { System.out.println("Usage: Vogar [options]... <actions>... [-- target args]..."); System.out.println(); System.out.println(" <actions>: .java files, directories, or class names."); - System.out.println(" These should be JUnit tests, jtreg tests, Caliper benchmarks"); + System.out.println(" These should be JUnit tests, TestNG tests, jtreg tests, Caliper benchmarks"); System.out.println(" or executable Java classes."); System.out.println(); - System.out.println(" When passing in a JUnit test class, it may have \"#method_name\""); + System.out.println(" When passing in a JUnit or TestNG test class, it may have \"#method_name\""); System.out.println(" appended to it, to specify a single test method."); System.out.println(); System.out.println(" [target args]: arguments passed to the target process. This is only useful when"); diff --git a/src/vogar/target/testng/TestNgAnnotationTransformer.java b/src/vogar/target/testng/TestNgAnnotationTransformer.java index 9dda070..a0e9862 100644 --- a/src/vogar/target/testng/TestNgAnnotationTransformer.java +++ b/src/vogar/target/testng/TestNgAnnotationTransformer.java @@ -20,6 +20,8 @@ import org.testng.annotations.ITestAnnotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** @@ -42,10 +44,19 @@ public class TestNgAnnotationTransformer implements IAnnotationTransformer { private final long timeoutMillis; private final AtomicReference<String> skipReference; + private final String qualification; + private final HashSet<String> args; - public TestNgAnnotationTransformer(int timeoutSeconds, AtomicReference<String> skipReference) { + public TestNgAnnotationTransformer( + int timeoutSeconds, + AtomicReference<String> skipReference, + String qualification, + String[] vogarArgs) { this.timeoutMillis = 1000L * timeoutSeconds; this.skipReference = skipReference; + this.qualification = qualification; + this.args = new HashSet<>(); + this.args.addAll(List.of(vogarArgs)); } @Override @@ -62,7 +73,7 @@ public class TestNgAnnotationTransformer implements IAnnotationTransformer { } } - // Skip up to and including given test name. + // Skip up to and including given test name if --skipPast provided. final String skipPast = skipReference.get(); if (skipPast != null && testClass == null && testMethod != null) { String name = testMethod.getName(); @@ -71,5 +82,17 @@ public class TestNgAnnotationTransformer implements IAnnotationTransformer { } annotation.setEnabled(false); } + + // Disable all but specified tests if qualified test name provided (package.class#method). + if (qualification != null && testMethod != null) { + if (!qualification.equals(testMethod.getName())) { + annotation.setEnabled(false); + } + } + if (!args.isEmpty() && testMethod != null) { + if (!args.contains(testMethod.getName())) { + annotation.setEnabled(false); + } + } } } diff --git a/src/vogar/target/testng/TestNgListenerAdapter.java b/src/vogar/target/testng/TestNgListenerAdapter.java index 68661e6..6f02c20 100644 --- a/src/vogar/target/testng/TestNgListenerAdapter.java +++ b/src/vogar/target/testng/TestNgListenerAdapter.java @@ -51,11 +51,11 @@ public class TestNgListenerAdapter implements ITestListener { @Override public void onTestFailure(ITestResult result) { Throwable thrown = result.getThrowable(); - monitor.outcomeFinished(Result.ERROR); ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream stackTrace = new PrintStream(out); thrown.printStackTrace(stackTrace); monitor.output(out.toString()); + monitor.outcomeFinished(Result.ERROR); } @Override diff --git a/src/vogar/target/testng/TestNgTargetRunner.java b/src/vogar/target/testng/TestNgTargetRunner.java index c6d0b4c..25b20b8 100644 --- a/src/vogar/target/testng/TestNgTargetRunner.java +++ b/src/vogar/target/testng/TestNgTargetRunner.java @@ -28,7 +28,9 @@ public class TestNgTargetRunner implements TargetRunner { private final TargetMonitor monitor; private final Class<?> testClass; private final TestEnvironment testEnvironment; + private final String qualification; private final TestNgAnnotationTransformer transformer; + private final String[] args; public TestNgTargetRunner( TargetMonitor monitor, @@ -41,20 +43,24 @@ public class TestNgTargetRunner implements TargetRunner { this.monitor = monitor; this.testClass = testClass; this.testEnvironment = testEnvironment; - this.transformer = new TestNgAnnotationTransformer(timeoutSeconds, skipPastReference); + this.qualification = qualification; + this.args = args; + this.transformer = + new TestNgAnnotationTransformer( + timeoutSeconds, skipPastReference, qualification, args); } @Override public boolean run() { // Set up TestNg core infrastructure. - TestNG testng = new TestNG(); + TestNG testng = new TestNG(false); // This transformer handles test timeout and overrides it if vogar was run // with specific timeout parameter (see --timeout option). testng.setAnnotationTransformer(transformer); - // Default listeners attempt to create html/xml reports. Disable them all completely. - testng.setUseDefaultListeners(false); + // Make TestNG less noisy. + testng.setVerbose(0); // Proxy to pass TestNg test lifecycle calls to vogar. TestNgListenerAdapter adapter = new TestNgListenerAdapter(monitor); testng.addListener(adapter); |