Skip to Content
Pythonによるアーキテクチャパターン
book

Pythonによるアーキテクチャパターン

by Harry Percival, Bob Gregory
March 2025
Intermediate to advanced
304 pages
4h 33m
Japanese
O'Reilly Media, Inc.
Content preview from Pythonによるアーキテクチャパターン

第5章. ハイギアとローギアのTDD

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

サービス・レイヤーを導入することで、実用的なアプリケーションに必要なオーケストレーションの責務を追加することができる。 サービス・レイヤーは、ユースケースとそれぞれのワークフローを明確に定義するのに役立つ。リポジトリから何を取得する必要があるのか、どのような事前チェックと現状検証を行うべきなのか、そして最後に何を保存するのか。

しかし現状では、単体テストの多くはより低いレベルで動作し、モデルに直接作用している。 この章では、これらのテストをサービスレイヤレベルに移行する際のトレードオフと、より一般的なテストのガイドラインについて説明する。

テストピラミッドはどうなっているか?

サービスレイヤーを使用し、独自のサービスレイヤーテストを持つようになったことで、 のテストピラミッドにどのような影響があるか見てみよう:

テストの種類をカウントする

$ grep -c test_ test_*.py
tests/unit/test_allocate.py:4
tests/unit/test_batches.py:8
tests/unit/test_services.py:3

tests/integration/test_orm.py:6
tests/integration/test_repository.py:2

tests/e2e/test_api.py:2

悪くない!単体テストが15個、統合テストが8個、エンドツーエンドテストが2個だけだ。 これはすでに健全なテストピラミッドだ。

ドメイン層のテストはサービス層に移行すべきか?

これをさらに一歩進めるとどうなるか見てみよう。サービスレイヤーに対してソフトウェアをテストできるようになったので、ドメインモデルに対するテストは必要なくなった。その代わりに、第1章のドメインレベルのテストをすべてサービスレイヤーの観点で書き直すことができる:

サービスレイヤーでドメインテストを書き直す (tests/unit/test_services.py)

# domain-layer test:
def test_prefers_current_stock_batches_to_shipments():
    in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None)
    shipment_batch = Batch ...
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

技術リーダーシップのための14のヒント

技術リーダーシップのための14のヒント

島田 浩二
Kubernetesで実践するクラウドネイティブDevOps

Kubernetesで実践するクラウドネイティブDevOps

John Arundel, Justin Domingus, 須田 一輝, 渡邉 了介
AWS上のシステム設計

AWS上のシステム設計

Jayanth Kumar, Mandeep Singh

Publisher Resources

ISBN: 9798341624863