Test-Driven Python Development
eBook - ePub

Test-Driven Python Development

Siddharta Govindaraj

Buch teilen
  1. 264 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Test-Driven Python Development

Siddharta Govindaraj

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

About This Book

  • Write robust and easily maintainable code using the principles of test driven development
  • Get solutions to real-world problems faced by Python developers
  • Go from a unit testing beginner to a master through a series of step-by-step tutorials that are easy to follow

Who This Book Is For

This book is intended for Python developers who want to use the principles of test-driven development (TDD) to create efficient and robust applications. In order to get the best out of this book, you should have development experience with Python.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Test-Driven Python Development als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Test-Driven Python Development von Siddharta Govindaraj im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Informatik & Programmierung in Python. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2015
ISBN
9781783987931

Test-Driven Python Development


Table of Contents

Test-Driven Python Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with Test-Driven Development
Prerequisites
Understanding test-driven development
TDD versus unit testing versus integration testing
Using TDD to build a stock alert application
Writing our first test
Analyzing the test output
Test errors versus test failures
Making the test pass
Reorganizing the test code
Running the tests after the reorganization
Summary
2. Red-Green-Refactor – The TDD Cycle
Tests are executable requirements
Arrange-Act-Assert
Documenting our tests
Testing for exceptions
Exploring assert methods
Specific asserts versus generic asserts
Setup and teardown
Brittle tests
Refactoring the design
Refactoring tests
Exploring the Rule classes
Exercise
Summary
3. Code Smells and Refactoring
A dual crossover moving average
Implementing the dual crossover moving average
Identifying code smells
Refactoring
The Rename Variable and Rename Method refactorings
Commenting Styles
Replace Magic Literals with Constants
The Extract Method refactoring
Replace Calculation with Temporary Variable
Extract Conditional to Method
The DRY principle
Single Responsibility Principle
Extract Class
Move Method to Class
The importance of tests
Exercise
Wrapping up
Summary
4. Using Mock Objects to Test Interactions
Hand writing a simple mock
Using the Python mocking framework
Mocking objects
Mocking return values
Mocking side effects
How much mocking is too much?
Mocks versus stubs versus fakes versus spies
Patching methods
An important gotcha when patching
Tying it all together
Summary
5. Working with Legacy Code
What is legacy code?
Understanding the code
What are characterization tests?
Using the Python interactive shell to understand the code
Writing a characterization test
Using pdb to understand the code
Some common pdb commands
Walking through a pdb session
Techniques to break dependencies
The Rope refactoring library
Separate initialization from execution
Use default values for parameters
Extract the method and test
Inject dependencies
Inherit and test
Stubbing local methods
Extract the method and stub
The cycle continues
Time to refactor
Long-term refactoring
Summary
6. Maintaining Your Test Suite
Goals of test maintenance
Organizing tests
Filesystem layout
Naming conventions
Test suite grouping
Making tests readable
Using docstrings
Using fixtures
Fixtures and patching
Using a custom test case class hierarchy
Writing tests closer to the domain
Writing helper methods
Writing better asserts
Using custom equality checkers
Using matchers
Summary
7. Executable Documentation with doctest
Our first doctest
Running the doctest
Test failures
Testing for exceptions
Package-level doctests
Maintaining doctests
Running a suite of doctests
Setup and teardown
Limitations of doctest
Doctest directives
How do doctests fit into the TDD process?
Summary
8. Extending unittest with nose2
Getting started with nose2
Writing tests for nose2
Setup and teardown
Parameterized tests
Generated tests
Layers
nose2 plugins
Doctest support
Writing test results to an XML file
Measuring test coverage
Debugging test failures
nose2 configuration
Summary
9. Unit Testing Patterns
Pattern – fast tests
Pattern – running a subset of tests
Test loaders
Using the load_tests protocol
Skipping tests
Pattern – using attributes
Attributes with vanilla unittests
Pattern – expected failures
Pattern – data-driven tests
Pattern – integration and system tests
Pattern – spies
Pattern – asserting a sequence of calls
Pattern – patching the open function
Pattern – mocking with mutable args
Summary
10. Tools to Improve Test-Driven Development
TDD tools
py.test
py.test versus nose2
Trial
Sure
PyHamcrest
Integrating with build tools
Paver
Integrating with packaging tools
Setuptools
Distutils
Integrating with continuous integration tools
Jenkins
Travis CI
Other tools
tox
Sphinx
IDE integration
Summary
A. Answers to Exercises
Red-Green-Refactor – The TDD Cycle
Code Smells and Refactoring
B. Working with Older Python Versions
Writing code that is compatible across versions
Running tests from the command line
Running the examples in this book
Index

Test-Driven Python Development

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2015
Production reference: 1210415
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-792-4
www.packtpub.com
Cover image by Jayanand Govindaraj ()

Credits

Author
Siddharta Govindaraj
Reviewers
Sharad Bagri
Kevin Davenport
Vivek Vidyasagaran
Devesh Chanchlani
Dorai Thodla
Kiran Gangadharan
Cees de Groot
David Simick
Fernando Ramos
Christopher Humphries
Commissioning Editor
Julian Ursell
Acquisition Editor
Nikhil Karkal
Content Development Editor
Sumeet Sawant
Technical Editor
Ruchi Desai
Copy Editors
Vikrant Phadke
Sameen Siddiqui
Alpha Singh
Project Coordinator
Danuta Jones
Proofreaders
Simran Bhogal
Stephen Copestake
Safis Editing
Paul Hindle
Indexer
Mariammal Chettiyar
Graphics
Jason Monteiro
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta

About the Author

Siddharta Govindaraj first encountered Python in 2002, and it has remained his favorite programming language ever since. He was an early participant in BangPypers, India's first Python community, and a speaker at InPycon. He is the founder of Silver Stripe Software, an Indian product start-up that develops products based on Python. A vocal proponent of the language, Siddharta extols the virtues of Python to anyone who will listen—and also to those who won't. His blog is at http://www.siddharta.me.
2323__perlego__chap...

Inhaltsverzeichnis