2012-11-23 27 views
14

私は受け入れテストケースを持っています。結果はプレーンテキストです。私は結果を示すためにJenkinsを使用したいと思います.JUnit形式は私には適しています。別のテスト結果からJUnitレポートを生成するPythonスクリプト

私は、JUnit形式のXMLを生成するための既存のPythonコードがあるかどうかをチェックしたいので、簡単に解析コードを追加することができます。

Related question

答えて

0

は、Fiを提供して見えます私の必要性に合わせてTHXデビッドそこ

# 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

あなたはは、PyPIにjunitxml(パイソンのJUnit XMLレポーター)

を使用することができます。http://pypi.python.org/pypi/junitxml

あなたはsuiteと呼ばれる標準unittestテストスイートを持っていた場合。あなたはそれを実行し、このようなXMLファイルに結果を書き込むことができます:

import junitxml 

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

またはstdoutにテストやプリントのxmlを発見する:

python -m junitxml.main discover 

別のオプションは、noseと実行を使用することですあなたのスイート:私は1つのpythonモジュールhttps://bitbucket.org/db_atlass/python-junit-xml-output-module/を見つけ

nosetests --with-xunit 
+0

私はちょうど既存のテスト結果からjunit xmlファイルを生成したい、junitxmlは主にPythonユニットテスト用です –

+0

larrycal、ユニットテストフレームワークでテストをラップします –

+0

thx、これは選択肢かもしれませんが、ログをjunitフォーマットに変換してください –

14

ブラック提案junitxml上記コーリーが、私はPythonのコードをテストするためにユニットテストを書いていないよという点でlarrycaiと同じ船に乗っていました。私はブラックボックスシステムのテストを行うためのPythonスクリプトを書いていますが、ホイールを改革することなくJUnit XMLで結果を出力したいだけでした。

私は、上記のlarrycaiによって提案されたDavid Blackの「python junit xml出力モジュール」を簡単に見てきましたが、もう1つの同様のパッケージで終了しました。私はこの1つを試して以来、どちらが良いのか分かりませんが、それは私のためにきれいに働きました。

1つの文字だけが異なるが、パッケージには、「JUnitの-XML」です: https://pypi.python.org/pypi/junit-xml/1.0

用心...彼のreadmeの例は、エラーを持っていると動作しません。私はgithub(pypiページに含まれているgithubのリンク)のエラーを報告しました。 "prettyprint" arg処理のバグもありますが、githubにも報告した#3の問題を参照してください。これには私の修正が含まれています。ソースをダウンロードした場合、彼のtest.pyユニットテストを見ることができますが、ここで私がテストした/テストしたいくつかの例(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

ありがとう、より完全に見える –

+0

「add_failure_info」メソッドの例を与えてくれてありがとうございます。これは、公式の "docs"には欠けています。 – MarkHu

0

良い答えから別のパッケージを得た:(それを行うには多くの方法がある) Python unittests in Jenkins?

最良の方法は、書き込みPythonのunittestのです私見テストpytest(yum install pytestのようなもの)をインストールしてpy.testをインストールします。 次にのようなテストを実行します: 'py.test --junitxml results.xml test.py'。任意のunittestのpythonスクリプトを実行し、jUnit xmlの結果を取得できます。ジェンキンスで

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

設定、ビルド後のアクションはあなたが作るresult.xml、それ以上の試験結果ファイルを「JUnitテスト結果報告書を公開」アクションを追加構築します。