Unittest Framework 简明教程

Nose Testing - Tools

nose.tools 模块提供了一些您可能发现有用的测试辅助工具,包括用于限制测试执行时间和测试异常的装饰器,以及 unittets.TestCase 中的所有同类 assertX 方法。

The nose.tools module provides a number of testing aids that you may find useful, including decorators for restricting test execution time and testing for exceptions, and all of the same assertX methods found in unittest.TestCase.

  1. nose.tools.ok_(expr, msg = None) − Shorthand for assert.

  2. nose.tools.eq_(a, b, msg = None) − Shorthand for ‘assert a == b, “%r != %r” % (a, b)

  3. nose.tools.make_decorator(func) − Wraps a test decorator so as to properly replicate metadata of the decorated function, including nose’s additional stuff (namely, setup and teardown).

  4. nose.tools.raises(*exceptions) − Test must raise one of expected exceptions to pass.

  5. nose.tools.timed(limit) − Test must finish within specified time limit to pass

  6. nose.tools.istest(func) − Decorator to mark a function or method as a test

  7. nose.tools.nottest(func) − Decorator to mark a function or method as not a test

Parameterized Testing

Python 的测试框架 unittest 没有一个简单的方法来运行参数化测试用例。换句话说,您无法轻松从外部向 unittest.TestCase 传递参数。

Python’s testing framework, unittest, doesn’t have a simple way of running parametrized test cases. In other words, you can’t easily pass arguments into a unittest.TestCase from outside.

然而,pytest 模块通过几种完全集成的途径实现测试参数传递 −

However, pytest module ports test parametrization in several well-integrated ways −

  1. pytest.fixture() allows you to define parametrization at the level of fixture functions.

  2. @pytest.mark.parametrize allows to define parametrization at the function or class level. It provides multiple argument/fixture sets for a particular test function or class.

  3. pytest_generate_tests enables implementing your own custom dynamic parametrization scheme or extensions.

第三方模块“nose-parameterized”允许对任何 Python 测试框架进行参数化测试。可以从以下链接下载它 − https://github.com/wolever/nose-parameterized

A third party module 'nose-parameterized' allows Parameterized testing with any Python test framework. It can be downloaded from this link − https://github.com/wolever/nose-parameterized