具有类似ant/junit的xml报告的基于unittest的测试运行器。
unittest-xml-reporting的Python项目详细描述
unittest xml报告(也称为xmlrunner)
unittest测试运行程序,可以将测试结果保存为xunit格式的xml文件。 这些文件可以被很多工具使用,比如构建系统、ide 以及持续集成服务器。
模式
有许多模式略有不同。
我们使用一个与jenkins-xunit插件兼容的插件,一个副本是
在tests/vendor/jenkins/xunit-plugin/junit-10.xsd
下提供(请参阅附加的许可证)。
您还可能发现这些资源很有用:
- https://stackoverflow.com/questions/4922867/what-is-the-junit-xml-format-specification-that-hudson-supports
- https://stackoverflow.com/questions/11241781/python-unittests-in-jenkins
- Jenkins (junit-10.xsd)
- JUnit-Schema (JUnit.xsd)
- Windyroad (JUnit.xsd)
- a gist (Jenkins xUnit test result schema)
有些破损的东西
Python 3有一个关于{{CD2>}的子测试的概念;这并不能很好地映射到现有的 xunit概念,所以在模式中找不到它。这意味着,你失去了一些粒度 在子测试的报告中。
要求
- python 2.7或3.3+
- 请注意,Python2.6的终止时间是2013年10月,支持2.6的最新版本是1.14.0
安装
安装unittest xml报告最简单的方法是通过 Pip:
$ pip install unittest-xml-reporting
如果您使用git并希望获得最新的development版本:
$ git clone https://github.com/xmlrunner/unittest-xml-reporting.git
$ cd unittest-xml-reporting
$ sudo python setup.py install
或者获取最新的development版本作为tarball:
$ wget https://github.com/xmlrunner/unittest-xml-reporting/archive/master.zip
$ unzip master.zip
$ cd unittest-xml-reporting
$ sudo python setup.py install
或者您可以手动从 PyPI。
命令行
python -m xmlrunner [options] python -m xmlrunner discover [options]# help python -m xmlrunner -h
例如
python -m xmlrunner discover -t ~/mycode/tests -o /tmp/build/junit-reports
用法
下面的剧本改编自
unittest,演示如何使用
XMLTestRunner
非常简单。事实上,
此脚本和原始脚本是最后一行:
importrandomimportunittestimportxmlrunnerclassTestSequenceFunctions(unittest.TestCase):defsetUp(self):self.seq=list(range(10))@unittest.skip("demonstrating skipping")deftest_skipped(self):self.fail("shouldn't happen")deftest_shuffle(self):# make sure the shuffled sequence does not lose any elementsrandom.shuffle(self.seq)self.seq.sort()self.assertEqual(self.seq,list(range(10)))# should raise an exception for an immutable sequenceself.assertRaises(TypeError,random.shuffle,(1,2,3))deftest_choice(self):element=random.choice(self.seq)self.assertTrue(elementinself.seq)deftest_sample(self):withself.assertRaises(ValueError):random.sample(self.seq,20)forelementinrandom.sample(self.seq,5):self.assertTrue(elementinself.seq)if__name__=='__main__':unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'),# these make sure that some options that are not applicable# remain hidden from the help menu.failfast=False,buffer=False,catchbreak=False)
向单个文件报告
if__name__=='__main__':withopen('/path/to/results.xml','wb')asoutput:unittest.main(testRunner=xmlrunner.XMLTestRunner(output=output),failfast=False,buffer=False,catchbreak=False)
doctest支持
XmlTestRunner还可以用于报告DocStrings样式的测试。
importdoctestimportxmlrunnerdeftwice(n):""" >>> twice(5) 10 """return2*nclassMultiplicator(object):defthreetimes(self,n):""" >>> Multiplicator().threetimes(5) 15 """return3*nif__name__=="__main__":suite=doctest.DocTestSuite()xmlrunner.XMLTestRunner().run(suite)
django支持
为了将XMLTestRunner
插入django项目,添加以下内容
致您的settings.py
:
TEST_RUNNER='xmlrunner.extra.djangotestrunner.XMLTestRunner'
此外,还提供了以下设置,以便您可以微调报表:
setting | default | values | description |
---|---|---|---|
^{ | ^{ | ^{ | Besides the XML reports generated by the test runner, a bunch of useful information is printed to the ^{ |
^{ | ^{ | ^{ | If your test methods contains docstrings, you can display such docstrings instead of display the test name (ex: ^{ In order to use this feature, you have to enable verbose output by setting ^{ Only effects stdout and not XML output. |
^{ | ^{ | ^{ | Tells the test runner where to put the XML reports. If the directory couldn't be found, the test runner will try to create it before generate the XML files. |
^{ | ^{ | ^{ | Tells the test runner to output a single XML report with this filename under ^{ Please note that for long running tests, this will keep the results in memory for a longer time than multiple reports, and may use up more resources. |
贡献
我们一直在寻找好的贡献,所以请 存储库和发送拉取请求(带测试!).
如果您想对存储库进行写访问,或者成为维护人员, 随时联系。
使用tox
测试更改
在发送请求之前,请使用tox
测试您的更改。
您可以在https://testrun.org/tox/latest/找到有关tox
的更多信息。
$ pip install tox # basic sanity test, friendly output $ tox -e pytest # all combinations $ tox
进行中的工作
有一个rewrite branch包含更好的单元测试并使用不同的运行程序。它
引入一个CompositeTestResult
,它允许同时使用TextTestResult
以及XMLTestResult
作为代表。如果你愿意帮助双方和解
欢迎分支、拉取请求。