Search in sources :

Example 11 with TestCaseSummary

use of com.facebook.buck.test.TestCaseSummary in project buck by facebook.

the class TestResultFormatterTest method failingTestShouldShowTestStatusMessages.

@Test
public void failingTestShouldShowTestStatusMessages() throws IOException {
    TestResultFormatter formatter = createSilentFormatter();
    TestCaseSummary summary = new TestCaseSummary("com.example.FooTest", ImmutableList.of(successTest, failingTest));
    TestResults results = TestResults.of(BuildTargetFactory.newInstance("//foo:bar"), ImmutableList.of(summary), /* contacts */
    ImmutableSet.of(), /* labels */
    ImmutableSet.of());
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    formatter.runComplete(builder, ImmutableList.of(results), ImmutableList.of(TestStatusMessage.of("Hello world", Level.INFO, 1450473060000L)));
    String expectedOutput = Joiner.on('\n').join("====TEST STATUS MESSAGES====", "[2015-12-18 13:11:00.000][INFO] Hello world", "TESTS FAILED: 1 FAILURE", "Failed target: //foo:bar", "FAIL com.example.FooTest");
    assertEquals(expectedOutput, 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 TestCaseSummary

use of com.facebook.buck.test.TestCaseSummary 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 13 with TestCaseSummary

use of com.facebook.buck.test.TestCaseSummary 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 14 with TestCaseSummary

use of com.facebook.buck.test.TestCaseSummary 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 15 with TestCaseSummary

use of com.facebook.buck.test.TestCaseSummary 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)

Aggregations

TestCaseSummary (com.facebook.buck.test.TestCaseSummary)34 Test (org.junit.Test)28 TestResults (com.facebook.buck.test.TestResults)25 FakeTestResults (com.facebook.buck.test.FakeTestResults)22 ImmutableList (com.google.common.collect.ImmutableList)18 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)17 TestResultSummary (com.facebook.buck.test.TestResultSummary)16 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)7 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)7 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)7 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)7 Path (java.nio.file.Path)7 BuildTarget (com.facebook.buck.model.BuildTarget)6 RuleKey (com.facebook.buck.rules.RuleKey)6 FakeTestRule (com.facebook.buck.rules.FakeTestRule)4 ExecutionContext (com.facebook.buck.step.ExecutionContext)4 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)4 ActionGraphEvent (com.facebook.buck.event.ActionGraphEvent)3 BuckEventBus (com.facebook.buck.event.BuckEventBus)3 ConsoleTestUtils.postStoreFinished (com.facebook.buck.event.listener.ConsoleTestUtils.postStoreFinished)3