没有合适的资源?快使用搜索试试~ 我知道了~
Test-Driven Development with Python_完美版
4星 · 超过85%的资源 需积分: 9 7 下载量 117 浏览量
2015-02-24
06:34:08
上传
评论
收藏 6.31MB PDF 举报
温馨提示
试读
476页
English Version, Test-Driven Development with Python, Best Python Test Book
资源推荐
资源详情
资源评论
Harry Percival
Test-Driven Development with
Python
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
Indexer: Wendy Catalano
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Rebecca Demarest
June 2014: First Edition
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
1.
Getting Django Set Up Using a Functional Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Obey the Testing Goat! Do Nothing Until You Have a Test 3
Getting Django Up and Running 6
Starting a Git Repository 8
2.
Extending Our Functional Test Using the unittest Module. . . . . . . . . . . . . . . . . . . . . . . . 13
Using a Functional Test to Scope Out a Minimum Viable App 13
The Python Standard Library’s unittest Module 16
Implicit waits 18
Commit 18
3.
Testing a Simple Home Page with Unit Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Our First Django App, and Our First Unit Test 22
Unit Tests, and How They Differ from Functional Tests 22
Unit Testing in Django 23
Django’s MVC, URLs, and View Functions 24
At Last! We Actually Write Some Application Code! 26
urls.py 27
Unit Testing a View 30
The Unit-Test/Code Cycle 31
4.
What Are We Doing with All These Tests?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Programming Is like Pulling a Bucket of Water up from a Well 36
v
Using Selenium to Test User Interactions 37
The “Don’t Test Constants” Rule, and Templates to the Rescue 40
Refactoring to Use a Template 40
On Refactoring 44
A Little More of Our Front Page 45
Recap: The TDD Process 47
5.
Saving User Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Wiring Up Our Form to Send a POST Request 51
Processing a POST Request on the Server 54
Passing Python Variables to Be Rendered in the Template 55
Three Strikes and Refactor 59
The Django ORM and Our First Model 60
Our First Database Migration 62
The Test Gets Surprisingly Far 63
A New Field Means a New Migration 64
Saving the POST to the Database 65
Redirect After a POST 68
Better Unit Testing Practice: Each Test Should Test One Thing 68
Rendering Items in the Template 69
Creating Our Production Database with migrate 71
6.
Getting to the Minimum Viable Site. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Ensuring Test Isolation in Functional Tests 77
Running Just the Unit Tests 80
Small Design When Necessary 81
YAGNI! 82
REST 82
Implementing the New Design Using TDD 83
Iterating Towards the New Design 86
Testing Views, Templates, and URLs Together with the Django Test Client 87
A New Test Class 88
A New URL 88
A New View Function 89
A Separate Template for Viewing Lists 90
Another URL and View for Adding List Items 92
A Test Class for New List Creation 93
A URL and View for New List Creation 94
Removing Now-Redundant Code and Tests 95
Pointing Our Forms at the New URL 96
Adjusting Our Models 97
A Foreign Key Relationship 99
vi | Table of Contents
Adjusting the Rest of the World to Our New Models 100
Each List Should Have Its Own URL 102
Capturing Parameters from URLs 103
Adjusting new_list to the New World 104
One More View to Handle Adding Items to an Existing List 105
Beware of Greedy Regular Expressions! 106
The Last New URL 106
The Last New View 107
But How to Use That URL in the Form? 108
A Final Refactor Using URL includes 110
Part II. Web Development Sine Qua Nons
7.
Prettification: Layout and Styling, and What to Test About It. . . . . . . . . . . . . . . . . . . . 115
What to Functionally Test About Layout and Style 115
Prettification: Using a CSS Framework 118
Django Template Inheritance 120
Integrating Bootstrap 121
Rows and Columns 122
Static Files in Django 122
Switching to StaticLiveServerCase 124
Using Bootstrap Components to Improve the Look of the Site 125
Jumbotron! 125
Large Inputs 125
Table Styling 126
Using Our Own CSS 126
What We Glossed Over: collectstatic and Other Static Directories 127
A Few Things That Didn’t Make It 130
8.
Testing Deployment Using a Staging Site. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
TDD and the Danger Areas of Deployment 132
As Always, Start with a Test 133
Getting a Domain Name 135
Manually Provisioning a Server to Host Our Site 136
Choosing Where to Host Our Site 136
Spinning Up a Server 137
User Accounts, SSH, and Privileges 137
Installing Nginx 138
Configuring Domains for Staging and Live 139
Using the FT to Confirm the Domain Works and Nginx Is Running 139
Deploying Our Code Manually 140
Table of Contents | vii
剩余475页未读,继续阅读
资源评论
- donotgo2015-10-20没水印,可惜少了前两页封面。。。其他都不错
wuzengde
- 粉丝: 1
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功