2012-06-28 29 views
103

Làm thế nào để bạn có được Jenkins để thực thi các trường hợp không có dấu chân không? Có thể kết xuất đầu ra XML kiểu JUnit từ gói unittest được dựng sẵn không?Python unittests trong Jenkins?

+1

Tất cả các câu trả lời cho rằng bạn muốn bắt đầu các trường hợp thử nghiệm từ dòng lệnh. Nhưng nếu bạn muốn chạy thử nghiệm theo chương trình, hãy thử điều này: 'nhập khẩu mũi; nose.runmodule() # aka nose.run (defaultTest = __ name __) ' – MarkHu

+1

IMHO đơn giản 'py.test --junitxml results.xml test.py' đề xuất trả lời câu hỏi tốt nhất. 'yum install pytest' để cài đặt py.test. Sau đó, bạn có thể chạy bất kỳ tập lệnh python không liên quan nào và nhận các kết quả jUnit xml – gaoithe

+0

@gaoithe trả lời phần jenkins, nhưng không đáp ứng yêu cầu sử dụng mô-đun unittest tích hợp. Trong dự án đó, nó là một yêu cầu nhất định. – erikbwork

Trả lời

134

kiểm tra mẫu:

tests.py:

# tests.py 

import random 
try: 
    import unittest2 as unittest 
except ImportError: 
    import unittest 

class SimpleTest(unittest.TestCase): 
    @unittest.skip("demonstrating skipping") 
    def test_skipped(self): 
     self.fail("shouldn't happen") 

    def test_pass(self): 
     self.assertEqual(10, 7 + 3) 

    def test_fail(self): 
     self.assertEqual(11, 7 + 3) 

JUnit with pytest

chạy thử nghiệm với:

py.test --junitxml results.xml tests.py 

results.xml:

<?xml version="1.0" encoding="utf-8"?> 
<testsuite errors="0" failures="1" name="pytest" skips="1" tests="2" time="0.097"> 
    <testcase classname="tests.SimpleTest" name="test_fail" time="0.000301837921143"> 
     <failure message="test failure">self = &lt;tests.SimpleTest testMethod=test_fail&gt; 

    def test_fail(self): 
&gt;  self.assertEqual(11, 7 + 3) 
E  AssertionError: 11 != 10 

tests.py:16: AssertionError</failure> 
    </testcase> 
    <testcase classname="tests.SimpleTest" name="test_pass" time="0.000109910964966"/> 
    <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000164031982422"> 
     <skipped message="demonstrating skipping" type="pytest.skip">/home/damien/test-env/lib/python2.6/site-packages/_pytest/unittest.py:119: Skipped: demonstrating skipping</skipped> 
    </testcase> 
</testsuite> 

JUnit with nose

chạy thử nghiệm với:

nosetests --with-xunit 

nosetests.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<testsuite name="nosetests" tests="3" errors="0" failures="1" skip="1"> 
    <testcase classname="tests.SimpleTest" name="test_fail" time="0.000"> 
     <failure type="exceptions.AssertionError" message="11 != 10"> 
      <![CDATA[Traceback (most recent call last): 
File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 340, in run 
testMethod() 
File "/home/damien/tests.py", line 16, in test_fail 
self.assertEqual(11, 7 + 3) 
File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 521, in assertEqual 
assertion_func(first, second, msg=msg) 
File "/opt/python-2.6.1/lib/python2.6/site-packages/unittest2-0.5.1-py2.6.egg/unittest2/case.py", line 514, in _baseAssertEqual 
raise self.failureException(msg) 
AssertionError: 11 != 10 
]]> 
     </failure> 
    </testcase> 
    <testcase classname="tests.SimpleTest" name="test_pass" time="0.000"></testcase> 
    <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000"> 
     <skipped type="nose.plugins.skip.SkipTest" message="demonstrating skipping"> 
      <![CDATA[SkipTest: demonstrating skipping 
]]> 
     </skipped> 
    </testcase> 
</testsuite> 

JUnit with nose2

Bạn sẽ cần phải sử dụng các plugin nose2.plugins.junitxml. Bạn có thể định cấu hình nose2 bằng tệp cấu hình như bình thường, hoặc với tùy chọn dòng lệnh --plugin.

chạy thử nghiệm với:

nose2 --plugin nose2.plugins.junitxml --junit-xml tests 

nose2-junit.xml:

<testsuite errors="0" failures="1" name="nose2-junit" skips="1" tests="3" time="0.001"> 
    <testcase classname="tests.SimpleTest" name="test_fail" time="0.000126"> 
    <failure message="test failure">Traceback (most recent call last): 
    File "/Users/damien/Work/test2/tests.py", line 18, in test_fail 
    self.assertEqual(11, 7 + 3) 
AssertionError: 11 != 10 
</failure> 
    </testcase> 
    <testcase classname="tests.SimpleTest" name="test_pass" time="0.000095" /> 
    <testcase classname="tests.SimpleTest" name="test_skipped" time="0.000058"> 
    <skipped /> 
    </testcase> 
</testsuite> 

JUnit with unittest-xml-reporting

Nối sau vào tests.py

if __name__ == '__main__': 
    import xmlrunner 
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports')) 

chạy thử nghiệm với:

python tests.py 

thử nghiệm báo cáo/TEST-SimpleTest-20131001140629.xml:

<?xml version="1.0" ?> 
<testsuite errors="1" failures="0" name="SimpleTest-20131001140629" tests="3" time="0.000"> 
    <testcase classname="SimpleTest" name="test_pass" time="0.000"/> 
    <testcase classname="SimpleTest" name="test_fail" time="0.000"> 
     <error message="11 != 10" type="AssertionError"> 
<![CDATA[Traceback (most recent call last): 
    File "tests.py", line 16, in test_fail 
    self.assertEqual(11, 7 + 3) 
AssertionError: 11 != 10 
]]>  </error> 
    </testcase> 
    <testcase classname="SimpleTest" name="test_skipped" time="0.000"> 
     <skipped message="demonstrating skipping" type="skip"/> 
    </testcase> 
    <system-out> 
<![CDATA[]]> </system-out> 
    <system-err> 
<![CDATA[]]> </system-err> 
</testsuite> 
+2

+1 cho đề xuất 'py.test --junitxml results.xml test.py' đơn giản. 'yum install pytest' để cài đặt py.test. Sau đó, 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. – gaoithe

+0

Nếu bạn muốn sử dụng _unittest-xml-reporting_ và hưởng lợi từ tính năng [Kiểm tra khám phá] (https://docs.python.org/3/library/unittest.html#unittest-test-discovery), bạn có thể đặt ' unittest.main (module = None, testRunner = xmlrunner.XMLTestRunner (output = 'test-reports')) '. –

+0

@RosbergLinhares tại sao bạn cần 'module = None' để sử dụng Kiểm tra khám phá? Nó hoạt động chính xác như được mô tả trong câu trả lời 'unittest.main (testRunner = xmlrunner.XMLTestRunner (output = 'test-reports'))'. – acm

5

Tôi đã sử dụng nosetests. Có addons để xuất XML cho Jenkins

3

Khi sử dụng buildout, chúng tôi sử dụng collective.xmltestreport để tạo đầu ra XML kiểu JUnit, có lẽ là source code hoặc chính mô-đun có thể trợ giúp.

8

Bạn có thể cài đặt gói unittest-xml-reporting để thêm nhân tố thử nghiệm tạo XML thành tích hợp unittest.

Chúng tôi sử dụng pytest, có đầu ra XML được tích hợp sẵn (đó là tùy chọn dòng lệnh).

Dù bằng cách nào, việc thực hiện các kiểm tra đơn vị có thể được thực hiện bằng cách chạy lệnh trình bao.

19

Tôi sẽ sử dụng mũi thứ hai. Báo cáo XML cơ bản hiện được tích hợp. Chỉ cần sử dụng tùy chọn dòng lệnh --with-xunit và nó sẽ tạo ra một tệp nosetests.xml. Ví dụ:

nosetests --with-xUnit

Sau đó, thêm một "Xuất bản báo cáo kết quả kiểm tra JUnit" bài build hành động, và điền vào "XMLs báo cáo Test" lĩnh vực với nosetests.xml (giả định rằng bạn đã chạy nosetests trong $ WORKSPACE).

1
python -m pytest --junit-xml=pytest_unit.xml source_directory/test/unit || true # tests may fail 

Chạy trang này dưới dạng vỏ từ jenkins, bạn có thể lấy báo cáo trong tệp pytest_unit.xml làm tạo phẩm.