]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add PytestTestEngine
authorDavid Cramer <dcramer@gmail.com>
Thu, 13 Jun 2013 21:23:45 +0000 (14:23 -0700)
committerDavid Cramer <dcramer@gmail.com>
Thu, 13 Jun 2013 21:23:45 +0000 (14:23 -0700)
src/__phutil_library_map__.php
src/unit/engine/PytestTestEngine.php [new file with mode: 0644]

index a4963e2bdb564d3db8592b85c1ac14dfffdca89b..4149409d2ec96a312af63835c8ed05eb46028213 100644 (file)
@@ -168,6 +168,7 @@ phutil_register_library_map(array(
     'PhutilLintEngine' => 'lint/engine/PhutilLintEngine.php',
     'PhutilUnitTestEngine' => 'unit/engine/PhutilUnitTestEngine.php',
     'PhutilUnitTestEngineTestCase' => 'unit/engine/__tests__/PhutilUnitTestEngineTestCase.php',
+    'PytestTestEngine' => 'unit/engine/PytestTestEngine.php',
     'UnitTestableArcanistLintEngine' => 'lint/engine/UnitTestableArcanistLintEngine.php',
   ),
   'function' =>
@@ -299,6 +300,7 @@ phutil_register_library_map(array(
     'PhutilLintEngine' => 'ArcanistLintEngine',
     'PhutilUnitTestEngine' => 'ArcanistBaseUnitTestEngine',
     'PhutilUnitTestEngineTestCase' => 'ArcanistTestCase',
+    'PytestTestEngine' => 'ArcanistBaseUnitTestEngine',
     'UnitTestableArcanistLintEngine' => 'ArcanistLintEngine',
   ),
 ));
diff --git a/src/unit/engine/PytestTestEngine.php b/src/unit/engine/PytestTestEngine.php
new file mode 100644 (file)
index 0000000..37a43b7
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * Very basic 'py.test' unit test engine wrapper.
+ *
+ * @group unitrun
+ */
+final class PytestTestEngine extends ArcanistBaseUnitTestEngine {
+
+  public function run() {
+    $junit_tmp = new TempFile();
+
+    $cmd_line = csprintf('py.test --junitxml=%s',
+                         $junit_tmp);
+
+    $future = new ExecFuture('%C', $cmd_line);
+    list($stdout, $stderr) = $future->resolvex();
+
+    return $this->parseTestResults($junit_tmp);
+  }
+
+  public function parseTestResults($junit_tmp) {
+    // xunit xsd: https://gist.github.com/959290
+    $xunit_dom = new DOMDocument();
+    $xunit_dom->loadXML(Filesystem::readFile($junit_tmp));
+
+    $results = array();
+    $testcases = $xunit_dom->getElementsByTagName("testcase");
+    foreach ($testcases as $testcase) {
+      $classname = $testcase->getAttribute("classname");
+      $name = $testcase->getAttribute("name");
+      $time = $testcase->getAttribute("time");
+
+      $status = ArcanistUnitTestResult::RESULT_PASS;
+      $user_data = "";
+
+      // A skipped test is a test which was ignored using framework
+      // mechanizms (e.g. @skip decorator)
+      $skipped = $testcase->getElementsByTagName("skipped");
+      if ($skipped->length > 0) {
+        $status = ArcanistUnitTestResult::RESULT_SKIP;
+        $messages = array();
+        for ($ii = 0; $ii < $skipped->length; $ii++) {
+          $messages[] = trim($skipped->item($ii)->nodeValue, " \n");
+        }
+
+        $user_data .= implode("\n", $messages);
+      }
+
+      // Failure is a test which the code has explicitly failed by using
+      // the mechanizms for that purpose. e.g., via an assertEquals
+      $failures = $testcase->getElementsByTagName("failure");
+      if ($failures->length > 0) {
+        $status = ArcanistUnitTestResult::RESULT_FAIL;
+        $messages = array();
+        for ($ii = 0; $ii < $failures->length; $ii++) {
+          $messages[] = trim($failures->item($ii)->nodeValue, " \n");
+        }
+
+        $user_data .= implode("\n", $messages)."\n";
+      }
+
+      // An errored test is one that had an unanticipated problem. e.g., an
+      // unchecked throwable, or a problem with an implementation of the
+      // test.
+      $errors = $testcase->getElementsByTagName("error");
+      if ($errors->length > 0) {
+        $status = ArcanistUnitTestResult::RESULT_BROKEN;
+        $messages = array();
+        for ($ii = 0; $ii < $errors->length; $ii++) {
+          $messages[] = trim($errors->item($ii)->nodeValue, " \n");
+        }
+
+        $user_data .= implode("\n", $messages)."\n";
+      }
+
+      $result = new ArcanistUnitTestResult();
+      $result->setName($classname.".".$name);
+      $result->setResult($status);
+      $result->setDuration($time);
+      $result->setUserData($user_data);
+
+      $results[] = $result;
+    }
+
+    return $results;
+  }
+
+}