$ py.test
。本文中咱们使用 pytest 指代这个测试框架,py.test 特指运行命令。 <br /> ##较于 nose这里没有使用像前三篇同样(简介-举例-discovery-环境)式的分段展开,是由于 pytest 与 nose 的基本用法极其类似。所以只作一个比较就行了。他俩的区别仅在于python
$ py.test
下面使用一个例子说明 pytest 的 setup/teardown 使用方式。api
some_test.py:session
lang:python import pytest @pytest.fixture(scope='function') def setup_function(request): def teardown_function(): print("teardown_function called.") request.addfinalizer(teardown_function) print('setup_function called.') @pytest.fixture(scope='module') def setup_module(request): def teardown_module(): print("teardown_module called.") request.addfinalizer(teardown_module) print('setup_module called.') def test_1(setup_function): print('Test_1 called.') def test_2(setup_module): print('Test_2 called.') def test_3(setup_module): print('Test_3 called.')
pytest 建立测试环境(fixture)的方式如上例所示,经过显式指定 scope=''
参数来选择须要使用的 pytest.fixture 装饰器。即一个 fixture 函数的类型从你定义它的时候就肯定了,这与使用 @nose.with_setup()
十分不一样。对于 scope='function'
的 fixture 函数,它就是会在测试用例的先后分别调用 setup/teardown。测试用例的参数如 def test_1(setup_function)
只负责引用具体的对象,它并不关心对方的做用域是函数级的仍是模块级的。框架
有效的 scope 参数限于:'function','module','class','session'
,默认为 function
。函数
运行上例:$ py.test some_test.py -s
。 -s
用于显示 print() 函数性能
============================= test session starts ============================= platform win32 -- Python 3.3.2 -- py-1.4.20 -- pytest-2.5.2 collected 3 items test.py setup_function called. Test_1 called. .teardown_function called. setup_module called. Test_2 called. .Test_3 called. .teardown_module called. ========================== 3 passed in 0.02 seconds ===========================
首先,单是从不须要使用特定类模板的角度上,nose 和 pytest 就较于 unittest 好出太多了。doctest 比较奇葩咱们在这里不比。所以对于 “选一个本身喜欢的测试框架来用” 的问题,就变成了 nose 和 pytest 二选一的问题。单元测试
pythontesting.net 的做者很是喜欢 pytest,并表示测试
<span style="text-decoration:line-through;">pytest 赛高,不服 solo</span>spa
好吧,其实他说的是 “若是你挑不出 pytest 的毛病,就用这个吧”。.net
因而下面咱们就来挑挑 pytest 的毛病:
毕竟 unittest 仍是 Python 自带的单元测试框架,确定有不少怕麻烦的人在用,因此与其语法保持必定兼容性能避免不少麻烦。即便 pytest 在命令行中有彩色输出让我很喜欢,但这仍是不如第一条重要。
实际上,PyPI 中 nose 的下载量也是 pytest 的 8 倍多。
因此假如再继续写某一个框架的详解的话,大概我会选 nose 吧。