
    -Ph9                         d 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	ddl
mZ  G d de	j        j        j                  Z G d d          Z G d	 d
e          Z G d de          ZdS )a'  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.assertTrue(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.assertTrue(element in self.seq)

if __name__ == '__main__':
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
    N)
TestResultTextTestResultTextTestRunner)StringIOc                       e Zd Zd ZdS )XMLDocumentc                 ^    d|v r|                      |          S |                     |          S )Nz]]>)createTextNodecreateCDATASection)selfdatas     V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/Tests/xmlrunner.pycreateCDATAOrTextzXMLDocument.createCDATAOrText2   s3    D==&&t,,,&&t,,,    N)__name__
__module____qualname__r    r   r   r   r   1   s#        - - - - -r   r   c                   N    e Zd ZdZ ed          \  ZZZedfdZd Z	d Z
d ZdS )	_TestInfoz\This class is used to keep useful information about the execution of a
    test method.
       Nc                 "   || _         || _        || _        || _        |j        r+|j                                                                        pd| _        |j        r+|j                                                                        pd| _        dS )z#Create a new instance of _TestInfo. N)test_resulttest_methodoutcomeerrstdoutgetvaluestripstderr)r   r   r   r   r   s        r   __init__z_TestInfo.__init__@   s    &&!(R[-?-H-H-J-J-P-P-R-RXVX!(R[-?-H-H-J-J-P-P-R-RXVXr   c                 4    | j         j        | j         j        z
  S )zUReturn the time that shows how long the test method took to
        execute.
        )r   	stop_time
start_timer   s    r   get_elapsed_timez_TestInfo.get_elapsed_timeI   s     )D,<,GGGr   c                 @    | j                             | j                  S )z0Return a text representation of the test method.)r   getDescriptionr   r&   s    r   get_descriptionz_TestInfo.get_descriptionO   s    ..t/?@@@r   c                 ^    | j         sdS | j                            | j         | j                  S )zVReturn a text representation of an exception thrown by a test
        method.
        r   )r   r   _exc_info_to_stringr   r&   s    r   get_error_infoz_TestInfo.get_error_infoS   s8     x 	233Hd&( ( 	(r   )r   r   r   __doc__rangeSUCCESSFAILUREERRORr"   r'   r*   r-   r   r   r   r   r   8   s         
 !&aWgu9@d Y Y Y YH H HA A A( ( ( ( (r   r   c                       e Zd ZdZej        dddf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dS )_XMLTestResultzcA test result class that can express test results in a XML report.

    Used by XMLTestRunner.
       Tc                 l    t          j        | |||           g | _        d| _        || _        d| _        dS )z(Create a new instance of _XMLTestResult.NF)r   r"   	successescallbackelapsed_timesoutput_patched)r   streamdescriptions	verbosityr9   s        r   r"   z_XMLTestResult.__init__b   s?     	flIFFF*#r   c                 V     |                                 fd}| _        dS )zxAppend a _TestInfo to the given target list and sets a callback
        method to be called by stopTest method.
        c                       j         sdx _         _         j        r3 j                            d                                fz             dS  j        r j                                       dS dS )zqThis callback prints the test method outcome to the stream,
            as well as the elapsed time.
            r   z
(%.3fs) %sN)	r9   r%   r$   showAllr;   writelnr'   dotswrite)r   	short_str	test_infoverbose_strs   r   r8   z2_XMLTestResult._prepare_callback.<locals>.callbackp   s     % 5344$.| -##L//11;?%@ A A A A A -!!),,,,,- -r   N)appendr8   )r   rE   target_listrF   rD   r8   s   `` `` r   _prepare_callbackz _XMLTestResult._prepare_callbackk   sQ     	9%%%	- 	- 	- 	- 	- 	- 	- 	- !r   c                     | j         s*t          j        t          j        c| _        | _        d| _         t                      t                      fx\  t          _        t          _        \  | _        | _        dS )zwReplace the stdout and stderr streams with string-based streams
        in order to capture the tests' output.
        TN)r:   sysr   r!   
old_stdout
old_stderrr   r&   s    r   _patch_standard_outputz%_XMLTestResult._patch_standard_output   sZ     " 	'25*cj.T_do"&DZZ$	% SZ#=DKr   c                 Z    | j         | j        ct          _        t          _        d| _        dS )z&Restore the stdout and stderr streams.FN)rL   rM   rK   r   r!   r:   r&   s    r   _restore_standard_outputz'_XMLTestResult._restore_standard_output   s$    $(OT_ SZ#r   c                 .   |                                   t          j                    | _        t          j        | |           | j        rL| j                            d|                     |          z              | j                            d           dS dS )z'Called before execute each test method.z  z ... N)	rN   timer%   r   	startTestr@   r;   rC   r)   r   tests     r   rS   z_XMLTestResult.startTest   s    ##%%%)++T4(((< 	'KdT%8%8%>%>>???Kg&&&&&	' 	'r   c                     |                                   t          j        | |           t          j                    | _        | j        r1t          | j                  r|                                  d| _        dS dS dS )z&Called after execute each test method.N)rP   r   stopTestrR   r$   r8   callablerT   s     r   rW   z_XMLTestResult.stopTest   s{    %%'''d+++= 	!Xdm44 	!MMOOO DMMM	! 	! 	! 	!r   c                 \    |                      t          | |          | j        dd           dS )z)Called when a test executes successfully.OK.N)rI   r   r7   rT   s     r   
addSuccessz_XMLTestResult.addSuccess   s9    yt44#~tS	: 	: 	: 	: 	:r   c                 t    |                      t          | |t          j        |          | j        dd           dS )z Called when a test method fails.FAILFN)rI   r   r1   failuresr   rU   r   s      r   
addFailurez_XMLTestResult.addFailure   s@    ytY5FLL#}fc	; 	; 	; 	; 	;r   c                 t    |                      t          | |t          j        |          | j        dd           dS )z*Called when a test method raises an error.r2   EN)rI   r   r2   errorsra   s      r   addErrorz_XMLTestResult.addError   s?    ytY_cJJ#{GS	: 	: 	: 	: 	:r   c                    |D ]P}t          |t                    r|\  }}	 |                                }n# t          $ r d}Y nw xY w	 |                                }nE# t          $ r8 	 |                                }n# t          $ r t          |          }Y nw xY wY nw xY w	 |                                }n# t          $ r t          |          }Y nw xY w| j        	                    | j
                   | j        	                    d|||fz             | j        	                    | j                   | j        	                    d|z             RdS )z=Write some information about the FAIL or ERROR to the stream.r   z%s [%.3fs]: %sz%sN)
