ó
¦V]c           @@  sÿ   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z m Z m	 Z	 d d l
 Z y d d l m Z Wn! e k
 r™ d d l m Z n Xd e j j j f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e	 f d „  ƒ  YZ d S(   s!  unittest-xml-reporting is a PyUnit-based TestRunner that can export test
results to XML files that can be consumed by a wide range of tools, such as
build systems, IDEs and Continuous Integration servers.

This module provides the XMLTestRunner class, which is heavily based on the
default TextTestRunner. This makes the XMLTestRunner very simple to use.

The script below, adapted from the unittest documentation, shows how to use
XMLTestRunner in a very simple way. In fact, the only difference between this
script and the original one is the last line:

import random
import unittest
import xmlrunner

class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.seq = range(10)

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, range(10))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assert_(element in self.seq)

    def test_sample(self):
        self.assertRaises(ValueError, random.sample, self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assert_(element in self.seq)

if __name__ == '__main__':
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
i    (   t   absolute_importN(   t
   TestResultt   _TextTestResultt   TextTestRunner(   t   StringIOt   XMLDocumentc           B@  s   e  Z d  „  Z RS(   c         C@  s&   d | k r |  j  | ƒ S|  j | ƒ S(   Ns   ]]>(   t   createTextNodet   createCDATASection(   t   selft   data(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   createCDATAOrText7   s    (   t   __name__t
   __module__R
   (    (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR   6   s   t	   _TestInfoc           B@  sM   e  Z d  Z e d ƒ \ Z Z Z e d d „ Z d „  Z	 d „  Z
 d „  Z RS(   s\   This class is used to keep useful information about the execution of a
    test method.
    i   c         C@  sv   | |  _  | |  _ | |  _ | |  _ | j rB | j j ƒ  j ƒ  pE d |  _ | j ri | j j ƒ  j ƒ  pl d |  _ d S(   s#   Create a new instance of _TestInfo.t    N(   t   test_resultt   test_methodt   outcomet   errt   stdoutt   getvaluet   stript   stderr(   R   R   R   R   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   __init__E   s    				'c         C@  s   |  j  j |  j  j S(   sU   Return the time that shows how long the test method took to
        execute.
        (   R   t	   stop_timet
   start_time(   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   get_elapsed_timeN   s    c         C@  s   |  j  j |  j ƒ S(   s0   Return a text representation of the test method.(   R   t   getDescriptionR   (   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   get_descriptionT   s    c         C@  s&   |  j  s d S|  j j |  j  |  j ƒ S(   sV   Return a text representation of an exception thrown by a test
        method.
        R   (   R   R   t   _exc_info_to_stringR   (   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   get_error_infoX   s    		N(   R   R   t   __doc__t   ranget   SUCCESSt   FAILUREt   ERRORt   NoneR   R   R   R   (    (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR   =   s   			t   _XMLTestResultc           B@  sÈ   e  Z d  Z e j d d e d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z RS(   sc   A test result class that can express test results in a XML report.

    Used by XMLTestRunner.
    i   c         C@  s>   t  j |  | | | ƒ g  |  _ d |  _ | |  _ t |  _ d S(   s(   Create a new instance of _XMLTestResult.N(   R   R   t	   successesR$   t   callbackt   elapsed_timest   Falset   output_patched(   R   t   streamt   descriptionst	   verbosityR(   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR   g   s
    			c         @  s2   | j  ˆ ƒ ‡  ‡ ‡ ‡ f d †  } | ˆ  _ d S(   sx   Append a _TestInfo to the given target list and sets a callback
        method to be called by stopTest method.
        c           @  sh   ˆ  j  s d ˆ  _ ˆ  _ n  ˆ  j rH ˆ  j j d ˆ j ƒ  ˆ f ƒ n ˆ  j rd ˆ  j j ˆ ƒ n  d S(   sq   This callback prints the test method outcome to the stream,
            as well as the elapsed time.
            i    s
   (%.3fs) %sN(	   R(   R   R   t   showAllR+   t   writelnR   t   dotst   write(    (   R   t	   short_strt	   test_infot   verbose_str(    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR'   v   s    			N(   t   appendR'   (   R   R3   t   target_listR4   R2   R'   (    (   R   R2   R3   R4   s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _prepare_callbackp   s    c         C@  s`   |  j  s. t j t j |  _ |  _ t |  _  n  t ƒ  t ƒ  f \ t _ t _ \ |  _ |  _ d S(   sw   Replace the stdout and stderr streams with string-based streams
        in order to capture the tests' output.
        N(   R*   t   sysR   R   t
   old_stdoutt
   old_stderrt   TrueR   (   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _patch_standard_output†   s    	c         C@  s&   |  j  |  j t _ t _ t |  _ d S(   s&   Restore the stdout and stderr streams.N(   R9   R:   R8   R   R   R)   R*   (   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _restore_standard_output   s    c         C@  sf   |  j  ƒ  t j ƒ  |  _ t j |  | ƒ |  j rb |  j j d |  j | ƒ ƒ |  j j d ƒ n  d S(   s'   Called before execute each test method.s     s    ... N(	   R<   t   timeR   R   t	   startTestR.   R+   R1   R   (   R   t   test(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR?   •   s    
	c         C@  s[   |  j  ƒ  t j |  | ƒ t j ƒ  |  _ |  j rW t |  j ƒ rW |  j ƒ  d |  _ n  d S(   s&   Called after execute each test method.N(   R=   R   t   stopTestR>   R   R'   t   callableR$   (   R   R@   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyRA   Ÿ   s    

c         C@  s&   |  j  t |  | ƒ |  j d d ƒ d S(   s)   Called when a test executes successfully.t   OKt   .N(   R7   R   R&   (   R   R@   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt
   addSuccess©   s    c         C@  s/   |  j  t |  | t j | ƒ |  j d d ƒ d S(   s    Called when a test method fails.t   FAILt   FN(   R7   R   R"   t   failures(   R   R@   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt
   addFailure®   s    c         C@  s/   |  j  t |  | t j | ƒ |  j d d ƒ d S(   s*   Called when a test method raises an error.R#   t   EN(   R7   R   R#   t   errors(   R   R@   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   addError³   s    c         C@  s8  x1| D])} t  | t ƒ r+ | \ } } n  y | j ƒ  } Wn t k
 rT d } n Xy | j ƒ  } WnA t k
 r¨ y | j ƒ  } Wq© t k
 r¤ t | ƒ } q© Xn Xy | j ƒ  } Wn t k
 rØ t | ƒ } n X|  j j	 |  j
 ƒ |  j j	 d | | | f ƒ |  j j	 |  j ƒ |  j j	 d | ƒ q Wd S(   s=   Write some information about the FAIL or ERROR to the stream.i    s   %s [%.3fs]: %ss   %sN(   t
   isinstancet   tupleR   t   AttributeErrorR   R   t   strR   R+   R/   t
   separator1t
   separator2(   R   t   flavourRK   R3   t   exc_infot   tt   descrt   err_info(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   printErrorList¸   s,    
c         C@  sÇ   i  } xº |  j  |  j |  j f D]  } x— | D] } t | t ƒ sS d | f GHq, n  t | j ƒ } | j d } | d k r„ d } n  | | j } | | k rª g  | | <n  | | j	 | ƒ q, Wq W| S(   s¾   This method organizes test results by TestCase module. This
        information is used during the report generation, where a XML report
        will be generated for each TestCase.
        s   Unexpected test result type: %rRD   s	   __main__.R   (
   R&   RH   RK   RM   R   t   typeR   R   R   R5   (   R   t   tests_by_testcaset   testsR3   t   testcaset   modulet   testcase_name(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _get_info_by_testcaseÓ   s    	c         C@  s  | j  d ƒ } | j | ƒ | j d t |  ƒ ƒ | j d t t | ƒ ƒ ƒ | j d d t g  | D] } | j ƒ  ^ qd ƒ ƒ t g  | D] } | j t j	 k r‹ d ^ q‹ ƒ } | j d t | ƒ ƒ t g  | D] } | j t j
 k rÒ d ^ qÒ ƒ } | j d t | ƒ ƒ | S(	   s2   Appends the testsuite section to the XML document.t	   testsuitet   nameR[   R>   s   %.3fi   RH   RK   (   t   createElementt   appendChildt   setAttributeRP   t   lent   sumR   R   R   R"   R#   (   t
   suite_nameR[   t   xml_documentR`   t   eRH   RK   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _report_testsuiteí   s    '11c   	      C@  s.  | j  d ƒ } | j | ƒ | j d t |  ƒ ƒ | j d | j j ƒ  pe t | j d t | j ƒ ƒ ƒ | j d d | j ƒ  ƒ | j t	 j
 k r*d | j d	 } | j  | ƒ } | j | ƒ | j d
 t | j d j ƒ ƒ | j d t | j d	 ƒ ƒ | j ƒ  } | j | ƒ } | j | ƒ n  d S(   s/   Appends a testcase section to the XML document.R\   t	   classnameRa   t   _testMethodNameR>   s   %.3ft   failuret   errori   RY   i    t   messageN(   Rm   Rn   (   Rb   Rc   Rd   RP   R   t   shortDescriptiont   getattrR   R   R   R!   R   R   R   R
   (	   Rg   R   t   xml_testsuiteRh   R\   t	   elem_nameRm   t
   error_infot   failureText(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _report_testcase  s      c   	      C@  st   | j  d ƒ } | j | ƒ | j | ƒ } | j | ƒ | j  d ƒ } | j | ƒ | j | ƒ } | j | ƒ d S(   sC   Appends the system-out and system-err sections to the XML document.s
   system-outs
   system-errN(   Rb   Rc   R
   (	   t   test_runnerRr   Rh   R   R   t	   systemoutt   systemout_textt	   systemerrt   systemerr_text(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _report_output  s    c         C@  s¹  |  j  ƒ  } t | j ƒ t k rJ t j j | j ƒ rJ t j | j ƒ n  xh| j ƒ  D]Z\ } } t	 ƒ  } t
 j | | | ƒ } g  g  } } xz | D]r }	 t
 j | |	 | | ƒ |	 j rÜ | j d |	 j ƒ  |	 j g ƒ n  |	 j r• | j d |	 j ƒ  |	 j g ƒ q• q• Wt
 j | | | d j | ƒ d j | ƒ ƒ | j d d ƒ }
 t | j ƒ t k r¡t d | j t j | f d ƒ } z | j |
 ƒ Wd | j ƒ  XqW | j j |
 ƒ qW Wd S(   s:   Generates the XML reports to a given XMLTestRunner object.s   *****************s   
t   indents   	s   %s%sTEST-%s.xmlt   wN(   R_   RY   t   outputRP   t   ost   patht   existst   makedirst   itemsR   R%   Rj   Rv   R   t   extendR   R   R|   t   joint   toprettyxmlt   opent   sepR1   t   close(   R   Rw   t   all_resultst   suiteR[   t   docR`   R   R   R@   t   xml_contentt   report_file(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   generate_reports+  s0    		"	&(   R   R   R   R8   R   R;   R   R7   R<   R=   R?   RA   RE   RI   RL   RX   R_   Rj   t   staticmethodRv   R|   R   (    (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR%   b   s(   		
		
	
								t   XMLTestRunnerc           B@  s8   e  Z d  Z d d e e e d „ Z d „  Z d „  Z RS(   sJ   A test runner class that outputs the results in JUnit like XML files.
    RD   c         C@  sN   | d k r t j } n  d | } t j |  | | | ƒ | |  _ | |  _ d S(   s'   Create a new instance of XMLTestRunner.i   i   N(   i   i   (   R$   R8   R   R   R   R   R(   (   R   R   R+   R,   t   verboseR(   R-   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR   R  s    
	c         C@  s   t  |  j |  j |  j |  j ƒ S(   so   Create the TestResult object which will be used to store
        information about the executed tests.
        (   R%   R+   R,   R-   R(   (   R   (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   _make_result[  s    c   	      C@  s©  |  j  ƒ  } |  j j ƒ  |  j j d ƒ |  j j | j ƒ t j ƒ  } | | ƒ t j ƒ  } | | } |  j j ƒ  |  j j d ƒ | j |  ƒ | j ƒ  |  j j | j ƒ | j } |  j j d | | d k rÙ d pÜ d | f ƒ |  j j ƒ  | j ƒ  s•|  j j	 d ƒ t
 | j ƒ t
 | j ƒ } } | rL|  j j	 d | ƒ n  | r‚| rk|  j j	 d	 ƒ n  |  j j	 d
 | ƒ n  |  j j d ƒ n |  j j d ƒ | S(   s&   Run the given test case or test suite.s   Running tests...s   Generating XML reports...s   Ran %d test%s in %.3fsi   t   sR   s   FAILED (s   failures=%ds   , s	   errors=%dt   )RC   (   R”   R+   R/   RR   R>   R   t   printErrorst   testsRunt   wasSuccessfulR1   Re   RH   RK   (	   R   R@   t   resultR   R   t
   time_takent   runt   failedt   errored(    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyRœ   b  s:    


	#N(	   R   R   R   R$   R;   R)   R   R”   Rœ   (    (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyR’   O  s   		(   R   t
   __future__R    R€   R8   R>   t   unittestR   R   R   t   xml.dom.minidomt   xmlR   t   ImportErrort   iot   domt   minidomt   DocumentR   t   objectR   R%   R’   (    (    (    s5   lib/python2.7/site-packages/Cython/Tests/xmlrunner.pyt   <module>'   s   %í