How do you get Jenkins to execute python unittest cases? Is it possible to JUnit style XML output from the builtin unittest
package?
问题:
回答1:
sample tests:
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
run the tests with:
py.test --junitxml results.xml tests.py
results.xml:
self = <tests.SimpleTest testMethod=test_fail> def test_fail(self): > self.assertEqual(11, 7 + 3) E AssertionError: 11 != 10 tests.py:16: AssertionError /home/damien/test-env/lib/python2.6/site-packages/_pytest/unittest.py:119: Skipped: demonstrating skipping
JUnit with nose
run the tests with:
nosetests --with-xunit
nosetests.xml:
JUnit with nose2
You would need to use the nose2.plugins.junitxml
plugin. You can configure nose2
with a config file like you would normally do, or with the --plugin
command-line option.
run the tests with:
nose2 --plugin nose2.plugins.junitxml --junit-xml tests
nose2-junit.xml:
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
JUnit with unittest-xml-reporting
Append the following to tests.py
if __name__ == '__main__': import xmlrunner unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
run the tests with:
python tests.py
test-reports/TEST-SimpleTest-20131001140629.xml:
回答2:
I would second using nose. Basic XML reporting is now built in. Just use the --with-xunit command line option and it will produce a nosetests.xml file. For example:
nosetests --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).
回答3:
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.
回答4:
I used nosetests. There are addons to output the XML for Jenkins
回答5:
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.
回答6:
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.