跳转到主要内容

编写测试的实用工具:沙盒目录、模拟外部程序、用于cairo表面的图形文档测试、线程中的错误处理。

项目描述

tl.testing发行版

此包提供各种在编写测试时可以使用的实用工具。它与Python 2.6和2.7版本兼容。

目录和文件的沙盒

当测试修改目录和文件的代码时,能够轻松创建和检查目录和文件的样本树非常有用。`tl.testing.fs`模块提供了从文本描述创建树、以相同格式列出它并在自身之后清理的支持。

在文档测试中,可以使用以下方式创建和列出目录、文件和符号链接

>>> from tl.testing.fs import new_sandbox, ls
>>> new_sandbox("""\
... d foo
... f foo/bar asdf
... l baz -> foo/bar
... """)
>>> ls()
l baz -> foo/bar
d foo
f foo/bar asdf

有关如何设置和拆除测试使用文件系统沙盒的进一步建议,请参阅源代码中找到的`fs.txt`文件。

安装可调用脚本

可能想要测试的一些功能会使用到外部程序,例如分页器或文本编辑器。`tl.testing.script` 模块提供了安装简单模拟脚本的实用程序,这些脚本位于将被测试的代码找到它们的地方。它们接受一串 Python 代码并创建一个包装脚本,该脚本设置 Python 路径以匹配测试,并运行该代码。

这就是在文档测试中可能如何使用这样的模拟脚本。

>>> from tl.testing.script import install
>>> script_path = install("print 'A simple script.'")
>>> print open(script_path).read()
#!...
<BLANKLINE>
import sys
sys.path[:] = [...]
<BLANKLINE>
print 'A simple script.'
>>> import subprocess
>>> sub = subprocess.Popen(script_path, shell=True, stdout=subprocess.PIPE)
>>> stdout, stderr = sub.communicate()
>>> print stdout
A simple script.

请参阅与源代码一起找到的文件 script.txt,以了解如何安装和访问模拟脚本,以及如何使用模拟脚本拆除测试的更多可能性。

测试 cairo 表面的图形内容

虽然在 Python 代码中直接比较两个 cairo 表面的内容很简单,但处理图形超出了文档测试的范围。然而,可以使用 manuel 包从文本文档中提取更通用的测试用例,同时允许以自然的方式将它们与文档测试混合。

`tl.testing.cairo` 模块提供了一个测试套件工厂,它使用 manuel 执行以 reStructuredText 图像形式表述的图形测试。这样一个图像的标题应该是一个字面量 Python 表达式,其值是一个 cairo 表面,并且它的图像用作测试期望。

这就是如何在文档测试中将表面与期望的图像进行比较。

>>> import cairo
>>> from pkg_resources import resource_filename

>>> image = resource_filename('tl.testing', 'testimages/correct.png')

.. figure:: tl/testing/testimages/correct.png

    ``cairo.ImageSurface.create_from_png(image)``

请参阅与源代码一起找到的文件 cairo.txt,以获取有关可能的测试输出的更多建议和文档。

在测试代码中处理线程

标准的 TestCase 类不会收集在主线程之外发生的错误和失败。`tl.testing.thread` 模块提供了线程类和一个 ThreadAwareTestCase 类,允许这样做,以及一些与线程测试相关的其他便利:防止预期的未处理的异常在线程中打印到测试输出中,报告测试留下的线程,在守护线程中运行代码,连接线程并计算测试运行期间启动的线程数。

>>> import time
>>> import tl.testing.thread
>>> class SampleTest(tl.testing.thread.ThreadAwareTestCase):
...
...     def test_error_in_thread_should_be_reported(self):
...         with tl.testing.thread.ThreadJoiner(1):
...             self.run_in_thread(lambda: 1/0)
...
...     def test_active_count_should_count_only_new_threads(self):
...         with tl.testing.thread.ThreadJoiner(1):
...             self.run_in_thread(lambda: time.sleep(0.1))
...             self.assertEqual(1, self.active_count())
...         self.assertEqual(0, self.active_count())
>>> import unittest
>>> run(unittest.makeSuite(SampleTest))
======================================================================
ERROR: test_error_in_thread_should_be_reported (__builtin__.SampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  ...
ZeroDivisionError: integer division or modulo by zero
----------------------------------------------------------------------
Ran 2 tests in N.NNNs
FAILED (errors=1)

请参阅与源代码一起找到的文件 thread.txt,以获取有关 ThreadAwareTestCase 类的更多详细信息。

构建使用 manuel 的测试套件

由于 `manuel` 除了标准的 doctests 之外还提供了一些强大的功能,因此 `manuel` 测试套件的设置与标准套件略有不同。`tl.testing.doctest` 模块实现了与标准之一相同的 `DocFileSuite` 工厂,但创建了一个使用 `manuel` 并允许一些与 `manuel` 相关的额外配置的测试套件,其中包括解释曾经使用已弃用的 zope.testing.doctest 执行的脚注的能力。

>>> sample_txt = write('sample.txt', """\
... [#footnote]_
... >>> x
... 1
...
... .. [#footnote]
...     >>> x = 1
... """)
>>> from tl.testing.doctest import DocFileSuite
>>> run(DocFileSuite(sample_txt, footnotes=True))
----------------------------------------------------------------------
Ran 1 test in N.NNNs
OK
>>> sample_txt = write('sample.txt', """\
... .. code-block:: python
...     x = 1
...
... >>> x
... 1
... """)
>>> import manuel.codeblock
>>> run(DocFileSuite(sample_txt, manuel=manuel.codeblock.Manuel()))
----------------------------------------------------------------------
Ran 1 test in N.NNNs
OK

关于 tl.testing

作者::

Thomas Lotze <thomas@thomas-lotze.de>

在线文档::

http://packages.python.org/tl.testing/http://tltesting.readthedocs.org/

PyPI 页面::

http://pypi.python.org/pypi/tl.testing/

问题跟踪器::

https://bitbucket.org/tlotze/tl.testing/issues/

源代码::

https://bitbucket.org/tlotze/tl.testing/

当前变更日志::

https://bitbucket.org/tlotze/tl.testing/raw/tip/CHANGES.txt

支持项目::
Flattr this

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

tl.testing-0.5.tar.gz (34.1 kB 查看哈希值)

上传时间 源代码

由以下支持