Search in sources :

Example 11 with TestResults

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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) FakeTestResults(com.facebook.buck.test.FakeTestResults) TestResults(com.facebook.buck.test.TestResults) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 12 with TestResults

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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) FakeTestResults(com.facebook.buck.test.FakeTestResults) TestResults(com.facebook.buck.test.TestResults) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 13 with TestResults

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;
}
Also used : StringWriter(java.io.StringWriter) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) TestResults(com.facebook.buck.test.TestResults) TestResultSummary(com.facebook.buck.test.TestResultSummary) FutureCallback(com.google.common.util.concurrent.FutureCallback) PrintWriter(java.io.PrintWriter)

Example 14 with TestResults

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());
        }
    }
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) TestResults(com.facebook.buck.test.TestResults) IOException(java.io.IOException) Date(java.util.Date) TestStatusMessage(com.facebook.buck.test.TestStatusMessage) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) SimpleDateFormat(java.text.SimpleDateFormat)

Example 15 with TestResults

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));
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) FakeTestResults(com.facebook.buck.test.FakeTestResults) TestResults(com.facebook.buck.test.TestResults) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

TestResults (com.facebook.buck.test.TestResults)33 Test (org.junit.Test)28 FakeTestResults (com.facebook.buck.test.FakeTestResults)27 TestCaseSummary (com.facebook.buck.test.TestCaseSummary)25 ImmutableList (com.google.common.collect.ImmutableList)20 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)17 ExecutionContext (com.facebook.buck.step.ExecutionContext)10 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)10 TestResultSummary (com.facebook.buck.test.TestResultSummary)10 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)8 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)8 FakeTestRule (com.facebook.buck.rules.FakeTestRule)8 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)8 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)8 BuildResult (com.facebook.buck.rules.BuildResult)6 CachingBuildEngine (com.facebook.buck.rules.CachingBuildEngine)5 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)4 Path (java.nio.file.Path)4 BuildTarget (com.facebook.buck.model.BuildTarget)3 FakeBuildEngine (com.facebook.buck.rules.FakeBuildEngine)3