根据 https://blog.zengrong.net/pos...
得知:
flask项目自己使用的是pytest
nose是对标准库unittest的封装,如今比较流行,但文档没有pytest作的好,且近几年一直处于维护状态没有更新。
Flask-Testing flask扩展
最终选择:pytesthtml
在命令行输入以下命令检查pytest是否已安装python
py.test --version
若是没有flask
pip install -U pytest
# content of test_sample.py def func(x): return x+1 def test_func(): assert func(3) == 5
运行:
执行测试时须要下面几步:session
$ py.test ============================= test session starts ============================== platform darwin -- Python 3.5.1, pytest-2.8.1, py-1.4.30, pluggy-0.3.1 rootdir: /Users/fc/project/test/pytest_sample, inifile: collected 1 items test_sample.py F =================================== FAILURES =================================== __________________________________ test_func ___________________________________ def test_func(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:9: AssertionError =========================== 1 failed in 0.01 seconds ===========================
# content of test_class.py class TestClass(object): def test_one(self): x = 'this' assert 'h' in x def test_two(self): x = 'hello' assert hasattr(x, 'check')
运行
下面的-q是 quiet的意思,就是忽略一些很细节的信息
使用测试类时,注意下面几点:app
bogon:pytest_sample fc$ py.test -q test_class.py .F =================================== FAILURES =================================== ______________________________ TestClass.test_two ______________________________ self = <test_class.TestClass object at 0x10595b080> def test_two(self): x = 'hello' > assert hasattr(x, 'check') E assert hasattr('hello', 'check') test_class.py:11: AssertionError 1 failed, 1 passed in 0.01 seconds
import pytest params = [ (2, 3, 5), (4, 5, 9), (6, 7, 12) ] @pytest.mark.parametrize('a, b, expected', params) def test_add(a, b, expected): assert a + b == expected
运行结果框架
$ py.test -q test_params.py ..F =================================== FAILURES =================================== _______________________________ test_add[6-7-12] _______________________________ a = 6, b = 7, expected = 12 @pytest.mark.parametrize('a, b, expected', params) def test_add(a, b, expected): > assert a + b == expected E assert (6 + 7) == 12 test_params.py:12: AssertionError 1 failed, 2 passed in 0.01 seconds
说明:函数
import pytest @pytest.fixture(params=[1, 2, 3]) def test_data(request): return request.param def test_not_2(test_data): assert test_data != 2
运行结果:工具
$ py.test -q fixture_params.py .F. ======================================= FAILURES ======================================= ____________________________________ test_not_2[2] _____________________________________ test_data = 2 def test_not_2(test_data): > assert test_data != 2 E assert 2 != 2 fixture_params.py:10: AssertionError 1 failed, 2 passed in 0.01 seconds
说明:post
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'hello' @app.route('/login') def login(): return 'login' @app.route('/logout') def logout(): return 'logout' @app.errorhandler(404) def page_not_found(): return '404' if __name__ == '__main__': app.run()
from flaskr import app class TestClass(object): def setup_class(self): """测试开始时候执行, 用来作准备工做,通常用来初始化资源。""" app.config['TESTING'] = True # 这将会使得处理请求时的错误捕捉失效,以便于 您在进行对应用发出请求的测试时得到更好的错误反馈。 # 测试客户端将会给咱们一个通向应用的简单接口,咱们能够激发 对向应用发送请求的测试,而且此客户端也会帮咱们记录 Cookie 的 动态。 self.app = app.test_client() def teardown_class(self): """测试结束时执行, 用来作收尾工做, 通常用来关闭资源""" pass def test_login(self): response = self.app.get('/login') assert b'login' == response.data def test_logout(self): response = self.app.get('logout') assert b'logout' == response.data def test_index(self): response = self.app.get('/') assert b'hello' == response.data