显示python unittest结果以漂亮的表格形式显示

Ada*_*tan 21 python testing unit-testing python-unittest test-reporting

我正在编写一个Pythonic工具,用于验证某个系统的正确性.每个验证都是用Python编写的unittest,报告如下:

test_exclude_list_not_empty (__main__.TestRepoLists)
Assert the the exclude list is not empty ... ok
test_include_list_not_empty (__main__.TestRepoLists)
Assert the the include list is not empty ... ok
test_repo_list_not_empty (__main__.TestRepoLists)
Assert the the repo list is not empty ... ok
Run Code Online (Sandbox Code Playgroud)

在我看来,这种格式很难阅读,特别是对于非Python主义者.是否有任何报告生成器可以以漂亮的表格形式生成报告,例如:

+----------------------------------------------------------------+-----------+
| Test                                                           |  Status   |
+----------------------------------------------------------------+-----------+
| Assert the the exclude list is not empty                       |  OK       |
| Assert the the include list is not empty                       |  OK       |
| Assert the the repo list is not empty                          |  OK       |
| All the items in the include list should be in the repo list   |  OK       |
+----------------------------------------------------------------+-----------+
Run Code Online (Sandbox Code Playgroud)

澄清测试套件在远程终端上运行,所以我更喜欢命令行报告工具.

ale*_*cxe 29

这不是您要求的,但有几个选项可以在那里获得可读的测试输出:

  • HTMLTestRunner以表格形式生成易于使用的HTML测试报告.是一份样本报告.
  • 用于鼻子测试跑步者的nose-html-output插件
  • unittest-xml-reporting - 基于PyUnit的测试运行器,带有JUnit,如XML报告
  • --with-xunit选项的nose 将生成易于阅读和转换的junit xml样式报告

另见:

如果你想看到在控制台以表格形式的测试结果,无论如何,我认为一个好的想法是写你自己的鼻子插件或测试运行基于unittest.TestProgram因为它是在做HTMLTestRunner.

希望有所帮助.


Vin*_*tas 11

我想将我的信息作为评论添加到alecxe的答案中,但我没有足够的声誉.

如果有人仍在寻找答案,我将HTMLTestRunner分成一个简单的TestRunner,它有一个表格,彩色,终端友好的输出.这是其输出的示例:

例

源代码位于https://gist.github.com/viniciusd/73e6eccd39dea5e714b1464e3c47e067

我将很快从头开始重写它,但保持输出格式.