欢迎来到Nose2¶
nose2
是的继承者 nose
.
它是 unittest
带插件。
nose2
是一个新项目,不支持 nose
. 见 differences 做一次彻底的总结。
Nose2的目的是扩展 unittest
使测试更好、更容易理解。
快速启动¶
因为 nose2
基于UnitTest,您可以从Python标准库的 documentation for unittest 然后使用nose2在上面增加值。
nose2
在名称以开头的python文件中查找测试 test
并运行它发现的每个测试函数。
下面是一个用典型的UnitTest样式编写的简单测试示例:
# in test_simple.py
import unittest
class TestStrings(unittest.TestCase):
def test_upper(self):
self.assertEqual("spam".upper(), "SPAM")
然后您可以这样运行此测试::
$ nose2 -v
test_upper (test_simple.TestStrings) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
然而, nose2
支持更多的测试配置,并提供更多的工具 unittest
靠自己。
例如,这个测试只练习了 nose2
的功能:
# in test_fancy.py
from nose2.tools import params
@params("Sir Bedevere", "Miss Islington", "Duck")
def test_is_knight(value):
assert value.startswith('Sir')
然后像这样运行:
$ nose2 -v --pretty-assert
test_fancy.test_is_knight:1
'Sir Bedevere' ... ok
test_fancy.test_is_knight:2
'Miss Islington' ... FAIL
test_fancy.test_is_knight:3
'Duck' ... FAIL
======================================================================
FAIL: test_fancy.test_is_knight:2
'Miss Islington'
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/ebs/home/sirosen/tmp/test_fancy.py", line 6, in test_is_knight
assert value.startswith('Sir')
AssertionError
>>> assert value.startswith('Sir')
values:
value = 'Miss Islington'
value.startswith = <built-in method startswith of str object at 0x7f3c3172f430>
======================================================================
FAIL: test_fancy.test_is_knight:3
'Duck'
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/ebs/home/sirosen/tmp/test_fancy.py", line 6, in test_is_knight
assert value.startswith('Sir')
AssertionError
>>> assert value.startswith('Sir')
values:
value = 'Duck'
value.startswith = <built-in method startswith of str object at 0x7f3c3172d490>
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=2)
完整文档¶
完整的文档 nose2
可在 docs.nose2.io
贡献¶
如果你想做贡献,请阅读 contributing 指南。