2012-11-23 11 views

risposta

0

ho trovato un modulo python https://bitbucket.org/db_atlass/python-junit-xml-output-module/, sembra fi t al mio bisogno thx David Black ci

# code snippet for the usage 
""" a short example of how to use this module """ 
test_cases = [] 
for i in range(0, 5): 
    type_c = "" 
    if i % 2 == 0: 
     type_c = "failure" 
    test_cases.append(TestCase(i, str(i) + "contents", type_c)) 

junit_xml = JunitXml("demo test example", test_cases) 
6

è possibile utilizzare junitxml (Python JUnit XML giornalista)

su PyPI: http://pypi.python.org/pypi/junitxml

se si ha una suite di test standard unittest chiamato suite. si potrebbe correre, e scrivere i risultati in un file XML in questo modo:

import junitxml 

fp = file('results.xml', 'wb') 
result = junitxml.JUnitXmlResult(fp) 
result.startTestRun() 
TestSuite(suite).run(result) 
result.stopTestRun() 

o per scoprire prove e XML di stampa su stdout:

python -m junitxml.main discover 

un'altra opzione è quella di utilizzare nose ed eseguire vostra suite con:

nosetests --with-xunit 
+0

grazie, ma voglio solo generare file xml junit dal risultato del test esistente, junitxml è principalmente per prova unità python –

+0

larrycal, avvolgere i test nell'impianto unittest –

+0

thx, potrebbe essere una scelta, ma finora voglio solo trasferisci il log al formato junit –

14

Corey sopra junitxml suggerito, ma ero nella stessa barca, come larrycai a che non sto scrittura di unit test per testare il codice Python. Sto scrivendo script Python per fare test di sistema black box e volevo solo produrre risultati in JUnit XML senza reinventare la ruota.

Ho brevemente esaminato il "modulo di output xml python junit di David Black" suggerito da larrycai sopra, ma ho finito per andare con un altro pacchetto simile. Non sono sicuro di quale sia meglio visto che ho provato solo questo, ma ha funzionato abbastanza bene per me.

Solo diverso da un carattere, ma il pacchetto è "JUnit-xml": https://pypi.python.org/pypi/junit-xml/1.0

attenzione ... gli esempi della sua readme hanno errori e non funzionano. Ho segnalato gli errori su github (link github incluso nella pagina pypi). C'è anche un bug con la sua gestione di "prettyprint" arg, ma ti rimanderò al numero 3 che ho riportato anche su github, in cui ho incluso la mia correzione. Se scarichi il codice sorgente puoi vedere i suoi test unit test.py, ma qui è anche il mio script di test in cui ho testato/sperimentato diversi esempi (usando Python 3.3):

#junit-xml 1.0 downloaded from https://pypi.python.org/pypi/junit-xml 
from junit_xml import TestSuite, TestCase 

#Good article that has examples of how Jenkins parses JUnit XML to display output: 
#http://nelsonwells.net/2012/09/how-jenkins-ci-parses-and-displays-junit-output/ 

#One version of JUnit XML schema: http://windyroad.org/dl/Open%20Source/JUnit.xsd 


def testBasicToConsole(): 
    ''' Perform the very basic test with 1 suite and 1 test case, output to console. 
     This is the example from the above referenced pypi webpage, but corrected to 
     actually work. 
    ''' 

    test_cases = [TestCase('Test1', 'some.class.name', 123.345, 'I am stdout!', 'I am stderr!')] 
    ts = [TestSuite("my test suite", test_cases)] 
    # pretty printing is on by default but can be disabled using prettyprint=False 
    print(TestSuite.to_xml_string(ts, prettyprint=False)) 


def testBasicInfoToConsole(): 
    ''' Actually, even more basic than the test above, with classname, stdout, and stderror 
     removed to demonstrate they are optional. For system testing we often won't use them. 
     Output to console. 
    ''' 

    test_cases = [TestCase('PathCheck: ApplicationControl', '', .0523, '', '')] 
    ts = [TestSuite("DirectorITG2", test_cases)] 
    # pretty printing is on by default but can be disabled using prettyprint=False 
    print(TestSuite.to_xml_string(ts)) 

