a
    ûbÑ9  ã                   @   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zddlmZ W n eyr   ddlmZ Y n0 G dd„ dejjjƒZG dd	„ d	eƒZG d
d„ deƒZG d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'))
é    )Úabsolute_importN)Ú
TestResultÚTextTestResultÚTextTestRunner)ÚStringIOc                   @   s   e Zd Zdd„ ZdS )ÚXMLDocumentc                 C   s   d|v r|   |¡S |  |¡S )Nz]]>)ZcreateTextNodeZcreateCDATASection)ÚselfÚdata© r
   úf/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Tests/xmlrunner.pyÚcreateCDATAOrText7   s    
zXMLDocument.createCDATAOrTextN)Ú__name__Ú
__module__Ú__qualname__r   r
   r
   r
   r   r   6   s   r   c                   @   sD   e Zd ZdZedƒ\ZZZedfdd„Zdd„ Z	dd	„ Z
d
d„ ZdS )Ú	_TestInfoz\This class is used to keep useful information about the execution of a
    test method.
    é   Nc                 C   sP   || _ || _|| _|| _|jr,|j ¡  ¡ p.d| _|jrF|j ¡  ¡ pHd| _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   r
   r
   r   Ú__init__E   s    z_TestInfo.__init__c                 C   s   | j j| j j S )zUReturn the time that shows how long the test method took to
        execute.
        )r   Ú	stop_timeÚ
start_time©r   r
   r
   r   Úget_elapsed_timeN   s    z_TestInfo.get_elapsed_timec                 C   s   | j  | j¡S )z0Return a text representation of the test method.)r   ÚgetDescriptionr   r   r
   r
   r   Úget_descriptionT   s    z_TestInfo.get_descriptionc                 C   s   | j s
dS | j | j | j¡S )zVReturn a text representation of an exception thrown by a test
        method.
        r   )r   r   Z_exc_info_to_stringr   r   r
   r
   r   Úget_error_infoX   s
    ÿz_TestInfo.get_error_info)r   r   r   Ú__doc__ÚrangeÚSUCCESSÚFAILUREÚERRORr   r   r!   r"   r
   r
   r
   r   r   =   s   	r   c                   @   s¬   e Zd ZdZejdddfdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZd d!„ Zd"S )#Ú_XMLTestResultzcA test result class that can express test results in a XML report.

    Used by XMLTestRunner.
    é   Tc                 C   s,   t  | |||¡ g | _d| _|| _d| _dS )z(Create a new instance of _XMLTestResult.NF)r   r   Ú	successesÚcallbackÚelapsed_timesÚoutput_patched)r   ÚstreamÚdescriptionsÚ	verbosityr,   r
   r
   r   r   g   s
    z_XMLTestResult.__init__c                    s&   |  ˆ¡ ‡ ‡‡‡fdd„}|ˆ _dS )zxAppend a _TestInfo to the given target list and sets a callback
        method to be called by stopTest method.
        c                      sH   ˆ j sd ˆ _ˆ _ˆ jr2ˆ j dˆ ¡ ˆf ¡ nˆ jrDˆ j ˆ¡ dS )zqThis callback prints the test method outcome to the stream,
            as well as the elapsed time.
            r   z
(%.3fs) %sN)	r,   r   r   ÚshowAllr.   Úwritelnr   ÚdotsÚwriter
   ©r   Ú	short_strÚ	test_infoÚverbose_strr
   r   r+   v   s    
ÿz2_XMLTestResult._prepare_callback.<locals>.callbackN)Úappendr+   )r   r7   Ztarget_listr8   r6   r+   r
   r5   r   Ú_prepare_callbackp   s    
z _XMLTestResult._prepare_callbackc                 C   sB   | j stjtj | _| _d| _ tƒ tƒ f \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   r
   r
   r   Ú_patch_standard_output†   s
    
ÿz%_XMLTestResult._patch_standard_outputc                 C   s   | j | j t_t_d| _dS )z&Restore the stdout and stderr streams.FN)r<   r=   r;   r   r   r-   r   r
   r
   r   Ú_restore_standard_output   s    z'_XMLTestResult._restore_standard_outputc                 C   sJ   |   ¡  t ¡ | _t | |¡ | jrF| j d|  |¡ ¡ | j d¡ dS )z'Called before execute each test method.z  z ... N)	r>   Útimer   r   Ú	startTestr1   r.   r4   r    ©r   Útestr
   r
   r   rA   •   s    
