TestDriven Development with Python
'Test-Driven Development with Python' focuses on web development, with some coverage of JavaScript (inescapable for any web programmer)., This book uses a concrete example - the development of a web site, from scratch - to teach the TDD metholology, and how it applies to web programming, from the baTest-Driven Development withPythonHarry PercivalBeijing· Cambridge· Farnham·Kon· Sebastopol· Tokyo OREILLY°Test-Driven Development with Pythonby Harry PercivalCopyright O 2014 Harry Percival. All rights reservedPrinted in the United States of AmericaPublished by o reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA 95472OReilly books may be purchased for educational, business, or sales promotional use Online editions arealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformationcontactourcorporateinstitutionalsalesdepartment800-998-9938orcorporate@oreilly.comEditor: Meghan BlanchetteIndexer: Wendy catalanoProduction editor: Kara ebrahimCover Designer: Randy comerCopyeditor: Charles RoumeliotisInterior Designer: David FutatoProofreader: Gillian McGarveyllustrator: Rebecca demarestJune 2014first editionRevision History for the First Edition2014-06-09 First releaseSeehttp://oreilly.com/catalog/errata.csp?isbn=9781449364823forreleasedetailsNutshell Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO ReillyMedia, Inc. Test-Driven Development with Python, the image of a cashmere goat, and related trade dress aretrademarks of O Reilly media, IncMany of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O Reilly Media, Inc was aware of a trademarkclaim, the designations have been printed in caps or initial capsWhile every precaution has been taken in the preparation of this book, the publisher and authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information containedhereinISBN:978-1-449-36482-3[LSITable of contentsPrefabPrerequisites and assumptionsAcknowledgePart I. The basics of tdd and djangoGetting Django Set Up Using a Functional Test.Obey the Testing goat! Do Nothing Until You Have a Testg Django up and runningStarting a git repository2. Extending Our Functional Test Using the unittest Module13USing a Functional Test to Scope Out a Minimum Viable App13The Python Standard Library's unittest Module16Implicit waits18Commit183. Testing a Simple Home Page with Unit Tests.21Our First Django App, and Our First Unit Test22Unit Tests, and how They Differ from Functional Tests22Unit Testing in Django23Django's Mvc, URls, and view Functions24At Last! We Actually Write Some Application Code26urls.py27Unit Testing a View30The Unit- Test/ Code cycle314. What Are We Doing with All These Tests?35Programming Is like Pulling a bucket of Water up from a well36Using Selenium to Test User Interactions7The Dont Test Constants" Rule, and Templates to the rescueRefactoring to Use a TemplateOn refactoringA Little More of Our Front Page45Recap: The TDD Process5. Saving User Input..51Wiring Up Our Form to Send a POst request51Processing a post request on the server54Passing python variables to be rendered in the templateThree Strikes and Refactor59The Django orm and Our First modelOur First Database migrationThe Test Gets Surprisingly FarA New Field Means a New MigrationSaving the post to the database65Redirect After a Post8Better Unit Testing Practice: Each Test Should Test One Thing68Rendering Items in the Template69Creating Our Production Database with migrate716. Getting to the Minimum Viable Site.Ensuring Test Isolation in Functional TestsRunning just the Unit Tests80Small Design When Necessary81YAGNI82REST82Implementing the New Design Using TDD83Iterating Towards the New design86Testing Views, Templates, and URLs Together with the django Test client87A New Test class88a New url88A New view function89A Separate Template for Viewing Lists90Another URL and View for Adding list Items92A Test Class for New list creation93A URL and View for New List Creation94Removing now-Redundant Code and Tests95Pointing our forms at the New url96Adjusting Our ModelsA Foreign Key relationshipTable of contentsAdjusting the Rest of the World to Our New models100Each list should Have Its Own urL102Capturing parameters from URLS103Adjusting new_list to the New World104One more view to Handle adding Items to an Existing list105Beware of greedy regular expressionsThe last New url106The Last New view107But How to use that url in the form108A Final Refactor Using URL includes110Part I. Web Development sine Qua Nons7. Prettification: Layout and Styling, and What to Test About It.115What to Functionally Test about Layout and style115Prettification: Using a CSS Framework118Django Template Inheritance120Integrating Bootstrap121Rows and columns122Static Files in django122Switching to StaticLiveS124Using bootstrap Components to Improve the look of the site125Jumbotron125ge inputs125Table Styling126USing our Own CSs126What We Glossed Over: collectstatic and Other Static Directories127A Few Things That Didnt Make It1308. Testing Deployment Using a Staging Site.TDD and the Danger Areas of Deployment132As always, Start with a TestGetting a Domain Name135Manually provisioning a server to host Our site136Choosing Where to Host Our Site136Spinning up a server137User Accounts, SSH, and Privileges137Installing nginx138Configuring Domains for Staging and LiveUsing the Ft to Confirm the Domain Works and Nginx Is running139Deploying our Code manually140Table of contents|ⅶiAdjusting the Database Location141Creating a virtualenv142Simple nginx configuration144Creating the Database with migrate147Getting to a Production-Ready Deployment148Switching to unicorn148Getting nginx to Serve static Files149Switching to USing Unix Sockets150Switching debug to False and Setting alloWEd hosts151Using Upstart to Make Sure Unicorn Starts on Boot151Saving Our Changes: Adding Gunicorn to Our requirements.txt152autog152Saving Your progress1569. Automating Deployment with Fabr157Breakdown of a Fabric Script for Our Deployment158Trying It Out162Deploying to live163ginx and Unicorn Config Using sed165it Tag the rel166Further reading10. Input Validation and Test Organisation.Validation FT: Preventing blank Items169Skipping a test170Splitting Functional Tests out into Many Files171Running a single test file174Fleshing Out the ft174Using Model-Layer Validation175Refactoring Unit Tests into Several Files175Unit Testing Model Validation and the self. assertRaises Context Manager 177A Django Quirk: Model Save Doesnt Run Validation178Surfacing Model Validation Errors in the view178Checking Invalid Input Isnt Saved to the Database181Django Pattern: Processing POST Requests in the Same View as Renders theForm183Refactor: Transferring the new item Functionality into view_ list184Enforcing Model Validation in view list186Refactor: Removing Hardcoded URLs187The %url % Template Tag188I Table of ContentsUsing get_absolute_url for Redirects18811. A Simple Form...193Moving validation Logic into a Form193Exploring the Forms API with a Unit Test194Switching to a Django modelForm195Testing and Customising Form Validation196Using the Form in Our View198USing the Form in a View with a GET Request198A Big Find and Replace201Using the Form in a view That Takes POst requests203Adapting the unit Tests for the new list View203USing the Form in the view204Using the Form to Display errors in the Template205USing the Form in the Other View205A Helper Method for Several Short Tests206Using the forms own Save method20812. More advanced forms,,,,,211Another Ft for Duplicate items211Preventing Duplicates at the Model Layer212A Little Digression on Queryset Ordering and String Representations214Rewriting the old Model Test216Some Integrity Errors Do Show Up on Save217Experimenting with Duplicate Item Validation at the Views Layer218A More Complex Form to handle Uniqueness validation219USing the Existing List Item Form in the list View22113. Dipping Our Toes, Very Tentatively, into JavaScript............... 225Starting with an FT225Setting Up a Basic JavaScript Test Runner226Using jQuery and the Fixtures Div229Building a JavaScript Unit Test for Our Desired Functionality232Javascript Testing in the TDD Cycle234Columbo Says: Onload Boilerplate and Namespacing234A Few Things That Didnt Make It23514. Deploying Our New Code,237ging Deploy237Live Deploy237What to Do If You see a database error238Table of contentsWrap-Up: git tag the New release238Part Il. More Advanced Topics15. User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript. 241Mozilla Persona (browserid)242Exploratory Coding, aka"Spiking242Starting a Branch for the spike243Frontend and JavaScript Code243The Browser -ID Protocol244The Server Side: Custom Authentication245De-spiking251A Common Selenium Technique: Explicit Waits253Reverting Our Spiked Code255avaScript Unit Tests Involving External Components: Our First Mocks256Housekeeping: A Site-Wide Static Files Folder256Mocking: Who, Why, What?257Namespacingg258A Simple Mock to Unit Tests Our initialize Function258More Advanced Mocking264Checking Call Arguments267QUnit setup and teardown, Testing ajax268More Nested Callbacks! Testing asynchronous Code27216. Server-Side Authentication and Mocking in Python...........277A Look at Our Spiked login view277Mocking in Python278Testing Our View by Mocking Out authenticate278Checking the view actually logs the User In281De-spiking Our Custom Authentication Backend: Mocking Out an InternetReq285I if=1 More Test286Patching at the Class Level287Beware of Mocks in Boolean Comparisons290Creating a USer if Necessary291The get user Method291A Minimal custom user model293A Slight Disappointment295Tests as documentation296Users Are Authenticated297The Moment of Truth: will the ft pass?298x Table of Contents
下载地址
用户评论
景点好书可惜没读