根據 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