Permalink
Browse files

Added XML output for conversion to Junit and conversion tools

  • Loading branch information...
1 parent 321e4e5 commit 6a6a8e888d2fe24506a8e3ea88f7c4a990baf156 @chemicaloliver chemicaloliver committed Oct 29, 2011
Showing with 280 additions and 0 deletions.
  1. +47 −0 build/simpletest_to_junit.xsl
  2. +4 −0 build/unit_test_xml.sh
  3. +28 −0 tests/simpletest/xmltime.php
  4. +201 −0 unit_test_xml.php
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+ version="1.0">
+
+ <!-- convert xml output generated by simpletest xml into junit xml format -->
+ <xsl:output method="xml"/>
+
+ <xsl:template match="/">
+ <xsl:apply-templates select="run/group"/>
+ </xsl:template>
+
+ <xsl:template match="run/group">
+ <testsuite>
+ <xsl:attribute name="errors"><xsl:value-of select="count(.//exception)"/></xsl:attribute>
+ <xsl:attribute name="failures"><xsl:value-of select="count(.//fail)"/></xsl:attribute>
+ <xsl:attribute name="tests"><xsl:value-of select="count(.//test)"/></xsl:attribute>
+ <xsl:attribute name="name"><xsl:value-of select="name"/></xsl:attribute>
+ <xsl:attribute name="time"><xsl:value-of select="sum(//time)"/></xsl:attribute>
+ <xsl:apply-templates select=".//case/test"/>
+ <xsl:copy-of select="//system-err"/>
+ </testsuite>
+ </xsl:template>
+
+ <xsl:template match="case/test">
+ <testcase>
+ <xsl:attribute name="classname"><xsl:value-of select="../name"/></xsl:attribute>
+ <xsl:attribute name="name"><xsl:value-of select="name"/></xsl:attribute>
+ <xsl:attribute name="time"><xsl:value-of select="time"/></xsl:attribute>
+ <xsl:apply-templates select="fail"/>
+ <xsl:apply-templates select="exception"/>
+ </testcase>
+ </xsl:template>
+
+ <xsl:template match="fail">
+ <failure><xsl:attribute name="message"><xsl:value-of select="."/></xsl:attribute>
+ <!-- content is for stacktrace; not available / broken out by simpletest -->
+ </failure>
+ </xsl:template>
+
+ <xsl:template match="exception">
+ <!-- assuming same format as fail -->
+ <error><xsl:attribute name="message"><xsl:value-of select="."/></xsl:attribute>
+ <!-- content is for stacktrace; not available / broken out by simpletest -->
+ </error>
+ </xsl:template>
+
+</xsl:stylesheet>
View
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+php ../unit_test_xml.php > results.xml
+xsltproc simpletest_to_junit.xsl results.xml > results-1.xml
@@ -0,0 +1,28 @@
+<?php
+
+ /**
+ * extend default XmlReporter to record & report time to run each test method
+ */
+
+class XmlTimeReporter extends XmlReporter {
+ var $pre;
+
+ function __construct($test_name = FALSE) {
+ $this->pre = microtime();
+ parent::__construct($test_name);
+ }
+
+ function paintMethodEnd($test_name) {
+ $post = microtime();
+ if ($this->pre != null) {
+ $duration = $post - $this->pre;
+ // how can post time be less than pre? assuming zero if this happens..
+ if ($post < $this->pre) $duration = 0;
+ print $this->getIndent(1);
+ print "<time>$duration</time>\n";
+ }
+ parent::paintMethodEnd($test_name);
+ $this->pre = null;
+ }
+
+}
View
@@ -0,0 +1,201 @@
+<?php
+/**
+ * Please note this file shouldn't be exposed on a live server,
+ * there is no filtering of $_POST!!!!
+ */
+error_reporting(0);
+if(isset($argv))
+{
+ $cli_mode = setup_cli($argv); // Determines if running in cli mode
+}
+else
+{
+ $cli_mode = false;
+}
+/**
+ * Configure your paths here:
+ */
+define('MAIN_PATH', realpath(dirname(__FILE__)).'/');
+define('SIMPLETEST', MAIN_PATH .'tests/simpletest/'); // Directory of simpletest
+define('ROOT', MAIN_PATH); // Directory of codeigniter index.php
+define('TESTS_DIR', MAIN_PATH . 'tests/'); // Directory of your tests.
+define('APP_DIR', MAIN_PATH . 'application/'); // CodeIgniter Application directory
+
+
+//do not use autorun as it output ugly report upon no test run
+require_once SIMPLETEST . 'unit_tester.php';
+require_once SIMPLETEST . 'mock_objects.php';
+require_once SIMPLETEST . 'collector.php';
+require_once SIMPLETEST . 'web_tester.php';
+require_once SIMPLETEST . 'extensions/my_reporter.php';
+require_once SIMPLETEST . 'xml.php';
+require_once SIMPLETEST . 'xmltime.php';
+
+$test_suite = new TestSuite();
+$test_suite->_label = 'CodeIgniter Test Suite';
+
+class CodeIgniterUnitTestCase extends UnitTestCase {
+ protected $_ci;
+
+ public function __construct($name = '')
+ {
+ parent::__construct($name);
+ $this->_ci =& get_instance();
+ }
+
+ public function __get($var)
+ {
+ return $this->_ci->$var;
+ }
+}
+
+class CodeIgniterWebTestCase extends WebTestCase {
+ protected $_ci;
+
+ public function __construct($name = '')
+ {
+ parent::__construct($name);
+ $this->_ci =& get_instance();
+ }
+
+ public function __get($var)
+ {
+ return $this->_ci->$var;
+ }
+}
+
+// Because get is removed in ci we pull it out here.
+$run_all = FALSE;
+if (isset($_GET['all']) || isset($_POST['all']))
+{
+ $run_all = TRUE;
+}
+
+
+
+//Capture CodeIgniter output, discard and load system into $CI variable
+ob_start();
+ include(ROOT . 'index.php');
+ $CI =& get_instance();
+ob_end_clean();
+
+$CI->load->library('session');
+$CI->session->sess_destroy();
+
+$CI->load->helper('directory');
+
+
+// Get all main tests
+if ($run_all OR ( ! empty($_POST) && ! isset($_POST['test'])))
+{
+ $test_objs = array('controllers','models','views','libraries','bugs','helpers');
+
+ foreach ($test_objs as $obj)
+ {
+ if (isset($_POST[$obj]) OR $run_all)
+ {
+ $dir = TESTS_DIR . $obj;
+ $dir_files = directory_map($dir);
+ foreach ($dir_files as $file)
+ {
+ if ($file != 'index.html')
+ {
+ $test_suite->addFile($dir . '/' . $file);
+ }
+ }
+ }
+ }
+}
+elseif (isset($_POST['test'])) //single test
+{
+ $file = $_POST['test'];
+
+ if (file_exists(TESTS_DIR . $file))
+ {
+ $test_suite->addFile(TESTS_DIR . $file);
+ }
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Function to determine if in cli mode and if so set up variables to make it work
+ *
+ * @param Array of commandline args
+ * @return Boolean true or false if commandline mode setup
+ *
+ */
+function setup_cli($argv)
+{
+ if (php_sapi_name() == 'cli')
+ {
+ if(isset($argv[1]))
+ {
+ if(stripos($argv[1],'.php') !== false)
+ {
+ $_POST['test'] = $argv[1];
+ }
+ else
+ {
+ $_POST[$argv[1]] = $argv[1];
+ }
+ }
+ else
+ {
+ $_POST['all'] = 'all';
+ }
+ $_SERVER['HTTP_HOST'] = '';
+ $_SERVER['REQUEST_URI'] = '';
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Function to map tests and strip .html files.
+ *
+ *
+ * @param string
+ * @return array
+ */
+function map_tests($location = '')
+{
+ if (empty($location))
+ {
+ return FALSE;
+ }
+
+ $files = directory_map($location);
+ $return = array();
+
+ foreach ($files as $file)
+ {
+ if ($file != 'index.html')
+ {
+ $return[] = $file;
+ }
+ }
+ return $return;
+}
+
+//variables for report
+$controllers = map_tests(TESTS_DIR . 'controllers');
+$models = map_tests(TESTS_DIR . 'models');
+$views = map_tests(TESTS_DIR . 'views');
+$libraries = map_tests(TESTS_DIR . 'libraries');
+$bugs = map_tests(TESTS_DIR . 'bugs');
+$helpers = map_tests(TESTS_DIR . 'helpers');
+$form_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+
+//display the form
+//if ($cli_mode) {
+// exit ($test_suite->run(new TextReporter()) ? 0 : 1);
+//}
+//else {
+// include(TESTS_DIR . 'test_gui.php');
+//}
+
+if($cli_mode) {
+ exit ($test_suite->run(new XMLtimeReporter()) ? 0 : 1); }
+ else {
+ include(TESTS_DIR . 'test_gui.php');}

0 comments on commit 6a6a8e8

Please sign in to comment.