Unittest Framework 简明教程
Nose Testing - Framework
鼻塞解决方案于 2005 年版本中发布,即 py.test 获得现代外观的那一年。它由 Jason Pellerin 编写以用于 py.test 早已开创的测试惯用语,但其是一个更容易安装和维护的包。
The nose project was released in 2005, the year after py.test received its modern guise. It was written by Jason Pellerin to support the same test idioms that had been pioneered by py.test, but in a package that is easier to install and maintain.
nose 模块借助 pip 实用程序可以进行安装。
The nose module can be installed with the help of pip utility
pip install nose
这将在当前 Python 发行版和 nosetest.exe 中安装 nose 模块,这意味着可以使用该实用程序和 –m 开关来运行该测试。
This will install the nose module in the current Python distribution as well as a nosetest.exe, which means the test can be run using this utility as well as using –m switch.
C:\python>nosetests –v test_sample.py
Or
C:\python>python –m nose test_sample.py
nose 当然会从 unittest.TestCase 子类中收集测试。而且可以编写简单的测试函数,以及不是 unittest.TestCase 子类的测试类。nose 还提供了一些有用的函数,以编写时序测试、针对异常的测试以及其他常见用例。
nose collects tests from unittest.TestCase subclasses, of course. We can also write simple test functions, as well as test classes that are not subclasses of unittest.TestCase. nose also supplies a number of helpful functions for writing timed tests, testing for exceptions, and other common use cases.
nose 会自动收集测试。无需手动将测试用例收集至测试套件。运行测试具有响应性,因为 nose 会在加载第一个测试模块后立即开始运行测试。
nose collects tests automatically. There’s no need to manually collect test cases into test suites. Running tests is responsive, since nose begins running tests as soon as the first test module is loaded.
与 unittest 模块一样, nose 也支持包、模块、类和测试用例级别的 fixture,因此可以尽少地进行开销较大的初始化。
As with the unittest module, nose supports fixtures at the package, module, class, and test case level, so expensive initialization can be done as infrequently as possible.
Basic Usage
我们来看一下与之前所用脚本相类似的 nosetest.py −
Let us consider nosetest.py similar to the script used earlier −
# content of nosetest.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
要运行上述测试,请使用以下命令行语法 −
In order to run the above test, use the following command line syntax −
C:\python>nosetests –v nosetest.py
控制台上的输出如下 −
The output displayed on console will be as follows −
nosetest.test_answer ... FAIL
================================================================
FAIL: nosetest.test_answer
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python34\nosetest.py", line 6, in test_answer
assert func(3) == 5
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures = 1)
nose 可通过在上述命令行中使用 with-doctest 选项与 DocTest 进行集成。
nose can be integrated with DocTest by using with-doctest option in athe bove command line.
\nosetests --with-doctest -v nosetest.py
你可以在测试脚本中使用 nose −
You may use nose in a test script −
import nose
nose.main()
如果你不想让测试脚本在成功时退出为 0,在失败时退出为 1(如 unittest.main),请改用 nose.run()−
If you do not wish the test script to exit with 0 on success and 1 on failure (like unittest.main), use nose.run() instead −
import nose
result = nose.run()
如果测试运行成功,则结果为 true;如果失败或引发未捕获的异常,则结果为 false。
The result will be true if the test run is successful, or false if it fails or raises an uncaught exception.
nose 在包、模块、类和测试级别支持 fixture(setUp 和 tearDown 方法)。与 py.test 或 unittest fixture 一样,setUp 会始终在任何测试运行之前运行(对于测试包和模块,则在测试集合之前运行);tearDown 会在 setUp 顺利完成后运行,而与测试运行状态无关。
nose supports fixtures (setup and teardown methods) at the package, module, class, and test level. As with py.test or unittest fixtures, setup always runs before any test (or collection of tests for test packages and modules); teardown runs if setup has completed successfully, regardless of the status of the test run.