z_XMLTestResult.startTestc                 C   s@   |   ¡  t | |¡ t ¡ | _| jr<t| jƒr<|  ¡  d| _dS )z&Called after execute each test method.N)r?   r   ÚstopTestr@   r   r+   ÚcallablerB   r
   r
   r   rD   Ÿ   s    
z_XMLTestResult.stopTestc                 C   s   |   t| |ƒ| jdd¡ dS )z)Called when a test executes successfully.ÚOKÚ.N)r:   r   r*   rB   r
   r
   r   Ú
addSuccess©   s    ÿz_XMLTestResult.addSuccessc                 C   s"   |   t| |tj|ƒ| jdd¡ dS )z Called when a test method fails.ÚFAILÚFN)r:   r   r&   Úfailures©r   rC   r   r
   r
   r   Ú
addFailure®   s    ÿz_XMLTestResult.addFailurec                 C   s"   |   t| |tj|ƒ| jdd¡ dS )z*Called when a test method raises an error.r'   ÚEN)r:   r   r'   ÚerrorsrL   r
   r
   r   ÚaddError³   s    ÿz_XMLTestResult.addErrorc                 C   sö   |D ]ì}t |tƒr|\}}z| ¡ }W n ty<   d}Y n0 z| ¡ }W n: ty„   z| ¡ }W n ty~   t|ƒ}Y n0 Y n0 z| ¡ }W n ty¬   t|ƒ}Y n0 | j 	| j
¡ | j 	d|||f ¡ | j 	| j¡ | j 	d| ¡ qd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.   r2   Z
separator1Ú
separator2)r   ÚflavourrO   r7   Úexc_infoÚtÚdescrZerr_infor
   r
   r   ÚprintErrorList¸   s,    

z_XMLTestResult.printErrorListc                 C   sŠ   i }| j | j| jfD ]p}|D ]f}t|tƒs:td|f ƒ qt|jƒ}|jd }|dkrZd}||j	 }||vrtg ||< ||  
|¡ qq|S )z¾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.
        zUnexpected test result type: %rrG   z	__main__.r   )r*   rK   rO   rQ   r   ÚprintÚtyper   r   r   r9   )r   Ztests_by_testcaseÚtestsr7   ÚtestcaseÚmoduleZtestcase_namer
   r
   r   Ú_get_info_by_testcaseÓ   s    



z$_XMLTestResult._get_info_by_testcasec                 C   sž   |  d¡}| |¡ | dt| ƒ¡ | dtt|ƒƒ¡ | ddtdd„ |D ƒƒ ¡ tdd„ |D ƒƒ}| d	t|ƒ¡ td
d„ |D ƒƒ}| dt|ƒ¡ |S )z2Appends the testsuite section to the XML document.Ú	testsuiteÚnamer]   r@   ú%.3fc                 S   s   g | ]}|  ¡ ‘qS r
   )r   ©Ú.0Úer
   r
   r   Ú
<listcomp>ö   ó    z4_XMLTestResult._report_testsuite.<locals>.<listcomp>c                 S   s   g | ]}|j tjkrd ‘qS ©r)   )r   r   r&   rd   r
   r
   r   rg   ø   rh   rK   c                 S   s   g | ]}|j tjkrd ‘qS ri   )r   r   r'   rd   r
   r
   r   rg   û   rh   rO   )ÚcreateElementÚappendChildÚsetAttributerT   ÚlenÚsum)Ú
suite_namer]   Úxml_documentra   rK   rO   r
   r
   r   Ú_report_testsuiteí   s    

ÿz _XMLTestResult._report_testsuitec           	      C   sÚ   |  d¡}| |¡ | dt| ƒ¡ | d|j ¡ pFt|jdt|jƒƒ¡ | dd| ¡  ¡ |jt	j
krÖd|jd  }|  |¡}| |¡ | d	t|jd
 jƒ¡ | dt|jd ƒ¡ | ¡ }| |¡}| |¡ dS )z/Appends a testcase section to the XML document.r^   Ú	classnamerb   Z_testMethodNamer@   rc   )ÚfailureÚerrorr)   r\   r   ÚmessageN)rj   rk   rl   rT   r   ZshortDescriptionÚgetattrr   r   r   r%   r   r   r"   r   )	ro   r   Úxml_testsuiterp   r^   Z	elem_namers   Ú
