2012-11-23 23 views
14

Tôi có trường hợp kiểm tra chấp nhận, kết quả là văn bản thuần túy. Tôi muốn sử dụng Jenkins để hiển thị kết quả, và định dạng JUnit phù hợp với tôi.Tập lệnh Python để tạo báo cáo JUnit từ kết quả kiểm tra khác

Vì vậy, tôi muốn kiểm tra xem có mã python hiện có để tạo XML định dạng JUnit hay không, để tôi có thể dễ dàng thêm mã phân tích cú pháp của mình.

Related question.

Trả lời

0

Tôi tìm thấy một mô-đun python https://bitbucket.org/db_atlass/python-junit-xml-output-module/, trông fi theo nhu cầu của tôi. thx David Đen có

# 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

bạn có thể sử dụng junitxml (Python JUnit phóng XML)

trên PyPI: http://pypi.python.org/pypi/junitxml

nếu bạn đã có một bộ unittest kiểm tra tiêu chuẩn gọi là suite. bạn có thể chạy nó, và viết kết quả vào một tập tin xml như thế này:

import junitxml 

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

hoặc khám phá kiểm tra và in xml để stdout:

python -m junitxml.main discover 

tùy chọn khác là sử dụng nose và chạy bộ của bạn với:

nosetests --with-xunit 
+0

cảm ơn, nhưng tôi chỉ muốn tạo các tệp junit xml từ kết quả thử nghiệm hiện có, junitxml chủ yếu cho thử nghiệm đơn vị python –

+0

larrycal, quấn thử nghiệm của bạn trong khung unittest –

+0

thx, có thể là một lựa chọn, nhưng cho đến nay tôi chỉ muốn chuyển nhật ký sang định dạng junit –

14

Corey trên junitxml đề nghị, nhưng tôi đã ở cùng một thuyền như larrycai ở chỗ tôi không viết bài kiểm tra đơn vị để kiểm tra mã Python. Tôi đang viết kịch bản Python để làm thử nghiệm hệ thống hộp đen và chỉ muốn kết quả đầu ra trong JUnit XML mà không cần phát minh lại bánh xe.

Tôi đã xem nhanh mô hình đầu ra "python junit xml" của David Black do larrycai đề xuất ở trên, nhưng kết thúc bằng một gói tương tự khác. Không chắc cái nào tốt hơn vì tôi chỉ thử cái này, nhưng nó đã làm việc khá tốt cho tôi.

Chỉ khác nhau bởi một ký tự, nhưng gói là "junit-xml": https://pypi.python.org/pypi/junit-xml/1.0

Hãy coi chừng ... các ví dụ trong readme mình có lỗi và không có tác dụng. Tôi đã báo cáo lỗi trên github (liên kết github được đưa vào trang pypi). Ngoài ra còn có một lỗi với "prettyprint" arg xử lý của mình, nhưng tôi sẽ giới thiệu cho bạn để phát hành # 3 mà tôi cũng báo cáo về github, trong đó tôi bao gồm sửa chữa của tôi. Nếu bạn tải về mã nguồn, bạn có thể xem xét các test test đơn vị test.py của mình, nhưng đây cũng là kịch bản thử nghiệm của tôi, nơi tôi đã thử nghiệm/thử nghiệm với một vài ví dụ (sử dụng 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

cảm ơn, trông hoàn chỉnh hơn –

+0

Cảm ơn bạn đã đưa ra một ví dụ về phương thức 'add_failure_info', một cách kỳ lạ thiếu từ" tài liệu "chính thức. – MarkHu

0

câu trả lời tốt ở đây: (có rất nhiều cách để làm điều đó) Python unittests in Jenkins?

IMHO cách tốt nhất là ghi python unittest kiểm tracài đặt pytest (giống như 'yum install pytest') để cài đặt py.test. Sau đó, chạy các thử nghiệm như sau: 'py.test --junitxml results.xml test.py'. Bạn có thể chạy bất kỳ tập lệnh python không liên quan nào và nhận kết quả jUnit xml.

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

Trong Jenkins xây dựng cấu hình sau xây dựng hành động Thêm một "Xuất bản báo cáo kết quả kiểm tra JUnit" hành động với result.xml và nhiều hơn nữa bất kỳ tập tin kết quả xét nghiệm bạn sản xuất.

Các vấn đề liên quan