def testFailureInfoToConsole(): 
    ''' 1 suite and test case with failure info added. Output to console. 
    ''' 

    test_cases = TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '') 
    test_cases.add_failure_info('Invalid File \'DNC.exe\'.') 
    ts = [TestSuite("DirectorITG2", [test_cases])] 
    # pretty printing is on by default but can be disabled using prettyprint=False 
    print(TestSuite.to_xml_string(ts)) 

def testMultiTestCasesToConsole(): 
    ''' Demonstrates a single test suite with multiple test cases, one of which 
     has failure info. Output to console. 
    ''' 

    test_cases = [TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '')] 
    test_cases.append(TestCase('FileCheck: PropertyServer', '', .0452, '', '')) 
    test_cases[0].add_failure_info('Invalid File \'DNC.exe\'.') 
    ts = [TestSuite("DirectorITG2", test_cases)] 
    # pretty printing is on by default but can be disabled using prettyprint=False 
    print(TestSuite.to_xml_string(ts)) 

def testMultiTestSuitesToConsole(): 
    ''' Demonstrates adding multiple test suites. Output to console. 
    ''' 

    test_cases = [TestCase('FileCheck: DesktopNotificationCenter', '', .0451, '', '')] 
    ts = [TestSuite("FileChecks", test_cases)] 
    ts.append(TestSuite("ProcessChecks", [TestCase('ProcessCheck: ApplicationControl', '', 1.043, '', '')])) 
    # pretty printing is on by default but can be disabled using prettyprint=False 
    print(TestSuite.to_xml_string(ts)) 

def testMultiTestCasesToFile(): 
    ''' Demonstrates a single test suite with multiple test cases, one of which 
     has failure info. Output to a file with PrettyPrint disabled (Jenkins-friendly). 
    ''' 

    test_cases = [TestCase('DesktopNotificationCenter', 'Integration.FileCheck', .0451, '', '')] 
    test_cases.append(TestCase('PropertyServer', 'Integration.FileCheck', .5678, '', '')) 
    test_cases[0].add_failure_info('Invalid File \'DNC.exe\'.') 
    ts = [TestSuite("GII_2013_R1", test_cases)] 
    # open the file, then call the TestSuite to_File function with prettyprint off. 
    # use raw text here to protect slashes from becoming escape characters 
    with open(r'C:\Users\Administrator\.jenkins\workspace\IntegrationTests\FileCheck.xml', mode='a') as lFile: 
     TestSuite.to_file(lFile, ts, prettyprint=False) 
     lFile.close() 


if __name__ == '__main__': 
    ''' If this module is being run directly, run all of the example test functions. 
     Test functions output JUnit XML for various scenarios to either screen (Console) 
     or file. 

    ''' 
    testBasicToConsole() 
# testBasicInfoToConsole() 
# testFailureInfoToConsole() 
# testMultiTestCasesToConsole() 
# testMultiTestSuitesToConsole() 
# testMultiTestCasesToFile() 

else: 
    ''' Function calls for an external run of this script. 

    ''' 
    testMultiTestCasesToFile() 
+0

grazie, sembra più completo –

+0

Grazie per aver fornito un esempio del metodo 'add_failure_info', che è stranamente privo dei" documenti "ufficiali. – MarkHu

0

buone risposte qui: (ci sono molti modi per farlo) Python unittests in Jenkins?

IMHO il modo migliore è scrittura pitone unittest test e installa pytest (qualcosa come 'yum install pytest') per installare py.test. Quindi i test eseguono test come questo: 'py.test --junitxml results.xml test.py'. Puoi eseguire qualsiasi script python non vincolante e ottenere risultati jUnit xml.

https://docs.python.org/2.7/library/unittest.html

a Jenkins costruire la configurazione post-generazione Azioni Aggiungi un azione "Pubblica test JUnit rapporto dei risultati" con risultato.xml e gli eventuali altri file dei risultati di prova si producono.

Problemi correlati