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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17162682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:38:42  来源:igfitidea点击:

Display python unittest results in nice, tabular form

pythonunit-testingtestingpython-unittesttest-reporting

提问by Adam Matan

I am writing a Pythonic tool which validates the correctness of a certain system. Each validation is written as a Python unittest, and the report looks like:

我正在编写一个 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

In my opinion, this format is hard to read, especially for non-Pythonists. Is there any report generator that can generate a report in a nice, tabular form, e.g.:

在我看来,这种格式很难阅读,尤其是对于非 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       |
+----------------------------------------------------------------+-----------+

ClarificationThe test suite runs on a remote terminal, so I prefer command line reporting tools.

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

采纳答案by alecxe

This is not exactly what you are asking, but there are several options for having a readable test output there:

这不完全是您要问的,但是有几种选择可以在那里获得可读的测试输出:

  • HTMLTestRunnergenerates easy to use HTML test reports in a tabular form. Here's a sample report.
  • nose-html-outputplugin for nosetest runner
  • unittest-xml-reporting- PyUnit-based test runner with JUnit like XML reporting
  • nose with --with-xunitoption will produce junit xml style reports that are easy to read and convert
  • HTMLTestRunner以表格形式生成易于使用的 HTML 测试报告。是一个示例报告。
  • 鼻HTML输出插件鼻子测试运行
  • unittest-xml-reporting- 基于 PyUnit 的测试运行器,具有 JUnit 之类的 XML 报告
  • 带有--with-xunit选项的鼻子将生成易于阅读和转换的 junit xml 样式报告

Also see:

另见:

If you want to see test results in a tabular form in the console anyway, I think that a good idea would be to write your own nose pluginor test runner based on unittest.TestProgramas it was done in HTMLTestRunner.

无论如何,如果您想在控制台中以表格形式查看测试结果,我认为一个好主意是编写您自己的鼻子插件或测试运行程序,unittest.TestProgram就像在HTMLTestRunner中所做的那样

Hope that helps.

希望有帮助。

回答by Carlos V

Take a look at Twisted's Trial.

看看Twisted's Trial

By default, it uses the TreeReportertest runner, which looks like:

默认情况下,它使用TreeReporter测试运行器,如下所示:

Trial's reporting

审判报告

It has the following:

它具有以下功能:

  • It's a command line report, just run:

    trial test_name.py

  • Colored output: red for failure, green for success

  • The report uses a tree like structure. It displays the tests under the TestCases they belong, allowing you to quickly traverse the results to find a specific test. (Although it provides a couple of more reports).

  • It also includes a test library, derived from Python's unittest.TestCase. You can use this library by subclassing twisted.trial.unittest.TestCase. This provides a few more assertion methods.

  • It includes the option to generate statement coverage for your tests.

  • 这是一个命令行报告,只需运行:

    trial test_name.py

  • 彩色输出:红色表示失败,绿色表示成功

  • 该报告使用树状结构。它在它们所属的 TestCases 下显示测试,允许您快速遍历结果以查找特定测试。(虽然它提供了更多的报告)。

  • 它还包括一个测试库,派生自 Python 的unittest.TestCase. 您可以通过子类化来使用这个库 twisted.trial.unittest.TestCase。这提供了更多的断言方法

  • 它包括为您的测试生成语句覆盖率的选项。

回答by Vinicius Dantas

I would like to add my information as a comment into alecxe's answer, but I do not have enough reputation for that.

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

In case of someone still looking for an answer, I forked HTMLTestRunner into a simple TestRunner, which has a tabular, colored, terminal-friendly output. This is a sample of its output:

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

Example

例子

The source code is at https://gist.github.com/viniciusd/73e6eccd39dea5e714b1464e3c47e067

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

I shall rewrite it from scratch soon but keeping the output format.

我很快就会从头开始重写它,但保留输出格式。