use of com.facebook.buck.test.TestResults in project buck by facebook.
the class TestResultFormatterTest method allTestsPassingShouldBeAcknowledged.
@Test
public void allTestsPassingShouldBeAcknowledged() {
TestResultFormatter formatter = createSilentFormatter();
TestCaseSummary summary = new TestCaseSummary("com.example.FooTest", ImmutableList.of(successTest));
TestResults results = FakeTestResults.of(ImmutableList.of(summary));
ImmutableList.Builder<String> builder = ImmutableList.builder();
formatter.runComplete(builder, ImmutableList.of(results), ImmutableList.of());
assertEquals("TESTS PASSED", toString(builder));
}
use of com.facebook.buck.test.TestResults in project buck by facebook.
the class TestResultFormatterTest method allTestsPassingShouldNotShowTestStatusMessages.
@Test
public void allTestsPassingShouldNotShowTestStatusMessages() throws IOException {
TestResultFormatter formatter = createSilentFormatter();
TestCaseSummary summary = new TestCaseSummary("com.example.FooTest", ImmutableList.of(successTest));
TestResults results = FakeTestResults.of(ImmutableList.of(summary));
ImmutableList.Builder<String> builder = ImmutableList.builder();
formatter.runComplete(builder, ImmutableList.of(results), ImmutableList.of(TestStatusMessage.of("Hello world", Level.INFO, 12345L)));
assertEquals("TESTS PASSED", toString(builder));
}
use of com.facebook.buck.test.TestResults in project buck by facebook.
the class TestRunning method transformTestResults.
private static ListenableFuture<TestResults> transformTestResults(final CommandRunnerParams params, ListenableFuture<TestResults> originalTestResults, final TestRule testRule, final TestRule.TestReportingCallback testReportingCallback, final ImmutableSet<String> testTargets, final AtomicInteger lastReportedTestSequenceNumber, final int totalNumberOfTests) {
final SettableFuture<TestResults> transformedTestResults = SettableFuture.create();
FutureCallback<TestResults> callback = new FutureCallback<TestResults>() {
private TestResults postTestResults(TestResults testResults) {
if (!testRule.supportsStreamingTests()) {
// For test rules which don't support streaming tests, we'll
// stream test summary events after interpreting the
// results.
LOG.debug("Simulating streaming test events for rule %s", testRule);
testReportingCallback.testsDidBegin();
for (TestCaseSummary testCaseSummary : testResults.getTestCases()) {
for (TestResultSummary testResultSummary : testCaseSummary.getTestResults()) {
testReportingCallback.testDidBegin(testResultSummary.getTestCaseName(), testResultSummary.getTestName());
testReportingCallback.testDidEnd(testResultSummary);
}
}
testReportingCallback.testsDidEnd(testResults.getTestCases());
LOG.debug("Done simulating streaming test events for rule %s", testRule);
}
TestResults transformedTestResults = TestResults.builder().from(testResults).setSequenceNumber(lastReportedTestSequenceNumber.incrementAndGet()).setTotalNumberOfTests(totalNumberOfTests).build();
params.getBuckEventBus().post(IndividualTestEvent.finished(testTargets, transformedTestResults));
return transformedTestResults;
}
private String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
return sw.toString();
}
@Override
public void onSuccess(TestResults testResults) {
LOG.debug("Transforming successful test results %s", testResults);
postTestResults(testResults);
transformedTestResults.set(testResults);
}
@Override
public void onFailure(Throwable throwable) {
LOG.warn(throwable, "Test command step failed, marking %s as failed", testRule);
// If the test command steps themselves fail, report this as special test result.
TestResults testResults = TestResults.of(testRule.getBuildTarget(), ImmutableList.of(new TestCaseSummary(testRule.getBuildTarget().toString(), ImmutableList.of(new TestResultSummary(testRule.getBuildTarget().toString(), "main", ResultType.FAILURE, 0L, throwable.getMessage(), getStackTrace(throwable), "", "")))), testRule.getContacts(), testRule.getLabels().stream().map(Object::toString).collect(MoreCollectors.toImmutableSet()));
TestResults newTestResults = postTestResults(testResults);
transformedTestResults.set(newTestResults);
}
};
Futures.addCallback(originalTestResults, callback);
return transformedTestResults;
}
use of com.facebook.buck.test.TestResults in project buck by facebook.
the class TestResultFormatter method runComplete.
public void runComplete(ImmutableList.Builder<String> addTo, List<TestResults> completedResults, List<TestStatusMessage> testStatusMessages) {
// Print whether each test succeeded or failed.
boolean isDryRun = false;
boolean hasAssumptionViolations = false;
int numTestsPassed = 0;
int numTestsFailed = 0;
int numTestsSkipped = 0;
ListMultimap<TestResults, TestCaseSummary> failingTests = ArrayListMultimap.create();
ImmutableList.Builder<Path> testLogPathsBuilder = ImmutableList.builder();
for (TestResults summary : completedResults) {
testLogPathsBuilder.addAll(summary.getTestLogPaths());
// Get failures up-front to include class-level initialization failures
if (summary.getFailureCount() > 0) {
numTestsFailed += summary.getFailureCount();
failingTests.putAll(summary, summary.getFailures());
}
// Get passes/skips by iterating through each case
for (TestCaseSummary testCaseSummary : summary.getTestCases()) {
isDryRun = isDryRun || testCaseSummary.isDryRun();
numTestsPassed += testCaseSummary.getPassedCount();
numTestsSkipped += testCaseSummary.getSkippedCount();
hasAssumptionViolations = hasAssumptionViolations || testCaseSummary.hasAssumptionViolations();
}
}
// If no test runs to completion, don't fail, but warn
if (numTestsPassed == 0 && numTestsFailed == 0) {
String message;
if (hasAssumptionViolations) {
message = "NO TESTS RAN (assumption violations)";
} else if (numTestsSkipped > 0) {
message = "NO TESTS RAN (tests skipped)";
} else {
message = "NO TESTS RAN";
}
if (isDryRun) {
addTo.add(ansi.asHighlightedSuccessText(message));
} else {
addTo.add(ansi.asHighlightedWarningText(message));
}
return;
}
// When all tests pass...
if (numTestsFailed == 0) {
ImmutableList<Path> testLogPaths = testLogPathsBuilder.build();
if (testLogsPath.isPresent() && verbosity != Verbosity.SILENT) {
try {
if (MoreFiles.concatenateFiles(testLogsPath.get(), testLogPaths)) {
addTo.add("Updated test logs: " + testLogsPath.get().toString());
}
} catch (IOException e) {
LOG.warn(e, "Could not concatenate test logs %s to %s", testLogPaths, testLogsPath.get());
}
}
if (hasAssumptionViolations) {
addTo.add(ansi.asHighlightedWarningText("TESTS PASSED (with some assumption violations)"));
} else {
addTo.add(ansi.asHighlightedSuccessText("TESTS PASSED"));
}
return;
}
// When something fails...
if (!testStatusMessages.isEmpty()) {
addTo.add("====TEST STATUS MESSAGES====");
SimpleDateFormat timestampFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss.SSS]", Locale.US);
timestampFormat.setTimeZone(timeZone);
for (TestStatusMessage testStatusMessage : testStatusMessages) {
addTo.add(String.format(locale, "%s[%s] %s", timestampFormat.format(new Date(testStatusMessage.getTimestampMillis())), testStatusMessage.getLevel(), testStatusMessage.getMessage()));
}
}
addTo.add(ansi.asHighlightedFailureText(String.format(locale, "TESTS FAILED: %d %s", numTestsFailed, numTestsFailed == 1 ? "FAILURE" : "FAILURES")));
for (TestResults results : failingTests.keySet()) {
addTo.add("Failed target: " + results.getBuildTarget().getFullyQualifiedName());
for (TestCaseSummary summary : failingTests.get(results)) {
addTo.add(summary.toString());
}
}
}
use of com.facebook.buck.test.TestResults in project buck by facebook.
the class TestResultFormatterTest method allTestsPassingShouldIncludeNonEmptyTestLogs.
@Test
public void allTestsPassingShouldIncludeNonEmptyTestLogs() throws IOException {
Path testLogPath = vfs.getPath("test-log.txt");
Files.write(testLogPath, ImmutableList.of("Hello world"), StandardCharsets.UTF_8);
TestResultFormatter formatter = createSilentFormatter();
TestCaseSummary summary = new TestCaseSummary("com.example.FooTest", ImmutableList.of(successTest));
TestResults results = FakeTestResults.withTestLogs(ImmutableList.of(summary), ImmutableList.of(testLogPath));
ImmutableList.Builder<String> builder = ImmutableList.builder();
formatter.runComplete(builder, ImmutableList.of(results), ImmutableList.of());
assertEquals("Updated test logs: log.txt\nTESTS PASSED", toString(builder));
}
Aggregations