By taking you through the development of a real web application from beginning to end, the second edition of this hands-on guide demonstrates the practical advantages of test-driven development (TDD) with Python. You'll learn how to write and run tests before building each part of your app, and then develop the minimum amount of code required to pass those tests. The result? Clean code that works. In the process, you'll learn the basics of Django, Selenium, Git, jQuery, and Mock, along with current web development techniques. If you're ready to take your Python skills to the next level, this book-updated for Python 3.6-clearly demonstrates how TDD encourages simple designs and inspires confidence. Dive into the TDD workflow, including the unit test/code cycle and refactoring Use unit tests for classes and functions, and functional tests for user interactions within the browser Learn when and how to use mock objects, and the pros and cons of isolated vs. integrated tests Test and automate your deployments with a staging server Apply tests to the third-party plugins you integrate into your site Run tests automatically by using a Continuous Integration environment Use TDD to build a REST API with a front-end Ajax interface

25 The Token Social Bit, the Page Pattern, and an Exercise for the Reader

453

(10)

An FT with Multiple Users, and addCleanup

453

(2)

The Page Pattern

455

(3)

Extend the FT to a Second User, and the "My Lists" Page

458

(1)

An Exercise for the Reader

459

(4)

26 Fast Tests, Slow Tests, and Hot Lava

463

(10)

Thesis: Unit Tests Are Superfast and Good Besides That

464

(2)

Faster Tests Mean Faster Development

465

(1)

The Holy Flow State

465

(1)

Slow Tests Don't Get Run as Often, Which Causes Bad Code

465

(1)

We're Fine Now, but Integrated Tests Get Slower Over Time

465

(1)

Don't Take It from Me

466

(1)

And Unit Tests Drive Good Design

466

(1)

The Problems with "Pure" Unit Tests

466

(1)

Isolated Tests Can Be Harder to Read and Write

466

(1)

Isolated Tests Don't Automatically Test Integration

466

(1)

Unit Tests Seldom Catch Unexpected Bugs

466

(1)

Mocky Tests Can Become Closely Tied to Implementation

467

(1)

But All These Problems Can Be Overcome

467

(1)

Synthesis: What Do We Want from Our Tests, Anyway?

467

(2)

Correctness

467

(1)

Clean, Maintainable Code

467

(1)

Productive Workflow

468

(1)

Evaluate Your Tests Against the Benefits You Want from Them

468

(1)

Architectural Solutions

469

(1)

Ports and Adapters/Hexagonal/Clean Architecture

469

(1)

Functional Core, Imperative Shell

470

(1)

Conclusion

470

(3)

Further Reading

471

(2)

Obey the Testing Goat!

473

(92)

A PythonAnywhere

475

(4)

B Django Class-Based Views

479

(12)

C Provisioning with Ansible

491

(6)

D Testing Database Migrations

497

(6)

E Behaviour-Driven Development (BDD)

503

(16)

F Building a REST API: JSON, Ajax, and Mocking with JavaScript

519

(22)

G Django-Rest-Framework

541

(12)

H Cheat Sheet

553

(4)

I What to Do Next

557

(4)

J Source Code Examples

561

(4)

Bibliography

565

(2)

Index

567

After an idyllic childhood spent playing with BASIC on French 8-bit computers like the Thomson T-07 whose keys go "boop" when you press them, Harry went on to study Economics and Philosophy at Cambridge University. He then spent a few years being deeply unhappy as a management consultant. Soon he rediscovered his true geek nature, and was lucky enough to fall in with a bunch of XP fanatics, working on the pioneering but sadly defunct Resolver One spreadsheet. He now works at PythonAnywhere LLP, and spreads the gospel of TDD world-wide at talks, workshops and conferences, with all the passion and enthusiasm of a recent convert.