Search in sources :

Example 31 with TestResultSummary

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

the class TestRunningTest method testXmlGeneration.

/**
   * Tests the --xml flag, ensuring that test result data is correctly
   * formatted.
   */
@Test
public void testXmlGeneration() throws Exception {
    // Set up sample test data.
    TestResultSummary result1 = new TestResultSummary(/* testCaseName */
    "TestCase", /* testName */
    "passTest", /* type */
    ResultType.SUCCESS, /* time */
    5000, /* message */
    null, /* stacktrace */
    null, /* stdOut */
    null, /* stdErr */
    null);
    TestResultSummary result2 = new TestResultSummary(/* testCaseName */
    "TestCase", /* testName */
    "failWithMsg", /* type */
    ResultType.FAILURE, /* time */
    7000, /* message */
    "Index out of bounds!", /* stacktrace */
    "Stacktrace", /* stdOut */
    null, /* stdErr */
    null);
    TestResultSummary result3 = new TestResultSummary(/* testCaseName */
    "TestCase", /* testName */
    "failNoMsg", /* type */
    ResultType.SUCCESS, /* time */
    4000, /* message */
    null, /* stacktrace */
    null, /* stdOut */
    null, /* stdErr */
    null);
    List<TestResultSummary> resultList = ImmutableList.of(result1, result2, result3);
    TestCaseSummary testCase = new TestCaseSummary("TestCase", resultList);
    List<TestCaseSummary> testCases = ImmutableList.of(testCase);
    TestResults testResults = FakeTestResults.of(testCases);
    List<TestResults> testResultsList = ImmutableList.of(testResults);
    // Call the XML generation method with our test data.
    StringWriter writer = new StringWriter();
    TestRunning.writeXmlOutput(testResultsList, writer);
    ByteArrayInputStream resultStream = new ByteArrayInputStream(writer.toString().getBytes());
    // Convert the raw XML data into a DOM object, which we will check.
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(resultStream);
    // Check for exactly one <tests> tag.
    NodeList testsList = doc.getElementsByTagName("tests");
    assertEquals(testsList.getLength(), 1);
    // Check for exactly one <test> tag.
    Element testsEl = (Element) testsList.item(0);
    NodeList testList = testsEl.getElementsByTagName("test");
    assertEquals(testList.getLength(), 1);
    // Check for exactly three <testresult> tags.
    // There should be two failures and one success.
    Element testEl = (Element) testList.item(0);
    NodeList resultsList = testEl.getElementsByTagName("testresult");
    assertEquals(resultsList.getLength(), 3);
    // Verify the text elements of the first <testresult> tag.
    Element passResultEl = (Element) resultsList.item(0);
    assertEquals(passResultEl.getAttribute("name"), "passTest");
    assertEquals(passResultEl.getAttribute("time"), "5000");
    checkXmlTextContents(passResultEl, "message", "");
    checkXmlTextContents(passResultEl, "stacktrace", "");
    // Verify the text elements of the second <testresult> tag.
    assertEquals(testEl.getAttribute("name"), "TestCase");
    Element failResultEl1 = (Element) resultsList.item(1);
    assertEquals(failResultEl1.getAttribute("name"), "failWithMsg");
    assertEquals(failResultEl1.getAttribute("time"), "7000");
    checkXmlTextContents(failResultEl1, "message", "Index out of bounds!");
    checkXmlTextContents(failResultEl1, "stacktrace", "Stacktrace");
    // Verify the text elements of the third <testresult> tag.
    Element failResultEl2 = (Element) resultsList.item(2);
    assertEquals(failResultEl2.getAttribute("name"), "failNoMsg");
    assertEquals(failResultEl2.getAttribute("time"), "4000");
    checkXmlTextContents(failResultEl2, "message", "");
    checkXmlTextContents(failResultEl2, "stacktrace", "");
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) TestResults(com.facebook.buck.test.TestResults) FakeTestResults(com.facebook.buck.test.FakeTestResults) TestResultSummary(com.facebook.buck.test.TestResultSummary) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) Test(org.junit.Test)

