Skip to Content
Pythonによるテスト駆動開発, 3rd Edition
book

Pythonによるテスト駆動開発, 3rd Edition

by Harry Percival
October 2025
Intermediate to advanced
712 pages
9h 46m
Japanese
O'Reilly Media, Inc.
Content preview from Pythonによるテスト駆動開発, 3rd Edition

第10章 アプリを本番環境対応にする

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

コンテナは正常に動作しているが、本番環境対応ではない。 テストを活用して安全を確保しつつ、本番環境対応を目指そう。

ある意味、本番環境対応プロセスにレッド/グリーン/リファクタリングのサイクルを適用しているのだ。 ハック的なコンテナ設定でグリーン状態を達成した後は、リファクタリングを行う。 コーディング時と同様にインクリメントで進め、動作状態から動作状態へと移行し、FT(機能テスト)で後退を検知するのだ。

必要なこと

ハック的なコンテナイメージの問題点は? いくつかある:まず、アプリを「通常の」ポート80でホストする必要がある。そうすればユーザーは普通のURLでアクセスできる。

おそらくもっと重要なのは、本番環境でDjangoの開発サーバーを使うべきじゃないってことだ。 実際のワークロードに耐えられるようには設計されていない。 代わりに、人気のGunicorn Python WSGI HTTPサーバを使う。

注記

Djangoのrunserver はローカル開発とデバッグ向けに構築・最適化されている。 への同時アクセスは1ユーザのみを想定。 ソースコード保存時の自動再読み込みは対応するが、 パフォーマンス最適化は施されておらず、 セキュリティ脆弱性への対策も強化されていない。

さらに、settings.pyのいくつかの設定は現状では問題がある。DEBUG=True は本番環境では強く推奨されない。SECRET_KEYを一意にセットする必要があるし、後で見るように他にも問題が発生するだろう。

警告

DEBUG=True セキュリティリスクと見なされる。 Djangoデバッグページは変数の値やsettings.pyの大半の設定など機密情報を表示するためだ。

一つずつ修正できるか見ていこう。

Gunicornへの切り替え

Djangoのマスコットがポニーなのは知ってるか? その由来はこうだ。Djangoには欲しい機能が全て揃っている: ORM、各種ミドルウェア、管理画面…「他に何が欲しい?ポニーか?」さて、Gunicornは「Green Unicorn」の略だ。 ポニーを既に持っているなら、次に欲しいのはこれだろう…

まずコンテナにGunicornをインストールし、runserver の代わりに使う必要がある:

$ python -m pip install gunicorn
Collecting gunicorn
[...]
Successfully installed gunicorn-2[...]

Gunicornは「WSGIサーバ」へのパスを知る必要がある1 通常はapplication という関数だ。 Djangoはsuperlists/wsgi.pyでこれを提供している。 イメージの実行コマンドを変更しよう:

Dockerfile (ch10l001)

[...]
RUN pip install "django<6" gunicorn  1

COPY src /src

WORKDIR /src

CMD ["gunicorn", "--bind", ":8888" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

イーサリアムをマスターする, 2nd Edition

イーサリアムをマスターする, 2nd Edition

Carlo Parisi, Alessandro Mazza, Niccolo Pozzolini, Gavin Wood, Andreas M. Antonopoulos
Excel用Python

Excel用Python

Felix Zumstein

Publisher Resources

ISBN: 0642572274771