OReilly Test-Driven Development with Python (2014).pdf

(10043 KB) Pobierz
Praise for
Test-Driven Development with Python
“In this book, Harry takes us on an adventure of discovery with Python and testing. It’s an
excellent book, fun to read and full of vital information. It has my highest recommendations
for anyone interested in testing with Python, learning Django or wanting to use Selenium.
Testing is essential for developer sanity and it’s a notoriously difficult field, full of trade-
offs. Harry does a fantastic job of holding our attention whilst exploring real world testing
practices.”
— Michael Foord
Python Core Developer and Maintainer of unittest
“This book is far more than an introduction to Test Driven Development—it’s a complete
best-practices crash course, from start to finish, into modern web application development
with Python. Every web developer needs this book.”
— Kenneth Reitz
Fellow at Python Software Foundation
“Harry’s book is what we wish existed when we were learning Django. At a pace that’s
achievable and yet delightfully challenging, it provides excellent instruction for Django and
various test practices. The material on Selenium alone makes the book worth purchasing,
but there’s so much more!”
— Daniel and Audrey Roy Greenfield
authors of
Two Scoops of Django
(Two Scoops Press)
Test-Driven Development with
Python
Harry Percival
Test-Driven Development with Python
by Harry Percival
Copyright © 2014 Harry Percival. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editor:
Meghan Blanchette
Production Editor:
Kara Ebrahim
Copyeditor:
Charles Roumeliotis
Proofreader:
Gillian McGarvey
June 2014:
First Edition
Indexer:
Wendy Catalano
Cover Designer:
Randy Comer
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
Revision History for the First Edition:
2014-06-09:
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781449364823
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc.
Test-Driven Development with Python,
the image of a cashmere goat, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36482-3
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Prerequisites and Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Part I.
The Basics of TDD and Django
3
6
8
1. Getting Django Set Up Using a Functional Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Obey the Testing Goat! Do Nothing Until You Have a Test
Getting Django Up and Running
Starting a Git Repository
2. Extending Our Functional Test Using the unittest Module. . . . . . . . . . . . . . . . . . . . . . . . 13
Using a Functional Test to Scope Out a Minimum Viable App
The Python Standard Library’s unittest Module
Implicit waits
Commit
Our First Django App, and Our First Unit Test
Unit Tests, and How They Differ from Functional Tests
Unit Testing in Django
Django’s MVC, URLs, and View Functions
At Last! We Actually Write Some Application Code!
urls.py
Unit Testing a View
The Unit-Test/Code Cycle
13
16
18
18
22
22
23
24
26
27
30
31
36
v
3. Testing a Simple Home Page with Unit Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4. What Are We Doing with All These Tests?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Programming Is like Pulling a Bucket of Water up from a Well
Zgłoś jeśli naruszono regulamin