isinstancetupler'   AttributeErrorr*   r)   strr-   r;   rA   
separator1
separator2)r   flavourre   rE   exc_infotdescrerr_infos           r   printErrorListz_XMLTestResult.printErrorList   s    	1 	1I)U++ 0&/#	8..00!   +!1133! + + ++%4466EE% + + +	NNEEE++
*$3355! * * *y>>* K000K 0GQ3F FGGGK000Kx0000/	1 	1sW   6AA	A
B )A>=B >BB BB B $B99CCc                 :   i }| j         | j        | j        fD ]}|D ]~}t          |t                    st          d|           *t          |j                  }|j        dz   }|dk    rd}||j	        z   }||vrg ||<   ||         
                    |           |S )zThis 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.
        zUnexpected test result type: r[   z	__main__.r   )r7   r`   re   rh   r   printtyper   r   r   rG   )r   tests_by_testcasetestsrE   testcasemoduletestcase_names          r   _get_info_by_testcasez$_XMLTestResult._get_info_by_testcase   s    
 ndmT[A 	C 	CE" C C	!)Y77 EyyJKKK	 566 ",s2[((F &): : (99979%m4!-077	BBBBC  ! r   c                 P   |                     d          }|                    |           |                    dt          |                      |                    dt          t	          |                               |                    ddt          d |D                       z             t	          d |D                       }|                    dt          |                     t	          d	 |D                       }|                    d
t          |                     |S )z2Appends the testsuite section to the XML document.	testsuitenamerx   rR   %.3fc                 6    g | ]}|                                 S r   )r'   .0es     r   
<listcomp>z4_XMLTestResult._report_testsuite.<locals>.<listcomp>   s$    555!##%%555r   c                 <    g | ]}|j         t          j        k    d S r5   )r   r   r1   r   s     r   r   z4_XMLTestResult._report_testsuite.<locals>.<listcomp>   s'    KKKaAI9J,J,J,J,J,Jr   r`   c                 <    g | ]}|j         t          j        k    d S r   )r   r   r2   r   s     r   r   z4_XMLTestResult._report_testsuite.<locals>.<listcomp>   s&    GGGA!)y*F*Fa*F*F*Fr   re   )createElementappendChildsetAttributerk   lensum)
suite_namerx   xml_documentr~   r`   re   s         r   _report_testsuitez _XMLTestResult._report_testsuite   s    ..{;;	  +++vs:777wCJJ888vv55u55566(7 	8 	8 	8 KK5KKKLLz3x==999GGGGGHHxV555r   c           
         |                     d          }|                    |           |                    dt          |                      |                    d|j                                        p't          |j        dt          |j                                       |                    dd|                                z             |j        t          j
        k    rd|j        dz
           }|                     |          }|                    |           |                    d	t          |j        d
         j                             |                    dt          |j        d                              |                                }|                    |          }|                    |           dS dS )z/Appends a testcase section to the XML document.ry   	classnamer   _testMethodNamerR   r   )failureerrorr5   rv   r   messageN)r   r   r   rk   r   shortDescriptiongetattrr'   r   r   r0   r   r   r-   r   )	r   r   xml_testsuiter   ry   	elem_namer   
error_infofailureTexts	            r   _report_testcasez_XMLTestResult._report_testcase   s   --j99!!(+++k3z??;;;fk&=&N&N&P&P 'G!()@BS),[-D)E)E"G "G	H 	H 	H 	ff{/K/K/M/M&MNNN9#444,[-@-BCI"00;;G  )))  [_Q-?-H)I)IJJJ  C0B,C,CDDD$3355J&88DDK,,,,, 54r   c                 V   |                     d          }|                    |           |                    |          }|                    |           |                     d          }|                    |           |                    |          }|                    |           dS )zCAppends the system-out and system-err sections to the XML document.z
system-outz
system-errN)r   r   r   )	test_runnerr   r   r   r!   	systemoutsystemout_text	systemerrsystemerr_texts	            r   _report_outputz_XMLTestResult._report_output  s     ..|<<	!!),,,%77??n--- ..|<<	!!),,,%77??n-----r   c           
         |                                  }t          |j        t                    r=t          j                            |j                  st	          j        |j                   |                                D ]\  }}t                      }t                              |||          }g g }}|D ]}	t                              ||	||           |	j        r/|                    d|	                                |	j        g           |	j        r/|                    d|	                                |	j        g           t                              |||d                    |          d                    |                     |                    d          }
t)          |j                  t          u rlt+          |j        t          j        d|dd          }	 |                    |
           |                                 # |                                 w xY w|j                            |
           dS )	z:Generates the XML reports to a given XMLTestRunner object.z*****************
	)indentzTEST-z.xmlwN)r|   rh   outputrk   ospathexistsmakedirsitemsr   r4   r   r   r   extendr*   r!   r   jointoprettyxmlrv   openseprC   close)r   r   all_resultssuiterx   docr~   r   r!   rU   xml_contentreport_files               r   generate_reportsz_XMLTestResult.generate_reports%  s4   0022k(#.. 	,rw~~kFX7Y7Y 	,K*+++'--// 	6 	6LE5--C '88sKKIFF ^ ^//tYLLL; ^MM#68L8L8N8NPTP["\]]]; ^MM#68L8L8N8NPTP["\]]]))+y#*.))F*;*;TYYv=N=NP P P///66KK&''3.." ''$89<> >(%%k222%%''''K%%'''' "((55553	6 	6s   H

H N)r   r   r   r.   rK   r!   r"   rI   rN   rP   rS   rW   r\   rb   rf   rs   r|   r   staticmethodr   r   r   r   r   r   r4   r4   ]   sI         "jqA#$ $ $ $! ! !*% % %$ $ $
' ' '! ! !: : :
; ; ;
: : :
1 1 16! ! !4  & %%677- - -. $|$455. . . "\.11N 6  6  6  6  6r   r4   c                   &    e Zd ZdZd	dZd Zd ZdS )
XMLTestRunnerzJA test runner class that outputs the results in JUnit like XML files.
    r[   NTFc                 |    |t           j        }d|         }t          j        | |||           || _        || _        dS )z'Create a new instance of XMLTestRunner.N)r5      )rK   r!   r   r"   r   r9   )r   r   r;   r<   verboser9   r=   s          r   r"   zXMLTestRunner.__init__K  sC    >ZF7O	flIFFF*r   c                 N    t          | j        | j        | j        | j                  S )zoCreate the TestResult object which will be used to store
        information about the executed tests.
        )r4   r;   r<   r=   r9   r&   s    r   _make_resultzXMLTestRunner._make_resultT  s)     dk4+<ND.0 0 	0r   c                    |                                  }| j                                         | j                            d           | j                            |j                   t	          j                    } ||           t	          j                    }||z
  }| j                                         | j                            d           |                    |            |                                 | j                            |j                   |j        }| j                            d||dk    rdpd|fz             | j                                         |                                s| j        	                    d           t          |j                  t          |j                  }}|r| j        	                    d|z             |r9|r| j        	                    d	           | j        	                    d
|z             | j                            d           n| j                            d           |S )z&Run the given test case or test suite.zRunning tests...zGenerating XML reports...zRan %d test%s in %.3fsr5   sr   zFAILED (zfailures=%dz, z	errors=%d)rZ   )r   r;   rA   rm   rR   r   printErrorstestsRunwasSuccessfulrC   r   r`   re   )	r   rU   resultr%   r$   
time_takenrunfailederroreds	            r   r   zXMLTestRunner.run[  s.    ""$$ 	.///F-... Y[[
VIKK	+
 	7888%%% 	F-...o4#("s(b*56 	7 	7 	7 ##%% 	&Kj)))"6?33S5G5GGF :!!-&"8999 9 ,K%%d+++!!+"7888K$$$$K%%%r   )r[   NTFT)r   r   r   r.   r"   r   r   r   r   r   r   r   H  sP         + + + +0 0 0+ + + + +r   r   )r.   r   rK   rR   unittestr   r   r   xml.dom.minidomxmlior   domminidomDocumentr   r   r4   r   r   r   r   <module>r      s*  $ $N 
			 



  ? ? ? ? ? ? ? ? ? ?          - - - - -#'/* - - -"( "( "( "( "( "( "( "(Jh6 h6 h6 h6 h6^ h6 h6 h6V> > > > >N > > > > >r   