Unittest Framework 简明教程

UnitTest Framework - Signal Handling

通过使用 -c/--catch 命令行选项与 catchbreak 参数,在测试运行期间可以提高对 control-C 的处理效率。启用 catch break 行为后,control-C 将允许当前正在运行的测试完成,然后测试运行将结束并报告迄今为止的所有结果。第二次 control-c 将按通常方式引发 KeyboardInterrupt。

More efficient handling of control-C during a test run is provided by The -c/--catch command-line option to unittest, along with the catchbreak parameter. With catch break behavior enabled, control-C will allow the currently running test to complete, and the test run will then end and report all the results so far. A second control-c will raise a KeyboardInterrupt in the usual way.

如果调用了 unittest 处理程序但未安装 signal.SIGINT 处理程序,则它将调用默认处理程序。这通常是替换已安装处理程序并委派给它的代码的预期行为。对于需要禁用 unittest control-c 处理的各个测试,可以使用 removeHandler() 装饰器。

If the unittest handler is called but signal.SIGINT handler isn’t installed, then it calls for the default handler. This will normally be the expected behavior by code that replaces an installed handler and delegates to it. For individual tests that need unittest control-c handling disabled, the removeHandler() decorator can be used.

以下实用函数在测试框架中启用 control-c 处理功能:

The following utility functions enable control-c handling functionality within test frameworks −

unittest.installHandler()

安装 control-c 处理程序。当接收到 signal.SIGINT 时,会调用所有已注册结果的 TestResult.stop()。

Install the control-c handler. When a signal.SIGINT is received all registered results have TestResult.stop() called.

unittest.registerResult(result)

注册 TestResult 对象以进行 control-c 处理。注册结果将存储对其的弱引用,因此不会阻止垃圾收集该结果。

Register a TestResult object for control-c handling. Registering a result stores a weak reference to it, so it doesn’t prevent the result from being garbage collected.

unittest.removeResult(result)

移除已注册的结果。移除结果后,control-c 将不再响应该结果对象调用 TestResult.stop()。

Remove a registered result. Once a result has been removed then TestResult.stop() will no longer be called on that result object in response to a control-c.

unittest.removeHandler(function = None)

在未带参数调用时,如果已安装此函数,此函数将移除 control-c 处理程序。此函数还可以用作测试装饰器,以便在执行测试时临时移除处理程序。

When called without arguments, this function removes the control-c handler if it has been installed. This function can also be used as a test decorator to temporarily remove the handler whilst the test is being executed.

GUI Test Runner

安装 unittest 模块以交互方式发现并运行测试。此实用程序,Python 脚本“inittestgui.py”,使用了 Tkinter 模块,它是 TK 图形工具包的 Python 移植。它提供了一个易于使用的 GUI,用于发现和运行测试。

The unittest module is installed to discover and run tests interactively. This utility, a Python script 'inittestgui.py' uses Tkinter module which is a Python port for TK graphics tool kit. It gives an easy to use GUI for discovery and running tests.

Python unittestgui.py
running tests

单击“发现测试”按钮。将出现一个小的对话框,您可以在其中选择要运行测试的目录和模块。

Click the 'Discover Tests' button. A small dialog box appears where you can select directory and modules from which test are to be run.

discover tests

最后,点击“开始”按钮。将会从选定的路径和模块名称发现测试,结果窗格将显示结果。

Finally, click the start button. Tests will be discovered from the selected path and module names, and the result pane will display the results.

result pane

为了看到单个测试的详细信息,选择并单击结果框中的测试即可 −

In order to see the details of individual test, select and click on test in the result box −

individual test details

如果在 Python 安装中找不到此实用程序,可以从项目页面 http://pyunit.sourceforge.net/ 获取。

If you do not find this utility in the Python installation, you can obtain it from the project page http://pyunit.sourceforge.net/.

类似基于 wxpython 工具包的实用程序也可在该处获得。

Similar, utility based on wxpython toolkit is also available there.