error_infoZfailureTextr
   r
   r   Ú_report_testcase  s$    

ÿÿ


z_XMLTestResult._report_testcasec           	      C   sT   |  d¡}| |¡ | |¡}| |¡ |  d¡}| |¡ | |¡}| |¡ dS )zCAppends the system-out and system-err sections to the XML document.z
system-outz
system-errN)rj   rk   r   )	Útest_runnerrw   rp   r   r   Z	systemoutZsystemout_textZ	systemerrZsystemerr_textr
   r
   r   Ú_report_output  s    






z_XMLTestResult._report_outputc              
   C   s8  |   ¡ }t|jƒtkr0tj |j¡s0t |j¡ | ¡ D ]ú\}}t	ƒ }t
 |||¡}g g  }}|D ]L}	t
 ||	||¡ |	jr’| d|	 ¡ |	jg¡ |	jrb| d|	 ¡ |	jg¡ qbt
 |||d |¡d |¡¡ |jdd}
t|jƒtu r&td|jtj|f dƒ}z| |
¡ W | ¡  n
| ¡  0 q8|j |
¡ q8dS )z:Generates the XML reports to a given XMLTestRunner object.z*****************Ú
ú	)Úindentz%s%sTEST-%s.xmlÚwN)r`   r\   ÚoutputrT   ÚosÚpathÚexistsÚmakedirsÚitemsr   r(   rq   ry   r   Úextendr!   r   r{   ÚjoinZtoprettyxmlÚopenÚsepr4   Úclose)r   rz   Zall_resultsZsuiter]   Údocra   r   r   rC   Zxml_contentZreport_filer
   r
   r   Úgenerate_reports+  s:    ÿ

ÿÿÿz_XMLTestResult.generate_reportsN)r   r   r   r#   r;   r   r   r:   r>   r?   rA   rD   rH   rM   rP   rZ   r`   rq   Ústaticmethodry   r{   rŒ   r
   r
   r
   r   r(   b   s*   ÿ
	


r(   c                   @   s*   e Zd ZdZddd„Zdd	„ Zd
d„ ZdS )ÚXMLTestRunnerzJA test runner class that outputs the results in JUnit like XML files.
    rG   NTFc                 C   s6   |du rt j}d| }t | |||¡ || _|| _dS )z'Create a new instance of XMLTestRunner.N)r)   é   )r;   r   r   r   r€   r,   )r   r€   r.   r/   Úverboser,   r0   r
   r
   r   r   R  s    zXMLTestRunner.__init__c                 C   s   t | j| j| j| jƒS )zoCreate the TestResult object which will be used to store
        information about the executed tests.
        )r(   r.   r/   r0   r,   r   r
   r
   r   Ú_make_result[  s    
ÿzXMLTestRunner._make_resultc           	      C   s:  |   ¡ }| j ¡  | j d¡ | j |j¡ t ¡ }||ƒ t ¡ }|| }| j ¡  | j d¡ | | ¡ | ¡  | j |j¡ |j}| j d||dkrždp d|f ¡ | j ¡  | ¡ s*| j 	d¡ t
|jƒt
|jƒ }}|rô| j 	d| ¡ |r|r| j 	d	¡ | j 	d
| ¡ | 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 %.3fsr)   Úsr   zFAILED (zfailures=%dz, z	errors=%dú)rF   )r‘   r.   r2   rU   r@   rŒ   ZprintErrorsZtestsRunZwasSuccessfulr4   rm   rK   rO   )	r   rC   Úresultr   r   Z
time_takenÚrunÚfailedZerroredr
   r
   r   r•   b  s<    


ÿ

zXMLTestRunner.run)rG   NTFT)r   r   r   r#   r   r‘   r•   r
   r
   r
   r   rŽ   O  s   
	rŽ   )r#   Ú
__future__r   r   r;   r@   Zunittestr   r   r   Zxml.dom.minidomÚxmlr   ÚImportErrorÚioÚdomÚminidomÚDocumentr   Úobjectr   r(   rŽ   r
   r
   r
   r   Ú<module>   s   &% n