このエントリでは、PytestのWarning表示から、
依存ライブラリで出力されるものを非表示にする手順を示します。

結論から言うと、次のようにpytest.iniの設定で、
filterwarningsに無視したいライブラリを指定することで対応できます。

pytest.ini

[pytest]
testpaths = tests
pythonpath = .
filterwarnings =
  ignore::DeprecationWarning:flask.*
  ignore::DeprecationWarning:werkzeug.*

はじめに

pytestでは、
テスト実行時にWarningが発生すると自動的にcaptureして、
Warningが発生した旨をテスト結果に表示します。

参考: How to capture warnings | pytest
https://docs.pytest.org/en/stable/how-to/capture-warnings.html

しかし、開発中のアプリケーション自体で無く、
依存ライブラリ側で発生しているWarningは解消に手間がかかるケースがあります。

Warningが出ている状態が当たり前の状態になってしまうと、
Warningを解消して品質改善を行う機会を逸することになるので、
すぐに解消できないWarningは除外してしまうのも一つの方法かと思います。

そこで、
ここでは依存ライブラリ側で発生している警告を非表示にする手順を示します。

設定手順

ディレクトリを作って、Python仮想環境を用意します。

mkdir pytest-warning-filter-study && cd $_
python -m venv venv
. venv/bin/activate

Warningが出力されるように、古めのflaskをインストールします。

pip install flask==2.2.0
pip install Werkzeug==2.3.8
pip install pytest
pip freeze > requirements.txt

アプリケーションのコード・テストコードを準備します。

main.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__ == "__main__":
    app.run()

tests/test_main.py

from main import app

def test_helloworld():
    app.config['TESTING'] = True
    client = app.test_client() 
    result = client.get('/')
    assert result.data == b"<p>Hello, World!</p>"

pytestの設定も準備します。

pytest.ini

[pytest]
testpaths = tests
pythonpath = .

pytestを実行すると、Warningが表示されます。

$ pytest
======================================= test session starts =======================================
platform linux -- Python 3.11.9, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/takemikami/tmp/pytest-warning-filter-study
configfile: pytest.ini
testpaths: tests
collected 1 item

tests/test_main.py .                                                                        [100%]

======================================== warnings summary =========================================
tests/test_main.py::test_helloworld
  /home/takemikami/tmp/pytest-warning-filter-study/venv/lib/python3.11/site-packages/flask/testing.py:71: DeprecationWarning: 'werkzeug.urls.url_parse' is deprecated and will be removed in Werkzeug 3.0. Use 'urllib.parse.urlsplit' instead.
    url = url_parse(path)

tests/test_main.py::test_helloworld
  /home/takemikami/tmp/pytest-warning-filter-study/venv/lib/python3.11/site-packages/werkzeug/urls.py:545: DeprecationWarning: 'werkzeug.urls.URL' is deprecated and will be removed in Werkzeug 3.0. Use the 'urllib.parse' library instead.
    return result_type(scheme, netloc, url, query, fragment)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================== 1 passed, 2 warnings in 0.07s ==================================

pytestの設定にfilterを追加します。

pytest.ini

[pytest]
testpaths = tests
pythonpath = .
filterwarnings =
  ignore::DeprecationWarning:flask.*
  ignore::DeprecationWarning:werkzeug.*

pytestを実行すると、Warningが表示されなくなります。

$ pytest
======================================= test session starts =======================================
platform linux -- Python 3.11.9, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/takemikami/tmp/pytest-warning-filter-study
configfile: pytest.ini
testpaths: tests
collected 1 item

tests/test_main.py .                                                                        [100%]

======================================== 1 passed in 0.07s ========================================

以上。