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

第17章 JavaScriptのやさしい探求

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

一つの言語を理解するには、少なくとも二つを理解しなければならない。

ジェフリー・ウィランズ(英国の作家・ジャーナリスト

新しい検証ロジックは良いものだ。 だが、ユーザが修正を始めたら重複項目のエラーメッセージが消えるようにしたらどうだろう? HTML5のバリデーションエラーのようにね。

試してみよう——./src/manage.py runserver でサイトを起動し、 リストを作成する。空の項目を送信しようとすると、 「このフィールドに入力してください」というポップアップが表示され、 テキストを入力するとすぐに消える。 一方、同じ項目を二度入力すると、 赤い文字で「この項目は既にリストにあります」というメッセージが表示される。 たとえ有効な内容に修正しても、 フォームを送信するまでエラーは消えない(図17-1参照)。

A screenshot of the 'Please fill out this field' error in red, still shown despite the fact that the input value has been modified to be different from the existing item in the list
図17-1. でも 修正したよ!

このエラーを動的に消すには、ほんの少しのJavaScriptが必要だ。 Pythonはプログラミングに楽しい言語だ。 JavaScriptは常にそうだったわけではない。 だが多くの粗削りな部分は滑らかになり、 今ではJavaScriptは実に優れていると言えるだろう。 そしてウェブ開発の世界では、JavaScriptの使用は避けられない。 じゃあ、ちょっとだけ触ってみて、楽しめないか試してみよう。

JavaScriptの構文の基本は知っていると仮定する。 知らないなら、Mozillaのガイド MDN は常に品質が高い。 また、実物の本が好みなら『Eloquent JavaScript』も評判が良いと聞いた。

FTから始める

ItemValidationTest クラスに新しい機能テスト(FT)を追加しよう。 入力開始時にエラーメッセージが消えることを確認するテストだ:

src/functional_tests/test_list_item_validation.py (ch17l001)

def test_error_messages_are_cleared_on_input(self):
    # Edith starts a list and causes a validation error:
    self.browser.get(self.live_server_url)
    self.get_item_input_box().send_keys("Banter too thick")
    self.get_item_input_box().send_keys(Keys.ENTER)
    self.wait_for_row_in_list_table("1: Banter too thick")
    self.get_item_input_box().send_keys("Banter too thick")
    self.get_item_input_box().send_keys(Keys.ENTER)
    self.wait_for(  
        lambda: self.assertTrue(  
            self ...
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