Example 32 with TestResultSummary

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

the class TestRunningTest method whenAllTestsAreSeparateTestsRunInOrder.

@Test
public void whenAllTestsAreSeparateTestsRunInOrder() throws Exception {
    CommandRunnerParams commandRunnerParams = CommandRunnerParamsForTesting.builder().build();
    AtomicInteger atomicExecutionOrder = new AtomicInteger(0);
    ExecutionOrderAwareFakeStep separateTestStep1 = new ExecutionOrderAwareFakeStep("teststep1", "teststep1", 0, atomicExecutionOrder);
    final TestResults fakeTestResults = FakeTestResults.of(ImmutableList.of(new TestCaseSummary("TestCase", ImmutableList.of(new TestResultSummary("TestCaseResult", "passTest", ResultType.SUCCESS, 5000, null, null, null, null)))));
    BuildTarget separateTest1Target = BuildTargetFactory.newInstance("//:test1");
    FakeTestRule separateTest1 = new FakeTestRule(new FakeBuildRuleParamsBuilder(separateTest1Target).build(), new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()))), ImmutableSet.of(), Optional.of(Paths.get("separateTestStep1OutputDir")), // runTestSeparately
    true, ImmutableList.of(separateTestStep1), () -> fakeTestResults);
    ExecutionOrderAwareFakeStep separateTestStep2 = new ExecutionOrderAwareFakeStep("teststep2", "teststep2", 0, atomicExecutionOrder);
    BuildTarget separateTest2Target = BuildTargetFactory.newInstance("//:test2");
    FakeTestRule separateTest2 = new FakeTestRule(new FakeBuildRuleParamsBuilder(separateTest2Target).build(), new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()))), ImmutableSet.of(), Optional.of(Paths.get("separateTestStep2OutputDir")), // runTestSeparately
    true, ImmutableList.of(separateTestStep2), () -> fakeTestResults);
    ExecutionOrderAwareFakeStep separateTestStep3 = new ExecutionOrderAwareFakeStep("teststep3", "teststep3", 0, atomicExecutionOrder);
    BuildTarget separateTest3Target = BuildTargetFactory.newInstance("//:test3");
    FakeTestRule separateTest3 = new FakeTestRule(new FakeBuildRuleParamsBuilder(separateTest3Target).build(), new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()))), ImmutableSet.of(), Optional.of(Paths.get("separateTestStep3OutputDir")), // runTestSeparately
    true, ImmutableList.of(separateTestStep3), () -> fakeTestResults);
    // We explicitly use an actual thread pool here; the logic should ensure the
    // separate tests are run in the correct order.
    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
    FakeBuildEngine fakeBuildEngine = new FakeBuildEngine(ImmutableMap.of(separateTest1Target, BuildResult.success(separateTest1, BUILT_LOCALLY, CacheResult.miss()), separateTest2Target, BuildResult.success(separateTest2, BUILT_LOCALLY, CacheResult.miss()), separateTest3Target, BuildResult.success(separateTest3, BUILT_LOCALLY, CacheResult.miss())), ImmutableMap.of(separateTest1Target, new RuleKey("00"), separateTest2Target, new RuleKey("00"), separateTest3Target, new RuleKey("00")));
    ExecutionContext fakeExecutionContext = TestExecutionContext.newInstance();
    DefaultStepRunner stepRunner = new DefaultStepRunner();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    int ret = TestRunning.runTests(commandRunnerParams, ImmutableList.of(separateTest1, separateTest2, separateTest3), fakeExecutionContext, DEFAULT_OPTIONS, service, fakeBuildEngine, stepRunner, new SourcePathResolver(ruleFinder), ruleFinder);
    assertThat(ret, equalTo(0));
    assertThat(separateTestStep1.getExecutionBeginOrder(), equalTo(Optional.of(0)));
    assertThat(separateTestStep1.getExecutionEndOrder(), equalTo(Optional.of(1)));
    assertThat(separateTestStep2.getExecutionBeginOrder(), equalTo(Optional.of(2)));
    assertThat(separateTestStep2.getExecutionEndOrder(), equalTo(Optional.of(3)));
    assertThat(separateTestStep3.getExecutionBeginOrder(), equalTo(Optional.of(4)));
    assertThat(separateTestStep3.getExecutionEndOrder(), equalTo(Optional.of(5)));
}
Also used : ExecutionOrderAwareFakeStep(com.facebook.buck.step.ExecutionOrderAwareFakeStep) RuleKey(com.facebook.buck.rules.RuleKey) TestResults(com.facebook.buck.test.TestResults) FakeTestResults(com.facebook.buck.test.FakeTestResults) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) TestResultSummary(com.facebook.buck.test.TestResultSummary) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) FakeTestRule(com.facebook.buck.rules.FakeTestRule) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultStepRunner(com.facebook.buck.step.DefaultStepRunner) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) FakeBuildEngine(com.facebook.buck.rules.FakeBuildEngine) Test(org.junit.Test)

