Search in sources :

Example 1 with TestCaseSummary

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

the class TestRunning method writeXmlOutput.

/**
   * Writes the test results in XML format to the supplied writer.
   *
   * This method does NOT close the writer object.
   * @param allResults The test results.
   * @param writer The writer in which the XML data will be written to.
   */
public static void writeXmlOutput(List<TestResults> allResults, Writer writer) throws IOException {
    try {
        // Build the XML output.
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        // Create the <tests> tag. All test data will be within this tag.
        Element testsEl = doc.createElement("tests");
        doc.appendChild(testsEl);
        for (TestResults results : allResults) {
            for (TestCaseSummary testCase : results.getTestCases()) {
                // Create the <test name="..." status="..." time="..."> tag.
                // This records a single test case result in the test suite.
                Element testEl = doc.createElement("test");
                testEl.setAttribute("name", testCase.getTestCaseName());
                testEl.setAttribute("status", testCase.isSuccess() ? "PASS" : "FAIL");
                testEl.setAttribute("time", Long.toString(testCase.getTotalTime()));
                testsEl.appendChild(testEl);
                // Loop through the test case and add XML data (name, message, and
                // stacktrace) for each individual test, if present.
                addExtraXmlInfo(testCase, testEl);
            }
        }
        // Write XML to the writer.
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException | ParserConfigurationException ex) {
        throw new IOException("Unable to build the XML document!");
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) TestResults(com.facebook.buck.test.TestResults) IOException(java.io.IOException) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with TestCaseSummary

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

the class TestResultFormatter method reportResult.

/** Writes a detailed summary that ends with a trailing newline. */
public void reportResult(ImmutableList.Builder<String> addTo, TestResults results) {
    if (verbosity.shouldPrintBinaryRunInformation() && results.getTotalNumberOfTests() > 1) {
        addTo.add("");
        addTo.add(String.format(locale, "Results for %s (%d/%d) %s", results.getBuildTarget().getFullyQualifiedName(), results.getSequenceNumber(), results.getTotalNumberOfTests(), verbosity));
    }
    boolean shouldReportLogSummaryAfterTests = false;
    for (TestCaseSummary testCase : results.getTestCases()) {
        // Only mention classes with tests.
        if (testCase.getPassedCount() == 0 && testCase.getFailureCount() == 0 && testCase.getSkippedCount() == 0) {
            continue;
        }
        String oneLineSummary = testCase.getOneLineSummary(locale, results.getDependenciesPassTheirTests(), ansi);
        addTo.add(oneLineSummary);
        // violations)
        if (testCase.isSuccess()) {
            continue;
        }
        for (TestResultSummary testResult : testCase.getTestResults()) {
            if (!results.getDependenciesPassTheirTests()) {
                continue;
            }
            // Report on either explicit failure
            if (!testResult.isSuccess()) {
                shouldReportLogSummaryAfterTests = true;
                reportResultSummary(addTo, testResult);
            }
        }
    }
    if (shouldReportLogSummaryAfterTests && verbosity != Verbosity.SILENT) {
        for (Path testLogPath : results.getTestLogPaths()) {
            if (Files.exists(testLogPath)) {
                reportLogSummary(locale, addTo, testLogPath, summaryVerbosity.getMaxDebugLogLines().orElse(DEFAULT_MAX_LOG_LINES));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) TestResultSummary(com.facebook.buck.test.TestResultSummary)

Example 3 with TestCaseSummary

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

the class TestCaseSummariesBuildingXctoolEventHandlerTest method noTestCasesAndOcunitFailureReturnsFailedTestResultSummary.

@Test
public void noTestCasesAndOcunitFailureReturnsFailedTestResultSummary() throws Exception {
    Path jsonPath = TestDataHelper.getTestDataDirectory(this).resolve("xctool-output/ocunit-failure.json");
    try (Reader jsonReader = Files.newBufferedReader(jsonPath, StandardCharsets.UTF_8)) {
        TestCaseSummariesBuildingXctoolEventHandler testCaseSummariesBuilder = new TestCaseSummariesBuildingXctoolEventHandler(TestRule.NOOP_REPORTING_CALLBACK);
        XctoolOutputParsing.streamOutputFromReader(jsonReader, testCaseSummariesBuilder);
        List<TestCaseSummary> summaries = testCaseSummariesBuilder.getTestCaseSummaries();
        assertThat(summaries, hasSize(1));
        Matcher<TestResultSummary> summaryMatcher = allOf(hasProperty("type", equalTo(ResultType.FAILURE)), hasProperty("message", containsString("dyld: app was built for iOS 8.3 which is newer than this simulator 8.1")));
        assertThat(summaries.get(0).getTestResults(), contains(summaryMatcher));
    }
}
Also used : Path(java.nio.file.Path) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) Reader(java.io.Reader) TestResultSummary(com.facebook.buck.test.TestResultSummary) Test(org.junit.Test)

Example 4 with TestCaseSummary

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

the class TestCaseSummariesBuildingXctoolEventHandlerTest method mixedPassAndFailReturnsMixedResultSummary.

@Test
@SuppressWarnings("unchecked")
public void mixedPassAndFailReturnsMixedResultSummary() throws Exception {
    Path jsonPath = TestDataHelper.getTestDataDirectory(this).resolve("xctool-output/mixed-pass-and-fail.json");
    try (Reader jsonReader = Files.newBufferedReader(jsonPath, StandardCharsets.UTF_8)) {
        TestCaseSummariesBuildingXctoolEventHandler testCaseSummariesBuilder = new TestCaseSummariesBuildingXctoolEventHandler(TestRule.NOOP_REPORTING_CALLBACK);
        XctoolOutputParsing.streamOutputFromReader(jsonReader, testCaseSummariesBuilder);
        List<TestCaseSummary> summaries = testCaseSummariesBuilder.getTestCaseSummaries();
        assertThat(summaries, hasSize(2));
        Matcher<TestResultSummary> isOtherTestsTestSomethingSuccess = allOf(hasProperty("testCaseName", equalTo("OtherTests")), hasProperty("testName", equalTo("-[OtherTests testSomething]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(3L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        List<TestResultSummary> otherTestsResults = summaries.get(0).getTestResults();
        assertThat(otherTestsResults, contains(isOtherTestsTestSomethingSuccess));
        Matcher<TestResultSummary> isSomeTestsTestBacktraceOutputIsCaptured = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testBacktraceOutputIsCaptured]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(0L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", containsString("-[SenTestCase performTest:]")), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestOutputMerging = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testOutputMerging]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(0L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", containsString("stdout-line1\nstderr-line1\n")), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestPrintSDK = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testPrintSDK]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(1L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", containsString("SDK: 6.1")), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestStream = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testStream]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(754L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", containsString(">>>> i = 0")), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestWillFail = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testWillFail]")), hasProperty("type", equalTo(ResultType.FAILURE)), hasProperty("time", equalTo(0L)), hasProperty("message", containsString("SomeTests.m:40: 'a' should be equal to 'b'")), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestWillPass = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("-[SomeTests testWillPass]")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(0L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        List<TestResultSummary> someTestsResults = summaries.get(1).getTestResults();
        assertThat(someTestsResults, contains(isSomeTestsTestBacktraceOutputIsCaptured, isSomeTestsTestOutputMerging, isSomeTestsTestPrintSDK, isSomeTestsTestStream, isSomeTestsTestWillFail, isSomeTestsTestWillPass));
    }
}
Also used : Path(java.nio.file.Path) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) Reader(java.io.Reader) TestResultSummary(com.facebook.buck.test.TestResultSummary) Test(org.junit.Test)

Example 5 with TestCaseSummary

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

the class XctestOutputParsingTest method mixedPassAndFailReturnsMixedResultSummary.

@Test
@SuppressWarnings("unchecked")
public void mixedPassAndFailReturnsMixedResultSummary() throws Exception {
    Path outputPath = TestDataHelper.getTestDataDirectory(this).resolve("xctest-output/mixed-pass-and-fail.txt");
    try (Reader outputReader = Files.newBufferedReader(outputPath, StandardCharsets.UTF_8)) {
        TestCaseSummariesBuildingXctestEventHandler xctestEventHandler = new TestCaseSummariesBuildingXctestEventHandler(TestRule.NOOP_REPORTING_CALLBACK);
        XctestOutputParsing.streamOutput(outputReader, xctestEventHandler);
        List<TestCaseSummary> summaries = xctestEventHandler.getTestCaseSummaries();
        assertThat(summaries, hasSize(2));
        Matcher<TestResultSummary> isOtherTestsTestSomethingSuccess = allOf(hasProperty("testCaseName", equalTo("OtherTests")), hasProperty("testName", equalTo("testSomething")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(3L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        List<TestResultSummary> otherTestsResults = summaries.get(0).getTestResults();
        assertThat(otherTestsResults, contains(isOtherTestsTestSomethingSuccess));
        Matcher<TestResultSummary> isSomeTestsTestWillFail = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("testWillFail")), hasProperty("type", equalTo(ResultType.FAILURE)), hasProperty("time", equalTo(0L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        Matcher<TestResultSummary> isSomeTestsTestWillPass = allOf(hasProperty("testCaseName", equalTo("SomeTests")), hasProperty("testName", equalTo("testWillPass")), hasProperty("type", equalTo(ResultType.SUCCESS)), hasProperty("time", equalTo(0L)), hasProperty("message", nullValue(String.class)), hasProperty("stacktrace", nullValue(String.class)), hasProperty("stdOut", nullValue(String.class)), hasProperty("stdErr", nullValue(String.class)));
        List<TestResultSummary> someTestsResults = summaries.get(1).getTestResults();
        assertThat(someTestsResults, contains(isSomeTestsTestWillFail, isSomeTestsTestWillPass));
    }
}
Also used : Path(java.nio.file.Path) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) Reader(java.io.Reader) StringReader(java.io.StringReader) TestResultSummary(com.facebook.buck.test.TestResultSummary) Test(org.junit.Test)

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