<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 測試級別 * 單元: 開發者的角度,測試單個代碼單元測功能。 * 功能性(集成測試):最終用戶的角度, 測試軟件產品的多個組件,確保這些組件能夠正常工作。 * 端到端 測試應與CI (持續集成)流程相結合,確保不斷執行測試。 ## 測試的內容 在Flask 中, 單元測試集中單獨測試小代碼單元: 1. 數據庫模型 ( 通常在models.py中定義) 2. 視圖函數調用的函數(例如:服務器端驗證檢查) 功能測試關注視圖功能的運行方式,比如: 1. 視圖函數的觸發(GET,POST) 2. 視圖函數正確處理無效的HTTP方法 3. 無效參數傳遞給視圖函數 ## Python 內置的測試框架,unittest 包含 1. 單元測試的工具, 包括一整套assert 語法用來執行檢查 2. 單元測試和單元測試套件的結果 3. 執行測試的測試運行器 pytest 和unittest 的區別 | 對比項 | pytest | unittest | | --- | --- | --- | | 安裝 | 第三方庫 | 核心標準庫的一部分 | | 測試設置和拆卸 | fixtures | setup() 和tearDown() 方法 | | 斷言格式 | 內置斷言 | | | 結構 | 功能性 | 面向對象 | pytest 的優點 1. 需要更少的樣板代碼,測試代碼更可讀 2. 支持簡單的assert 語句,這比assertEquals, assertTrue, 和assertContains 更可讀 3. 因為不是標準庫的一部分, 所以更新更頻繁 4. 簡化測試狀態的建立和拆除 5. 支持 Fixture ## 測試 項目結構 比較建議的方式 1. 測試放在 test 目錄中 2. 單元測試和功能測試分目錄存放 ( unit, functional) ``` -app.py -project -__init__.py - models.py - blueprint folder -requirements.txt -tests - contest.py - functional - __init__.py -test_users.py -unit __init__.py test_models.py venv ``` ### 功能測試 ``` from project import create_app def test_home_page(): """ GIVEN a Flask application configured for testing WHEN the '/' page is requested (GET) THEN check that the response is valid """ # Set the Testing configuration prior to creating the Flask application os.environ['CONFIG_TYPE'] = 'config.TestingConfig' flask_app = create_app() # Create a test client using the Flask application configured for testing with flask_app.test_client() as test_client: response = test_client.get('/') assert response.status_code == 200 assert b"Welcome to the" in response.data assert b"Flask User Management Example!" in response.data assert b"Need an account?" in response.data assert b"Existing user?" in response.data ``` ## Fixture ### xUnit 經典方式 ``` setUp() 運行測試 TearDown() ``` ### Fixture的優點 1. Fixture 被定義成函數 2. 多個Fixture 可以被用來設置測試函數的初始狀態,Fixture 還可以呼叫 Fixture ,所以可以組合使用設置狀態 3. Fixture 可以在不同的范圍運行 * function - 每個測試函數運行一次 * class - 每個測試類運行一次 * module - 每個模塊運行一次 * session - 每個會話運行一次 Fixture 在conftest.py 中創建。 ### 單元測試 Fixture ``` ~~~ user = User('patkennedy79@gmail.com', 'FlaskIsAwesome') assert user.email == 'patkennedy79@gmail.com' assert user.hashed_password != 'FlaskIsAwesome' assert user.role == 'user' ~~~ ``` Fixture ``` @pytest.fixture(scope='module') def new_user(): user = User('patkennedy79@gmail.com', 'FlaskIsAwesome') return user ``` * 裝飾@pytest.fixture器指定此函數是具有module-level 范圍的固定裝置 ### 功能測試 Fixture ``` @pytest.fixture(scope='module') def test_client(): # Set the Testing configuration prior to creating the Flask application os.environ['CONFIG_TYPE'] = 'config.TestingConfig' flask_app = create_app() # Create a test client using the Flask application configured for testing with flask_app.test_client() as testing_client: # Establish an application context with flask_app.app_context(): yield testing_client # this is where the testing happens! ``` ## * pytest 將遞歸搜索項目結構以查找以 開頭的 Python 文件test_*.py,然后運行這些文件中以 開頭的函數test_。無需配置即可識別測試文件的位置 * 運行特定 ` python -m pytest tests/functional/` pytest 可以提供裝置的調用結構并使用參數進行測試--setup-show ## --setup-show ` pytest --setup-show`命令用于運行單元測試時,展示setup和teardown方法的執行情況。 打開Pytest的setup和teardown試圖功能(`--setup-show`)會在每次調用setup和teardown時顯示一行詳細信息。這可以幫助測試統計test suite,檢測性能問題以及了解哪些fixtures甚至可以在模塊或session級別使用。這特別對于新的test suite或使用了新的fixture規則的舊的test suite非常有用。 正常情況下,在運行pytest時,pytest會在背后靜默地處理fixture setup和teardown。然而,在分析測試時,這些信息可能會對了解測試運行方式有所幫助。 例如: ~~~ shCopy codepytest --setup-show test_mod.py ~~~ 這將運行`test_mod.py`的測試,并顯示關于該測試的setup和teardown執行情況的詳細信息。 請注意,這只是Pytest的一種測試調試工具,且這不會改變測試的執行方式。他只是提供了更多的透明度,幫助我們理解和診斷測試運行過程中可能出現的問題。 注意:執行`pytest --setup-show`需要已安裝pytest庫。如果未安裝,可以使用`pip install pytest`進行安裝。 ## 代碼覆蓋率 代碼覆蓋率的包: * coverage.py * pytest-cov : 與pytest 無縫集成 ~~~ python -m pytest --cov=project ~~~ ## pytest-cov `pytest-cov`是一個插件,提供了 pytest 進行代碼覆蓋率測量的功能。它是使用`coverage.py`的一種便利方式,`coverage.py`是廣泛使用的 Python 代碼覆蓋率工具。 在測試過程中,你可能需要知道哪些代碼被執行,哪些未被執行,以此來了解測試的完整性。這就是代碼覆蓋率(code coverage)的概念,`pytest-cov`就是這樣的工具。 例如,你可以如下使用: ~~~ shCopy codepytest --cov=myproj tests/ ~~~ 這將運行 tests 目錄下的所有測試,并測量 myproj 的代碼覆蓋率。結果會顯示每個文件的覆蓋率,以及總的覆蓋率。 你也可以指定一個報告類型,例如使用 HTML 報告: ~~~ shCopy codepytest --cov=myproj --cov-report html tests/ ~~~ 這會生成一個名為`htmlcov`的目錄,其中包含與覆蓋率測量相關的 HTML 文件。你可以打開`htmlcov/index.html`來查看完整的報告。 如果你還沒有安裝`pytest-cov`,你可以使用 pip 來安裝: ~~~ shCopy codepip install pytest-cov ~~~ ## 參考 https://testdriven.io/blog/flask-pytest/
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看