【问题标题】:How to write outputs of python unit test framework file into .xml file to visualize on jenkins?如何将python单元测试框架文件的输出写入.xml文件以在jenkins上可视化?
【发布时间】:2015-05-20 12:08:28
【问题描述】:

我有一个 python 文件 testing.py 包含以下内容:

import subprocess
import unittest
DETACHED_PROCESS = 0x00000008
def open_exe(file,mode):
    if mode == 1:
        if type(file) is str:
            object = subprocess.Popen([file],creationflags=DETACHED_PROCESS,shell=True)
        if object.pid is None:
            return 0
        else:
            print "Process id ", object.pid
            return 1
   if mode == 2:
       if type(file) is str:
           object = subprocess.Popen([file,"-configuration=" + "Other_base_0x1010.def"], creationflags=DETACHED_PROCESS,shell=True)
       if object.pid is None:
            return 0
       else:
            print "Process id ", object.pid
            return 1
    class testmyfunctions(unittest.TestCase):
       def test_contains_simple_true(self):
          self.assertEqual(open_exe("D:\\learning\\development\\MY_base.exe",1),1)              
      def test_first_process(self):
          self.assertEqual(open_exe("D:\\learning\\Projects\\Other_base.exe", 2), 1)     

if __name__ == '__main__':
    unittest.main()

这个 testing.py 文件实际上打开了两个 .exe,然后我做了两个测试用例。我的问题是如何将 testing.py 的输出写入要在 jenkins 上可视化的 xml 文件?有一些命令吗?至少建议我从哪里开始。

【问题讨论】:

  • 尝试标签--junit-xml unit_test_results.xml并检查此link

标签: python xml unit-testing jenkins


【解决方案1】:

首先,将 xmlrunner 安装到您的从节点中。您可以参考this link 以及如何在您的单元测试中使用它。

秒。回到 jenkins,您需要通过 2 个步骤设置作业:
step1:“Execute Shell”执行单元测试,确保使用 xmlrunner 生成 xml 输出。
step2:“添加构建后操作”==>“发布 JUnit 测试报告”,然后将您的报告路径(在步骤 1 中生成)放在那里。

祝你好运。

【讨论】:

    【解决方案2】:

    您可以使用nosetests 作为您的测试运行器。它与标准unittest 测试兼容,并具有多种输出格式选项。

    见:nose.readthedocs.org

    要创建 JUnitXML,您需要在命令行中指定 --with-xunit。因此,要运行您的测试,只需执行以下操作:

    $ nosetests --with-xunit
    

    这将运行您的测试套件,并在当前工作目录中生成一个名为nosetests.xml 的文件。

    【讨论】:

      猜你喜欢
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多