Created
June 14, 2012 04:40
-
-
Save ttddyy/2928025 to your computer and use it in GitHub Desktop.
util method for programmatically run and verify TestNG test classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestNGUtils { | |
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); | |
public static void runAndVerify(Class<?>... testClasses) { | |
if (ObjectUtils.isEmpty(testClasses)) { | |
throw new AssertionError("no test class specified."); | |
} | |
// run testng tests | |
TestListenerAdapter tla = new TestListenerAdapter(); | |
TestNG testNG = new TestNG(); | |
testNG.setTestClasses(testClasses); | |
testNG.addListener(tla); | |
testNG.run(); | |
List<ITestResult> failedTests = Lists.newArrayList(); | |
failedTests.addAll(tla.getFailedTests()); | |
failedTests.addAll(tla.getConfigurationFailures()); | |
if (!failedTests.isEmpty()) { | |
String header = String.format("Combined Messages (Total:%d)", failedTests.size()); | |
List<String> errorMessages = Lists.newArrayList(); | |
errorMessages.add(header); | |
errorMessages.addAll(Lists.transform(failedTests, new Function<ITestResult, String>() { | |
int i = 1; | |
@Override | |
public String apply(ITestResult testResult) { | |
String stackTraceString = Throwables.getStackTraceAsString(testResult.getThrowable()); | |
String template = "Message-%d: %n %s"; | |
return String.format(template, i++, stackTraceString); | |
} | |
})); | |
// transform messages to a single combined string | |
String message = Joiner.on(LINE_SEPARATOR).join(errorMessages); | |
throw new AssertionError(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment