젠킨스에서 파이썬 단위 테스트?
Jenkins가 파이썬 단위 테스트 사례를 어떻게 실행하도록합니까? 내장 unittest
패키지 에서 JUnit 스타일 XML 출력이 가능 합니까?
샘플 테스트 :
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)
pytest를 가진 JUnit
다음을 사용하여 테스트를 실행하십시오.
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 = <tests.SimpleTest testMethod=test_fail>
def test_fail(self):
> 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
다음을 사용하여 테스트를 실행하십시오.
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>
코를 가진 JUnit2
nose2.plugins.junitxml
플러그인 을 사용해야합니다 . nose2
평상시처럼 구성 파일을 사용하거나 --plugin
명령 줄 옵션을 사용하여 구성 할 수 있습니다 .
다음을 사용하여 테스트를 실행하십시오.
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>
unittest-xml-reporting이있는 JUnit
다음에 추가 tests.py
if __name__ == '__main__':
import xmlrunner
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
다음을 사용하여 테스트를 실행하십시오.
python tests.py
test-reports / 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>
두 번째로 코를 사용합니다. 기본 XML보고 기능이 내장되어 있습니다. --with-xunit 명령 줄 옵션을 사용하면 nosetests.xml 파일이 생성됩니다. 예를 들면 다음과 같습니다.
코 테스트 -with-xunit
Then add a "Publish JUnit test result report" post build action, and fill in the "Test report XMLs" field with nosetests.xml (assuming that you ran nosetests in $WORKSPACE).
You can install the unittest-xml-reporting package to add a test runner that generates XML to the built-in unittest
.
We use pytest, which has XML output built in (it's a command line option).
Either way, executing the unit tests can be done by running a shell command.
I used nosetests. There are addons to output the XML for Jenkins
When using buildout we use collective.xmltestreport
to produce JUnit-style XML output, perhaps it's source code or the module itself could be of help.
python -m pytest --junit-xml=pytest_unit.xml source_directory/test/unit || true # tests may fail
Run this as shell from jenkins , you can get the report in pytest_unit.xml as artifact.
참고URL : https://stackoverflow.com/questions/11241781/python-unittests-in-jenkins
'development' 카테고리의 다른 글
http 핸들러 인터페이스에서 bool IsReusable의 중요성 (0) | 2020.07.04 |
---|---|
Android 애플리케이션의 모든 버튼에 스타일을 적용하는 방법 (0) | 2020.07.04 |
클래스 패스 자원의 java.nio.file.Path (0) | 2020.07.04 |
std :: numeric_limits를 호출하기 전에 단항 "+"의 목적은 무엇입니까? (0) | 2020.07.04 |
문자열을 어떻게 연결합니까? (0) | 2020.07.04 |