ローカル専用だったテストをクラウド環境に持ち込む
Azure Functions(Python)を VS Code の「Deploy to Azure」で運用していると、pytest はローカルでしか実行できず、Azure 上の実際の環境でテストを走らせる方法が分かりにくいままになりがちです。
しかし App Service プランで動かしている Azure Functions なら、Azure Portal の「開発ツール → SSH」から Azure 上でも pytest を実行できます。 この記事では、そのために必要な構成と手順をシンプルにまとめます。
Azure 上で pytest を実行したい理由
Azure Functions はローカルと挙動が異なることが多く、ローカルだけでテストしていると次のような問題が起きます。
- Azure の Linux 環境でのみ発生するエラーがある
- ストレージ接続や環境変数など Azure 固有の設定がローカルでは再現しにくい
- 実際の Function App のディレクトリ構成がローカルと異なる
そのため、Azure の実行環境そのものを使って pytest を回せると、デバッグ効率が大幅に上がります。
Azure 上で pytest を実行するための構成
1. tests フォルダを Azure にデプロイする
pytest を Azure 上で実行するには、テストコードが Azure に存在している必要があります。 そのため、tests フォルダは本番にも含める構成にします。
project/
host.json
requirements.txt
function_app/
tests/tests が純粋なテストコードだけで構成されていれば、本番に含めても問題ないと判断しました。
pytest==9.0.2バージョン固定にしておくと、ローカルと Azure の挙動差が出にくくなります。
3. ローカル専用ファイルだけを .funcignore で除外する
tests は Azure に含めるため .funcignore には書きません。 代わりに、ローカル専用の CSV / BAT / LOG / Azurite DB / venv などを除外します。
.git*
.vscode/
__azurite_db*__.json
__blobstorage__/
__queuestorage__/
local.settings.json
.venv/
*.md
tmp/
htmlcov/
.coveragerc
.coverage
coverage.xmlAzure Portal の「開発ツール → SSH」から pytest を実行する
Azure Functions(App Service プラン)には、Azure Portal から直接コンテナに入れる SSH コンソール が用意されています。
手順
- Azure Portal → Function App
- 左メニュー「開発ツール」→「SSH」
- コンソールが開いたら次を実行:
cd /home/site/wwwroot
python -m pytestFeature-warning以外はテストが正常に終了している。

※ Azure Functions(Linux)では、実際に Python が読み込むのは /home/site/wwwroot です。
=========================================================================================================== test session starts ============================================================================================================
platform linux -- Python 3.11.13, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/site/wwwroot
collected 81 items
tests/test_Scr_1.py ........... [ 13%]
tests/test_Scr_2.py ..... [ 19%]
tests/test_Scr_3.py ..... [ 25%]
tests/test_Scr_4.py ........ [ 35%]
tests/test_Scr_5_6.py .............. [ 53%]
tests/test_Scr_7.py ..... [ 59%]
tests/test_common_func.py ................................. [100%]
============================================================================================================= warnings summary =============================================================================================================
tests/test_Scr_1.py::test_P_003
tests/test_Scr_1.py::test_P_006
/home/site/wwwroot/Scr_1.py:134: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['202404']' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
df_purchase.loc[:, "year_month"] = df_purchase["year_month"].astype(str)
tests/test_Scr_1.py::test_P_010
/home/site/wwwroot/Scr_1.py:717: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '['2024' '2025']' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
df_profit_color.loc[:, "fisc_year"] = df_profit_color["fisc_year"].astype(str)
.python_packages/lib/site-packages/_pytest/cacheprovider.py:475
/home/site/wwwroot/.python_packages/lib/site-packages/_pytest/cacheprovider.py:475: PytestCacheWarning: cache could not write path /home/site/wwwroot/.pytest_cache/v/cache/nodeids: [Errno 30] Read-only file system: '/home/site/wwwroot/.pytest_cache/v/cache/nodeids'
config.cache.set("cache/nodeids", sorted(self.cached_nodeids))
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===================================================================================================== 81 passed, 4 warnings in 10.25s ======================================================================================================・ドットがテスト成功
・FutureWarning やキャッシュ警告は無視してよい
この構成で得られるメリット
- Azure の実行環境でテストできる
- ローカルとの差異をすぐに検出できる
- ストレージ接続や環境変数など Azure 固有の挙動を確認できる
- テスト環境にデプロイしたコードをその場で検証できる
- VS Code Deploy の運用を変えずにテストをクラウドに持ち込める
- CI/CD を導入していなくても Azure 上でテストが完結する
特に 「ローカルでは動くのに Azure では動かない」問題の調査が圧倒的に楽になります。
まとめ
Azure Functions(App Service プラン)で pytest を Azure 上でも実行するには:
- tests を本番に含める構成にする
- pytest を requirements.txt に含める(バージョン固定)
- ローカル専用ファイルだけを .funcignore で除外する
- Azure Portal の「開発ツール → SSH」から pytest を実行する
これだけで、ローカル専用だった pytest を Azure 上でも実行できるようになります。


コメント