Example 33 with TestResultSummary

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

the class TestResultFormatterTest method shouldUseDecimalCommaForGerman.

@Test
public void shouldUseDecimalCommaForGerman() {
    TestResultFormatter formatter = new TestResultFormatter(new Ansi(false), Verbosity.COMMANDS, TestResultSummaryVerbosity.of(false, false), Locale.GERMAN, Optional.of(logPath), TimeZone.getTimeZone("America/Los_Angeles"));
    TestCaseSummary summary = new TestCaseSummary("com.example.FooTest", ImmutableList.of(new TestResultSummary("com.example.FooTest", "successTest", ResultType.SUCCESS, 12300, /*message*/
    null, /*stacktrace*/
    null, "good stdout", "good stderr")));
    TestResults results = FakeTestResults.of(ImmutableList.of(summary));
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    formatter.reportResult(builder, results);
    assertEquals("PASS     12,3s  1 Passed   0 Skipped   0 Failed   com.example.FooTest", 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) Ansi(com.facebook.buck.util.Ansi) TestResultSummary(com.facebook.buck.test.TestResultSummary) Test(org.junit.Test)

Example 34 with TestResultSummary

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

the class TestResultFormatterTest method createTestResults.

@Before
public void createTestResults() {
    stackTrace = Throwables.getStackTraceAsString(new Exception("Ouch"));
    successTest = new TestResultSummary("com.example.FooTest", "successTest", ResultType.SUCCESS, 500, /*message*/
    null, /*stacktrace*/
    null, "good stdout", "good stderr");
    failingTest = new TestResultSummary("com.example.FooTest", "failTest", ResultType.FAILURE, 200, "Unexpected fish found", stackTrace, "bad stdout", "bad stderr");
}
Also used : TestResultSummary(com.facebook.buck.test.TestResultSummary) IOException(java.io.IOException) Before(org.junit.Before)

Aggregations

TestResultSummary (com.facebook.buck.test.TestResultSummary)34 TestCaseSummary (com.facebook.buck.test.TestCaseSummary)17 Test (org.junit.Test)16 TestResults (com.facebook.buck.test.TestResults)11 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)10 BuildTarget (com.facebook.buck.model.BuildTarget)9 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)9 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)9 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)8 Path (java.nio.file.Path)8 ExecutionContext (com.facebook.buck.step.ExecutionContext)7 FakeTestResults (com.facebook.buck.test.FakeTestResults)7 ImmutableList (com.google.common.collect.ImmutableList)7 RuleKey (com.facebook.buck.rules.RuleKey)6 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)5 ResultType (com.facebook.buck.test.result.type.ResultType)5 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)4 BufferedReader (java.io.BufferedReader)4 Matcher (java.util.regex.Matcher)4 Document (org.w3c.dom.Document)4