2012-11-23 4 views

Antwort

0

ich ein python-Modul https://bitbucket.org/db_atlass/python-junit-xml-output-module/ gefunden, sucht fi t zu meinem Bedürfnis. thx David Black gibt

# 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

können Sie junitxml (Python JUnit XML-Reporter)

auf PyPI verwenden: http://pypi.python.org/pypi/junitxml

wenn Sie eine Standard unittest Testsuite suite genannt haben. Sie könnte es, laufen und die Ergebnisse in eine XML-Datei wie folgt schreiben:

import junitxml 

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

oder zu entdecken, Tests und Druck xml stdout:

python -m junitxml.main discover 

eine weitere Option ist nose und Lauf zu verwenden Ihre Suite mit:

nosetests --with-xunit 
+0

danke, aber ich will nur Junit XML-Dateien aus vorhandenen Testergebnis, Junitxml ist hauptsächlich für Python-Unit-Test –

+0

Larrycal, wickeln Sie Ihre Tests in Unittest-Framework –

+0

Thx, könnte eine Wahl sein, aber so weit ich will nur Übertragen Sie das Protokoll in Junit-Format –

14

Corey oben vorgeschlagen junitxml, aber ich war im selben Boot wie larrycai, dass ich nicht Unit-Tests schreiben Python Code zu testen. Ich schreibe Python-Skripte, um Blackbox-Systemtests durchzuführen, und wollte nur Ergebnisse in JUnit XML ausgeben, ohne das Rad neu zu erfinden.

ich sah kurz oben auf David Blacks „Python junit xml Ausgabemodul“ vorgeschlagen von larrycai, aber am Ende mit einem ähnlichen Paket gehen. Ich bin mir nicht sicher, was besser ist, da ich es nur probiert habe, aber es hat mir ganz gut geholfen.

Nur anders um ein Zeichen, aber das Paket ist „JUnit-xml“: https://pypi.python.org/pypi/junit-xml/1.0

Vorsicht ... die Beispiele in seiner Readme Fehler haben und funktionieren nicht. Ich habe die Fehler auf github gemeldet (github link auf der pypi-Seite). Es gibt auch einen Bug mit seiner "Prettyprint" -Arg-Behandlung, aber ich verweise euch auf die Ausgabe # 3, die ich auch über GitHub berichtet habe, in der ich meinen Fix eingefügt habe. Wenn Sie die Quelle herunterladen, können Sie sich seine test.py Unit-Tests ansehen, aber hier ist auch mein Testskript, wo ich mit einigen Beispielen (mit Python 3) experimentiert habe.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

danke, sieht vollständiger aus –

+0

Vielen Dank für ein Beispiel für die "add_failure_info" -Methode, die aus den offiziellen "Docs" seltsam fehlt. – MarkHu

0

Gute Antworten hier: (es gibt viele Möglichkeiten, es zu tun) Python unittests in Jenkins?

IMHO der beste Weg ist Schreib Python Unittest tests und install pytest (etwas wie 'yum install pytest'), um py.test installiert zu bekommen. Dann führen Tests wie folgt aus: 'py.test - junitxml results.xml test.py'. Sie können ein beliebiges Unittest-Python-Skript ausführen und jUnit-XML-Ergebnisse abrufen.

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

In jenkins bauen Konfiguration Post-Build-Aktionen hinzufügen Aktion mit result.xml "JUnit Testergebnis Bericht Publish" und kann keine weiteren Testergebnis Dateien, die Sie produzieren.

Verwandte Themen