具有类似ant/junit的xml报告的基于unittest的测试运行器。

unittest-xml-reporting的Python项目详细描述


LicenseLatest VersionDevelopment StatusDocumentation Status

Build StatusCode Healthcodecov.io Coverage StatusCoveralls Coverage StatusRequirements Status

unittest xml报告(也称为xmlrunner)

unittest测试运行程序,可以将测试结果保存为xunit格式的xml文件。 这些文件可以被很多工具使用,比如构建系统、ide 以及持续集成服务器。

模式

有许多模式略有不同。 我们使用一个与jenkins-xunit插件兼容的插件,一个副本是 在tests/vendor/jenkins/xunit-plugin/junit-10.xsd下提供(请参阅附加的许可证)。 您还可能发现这些资源很有用:

有些破损的东西

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'

此外,还提供了以下设置,以便您可以微调报表:

settingdefaultvaluesdescription
^{}^{}^{}Besides the XML reports generated by the test runner, a bunch of useful information is printed to the ^{} stream, just like the ^{} does. Use this setting to choose between a verbose and a non-verbose output.
^{}^{}^{}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作为代表。如果你愿意帮助双方和解 欢迎分支、拉取请求。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在sqlite数据库中保存特定列的历史记录   java如何更改/更新timeseriechart名称(JFreeChart)   java如何将整数转换为可绘制的   汇编什么解释Java的字节码   java查找已编译的类版本号   我应该什么时候在ColdFusion应用程序中使用Java?   java当一个实体的两个字段为(unique=true)时,如何处理JPA异常?   java为什么在所有其他实例都正确的情况下返回错误的布尔值?   java Hibernate每次都准备语句   java停留在平均字长上   对Java和日语字符进行编码   java如何将导致异常的方法的错误消息传递给侦听器中的onTestFailure方法   java代码没有打印结果   java为什么私有内部接口的方法必须是公共的?   休眠发生错误。有关详细信息,请参阅错误日志。JAVAlang.NullPointerException